-
Notifications
You must be signed in to change notification settings - Fork 0
/
leaflet.permalink.min.js
1 lines (1 loc) · 2.04 KB
/
leaflet.permalink.min.js
1
L.Permalink={setup:function(map){"use strict";let debug=false;if(debug){map.on("moveend",function(e){console.log("moveend zoom "+map.getZoom())})}var shouldUpdate=true;var updatePermalink=function(){if(map.options.reload){map.options.reload=false;return}if(!shouldUpdate){shouldUpdate=true;return}if(!map.options.initZoom&&!map.options.initCenter){return}var center=map.getCenter();var hash="#"+Math.round(center.lat*1e5)/1e5+","+Math.round(center.lng*1e5)/1e5+","+map.getZoom()+"z";var state={zoom:map.getZoom(),center:center};if(debug){console.log("old ",window.location.hash," new ",hash)}if(window.location.hash!==hash){if(debug){console.log("new location")}window.history.pushState(state,"map",hash)}};if(window.location.hash!==""){var hash=window.location.hash.replace("#","");var parts=hash.split(",");if(parts.length===3){map.options.initCenter={lat:parseFloat(parts[0]),lng:parseFloat(parts[1])};map.options.initZoom=parseInt(parts[2].slice(0,-1),10);map.setView(map.options.initCenter,map.options.initZoom);if(debug){console.log("init fixed",map.options.initCenter,map.options.initZoom)}}}else{document.getElementsByClassName("leaflet-container")[0].addEventListener("pointermove",()=>{map.options.initZoom=map.getZoom();map.options.initCenter=map.getCenter();if(debug){console.log("init",map.options.initCenter,map.options.initZoom)}},{capture:true,once:true})}if(debug){console.log("trigger moveend")}map.on("moveend",updatePermalink);window.addEventListener("popstate",function(event){if(debug){console.log("popstate")}if(event.state===null){return}map.setView(event.state.center,event.state.zoom);shouldUpdate=false});window.addEventListener("hashchange",function(event){if(shouldUpdate){if(window.location.hash!==""){var hash=window.location.hash.replace("#","");var parts=hash.split(",");if(parts.length===3){let center={lat:parseFloat(parts[0]),lng:parseFloat(parts[1])};let zoom=parseInt(parts[2].slice(0,-1),10);map.setView(center,zoom)}}else{if(debug){console.log("should reload")}map.options.reload=true;map.setView(map.options.initCenter,map.options.initZoom)}}})}};