forked from mlevans/leaflet-hash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
leaflet-hash.js
224 lines (189 loc) · 6.57 KB
/
leaflet-hash.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
(function(window) {
var HAS_HASHCHANGE = (function() {
var doc_mode = window.documentMode;
return ('onhashchange' in window) &&
(doc_mode === undefined || doc_mode > 7);
})();
L.Hash = function(map) {
this.onHashChange = L.Util.bind(this.onHashChange, this);
if (map) {
this.init(map);
}
};
// Change this to parse the different options as an object.
L.Hash.parseHash = function(hash) {
var params = getHashParams();
if( typeof params.zoom != 'undefined' || typeof params.lat != 'undefined' || typeof params.lon != 'undefined') {
var zoom = parseInt(params.zoom.values, 10),
lat = parseFloat(params.lat.values),
lon = parseFloat(params.lon.values);
if (isNaN(zoom) || isNaN(lat) || isNaN(lon)) {
return false;
} else {
return {
center: {'lat': lat, 'lon': lon},
zoom: zoom,
params: params
};
}
} else {
return false;
}
};
L.Hash.formatHash = function(map) {
var center = map.getCenter(),
zoom = map.getZoom(),
precision = Math.max(0, Math.ceil(Math.log(zoom) / Math.LN2)),
returnObj = {
'zoom': { values: zoom, comparator: '='},
'lat': { values: center.lat.toFixed(precision), comparator: '=' },
'lon': { values: center.lng.toFixed(precision), comparator: '=' }
};
return returnObj;
};
L.Hash.prototype = {
map: null,
lastHash: null,
parseHash: L.Hash.parseHash,
formatHash: L.Hash.formatHash,
init: function(map) {
this.map = map;
// 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
var hash;
if (this.movingMap || !this.map._loaded) {
return false;
}
// If getHashParams returns no zoom, etc it is created for initialization
if ( !this.parseHash(hash) ){
hash = getHashParams();
_.extend(hash, this.formatHash(this.map) );
updateUrlHash(hash);
} else {
hash = getHashParams();
var newParams = this.formatHash(this.map);
_.each(newParams, function(val,key){
hash[key].values = val.values;
hash[key].comparator = '=';
});
updateUrlHash(hash);
}
this.lastHash = hash;
},
movingMap: false,
update: function() {
// var hash = getHashParams();
if ( _.isEqual( hash, this.lastHash) ){
return false;
}
var parsed = this.parseHash(hash);
if (parsed) {
this.movingMap = true;
this.map.setView(parsed.center, parsed.zoom);
this.movingMap = false;
} else {
this.onMapMove(this.map);
}
},
// defer hash change updates every 200ms
changeDefer: 200,
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", this.onMapMove, this);
if (HAS_HASHCHANGE) {
L.DomEvent.addListener(window, "hashchange", this.onHashChange);
} else {
clearInterval(this.hashChangeInterval);
this.hashChangeInterval = setInterval(this.onHashChange, 500);
}
this.isListening = true;
},
stopListening: function() {
this.map.off("moveend", this.onMapMove, this);
if (HAS_HASHCHANGE) {
L.DomEvent.removeListener(window, "hashchange", this.onHashChange);
} else {
clearInterval(this.hashChangeInterval);
}
this.isListening = false;
}
};
L.hash = function(map) {
return new L.Hash(map);
};
L.Map.prototype.addHash = function() {
this._hash = L.hash(this);
};
L.Map.prototype.removeHash = function() {
this._hash.removeFrom();
};
})(window);
// Return the hash parameters from the current URL. [source](http://goo.gl/mebsOI)
function getHashParams(){
var hashParams = {};
var e,
a = /\+/g, // Regex for replacing addition symbol with a space
r = /([^!&;=<>]+)(!?[=><]?)([^&;]*)/g,
d = function (s) { return decodeURIComponent(s.replace(a, ' ')); },
q = window.location.hash.substring(1).replace(/^!\/?/, '');
while (e = r.exec(q)) {
hashParams[d(e[1])] = {
values: d(e[3]),
comparator: d(e[2])
};
}
return hashParams;
}
// The `generateUrlHash` method builds and returns a URL hash from a set of object parameters
function updateUrlHash(params) {
var newHash,
hashParams = [];
// Loop through params, stringify them and push them into the temp array.
function buildHashParam( param, name ) {
hashParams.push( name + '=' + param.values );
}
_.forEach( params, buildHashParam );
newHash = '&' + hashParams.join('&');
window.location.hash = newHash;
}
// Create a parameter from scratch (automatically builds object)
function addParam( paramName, values ){
var params = getHashParams();
params[paramName] = {};
params[paramName].values = values;
params[paramName].comparator = '=';
updateUrlHash(params);
}
/*
END HASH PARSING FUNCTIONS
*/