Repository navigation
Expand file tree
/
Copy pathlayer-control.js
More file actions
383 lines (351 loc) · 10.7 KB
/
Copy pathlayer-control.js
File metadata and controls
383 lines (351 loc) · 10.7 KB
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
import { h, t } from "./dom.js";
const layersSVG = `<svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M4 8L12 4L20 8L12 12L4 8Z" stroke="#000000" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
<path d="M4 12L12 16L20 12" stroke="#000000" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
<path d="M4 16L12 20L20 16" stroke="#000000" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
</svg>`;
const linkSVG = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 15 15">
<path fill="currentColor" d="M5.11 4C5.36 4.28 5.66 4.51 5.99 4.68L6.06 4.71L4.76 6L2 6L2 13L9 13L9 10.25L10.3 8.95L10.31 8.98C10.48 9.32 10.72 9.63 11 9.89L11 13C11 14.1 10.1 15 9 15L2 15C0.9 15 0 14.1 0 13L0 6C0 4.9 0.9 4 2 4ZM6.7 9.72C6.31 10.11 5.68 10.11 5.29 9.72C4.9 9.33 4.9 8.69 5.29 8.3L7.59 6L9.52 4.07L10.59 3L7.35 3C6.84 3 6.42 2.62 6.36 2.13L6.35 2C6.35 1.45 6.8 1 7.35 1L14 1L14 7.66C14 8.21 13.56 8.66 13 8.66C12.45 8.66 12 8.21 12 7.66L12 4.41L6.7 9.72Z"/>
</svg>`;
const toggleId = "maplibre-layer-ctrl-toggle";
const css = new CSSStyleSheet();
css.replaceSync(`
/* Size & placement of layer control icon */
.maplibre-layer-ctrl .maplibregl-ctrl-icon svg {
display: block;
height: 24px;
width: 24px;
margin: 2.5px
}
/* pointer */
.maplibregl-ctrl-icon {
cursor:pointer;
}
/* hide layer control icon if checkbox is clicked */
#${toggleId}:checked ~ .maplibregl-ctrl-icon {
display: none;
}
/* position ✕ for closing menu */
.maplibre-layer-ctrl-close {
float: right;
font-weight: bold;
line-height: 12px;
padding: 6px 6px;
margin: 0px 6px;
cursor: pointer;
}
/* Menu typography */
.maplibregl-layer-ctrl-menu h1 {
margin: 5px 25px 10px 5px;
font-weight: bold;
font-size: 20px;
}
.maplibre-layer-ctrl-layer.nodescription {
line-height: 40px;
}
.maplibre-layer-ctrl-layer img {
float: left;
height: 40px;
border-radius: 4px;
margin-right: 5px;
border: 2px solid white;
}
.maplibre-layer-ctrl-layer {
display: block;
cursor: pointer;
font-size: 1.25em;
min-height: 45px;
background: rgb(127,127,127,0.1);
margin: 2px;
padding: 5px 15px 0px 10px;
border-radius: 4px;
}
.maplibre-layer-ctrl-layer input {
float: right;
margin-top: 15px;
}
.maplibre-layer-ctrl-layer .maplibre-layer-ctrl-external-link {
float: right;
margin-top: 12px;
}
.maplibre-layer-ctrl-layer small {
display: block;
}
label.maplibre-layer-ctrl-layer:has(input:checked) img {
border: 2px solid black;
}
label.maplibre-layer-ctrl-layer:has(input:checked) {
font-weight: bold;
background: rgb(127,127,127,0.2);
}
.maplibre-layer-ctrl-external-link {
width: 15px;
height: 15px;
color: #333;
}
.maplibre-layer-ctrl-external-link:hover {
color: #000;
}
.maplibre-layer-ctrl-external-link svg {
display: block;
width: 100%;
height: 100%;
}
.maplibre-layer-ctrl-layer .maplibre-layer-ctrl-external-link:not(:only-child) {
margin-right: 5px;
}
/* show menu if checkbox is clicked */
.maplibregl-layer-ctrl-menu .maplibregl-ctrl-icon {
vertical-align: sub;
cursor: inherit;
}
.maplibregl-layer-ctrl-menu .maplibregl-ctrl-icon svg {
display: inline-block;
}
.maplibregl-layer-ctrl-menu {
display: none;
width: min(-20px + 100vw, 400px);
}
#${toggleId}:checked ~ .maplibregl-layer-ctrl-menu {
display: block;
}
/* don't show checkbox */
#${toggleId} {
display: none;
}
`);
export default class LayerControl {
#map;
#container;
#layers;
#button;
#menu;
#currentStyle;
#currentLayers;
constructor({ layers = [] }) {
this.#layers = layers;
this.#currentLayers = [];
}
#fmtURL(urlTemplate, { z, x, y }) {
const bounds = this.#map.getBounds();
const sw = bounds.getSouthWest();
const ne = bounds.getNorthEast();
return urlTemplate
.replaceAll("{z}", z)
.replaceAll("{Z}", z + 1)
.replaceAll("{zr}", Math.round(z))
.replaceAll("{Zr}", Math.round(z) + 1)
.replaceAll("{x}", x)
.replaceAll("{y}", y)
.replaceAll("{xMin}", sw.lng)
.replaceAll("{yMin}", sw.lat)
.replaceAll("{xMax}", ne.lng)
.replaceAll("{yMax}", ne.lat)
.replaceAll("{xDelta}", ne.lng - sw.lng)
.replaceAll("{yDelta}", ne.lat - sw.lat);
}
#updateLinks() {
const center = this.#map.getCenter();
const zoom = this.#map.getZoom();
const xyz = { z: zoom, x: center.lng, y: center.lat };
this.#menu
.querySelectorAll(".maplibre-layer-ctrl-external-link")
.forEach((a) => {
const urlTemplate = a.dataset.url;
if (urlTemplate) {
a.href = this.#fmtURL(urlTemplate, xyz);
}
});
}
#updateHash() {
//window.location.search
let frag = `layers=${this.#currentStyle}`;
for (const lyr of this.#currentLayers) {
frag += `,${lyr}`;
}
let fragments = window.location.hash.slice(1).split("&");
if (fragments.filter((f) => f.startsWith("layers=")).length === 0) {
fragments.push(frag);
} else {
fragments = fragments.map((f) => (f.startsWith("layers=") ? frag : f));
}
const location = window.location.href.replace(
/(#.*)?$/,
"#" + fragments.join("&"),
);
window.history.replaceState(window.history.state, null, location);
}
#enableLayer(layer) {
if (layer.style) {
if (this.#currentStyle !== layer.id) {
this.#currentStyle = layer.id;
this.#map.setStyle(layer.style, {
transformStyle: (_, style) => {
for (const layer of this.#layers.filter(({ id }) =>
this.#currentLayers.includes(id),
)) {
if (typeof style.sprite === "string") {
style.sprite = [{ id: "default", url: style.sprite }];
}
const layerIds = style.layers.map(({ id }) => id);
const spriteIds = style.sprite.map(({ id }) => id);
style.sources = { ...style.sources, ...layer.sources };
style.layers = [
...style.layers,
...layer.layers.filter(({ id }) => !layerIds.includes(id)),
];
style.sprite = [
...style.sprite,
...(layer.sprite || []).filter(
({ id }) => !spriteIds.includes(id),
),
];
}
return style;
},
});
}
} else {
if (!this.#currentLayers.includes(layer.id))
this.#currentLayers.push(layer.id);
for (const [id, src] of Object.entries(layer.sources || {})) {
this.#map.addSource(id, src);
}
const sprites = this.#map.getSprite();
for (const sprite of layer.sprite || []) {
if (sprites.filter(({ id }) => id === sprite.id).length === 0) {
sprites.push(sprite);
}
}
this.#map.setSprite(sprites);
for (const lyr of layer.layers || []) {
this.#map.addLayer(lyr);
}
}
}
#disableLayer(layer) {
this.#currentLayers = this.#currentLayers.filter((id) => id !== layer.id);
for (const lyr of layer.layers || []) {
this.#map.removeLayer(lyr.id);
}
for (const [id, src] of Object.entries(layer.sources || {})) {
this.#map.removeSource(id);
}
}
onAdd(map) {
document.adoptedStyleSheets.push(css);
this.#map = map;
let layers = window.location.hash
.slice(1)
.split("&")
.filter((f) => f.startsWith("layers="))[0];
if (layers) {
layers = layers.slice(7).split(",");
this.#currentLayers = layers.slice(1);
const [layer] = this.#layers.filter(({ id }) => id === layers[0]);
this.#enableLayer(layer);
} else {
// If no layers configured in hash, default to just using the first base style
this.#enableLayer(this.#layers.filter(({ style }) => style)[0]);
}
this.#button = h("label", {
className: "maplibregl-ctrl-icon",
for: toggleId,
title: "layers",
});
this.#button.innerHTML = layersSVG;
const icon = h("label", {
className: "maplibregl-ctrl-icon",
title: "layers",
});
icon.innerHTML = layersSVG;
const onclick = (e) => {
const [layer] = this.#layers.filter(({ id }) => id === e.target.value);
if (e.target.checked) {
this.#enableLayer(layer);
} else {
this.#disableLayer(layer);
}
this.#updateHash();
};
this.#menu = h(
"div",
{ class: "maplibregl-layer-ctrl-menu" },
h(
"label",
{ for: toggleId, className: "maplibre-layer-ctrl-close" },
t("✕"),
),
h("h1", {}, icon, t("Layers")),
...this.#layers.map(({ id, title, description, icon, style, url }, i) => {
const hasContent = style || (this.#layers[i].layers && this.#layers[i].layers.length > 0);
const children = [];
if (hasContent) {
children.push(h("input", {
type: style ? "radio" : "checkbox",
value: id,
id: `maplibre-layer-ctrl-layer-${id}`,
name: "style",
checked:
this.#currentStyle === id || this.#currentLayers.includes(id),
onclick,
}));
}
if (url) {
children.push(h(
"a",
{
className: "maplibre-layer-ctrl-external-link",
target: "_blank",
"data-url": url,
title: `Open in ${title}`,
onclick: (e) => e.stopPropagation(),
},
linkSVG,
));
}
if (icon) children.push(h("img", { src: icon }));
children.push(t(title));
if (description) {
children.push(h("br"));
children.push(h("small", {}, t(description)));
}
return h(
"label",
{
className:
"maplibre-layer-ctrl-layer" +
(description ? "" : " nodescription"),
onclick: !hasContent && url ? (e) => {
const center = this.#map.getCenter();
const zoom = this.#map.getZoom();
const xyz = { z: zoom, x: center.lng, y: center.lat };
window.open(this.#fmtURL(url, xyz), "_blank");
} : null,
},
...children
);
}),
);
this.#map.on("move", () => this.#updateLinks());
this.#updateLinks();
this.#container = h(
"div",
{
className: "maplibregl-ctrl maplibregl-ctrl-group maplibre-layer-ctrl",
},
h("input", { type: "checkbox", id: toggleId, checked: false }),
this.#button,
this.#menu,
);
return this.#container;
}
onRemove() {
this.#container.parentNode.removeChild(this.#container);
this.#container = undefined;
this.#map = undefined;
}
getDefaultPosition() {
return "bottom-right";
}
}