-
Notifications
You must be signed in to change notification settings - Fork 77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP]Embed feature in demo page #296
Changes from all commits
fdeac86
291b693
647e234
427b771
c478082
08e4061
9388519
cb26ca0
905bb3e
bf31b18
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,198 @@ | ||
(function(window) { | ||
var HAS_HASHCHANGE = (function() { | ||
var doc_mode = window.documentMode; | ||
return ('onhashchange' in window) && | ||
(doc_mode === undefined || doc_mode > 7); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need to add this to a separate folder outside example. Let's make one lib folder at the root level and change the script tag above. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should I move the whole lib folder to the root or just the fullUrlHash.js file? |
||
})(); | ||
|
||
L.FullHash = function(map, options) { | ||
this.onHashChange = L.Util.bind(this.onHashChange, this); | ||
|
||
if (map) { | ||
this.init(map, options); | ||
} | ||
}; | ||
|
||
L.FullHash.parseHash = function(locationHash) { | ||
// Modified from urlHash library -> https://github.com/jywarren/urlhash | ||
// ----------------------------------- | ||
var hash = locationHash || location.hash; | ||
if (hash) hash = hash.split('#')[1]; | ||
var pairs = hash.split('&'); | ||
var object = {}; | ||
pairs.forEach(function(pair, i) { | ||
pair = pair.split('='); | ||
if (pair[0] != '') object[pair[0]] = pair[1]; | ||
}); | ||
|
||
|
||
if(isNaN(object.lat) || isNaN(object.lon) || isNaN(object.zoom)) { | ||
return false; | ||
} else { | ||
return { | ||
center: new L.LatLng(object.lat, object.lon), | ||
zoom: object.zoom, | ||
layers: object.layers.split(',') | ||
}; | ||
} | ||
// ----------------------------------- | ||
}; | ||
|
||
L.FullHash.formatHash = function(map) { | ||
var center = map.getCenter(), | ||
zoom = map.getZoom(), | ||
precision = Math.max(0, Math.ceil(Math.log(zoom) / Math.LN2)), | ||
layers = []; | ||
|
||
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 "#lat=" + | ||
center.lat.toFixed(precision) + "&lon=" + | ||
center.lng.toFixed(precision) + "&zoom=" + | ||
zoom + "&layers=" + layers.join(",") | ||
}, | ||
|
||
L.FullHash.prototype = { | ||
map: null, | ||
lastHash: null, | ||
|
||
parseHash: L.FullHash.parseHash, | ||
formatHash: L.FullHash.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(locationHash) { | ||
var hash = locationHash || 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) { | ||
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.fullHash = function(map, options) { | ||
return new L.FullHash(map, options); | ||
}; | ||
L.Map.prototype.addHash = function() { | ||
this._fullhash = L.fullHash(this, this.options); | ||
}; | ||
L.Map.prototype.removeHash = function() { | ||
this._fullhash.removeFrom(); | ||
}; | ||
})(window); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -121,15 +121,18 @@ L.LayerGroup.environmentalLayers = L.LayerGroup.extend( | |
} | ||
|
||
if(this.options.embed) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Redundant code? |
||
this.overlayMaps[layer].addTo(map); | ||
// this.overlayMaps[layer].addTo(map); | ||
} | ||
|
||
} | ||
|
||
} | ||
if(this.options.embed) { | ||
L.control.embed().addTo(map); | ||
} | ||
|
||
L.control.layers(baseMaps,this.overlayMaps).addTo(map); | ||
|
||
if(this.options.hash) | ||
var hash = new L.Hash(map, this.overlayMaps); | ||
if(this.options.hash) | ||
var hash = new L.FullHash(map,this.overlayMaps); | ||
// Parse map data from location hash | ||
var parsed = hash.parseHash(this.options.currentHash); | ||
var layers = parsed.layers; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AWESOME!!!
Let's add this to all HTML demo files :)