8889841cclixcotz/gj.clix.co.tz/wp-content/plugins/wp-job-board-pro/assets/js/leaflet/Leaflet.GoogleMutant.js0000644 00000035740 15051553320 0030366 0 ustar 00 home // Based on https://github.com/shramov/leaflet-plugins // GridLayer like https://avinmathew.com/leaflet-and-google-maps/ , but using MutationObserver instead of jQuery // 🍂class GridLayer.GoogleMutant // 🍂extends GridLayer L.GridLayer.GoogleMutant = L.GridLayer.extend({ options: { minZoom: 0, maxZoom: 23, tileSize: 256, subdomains: 'abc', errorTileUrl: '', attribution: '', // The mutant container will add its own attribution anyways. opacity: 1, continuousWorld: false, noWrap: false, // 🍂option type: String = 'roadmap' // Google's map type. Valid values are 'roadmap', 'satellite' or 'terrain'. 'hybrid' is not really supported. type: 'roadmap', maxNativeZoom: 21 }, initialize: function (options) { L.GridLayer.prototype.initialize.call(this, options); this._ready = !!window.google && !!window.google.maps && !!window.google.maps.Map; this._GAPIPromise = this._ready ? Promise.resolve(window.google) : new Promise(function (resolve, reject) { var checkCounter = 0; var intervalId = null; intervalId = setInterval(function () { if (checkCounter >= 10) { clearInterval(intervalId); return reject(new Error('window.google not found after 10 attempts')); } if (!!window.google && !!window.google.maps && !!window.google.maps.Map) { clearInterval(intervalId); return resolve(window.google); } checkCounter++; }, 500); }); // Couple data structures indexed by tile key this._tileCallbacks = {}; // Callbacks for promises for tiles that are expected this._freshTiles = {}; // Tiles from the mutant which haven't been requested yet this._imagesPerTile = (this.options.type === 'hybrid') ? 2 : 1; this._boundOnMutatedImage = this._onMutatedImage.bind(this); }, onAdd: function (map) { L.GridLayer.prototype.onAdd.call(this, map); this._initMutantContainer(); this._GAPIPromise.then(function () { this._ready = true; this._map = map; this._initMutant(); map.on('viewreset', this._reset, this); if (this.options.updateWhenIdle) { map.on('moveend', this._update, this); } else { map.on('move', this._update, this); } map.on('zoomend', this._handleZoomAnim, this); map.on('resize', this._resize, this); //handle layer being added to a map for which there are no Google tiles at the given zoom google.maps.event.addListenerOnce(this._mutant, 'idle', function () { this._checkZoomLevels(); this._mutantIsReady = true; }.bind(this)); //20px instead of 1em to avoid a slight overlap with google's attribution map._controlCorners.bottomright.style.marginBottom = '20px'; map._controlCorners.bottomleft.style.marginBottom = '20px'; this._reset(); this._update(); if (this._subLayers) { //restore previously added google layers for (var layerName in this._subLayers) { this._subLayers[layerName].setMap(this._mutant); } } }.bind(this)); }, onRemove: function (map) { L.GridLayer.prototype.onRemove.call(this, map); this._observer.disconnect(); map._container.removeChild(this._mutantContainer); google.maps.event.clearListeners(map, 'idle'); google.maps.event.clearListeners(this._mutant, 'idle'); map.off('viewreset', this._reset, this); map.off('move', this._update, this); map.off('moveend', this._update, this); map.off('zoomend', this._handleZoomAnim, this); map.off('resize', this._resize, this); if (map._controlCorners) { map._controlCorners.bottomright.style.marginBottom = '0em'; map._controlCorners.bottomleft.style.marginBottom = '0em'; } }, getAttribution: function () { return this.options.attribution; }, setElementSize: function (e, size) { e.style.width = size.x + 'px'; e.style.height = size.y + 'px'; }, addGoogleLayer: function (googleLayerName, options) { if (!this._subLayers) this._subLayers = {}; return this._GAPIPromise.then(function () { var Constructor = google.maps[googleLayerName]; var googleLayer = new Constructor(options); googleLayer.setMap(this._mutant); this._subLayers[googleLayerName] = googleLayer; return googleLayer; }.bind(this)); }, removeGoogleLayer: function (googleLayerName) { var googleLayer = this._subLayers && this._subLayers[googleLayerName]; if (!googleLayer) return; googleLayer.setMap(null); delete this._subLayers[googleLayerName]; }, _initMutantContainer: function () { if (!this._mutantContainer) { this._mutantContainer = L.DomUtil.create('div', 'leaflet-google-mutant leaflet-top leaflet-left'); this._mutantContainer.id = '_MutantContainer_' + L.Util.stamp(this._mutantContainer); this._mutantContainer.style.zIndex = '800'; //leaflet map pane at 400, controls at 1000 this._mutantContainer.style.pointerEvents = 'none'; L.DomEvent.off(this._mutantContainer); } this._map.getContainer().appendChild(this._mutantContainer); this.setOpacity(this.options.opacity); this.setElementSize(this._mutantContainer, this._map.getSize()); this._attachObserver(this._mutantContainer); }, _initMutant: function () { if (!this._ready || !this._mutantContainer) return; if (this._mutant) { // reuse old _mutant, just make sure it has the correct size this._resize(); return; } this._mutantCenter = new google.maps.LatLng(0, 0); var map = new google.maps.Map(this._mutantContainer, { center: this._mutantCenter, zoom: 0, tilt: 0, mapTypeId: this.options.type, disableDefaultUI: true, keyboardShortcuts: false, draggable: false, disableDoubleClickZoom: true, scrollwheel: false, streetViewControl: false, styles: this.options.styles || {}, backgroundColor: 'transparent' }); this._mutant = map; google.maps.event.addListenerOnce(map, 'idle', function () { var nodes = this._mutantContainer.querySelectorAll('a'); for (var i = 0; i < nodes.length; i++) { nodes[i].style.pointerEvents = 'auto'; } }.bind(this)); // 🍂event spawned // Fired when the mutant has been created. this.fire('spawned', {mapObject: map}); }, _attachObserver: function _attachObserver (node) { // console.log('Gonna observe', node); if (!this._observer) this._observer = new MutationObserver(this._onMutations.bind(this)); // pass in the target node, as well as the observer options this._observer.observe(node, { childList: true, subtree: true }); // if we are reusing an old _mutantContainer, we must manually detect // all existing tiles in it Array.prototype.forEach.call( node.querySelectorAll('img'), this._boundOnMutatedImage ); }, _onMutations: function _onMutations (mutations) { for (var i = 0; i < mutations.length; ++i) { var mutation = mutations[i]; for (var j = 0; j < mutation.addedNodes.length; ++j) { var node = mutation.addedNodes[j]; if (node instanceof HTMLImageElement) { this._onMutatedImage(node); } else if (node instanceof HTMLElement) { Array.prototype.forEach.call( node.querySelectorAll('img'), this._boundOnMutatedImage ); // Check for, and remove, the "Google Maps can't load correctly" div. // You *are* loading correctly, you dumbwit. if (node.style.backgroundColor === 'white') { L.DomUtil.remove(node); } // Check for, and remove, the "For development purposes only" divs on the aerial/hybrid tiles. if (node.textContent.indexOf('For development purposes only') === 0) { L.DomUtil.remove(node); } // Check for, and remove, the "Sorry, we have no imagery here" // empty