diff --git a/leaflet-hash.js b/leaflet-hash.js index b2759c5..85cbd47 100644 --- a/leaflet-hash.js +++ b/leaflet-hash.js @@ -4,28 +4,32 @@ return ('onhashchange' in window) && (doc_mode === undefined || doc_mode > 7); })(); - - L.Hash = function(map) { + + L.Hash = function(map, opts) { this.onHashChange = L.Util.bind(this.onHashChange, this); - + this.opts = opts || {path: '{z}/{lat}/{lng}'}; if (map) { this.init(map); } }; - + L.Hash.prototype = { map: null, lastHash: null, - + parseHash: function(hash) { + var path = this.opts.path.split("/"), + zIndex = path.indexOf("{z}"), + latIndex = path.indexOf("{lat}"), + lngIndex = path.indexOf("{lng}"); if(hash.indexOf('#') == 0) { hash = hash.substr(1); } var args = hash.split("/"); - if (args.length == 3) { - var zoom = parseInt(args[0], 10), - lat = parseFloat(args[1]), - lon = parseFloat(args[2]); + if (args.length == path.length) { + var zoom = parseInt(args[zIndex], 10), + lat = parseFloat(args[latIndex]), + lon = parseFloat(args[lngIndex]); if (isNaN(zoom) || isNaN(lat) || isNaN(lon)) { return false; } else { @@ -38,54 +42,56 @@ return false; } }, - + formatHash: function(map) { var center = map.getCenter(), zoom = map.getZoom(), precision = Math.max(0, Math.ceil(Math.log(zoom) / Math.LN2)); - - return "#" + [zoom, - center.lat.toFixed(precision), - center.lng.toFixed(precision) - ].join("/"); + + var path = this.opts.path.split("/"); + path[path.indexOf("{z}")] = zoom; + path[path.indexOf("{lat}")] = center.lat.toFixed(precision); + path[path.indexOf("{lng}")] = center.lng.toFixed(precision); + + return "#" + path.join("/"); }, - + init: function(map) { this.map = map; - + this.map.on("moveend", this.onMapMove, this); - + // reset the hash this.lastHash = null; this.onHashChange(); - + if (!this.isListening) { this.startListening(); } }, - + remove: function() { this.map = null; if (this.isListening) { this.stopListening(); } }, - + onMapMove: function(map) { // bail if we're moving the map (updating from a hash), // or if the map has no zoom set - + if (this.movingMap || this.map.getZoom() === 0) { 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; @@ -97,16 +103,16 @@ if (parsed) { // console.log("parsed:", parsed.zoom, parsed.center.toString()); this.movingMap = true; - + this.map.setView(parsed.center, parsed.zoom); - + this.movingMap = false; } else { // console.warn("parse error; resetting:", this.map.getCenter(), this.map.getZoom()); this.onMapMove(this.map); } }, - + // defer hash change updates every 100ms changeDefer: 100, changeTimeout: null, @@ -121,7 +127,7 @@ }, this.changeDefer); } }, - + isListening: false, hashChangeInterval: null, startListening: function() { @@ -133,7 +139,7 @@ } this.isListening = true; }, - + stopListening: function() { if (HAS_HASHCHANGE) { L.DomEvent.removeListener(window, "hashchange", this.onHashChange); @@ -144,7 +150,7 @@ } }; L.hash = function(map){ - return new L.Hash(map); + return new L.Hash(map); }; L.Map.prototype.addHash = function(){ this._hash = L.hash(this); @@ -152,4 +158,4 @@ L.Map.prototype.removeHash = function(){ this._hash.remove(); } -})(window); \ No newline at end of file +})(window);