-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathlayer.js
532 lines (503 loc) · 15.8 KB
/
layer.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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
import './leaflet.js'; // a lightly modified version of Leaflet for use as browser module
import './mapml.js'; // modified URI to make the function a property of window scope (possibly a bad thing to do).
export class MapLayer extends HTMLElement {
static get observedAttributes() {
return ['src', 'label', 'checked', 'hidden', 'opacity'];
}
get src() {
return this.hasAttribute('src') ? this.getAttribute('src') : '';
}
set src(val) {
if (val) {
this.setAttribute('src', val);
}
}
get label() {
if (this._layer) return this._layer.getName();
else return this.hasAttribute('label') ? this.getAttribute('label') : '';
}
set label(val) {
if (val) {
this.setAttribute('label', val);
}
}
get checked() {
return this.hasAttribute('checked');
}
set checked(val) {
if (val) {
this.setAttribute('checked', '');
} else {
this.removeAttribute('checked');
}
}
get hidden() {
return this.hasAttribute('hidden');
}
set hidden(val) {
if (val) {
this.setAttribute('hidden', '');
} else {
this.removeAttribute('hidden');
}
}
get opacity() {
// use ?? since 0 is falsy, || would return rhs in that case
return +(this._opacity ?? this.getAttribute('opacity'));
}
set opacity(val) {
if (+val > 1 || +val < 0) return;
this.setAttribute('opacity', val);
}
get extent() {
// calculate the bounds of all content, return it.
if (!this._layer.bounds) {
this._layer._calculateBounds();
}
return Object.assign(
M._convertAndFormatPCRS(
this._layer.bounds,
this._layer._properties.crs,
this._layer._properties.projection
),
{ zoom: this._layer.zoomBounds }
);
}
constructor() {
// Always call super first in constructor
super();
}
disconnectedCallback() {
// if the map-layer node is removed from the dom, the layer should be
// removed from the map and the layer control
if (this.hasAttribute('data-moving')) return;
this._onRemove();
}
_onRemove() {
if (this._layer) {
this._layer.off();
}
// if this layer has never been connected, it will not have a _layer
if (this._layer && this._layer._map) {
this._layer._map.removeLayer(this._layer);
}
if (this._layerControl && !this.hidden) {
this._layerControl.removeLayer(this._layer);
}
delete this._layer;
delete this._fetchError;
if (this.shadowRoot) {
this.shadowRoot.innerHTML = '';
}
}
connectedCallback() {
if (this.hasAttribute('data-moving')) return;
this._createLayerControlHTML = M._createLayerControlHTML.bind(this);
// this._opacity is used to record the current opacity value (with or without updates),
// the initial value of this._opacity should be set as opacity attribute value, if exists, or the default value 1.0
this._opacity = this.opacity || 1.0;
const doConnected = this._onAdd.bind(this);
this.parentElement
.whenReady()
.then(() => {
doConnected();
})
.catch(() => {
throw new Error('Map never became ready');
});
}
_onAdd() {
if (this.getAttribute('src') && !this.shadowRoot) {
this.attachShadow({ mode: 'open' });
}
new Promise((resolve, reject) => {
this.addEventListener(
'map-changestyle',
function (e) {
e.stopPropagation();
this.src = e.detail.src;
},
{ once: true }
);
this.addEventListener(
'changeprojection',
function (e) {
e.stopPropagation();
reject(e);
},
{ once: true }
);
let base = this.baseURI ? this.baseURI : document.baseURI;
const headers = new Headers();
headers.append('Accept', 'text/mapml');
if (this.src) {
fetch(this.src, { headers: headers })
.then((response) => {
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
return response.text();
})
.then((mapml) => {
let content = new DOMParser().parseFromString(mapml, 'text/xml');
if (
content.querySelector('parsererror') ||
!content.querySelector('mapml-')
) {
throw new Error('Parser error');
}
if (this._layer) {
this._onRemove();
}
this._layer = M.mapMLLayer(
new URL(this.src, base).href,
this,
content,
{
mapprojection: this.parentElement.projection,
opacity: this.opacity
}
);
this._createLayerControlHTML();
this._attachedToMap();
this._validateDisabled();
resolve();
})
.catch((error) => {
this._fetchError = true;
console.log('Error fetching layer content' + error);
});
} else {
if (this._layer) {
this._onRemove();
}
this._layer = M.mapMLLayer(null, this, null, {
mapprojection: this.parentElement.projection,
opacity: this.opacity
});
this._createLayerControlHTML();
this._attachedToMap();
this._validateDisabled();
resolve();
}
}).catch((e) => {
if (e.type === 'changeprojection') {
this.src = e.detail.href;
} else {
console.log(e);
}
});
}
_attachedToMap() {
// set i to the position of this layer element in the set of layers
var i = 0,
position = 1;
for (var nodes = this.parentNode.children; i < nodes.length; i++) {
if (this.parentNode.children[i].nodeName === 'LAYER-') {
if (this.parentNode.children[i] === this) {
position = i + 1;
} else if (this.parentNode.children[i]._layer) {
this.parentNode.children[i]._layer.setZIndex(i + 1);
}
}
}
var proj = this.parentNode.projection
? this.parentNode.projection
: 'OSMTILE';
L.setOptions(this._layer, {
zIndex: position,
mapprojection: proj,
opacity: window.getComputedStyle(this).opacity
});
// make sure the Leaflet layer has a reference to the map
this._layer._map = this.parentNode._map;
if (this.checked) {
this._layer.addTo(this._layer._map);
}
this._layer.on('add remove', this._validateDisabled, this);
// toggle the this.disabled attribute depending on whether the layer
// is: same prj as map, within view/zoom of map
this._layer._map.on('moveend layeradd', this._validateDisabled, this);
// if controls option is enabled, insert the layer into the overlays array
if (this.parentNode._layerControl && !this.hidden) {
this._layerControl = this.parentNode._layerControl;
this._layerControl.addOrUpdateOverlay(this._layer, this.label);
}
// the mapml document associated to this layer can in theory contain many
// link[@rel=legend] elements with different @type or other attributes;
// currently only support a single link, don't care about type, lang etc.
// TODO: add support for full LayerLegend object, and > one link.
if (this._layer._legendUrl) {
this.legendLinks = [
{
type: 'application/octet-stream',
href: this._layer._legendUrl,
rel: 'legend',
lang: null,
hreflang: null,
sizes: null
}
];
}
// re-use 'loadedmetadata' event from HTMLMediaElement inteface, applied
// to MapML extent as metadata
// https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement/loadedmetadata_event
this.dispatchEvent(
new CustomEvent('loadedmetadata', { detail: { target: this } })
);
}
attributeChangedCallback(name, oldValue, newValue) {
switch (name) {
case 'label':
this.whenReady()
.then(() => {
this._layer.setName(newValue);
})
.catch((e) => {
console.log(e);
});
break;
case 'checked':
this.whenReady()
.then(() => {
if (typeof newValue === 'string') {
this.parentElement._map.addLayer(this._layer);
} else {
this.parentElement._map.removeLayer(this._layer);
}
this._layerControlCheckbox.checked = this.checked;
this.dispatchEvent(new CustomEvent('map-change'));
})
.catch((e) => {
console.log(e);
});
break;
case 'hidden':
var map = this.parentElement && this.parentElement._map;
if (map && this.parentElement.controls) {
if (typeof newValue === 'string') {
if (this._layer) {
this.parentElement._layerControl.removeLayer(this._layer);
}
} else {
this._layerControl = this.parentElement._layerControl;
this._layerControl.addOrUpdateOverlay(this._layer, this.label);
this._validateDisabled();
}
}
break;
case 'opacity':
if (oldValue !== newValue && this._layer) {
this._opacity = newValue;
this._layer.changeOpacity(newValue);
}
break;
case 'src':
if (oldValue !== newValue && this._layer) {
this._onRemove();
if (this.isConnected) {
this._onAdd();
}
// the original inline content will not be removed
// but has NO EFFECT and works as a fallback
}
}
}
_validateDisabled() {
// setTimeout is necessary to make the validateDisabled happen later than the moveend operations etc.,
// to ensure that the validated result is correct
setTimeout(() => {
let layer = this._layer,
map = layer?._map;
if (map) {
// prerequisite: no inline and remote mapml elements exists at the same time
const mapExtents = this.shadowRoot
? this.shadowRoot.querySelectorAll('map-extent')
: this.querySelectorAll('map-extent');
let disabledExtentCount = 0,
totalExtentCount = 0,
layerTypes = [
'_staticTileLayer',
'_imageLayer',
'_mapmlvectors',
'_templatedLayer'
];
for (let j = 0; j < layerTypes.length; j++) {
let type = layerTypes[j];
if (this.checked) {
if (type === '_templatedLayer' && mapExtents.length > 0) {
for (let i = 0; i < mapExtents.length; i++) {
totalExtentCount++;
if (mapExtents[i]._validateDisabled()) disabledExtentCount++;
}
} else if (layer[type]) {
// not a templated layer
totalExtentCount++;
if (!layer[type].isVisible) disabledExtentCount++;
}
}
}
// if all extents are not visible / disabled, set layer to disabled
if (
disabledExtentCount === totalExtentCount &&
disabledExtentCount !== 0
) {
this.setAttribute('disabled', '');
this.disabled = true;
} else {
this.removeAttribute('disabled');
this.disabled = false;
}
this.toggleLayerControlDisabled();
}
}, 0);
}
// disable/italicize layer control elements based on the layer-.disabled property
toggleLayerControlDisabled() {
let input = this._layerControlCheckbox,
label = this._layerControlLabel,
opacityControl = this._opacityControl,
opacitySlider = this._opacitySlider,
styleControl = this._styles;
if (this.disabled) {
input.disabled = true;
opacitySlider.disabled = true;
label.style.fontStyle = 'italic';
opacityControl.style.fontStyle = 'italic';
if (styleControl) {
styleControl.style.fontStyle = 'italic';
styleControl.querySelectorAll('input').forEach((i) => {
i.disabled = true;
});
}
} else {
input.disabled = false;
opacitySlider.disabled = false;
label.style.fontStyle = 'normal';
opacityControl.style.fontStyle = 'normal';
if (styleControl) {
styleControl.style.fontStyle = 'normal';
styleControl.querySelectorAll('input').forEach((i) => {
i.disabled = false;
});
}
}
}
getOuterHTML() {
let tempElement = this.cloneNode(true);
if (this.hasAttribute('src')) {
let newSrc = this._layer.getHref();
tempElement.setAttribute('src', newSrc);
}
if (this.querySelector('map-link')) {
let mapLinks = tempElement.querySelectorAll('map-link');
mapLinks.forEach((mapLink) => {
if (mapLink.hasAttribute('href')) {
mapLink.setAttribute(
'href',
decodeURI(
new URL(
mapLink.attributes.href.value,
this.baseURI ? this.baseURI : document.baseURI
).href
)
);
} else if (mapLink.hasAttribute('tref')) {
mapLink.setAttribute(
'tref',
decodeURI(
new URL(
mapLink.attributes.tref.value,
this.baseURI ? this.baseURI : document.baseURI
).href
)
);
}
});
}
let outerLayer = tempElement.outerHTML;
tempElement.remove();
return outerLayer;
}
zoomTo() {
this.whenElemsReady().then(() => {
let map = this.parentElement._map,
extent = this.extent,
tL = extent.topLeft.pcrs,
bR = extent.bottomRight.pcrs,
layerBounds = L.bounds(
L.point(tL.horizontal, tL.vertical),
L.point(bR.horizontal, bR.vertical)
),
center = map.options.crs.unproject(layerBounds.getCenter(true));
let maxZoom = extent.zoom.maxZoom,
minZoom = extent.zoom.minZoom;
map.setView(center, M.getMaxZoom(layerBounds, map, minZoom, maxZoom), {
animate: false
});
});
}
mapml2geojson(options = {}) {
return M.mapml2geojson(this, options);
}
pasteFeature(feature) {
switch (typeof feature) {
case 'string':
feature.trim();
if (
feature.slice(0, 12) === '<map-feature' &&
feature.slice(-14) === '</map-feature>'
) {
this.insertAdjacentHTML('beforeend', feature);
}
break;
case 'object':
if (feature.nodeName.toUpperCase() === 'MAP-FEATURE') {
this.appendChild(feature);
}
}
}
whenReady() {
return new Promise((resolve, reject) => {
let interval, failureTimer;
if (this._layer && (!this.src || this.shadowRoot?.childNodes.length)) {
resolve();
} else {
let layerElement = this;
interval = setInterval(testForLayer, 200, layerElement);
failureTimer = setTimeout(layerNotDefined, 5000);
}
function testForLayer(layerElement) {
if (
layerElement._layer &&
(!layerElement.src || layerElement.shadowRoot?.childNodes.length)
) {
clearInterval(interval);
clearTimeout(failureTimer);
resolve();
} else if (layerElement._fetchError) {
clearInterval(interval);
clearTimeout(failureTimer);
reject('Error fetching layer content');
}
}
function layerNotDefined() {
clearInterval(interval);
clearTimeout(failureTimer);
reject('Timeout reached waiting for layer to be ready');
}
});
}
// check if all child elements are ready
whenElemsReady() {
let elemsReady = [];
let target = this.shadowRoot || this;
for (let elem of [
...target.querySelectorAll('map-extent'),
...target.querySelectorAll('map-feature')
]) {
elemsReady.push(elem.whenReady());
}
return Promise.allSettled(elemsReady);
}
}