From 5bd679a2822cd9bc10718f675d0abb4aaf1f38a8 Mon Sep 17 00:00:00 2001 From: Gautier Pelloux-Prayer Date: Tue, 2 May 2017 16:23:10 +0200 Subject: [PATCH] Fix fullhash feature --- js/index.js | 5 +- js/plugin/leaflet-fullHash.js | 13 +- js/plugin/leaflet-fullHash.raw.js | 200 ------------------------------ 3 files changed, 15 insertions(+), 203 deletions(-) delete mode 100644 js/plugin/leaflet-fullHash.raw.js diff --git a/js/index.js b/js/index.js index e4d86a9..4813fe4 100644 --- a/js/index.js +++ b/js/index.js @@ -271,7 +271,8 @@ return params; }; - urlHash = new L.Hash(map, mapLayers); + // do not initialize immediately + urlHash = new L.Hash(null, null); urlHash.additionalCb = function() { var url = router.getUrl(routing.getWaypoints(), null); return '&' + url.substr('brouter?'.length+1); @@ -279,6 +280,8 @@ urlHash.onHashChangeCb = onHashChangeCb; urlHash.onInvalidHashChangeCb = onInvalidHashChangeCb; urlHash.layers = mapLayers; + urlHash.map = map; + urlHash.init(map, mapLayers); routingOptions.on('update', urlHash.onMapMove, urlHash); nogos.on('update', urlHash.onMapMove, urlHash); diff --git a/js/plugin/leaflet-fullHash.js b/js/plugin/leaflet-fullHash.js index 9c0e882..a9edd8a 100644 --- a/js/plugin/leaflet-fullHash.js +++ b/js/plugin/leaflet-fullHash.js @@ -23,7 +23,7 @@ var zoom = parseInt(mapsArgs[0], 10), lat = parseFloat(mapsArgs[1]), lon = parseFloat(mapsArgs[2]), - layers = decodeURIComponent(mapsArgs[3]).split('-'); + layers = decodeURIComponent(mapsArgs[3]).split('-'), additional = args[1]; if (isNaN(zoom) || isNaN(lat) || isNaN(lon)) { return false; @@ -56,6 +56,9 @@ }; }; }; + if (layers.length == 0) { + layers.push(Object.keys(options)[0]); + } var params = [ zoom, center.lat.toFixed(precision), @@ -140,7 +143,7 @@ this.movingMap = true; this.map.setView(parsed.center, parsed.zoom); - var layers = parsed.layers.length > 0 ? parsed.layers : [Object.keys(options)[0]], + var layers = parsed.layers, options = this.options, that = this; //Add/remove layer @@ -152,11 +155,17 @@ } } }); + var added = false; layers.forEach(function(element, index, array) { if (element in options) { + added = true; that.map.addLayer(options[element]); } }); + if (!added) { + // if we couldn't add layers (custom ones or invalid name), add the default one + this.map.addLayer(options[Object.keys(options)[0]]); + } if (this.onHashChangeCb != null) { this.onHashChangeCb(parsed.additional); diff --git a/js/plugin/leaflet-fullHash.raw.js b/js/plugin/leaflet-fullHash.raw.js deleted file mode 100644 index ee5f07a..0000000 --- a/js/plugin/leaflet-fullHash.raw.js +++ /dev/null @@ -1,200 +0,0 @@ -(function(window) { - var HAS_HASHCHANGE = (function() { - var doc_mode = window.documentMode; - return ('onhashchange' in window) && - (doc_mode === undefined || doc_mode > 7); - })(); - - L.Hash = function(map, options) { - this.onHashChange = L.Util.bind(this.onHashChange, this); - - if (map) { - this.init(map, options); - } - }; - - L.Hash.parseHash = function(hash) { - if(hash.indexOf('#') === 0) { - hash = hash.substr(1); - } - var args = hash.split("/"); - if (args.length == 4) { - var zoom = parseInt(args[0], 10), - lat = parseFloat(args[1]), - lon = parseFloat(args[2]), - layers = (args[3]).split("-"); - if (isNaN(zoom) || isNaN(lat) || isNaN(lon)) { - return false; - } else { - return { - center: new L.LatLng(lat, lon), - zoom: zoom, - layers: layers - }; - } - } else { - return false; - } - }; - - L.Hash.formatHash = function(map) { - var center = map.getCenter(), - zoom = map.getZoom(), - precision = Math.max(0, Math.ceil(Math.log(zoom) / Math.LN2)), - layers = []; - - //console.log(this.options); - var options = this.options; - //Check active layers - for(var key in options) { - if (options.hasOwnProperty(key)) { - if (map.hasLayer(options[key])) { - layers.push(key); - }; - }; - }; - - return "#" + [zoom, - center.lat.toFixed(precision), - center.lng.toFixed(precision), - layers.join("-") - ].join("/"); - }, - - L.Hash.prototype = { - map: null, - lastHash: null, - - parseHash: L.Hash.parseHash, - formatHash: L.Hash.formatHash, - - init: function(map, options) { - this.map = map; - L.Util.setOptions(this, options); - - // reset the hash - this.lastHash = null; - this.onHashChange(); - - if (!this.isListening) { - this.startListening(); - } - }, - - removeFrom: function(map) { - if (this.changeTimeout) { - clearTimeout(this.changeTimeout); - } - - if (this.isListening) { - this.stopListening(); - } - - this.map = null; - }, - - onMapMove: function() { - // bail if we're moving the map (updating from a hash), - // or if the map is not yet loaded - - if (this.movingMap || !this.map._loaded) { - return false; - } - - var hash = this.formatHash(this.map); - if (this.lastHash != hash) { - location.replace(hash); - this.lastHash = hash; - } - }, - - movingMap: false, - update: function() { - var hash = location.hash; - if (hash === this.lastHash) { - return; - } - var parsed = this.parseHash(hash); - if (parsed) { - this.movingMap = true; - - this.map.setView(parsed.center, parsed.zoom); - var layers = parsed.layers, - options = this.options, - that = this; - //Add/remove layers - this.map.eachLayer(function(layer) { - that.map.removeLayer(layer); - }); - - layers.forEach(function(element, index, array) { - //console.log(options[element]); - that.map.addLayer(options[element]); - }); - - this.movingMap = false; - } else { - this.onMapMove(this.map); - } - }, - - // defer hash change updates every 100ms - changeDefer: 100, - changeTimeout: null, - onHashChange: function() { - // throttle calls to update() so that they only happen every - // `changeDefer` ms - if (!this.changeTimeout) { - var that = this; - this.changeTimeout = setTimeout(function() { - that.update(); - that.changeTimeout = null; - }, this.changeDefer); - } - }, - - isListening: false, - hashChangeInterval: null, - startListening: function() { - this.map.on("moveend layeradd layerremove", this.onMapMove, this); - - if (HAS_HASHCHANGE) { - L.DomEvent.addListener(window, "hashchange", this.onHashChange); - } else { - clearInterval(this.hashChangeInterval); - this.hashChangeInterval = setInterval(this.onHashChange, 50); - } - this.isListening = true; - }, - - stopListening: function() { - this.map.off("moveend layeradd layerremove", this.onMapMove, this); - - if (HAS_HASHCHANGE) { - L.DomEvent.removeListener(window, "hashchange", this.onHashChange); - } else { - clearInterval(this.hashChangeInterval); - } - this.isListening = false; - }, - - _keyByValue: function(obj, value) { - for(var key in obj) { - if (obj.hasOwnProperty(key)) { - if (obj[key] === value) { - return key; - } else { return null; }; - }; - }; - } - }; - L.hash = function(map, options) { - return new L.Hash(map, options); - }; - L.Map.prototype.addHash = function() { - this._hash = L.hash(this, this.options); - }; - L.Map.prototype.removeHash = function() { - this._hash.removeFrom(); - }; -})(window);