-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathleaflet-hash-plus.js
331 lines (297 loc) · 9.33 KB
/
leaflet-hash-plus.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
(function(window) {
L.Hash = function(map, promise) {
this.map = map;
this.onHashChange = L.Util.bind(this.onHashChange, this);
if (map) {
this.init(map);
}
};
/**
* Attempts to parse and return properties from a hash.
*
* On success will return
* {
* view {
* center: L.LatLng,
* zoom: float|integer,
* },
* meta: array
* }
*
* On failure will return:
* {
* view false,
* meta: array
* }
*
* Reasons for failure include:
* > Less than 3 arguments passed (requires zoom, lat, lng)
* > Zoom is not a float (version 1.0.0+), or integer (before version 1)
* > Lat/Lng values are out of range.
*
* The response to "#18.00000/51.49867/-0.14442/buckingham/palace" is
* {
* view {
* center: L.LatLng(51.49867, -0.14442),
* zoom: 18.00000,
* },
* meta: ['buckingham', 'palace']
* }
*
* @param {string} hash The full hash from the page, e.g. "#a/b/c"
* @returns {object} See above notes.
*/
L.Hash.parseHash = function(hash) {
let args = hash.substring(1).split("/").filter(n => n); // Assume it starts with a '#'
// Assuming the map properties validate, everything after them is metadata.
let meta = args.length > 3 ? args.slice(3) : [];
if (args.length < 3) {
return {
view: false,
meta: meta,
}
}
var zoom = (L.version >= '1.0.0') ? parseFloat(args[0]) : parseInt(args[0], 10),
lat = parseFloat(args[1]),
lng = parseFloat(args[2]);
// Fail on invalid params
if (isNaN(zoom) || isNaN(lat) || isNaN(lng)) {
return {
view: false,
meta: meta,
}
}
if (lat < -90 || lat > 90 || lng < -180 || lng > 180) {
return {
view: false,
meta: meta,
}
}
// All valid, all in range.
return {
view: {
center: new L.LatLng(lat, lng),
zoom: zoom,
},
meta: meta,
};
};
/**
* Formats a hash for the given properties.
*
* Takes map details and formats them according to the following:
* #<zoom>/<lat>/<lng>/meta1/meta2
*
* @param {L.Map} map Leaflet map instance
* @param {array|null} meta Array of strings for meta data. If null then no
* values will be added.
*/
L.Hash.formatHash = function(map, meta) {
var center = map.getCenter(),
zoom = map.getZoom(),
precision = Math.max(0, Math.ceil(Math.log(zoom) / Math.LN2));
meta = typeof meta === 'undefined' ? [] : meta;
var viewHash = [
(L.version >= '1.0.0') ? zoom.toFixed(precision) : zoom,
center.lat.toFixed(precision),
center.lng.toFixed(precision)
];
return "#" + viewHash.concat(meta).join('/');
},
L.Hash.prototype = {
map: null,
isListening: false,
hashMeta: [],
isSetUp: false,
parseHash: L.Hash.parseHash,
formatHash: L.Hash.formatHash,
/**
* Associates the map.
*
* When map is fully loaded we will perform the setup and associate the
* current view and meta data to the state.
*
* @param {L.map} map
*/
init: function(map) {
this.map = map;
console.log('running map init()');
this.map.whenReady(this.setupMap, this);
},
/**
* Performs initial setup.
*
* Makes an initial attempt to parse the hash and fires hashmetainit with
* the initial meta state.
*
* Flag we are now "set up" so that subsequent onMapMove events can
* manipulate the map.
*
* If we fail to get #zoom/lat/lng from the URL we just call updateHash to
* set with current map state.
*
* Start listening for events after we have set up hash data.
*/
setupMap: function() {
var hash = this.parseHash(location.hash);
console.log('setupMap()', hash);
this.map.fire('hashmetainit', {meta: hash.meta});
// Force update of hash if the current one is invalid
this.isSetUp = true;
if (false === hash.view) {
console.log('hash view is false');
this.hashMeta = [];
this.updateHash();
this.startListening();
return;
} else {
this.setHashMeta(hash.meta, true);
}
this.startListening();
this.map.setView(hash.view.center, hash.view.zoom);
},
/**
* Returns the precision for toFixed() calls for zoom and lat/lng values.
*
* Values are rendered more accuratly when you zoom in.
*/
getMapPrecision: function() {
return Math.max(0, Math.ceil(Math.log(this.map.getZoom()) / Math.LN2));
},
/**
* Allows code outside of this plugin to manipulate hash meta data.
*
* Will override any existing meta data. Calling this will not result in
* a hashmetachange event.
*
* @param {array} meta Array of string values to be put on the end of the
* hash.
* @param {boolean} fireEvents Causes events like hashmetachange to be
* fired. False by default to avoid your own application being updated when
* you update the hash and a loop being created.
*/
setHashMeta: function(meta, fireEvents) {
// fireEvents is false by default
fireEvents = typeof fireEvents === 'undefined' ? false : fireEvents;
var metaChanges = JSON.stringify(this.hashMeta) !== JSON.stringify(meta)
console.log('metaChanges', metaChanges);
if (metaChanges) {
if (fireEvents) {
this.map.fire('hashmetachange', {meta: meta, previousMeta: this.hashMeta});
};
// Shallow copy the array or we start comparing to the same object.
this.hashMeta = [...meta];
this.updateHash();
}
},
/**
* Called whenever the hash in the URL is changed.
*
* When called we attempt to parse the hash that may (or may not) exist, and
* might be invalid.
*
* If parsing results in a missing/invalid hash, remove any meta data and
* force an update of the hash (which will populate the hash with the
* current map state).
*
* If hash has valid map view data, check if the meta data has changed: if
* so, fire a *hashmetachange* event with *meta* property.
*
* If the view data in the hash doesn't not match the current map (hash in
* URL has been change by user or external code), update the map view.
*
*/
onHashChange: function() {
var hash = this.parseHash(location.hash);
console.log('hash changed', hash);
// Force update of hash if the current one is invalid
if (false === hash.view) {
this.hashMeta = [];
this.updateHash();
return;
}
// Push the change in hash meta.
this.setHashMeta(hash.meta, true);
// Updating the view will update the hash, which in turn will cause this
// function to be called again. We won't get caught in a loop as if we
// update the hash, it will match the current map state.
if (! this.viewMatchesMapState(hash.view)) {
this.map.setView(hash.view.center, hash.view.zoom);
}
},
/**
* Checks to see if the passed view matches the current state of the map.
*
* An invalid view (false) value will result in a non-match.
*
* Zoom, Lat and Lng values must match to consider the view to match map
* state.
*
* @param {object} view Contains *center* (L.latlng) and *zoom* (float:int)
*/
viewMatchesMapState: function(view) {
if (false === view) {
return false;
}
center = this.map.getCenter();
precision = this.getMapPrecision();
return this.map.getZoom().toFixed(precision) == view.zoom.toFixed(precision)
&& center.lat.toFixed(precision) == view.center.lat
&& center.lng.toFixed(precision) == view.center.lng;
},
/**
* Called on my moveend events so that any zoom/pan operations update the
* hash.
*
* As we might want to pull meta data on initial page load, we hold back
* on allowing updateHash to be called until we have examined location.hash
*/
onMapMove: function() {
if (! this.isSetUp) {
return;
}
this.updateHash();
},
/**
* Called to update the hash for the map, based on map state and meta.
*/
updateHash: function() {
console.log('updating hash', {
from: location.hash,
to: this.formatHash(this.map, this.hashMeta),
});
location.hash = this.formatHash(this.map, this.hashMeta);
},
/**
* Registers event listeners. Window hash changes and map moveend events.
*/
startListening: function() {
if (this.isListening) {
return;
}
L.DomEvent.addListener(window, "hashchange", this.onHashChange);
this.map.on("moveend", this.onMapMove, this);
this.isListening = true;
},
/**
* Deregisters event listeners. Window hash changes and map moveend events.
*/
stopListening: function() {
if (! this.isListening) {
return;
}
this.map.off("moveend", this.onMapMove, this);
L.DomEvent.removeListener(window, "hashchange", this.onHashChange);
this.isListening = false;
},
};
L.hash = function(map, promise) {
return new L.Hash(map, promise);
};
L.Map.prototype.addHash = function() {
this._hash = L.hash(this);
};
L.Map.prototype.removeHash = function() {
this._hash.removeFrom();
};
})(window);