-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Link.js
462 lines (412 loc) · 11.4 KB
/
Link.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/**
* @module ol/interaction/Link
*/
import EventType from '../events/EventType.js';
import Interaction from './Interaction.js';
import MapEventType from '../MapEventType.js';
import {listen, unlistenByKey} from '../events.js';
import {toFixed} from '../math.js';
/**
* @param {number} number A number.
* @return {number} A number with at most 5 decimal places.
*/
function to5(number) {
return toFixed(number, 5);
}
/**
* @param {string} string A string.
* @return {number} A number representing the string.
*/
function readNumber(string) {
return parseFloat(string);
}
/**
* @param {number} number A number.
* @return {string} A string representing the number.
*/
function writeNumber(number) {
return to5(number).toString();
}
/**
* @param {number} a A number.
* @param {number} b A number.
* @return {boolean} The numbers are different.
*/
function differentNumber(a, b) {
if (isNaN(a)) {
return false;
}
return a !== readNumber(writeNumber(b));
}
/**
* @param {Array<number>} a An array of two numbers.
* @param {Array<number>} b An array of two numbers.
* @return {boolean} The arrays are different.
*/
function differentArray(a, b) {
return differentNumber(a[0], b[0]) || differentNumber(a[1], b[1]);
}
/** @typedef {'x'|'y'|'z'|'r'|'l'} Params */
/**
* @typedef {function(string):void} Callback
*/
/**
* @typedef {Object} Options
* @property {boolean|import('../View.js').AnimationOptions} [animate=true] Animate view transitions.
* @property {Array<Params>} [params=['x', 'y', 'z', 'r', 'l']] Properties to track. Default is to track
* `x` (center x), `y` (center y), `z` (zoom), `r` (rotation) and `l` (layers).
* @property {boolean} [replace=false] Replace the current URL without creating the new entry in browser history.
* By default, changes in the map state result in a new entry being added to the browser history.
* @property {string} [prefix=''] By default, the URL will be updated with search parameters x, y, z, and r. To
* avoid collisions with existing search parameters that your application uses, you can supply a custom prefix for
* the ones used by this interaction (e.g. 'ol:').
*/
/**
* @classdesc
* An interaction that synchronizes the map state with the URL.
*
* @api
*/
class Link extends Interaction {
/**
* @param {Options} [options] Link options.
*/
constructor(options) {
super();
options = Object.assign(
{
animate: true,
params: ['x', 'y', 'z', 'r', 'l'],
replace: false,
prefix: '',
},
options || {},
);
let animationOptions;
if (options.animate === true) {
animationOptions = {duration: 250};
} else if (!options.animate) {
animationOptions = null;
} else {
animationOptions = options.animate;
}
/**
* @type {import('../View.js').AnimationOptions|null}
* @private
*/
this.animationOptions_ = animationOptions;
/**
* @type {Object<Params, boolean>}
* @private
*/
this.params_ = options.params.reduce((acc, value) => {
acc[value] = true;
return acc;
}, {});
/**
* @private
* @type {boolean}
*/
this.replace_ = options.replace;
/**
* @private
* @type {string}
*/
this.prefix_ = options.prefix;
/**
* @private
* @type {!Array<import("../events.js").EventsKey>}
*/
this.listenerKeys_ = [];
/**
* @private
* @type {boolean}
*/
this.initial_ = true;
this.updateState_ = this.updateState_.bind(this);
/**
* The tracked parameter callbacks.
* @private
* @type {Object<string, Callback>}
*/
this.trackedCallbacks_ = {};
/**
* The tracked parameter values.
* @private
* @type {Object<string, string|null>}
*/
this.trackedValues_ = {};
}
/**
* @private
* @param {string} name A parameter name.
* @return {string} A name with the prefix applied.
*/
getParamName_(name) {
if (!this.prefix_) {
return name;
}
return this.prefix_ + name;
}
/**
* @private
* @param {URLSearchParams} params The search params.
* @param {string} name The unprefixed parameter name.
* @return {string|null} The parameter value.
*/
get_(params, name) {
return params.get(this.getParamName_(name));
}
/**
* @private
* @param {URLSearchParams} params The search params.
* @param {string} name The unprefixed parameter name.
* @param {string} value The param value.
*/
set_(params, name, value) {
if (!(name in this.params_)) {
return;
}
params.set(this.getParamName_(name), value);
}
/**
* @private
* @param {URLSearchParams} params The search params.
* @param {string} name The unprefixed parameter name.
*/
delete_(params, name) {
if (!(name in this.params_)) {
return;
}
params.delete(this.getParamName_(name));
}
/**
* @param {import("../Map.js").default|null} map Map.
*/
setMap(map) {
const oldMap = this.getMap();
super.setMap(map);
if (map === oldMap) {
return;
}
if (oldMap) {
this.unregisterListeners_(oldMap);
}
if (map) {
this.initial_ = true;
this.updateState_();
this.registerListeners_(map);
}
}
/**
* @param {import("../Map.js").default} map Map.
* @private
*/
registerListeners_(map) {
this.listenerKeys_.push(
listen(map, MapEventType.MOVEEND, this.updateUrl_, this),
listen(map.getLayerGroup(), EventType.CHANGE, this.updateUrl_, this),
listen(map, 'change:layergroup', this.handleChangeLayerGroup_, this),
);
if (!this.replace_) {
addEventListener('popstate', this.updateState_);
}
}
/**
* @param {import("../Map.js").default} map Map.
* @private
*/
unregisterListeners_(map) {
for (let i = 0, ii = this.listenerKeys_.length; i < ii; ++i) {
unlistenByKey(this.listenerKeys_[i]);
}
this.listenerKeys_.length = 0;
if (!this.replace_) {
removeEventListener('popstate', this.updateState_);
}
const url = new URL(window.location.href);
const params = url.searchParams;
this.delete_(params, 'x');
this.delete_(params, 'y');
this.delete_(params, 'z');
this.delete_(params, 'r');
this.delete_(params, 'l');
window.history.replaceState(null, '', url);
}
/**
* @private
*/
handleChangeLayerGroup_() {
const map = this.getMap();
if (!map) {
return;
}
this.unregisterListeners_(map);
this.registerListeners_(map);
this.initial_ = true;
this.updateUrl_();
}
/**
* @private
*/
updateState_() {
const url = new URL(window.location.href);
const params = url.searchParams;
for (const key in this.trackedCallbacks_) {
const value = params.get(key);
if (key in this.trackedCallbacks_ && value !== this.trackedValues_[key]) {
this.trackedValues_[key] = value;
this.trackedCallbacks_[key](value);
}
}
const map = this.getMap();
if (!map) {
return;
}
const view = map.getView();
if (!view) {
return;
}
let updateView = false;
/**
* @type {import('../View.js').AnimationOptions}
*/
const viewProperties = {};
const zoom = readNumber(this.get_(params, 'z'));
if ('z' in this.params_ && differentNumber(zoom, view.getZoom())) {
updateView = true;
viewProperties.zoom = zoom;
}
const rotation = readNumber(this.get_(params, 'r'));
if ('r' in this.params_ && differentNumber(rotation, view.getRotation())) {
updateView = true;
viewProperties.rotation = rotation;
}
const center = [
readNumber(this.get_(params, 'x')),
readNumber(this.get_(params, 'y')),
];
if (
('x' in this.params_ || 'y' in this.params_) &&
differentArray(center, view.getCenter())
) {
updateView = true;
viewProperties.center = center;
}
if (updateView) {
if (!this.initial_ && this.animationOptions_) {
view.animate(Object.assign(viewProperties, this.animationOptions_));
} else {
if (viewProperties.center) {
view.setCenter(viewProperties.center);
}
if ('zoom' in viewProperties) {
view.setZoom(viewProperties.zoom);
}
if ('rotation' in viewProperties) {
view.setRotation(viewProperties.rotation);
}
}
}
const layers = map.getAllLayers();
const layersParam = this.get_(params, 'l');
if (
'l' in this.params_ &&
layersParam &&
layersParam.length === layers.length
) {
for (let i = 0, ii = layers.length; i < ii; ++i) {
const value = parseInt(layersParam[i]);
if (!isNaN(value)) {
const visible = Boolean(value);
const layer = layers[i];
if (layer.getVisible() !== visible) {
layer.setVisible(visible);
}
}
}
}
}
/**
* Register a listener for a URL search parameter. The callback will be called with a new value
* when the corresponding search parameter changes due to history events (e.g. browser navigation).
*
* @param {string} key The URL search parameter.
* @param {Callback} callback The function to call when the search parameter changes.
* @return {string|null} The initial value of the search parameter (or null if absent from the URL).
* @api
*/
track(key, callback) {
this.trackedCallbacks_[key] = callback;
const url = new URL(window.location.href);
const params = url.searchParams;
const value = params.get(key);
this.trackedValues_[key] = value;
return value;
}
/**
* Update the URL with a new search parameter value. If the value is null, it will be
* deleted from the search parameters.
*
* @param {string} key The URL search parameter.
* @param {string|null} value The updated value (or null to remove it from the URL).
* @api
*/
update(key, value) {
const url = new URL(window.location.href);
const params = url.searchParams;
if (value === null) {
params.delete(key);
} else {
params.set(key, value);
}
if (key in this.trackedValues_) {
this.trackedValues_[key] = value;
}
this.updateHistory_(url);
}
/**
* @private
*/
updateUrl_() {
const map = this.getMap();
if (!map) {
return;
}
const view = map.getView();
if (!view) {
return;
}
const center = view.getCenter();
const zoom = view.getZoom();
const rotation = view.getRotation();
const layers = map.getAllLayers();
const visibilities = new Array(layers.length);
for (let i = 0, ii = layers.length; i < ii; ++i) {
visibilities[i] = layers[i].getVisible() ? '1' : '0';
}
const url = new URL(window.location.href);
const params = url.searchParams;
this.set_(params, 'x', writeNumber(center[0]));
this.set_(params, 'y', writeNumber(center[1]));
this.set_(params, 'z', writeNumber(zoom));
this.set_(params, 'r', writeNumber(rotation));
this.set_(params, 'l', visibilities.join(''));
this.updateHistory_(url);
this.initial_ = false;
}
/**
* @private
* @param {URL} url The URL.
*/
updateHistory_(url) {
if (url.href !== window.location.href) {
if (this.initial_ || this.replace_) {
window.history.replaceState(history.state, '', url);
} else {
window.history.pushState(null, '', url);
}
}
}
}
export default Link;