-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmandelbrot-hash-handler.js
73 lines (60 loc) · 1.97 KB
/
mandelbrot-hash-handler.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
import L from './leaflet-shim.js';
const Hash = L.Handler.extend({
addHooks() {
L.DomEvent.on(window, 'hashchange', this._onHashChange, this);
this._map.on('zoomend moveend iterationschange viewreset', this._updateHash, this);
this._map.whenReady(() => this._onHashChange({ newURL: window.location.href }));
},
removeHooks() {
L.DomEvent.off(window, 'hashchange', this._onHashChange, this);
this._map.off('zoomend moveend iterationschange viewreset', this._updateHash, this);
},
_updateHash() {
window.requestAnimationFrame(() => this._doUpdateHash());
},
_doUpdateHash() {
const center = this._map.getCenter();
const parts = [
center.lng,
center.lat,
this._map.getZoom(),
this._map.getIterations(),
];
this._ignoreNextHashChange = true;
window.location = this._partsToHash(parts);
},
_hashToParts(hash) {
hash = hash.slice(1);
const parts = hash.split(',')
.map(Number)
.filter(Number.isFinite);
if (parts.length === 4) {
return parts;
}
},
_partsToHash(parts) {
return '#' + parts.join(',');
},
_onHashChange(event) {
if (this._ignoreNextHashChange) {
this._ignoreNextHashChange = false;
return;
}
const hashIndex = event.newURL.indexOf('#');
if (hashIndex === -1) {
return;
}
const hash = event.newURL.slice(hashIndex);
const parts = this._hashToParts(hash);
if (parts === undefined) {
console.warn('Invalid hash. Expected something like: #0,0,1,64');
return;
}
this._setupMap(...parts);
},
_setupMap(real, imag, zoom, iterations) {
this._map.setIterations(iterations);
this._map.setView([imag, real], zoom);
}
});
L.Map.addInitHook('addHandler', 'mandelbrotHash', Hash);