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
|
import mapboxgl from "mapbox-gl";
var commandRegistry = {};
export function registerCustomElement(settings) {
const options = Object.assign(
{
onMount() {}
},
settings
);
if (options.token) {
mapboxgl.accessToken = options.token;
}
window.customElements.define(
"elm-mapbox-map",
class MapboxMap extends window.HTMLElement {
constructor() {
super();
this._refreshExpiredTiles = true;
this._renderWorldCopies = true;
this.interactive = true;
this._eventRegistrationQueue = {};
this._eventListenerMap = new Map();
}
get mapboxStyle() {
return this._style;
}
set mapboxStyle(value) {
if (this._map) this._map.setStyle(value);
this._style = value;
}
get minZoom() {
return this._minZoom;
}
set minZoom(value) {
if (this._map) this._map.setMinZoom(value);
this._minZoom = value;
}
get maxZoom() {
return this._maxZoom;
}
set maxZoom(value) {
if (this._map) this._map.setMaxZoom(value);
this._maxZoom = value;
}
get map() {
return this._map;
}
get maxBounds() {
return this._maxBounds;
}
set maxBounds(value) {
if (this._map) this._map.setBounds(value);
this._maxBounds = value;
}
get renderWorldCopies() {
return this._renderWorldCopies;
}
set renderWorldCopies(value) {
if (this._map) this._map.setRenderWorldCopies(value);
this._renderWorldCopies = value;
}
get center() {
return this._center;
}
set center(value) {
if (this._map) this._map.setCenter(value);
this._center = value;
}
get zoom() {
return this._zoom;
}
set zoom(value) {
if (this._map) this._map.setZoom(value);
this._zoom = value;
}
get bearing() {
return this._bearing;
}
set bearing(value) {
if (this._map) this._map.setBearing(value);
this._bearing = value;
}
get pitch() {
return this._pitch;
}
set pitch(value) {
if (this._map) this._map.setPitch(value);
this._pitch = value;
}
get featureState() {
return this._featureState;
}
set featureState(value) {
// TODO: Clean this up
function makeId({ id, source, sourceLayer }) {
return `${id}::${source}::${sourceLayer}`;
}
if (this._map) {
const map = new Map(
this._featureState.map(([feature, state]) => [
makeId(feature),
{ feature, state }
])
);
value.forEach(([feature, state]) => {
const id = makeId(feature);
if (map.has(id)) {
const prevValue = map.get(id).state;
const keys = Object.keys(prevValue);
let newValue = {};
keys.forEach(k => {
if (state[k] === undefined) {
newValue[k] = undefined;
}
});
this._map.setFeatureState(
feature,
Object.assign(newValue, state)
);
} else {
this._map.setFeatureState(feature, state);
}
map.delete(id);
});
map.forEach(({ feature, state }) => {
const keys = Object.keys(state);
let newValue = {};
keys.forEach(k => {
newValue[k] = undefined;
});
this._map.setFeatureState(feature, newValue);
});
}
this._featureState = value;
}
addEventListener(type, fn, ...args) {
if (this._map) {
var wrapped;
if (
[
"mousedown",
"mouseup",
"mouseover",
"mousemove",
"click",
"dblclick",
"mouseout",
"contextmenu",
"zoom",
"zoomstart",
"zoomend",
"rotate",
"rotatestart",
"rotateend"
].includes(type)
) {
wrapped = e => {
e.features = this._map.queryRenderedFeatures(e.point, {
layers: this.eventFeaturesLayers,
filter: this.eventFeaturesFilter
});
return fn(e);
};
} else if (["touchend", "touchmove", "touchcancel"].includes(type)) {
wrapped = e => {
e.features = this._map.queryRenderedFeatures([e.point], {
layers: this.eventFeaturesLayers,
filter: this.eventFeaturesFilter
});
e.perPointFeatures = e.points.map(point =>
this._map.queryRenderedFeatures(point, {
layers: this.eventFeaturesLayers,
filter: this.eventFeaturesFilter
})
);
return fn(e);
};
} else {
wrapped = fn;
}
this._eventListenerMap.set(fn, wrapped);
return this._map.on(type, wrapped);
} else {
this._eventRegistrationQueue[type] =
this._eventRegistrationQueue[type] || [];
return this._eventRegistrationQueue[type].push(fn);
}
}
removeEventListener(type, fn, ...args) {
if (this._map) {
const wrapped = this._eventListenerMap.get(fn);
this._eventListenerMap.delete(fn);
return this._map.off(type, wrapped);
} else {
const queue = this._eventRegistrationQueue[type] || [];
const index = queue.findIndex(fn);
if (index >= 0) {
queue.splice(index, 1);
}
return;
}
}
_createMapInstance() {
let mapOptions = {
container: this,
style: this._style,
minZoom: this._minZoom || 0,
maxZoom: this._maxZoom || 22,
interactive: this.interactive,
attributionControl: false,
logoPosition: this.logoPosition || "bottom-left",
refreshExpiredTiles: this._refreshExpiredTiles,
maxBounds: this._maxBounds,
renderWorldCopies: this._renderWorldCopies
};
if (this._center) {
mapOptions.center = this._center;
}
if (this._zoom) {
mapOptions.zoom = this._zoom;
}
if (this._bearing) {
mapOptions.bearing = this._bearing;
}
if (this._pitch) {
mapOptions.pitch = this._pitch;
}
this._map = new mapboxgl.Map(mapOptions);
Object.entries(this._eventRegistrationQueue).forEach(
([type, listeners]) => {
listeners.forEach(listener => {
this.addEventListener(type, listener);
});
}
);
this._eventRegistrationQueue = {};
options.onMount(this._map, this);
if (commandRegistry[this.id]) {
this._map.on("load", () => {
var cmd;
while ((cmd = commandRegistry[this.id].shift())) {
cmd(this._map);
}
});
}
return this._map;
}
connectedCallback() {
if (this.token) {
mapboxgl.accessToken = this.token;
}
this.style.display = "block";
this.style.width = "100%";
this.style.height = "100%";
this._map = this._createMapInstance();
}
disconnectedCallback() {
this._map.remove();
delete this._map;
}
}
);
}
export function registerPorts(elmApp, settings = {}) {
const options = Object.assign(
{
outgoingPort: "elmMapboxOutgoing",
incomingPort: "elmMapboxIncoming",
easingFunctions: {
linear: t => t
}
},
settings
);
if (elmApp.ports && elmApp.ports[options.outgoingPort]) {
function processOptions(opts) {
if (opts.easing) {
return Object.assign({}, opts, {
easing: options.easingFunctions[opts.easing]
});
}
return opts;
}
function waitForMap(target, cb) {
const el = document.getElementById(event.target);
if (el) {
cb(el.map);
} else {
var queue = commandRegistry[target];
if (!queue) queue = commandRegistry[target] = [];
queue.push(cb);
}
}
elmApp.ports[options.outgoingPort].subscribe(event => {
waitForMap(event.target, function(map) {
switch (event.command) {
case "resize":
return map.resize();
case "fitBounds":
return map.fitBounds(event.bounds, processOptions(event.options));
case "panBy":
return map.panBy(event.offset, processOptions(event.options));
case "panTo":
return map.panTo(event.location, processOptions(event.options));
case "zoomTo":
return map.zoomTo(event.zoom, processOptions(event.options));
case "zoomIn":
return map.zoomIn(processOptions(event.options));
case "zoomOut":
return map.zoomOut(processOptions(event.options));
case "rotateTo":
return map.rotateTo(event.bearing, processOptions(event.options));
case "jumpTo":
return map.jumpTo(processOptions(event.options));
case "easeTo":
return map.easeTo(processOptions(event.options));
case "flyTo":
return map.flyTo(processOptions(event.options));
case "stop":
return map.stop();
case "setRTLTextPlugin":
return map.setRTLTextPlugin(event.url);
case "getBounds":
return elmApp.ports[options.incomingPort].send({
type: "getBounds",
id: event.requestId,
bounds: map.getBounds().toArray()
});
case "queryRenderedFeatures":
return elmApp.ports[options.incomingPort].send({
type: "queryRenderedFeatures",
id: event.requestId,
features: event.query
? map.queryRenderedFeatures(processOptions(event.options))
: map.queryRenderedFeatures(
event.query,
processOptions(event.options)
)
});
}
});
});
} else {
throw new Error(
`Expected Elm App to expose ${
options.outgoingPort
} port. Please add https://github.com/gampleman/elm-mapbox/blob/master/examples/MapCommands.elm to your project and import it from your Main file.`
);
}
return elmApp;
}
export const supported = mapboxgl.supported;
|