From 33897d30b32b1323d8d4a3053b2a8a13cea5e5d5 Mon Sep 17 00:00:00 2001 From: Tom MacWright Date: Tue, 11 Jun 2013 12:33:50 -0700 Subject: [PATCH] Scope map variable. --- app/assets/javascripts/application.js | 4 +--- app/assets/javascripts/index.js | 25 +++++++++++++++---------- app/assets/javascripts/leaflet.share.js | 8 +++++--- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index c9eddfb9c..247df1b83 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -129,9 +129,7 @@ function makeShortCode(map) { } for (i = 0; i < ((zoom + 8) % 3); ++i) str += "-"; - /* - * Called to interlace the bits in x and y, making a Morton code. - */ + // Called to interlace the bits in x and y, making a Morton code. function interlace(x, y) { x = (x | (x << 8)) & 0x00ff00ff; x = (x | (x << 4)) & 0x0f0f0f0f; diff --git a/app/assets/javascripts/index.js b/app/assets/javascripts/index.js index 40fe11761..10f9f86f0 100644 --- a/app/assets/javascripts/index.js +++ b/app/assets/javascripts/index.js @@ -15,7 +15,7 @@ $(document).ready(function () { var marker; var params = OSM.mapParams(); - map = L.map("map", { + var map = L.map("map", { zoomControl: false, layerControl: false }); @@ -84,7 +84,7 @@ $(document).ready(function () { L.control.scale().addTo(map); - map.on("moveend layeradd layerremove", updateLocation); + map.on('moveend layeradd layerremove', updateLocation); if (!params.object_zoom) { if (params.bbox) { @@ -154,17 +154,22 @@ $(document).ready(function () { marker = L.marker(centre, {icon: getUserIcon()}).addTo(map); }); - function updateLocation() { + // generate a cookie-safe string of map state + function cookieContent(map) { var center = map.getCenter().wrap(); - var zoom = map.getZoom(); - var layers = getMapLayers(); - var extents = map.getBounds().wrap(); + return [center.lng, center.lat, map.getZoom(), getMapLayers(map)].join('|'); + } - updatelinks(center, zoom, layers, extents, params.object); + function updateLocation() { + updatelinks(map.getCenter().wrap(), + map.getZoom(), + getMapLayers(this), + map.getBounds().wrap(), + params.object); var expiry = new Date(); expiry.setYear(expiry.getFullYear() + 10); - $.cookie("_osm_location", [center.lng, center.lat, zoom, layers].join("|"), {expires: expiry}); + $.cookie("_osm_location", cookieContent(map), { expires: expiry }); } function remoteEditHandler() { @@ -229,8 +234,8 @@ function getMapBaseLayer() { } } -function getMapLayers() { - var layerConfig = ""; +function getMapLayers(map) { + var layerConfig = ''; for (var i in map._layers) { // TODO: map.eachLayer var layer = map._layers[i]; if (layer.options && layer.options.code) { diff --git a/app/assets/javascripts/leaflet.share.js b/app/assets/javascripts/leaflet.share.js index f085bc0e6..d43f99b1d 100644 --- a/app/assets/javascripts/leaflet.share.js +++ b/app/assets/javascripts/leaflet.share.js @@ -18,6 +18,8 @@ L.Control.Share = L.Control.extend({ this._uiPane = this.options.uiPane; + this._map = map; + var h2 = L.DomUtil.create('h2', '', this._uiPane); h2.innerHTML = I18n.t('javascripts.share.title'); @@ -35,9 +37,9 @@ L.Control.Share = L.Control.extend({ }, _update: function (e) { - var center = map.getCenter().wrap(); - var layers = getMapLayers(); - this._linkInput.value = this.options.getUrl(map); + var center = this._map.getCenter().wrap(); + var layers = getMapLayers(this._map); + this._linkInput.value = this.options.getUrl(this._map); }, _toggle: function() { -- 2.39.5