-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathlayer-behavior.html
556 lines (524 loc) · 19.5 KB
/
layer-behavior.html
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
<link rel="import" href="../polymer/polymer.html">
<script>
(function(MapboxGLPolymer) {
/*
* @polymerBehavior
*/
MapboxGLPolymer.LayerBehavior = {
properties: {
/*
* The (map)[https://www.mapbox.com/mapbox-gl-js/api/#Map] instance
* returned by mapboxgl-js
*/
map: {
type: Object
},
/*
* Unique layer name. (Required)
*/
layerId: {
type: String,
observer: '_layerIdChanged'
},
/*
* set layer to not visible if true.
*/
hidden: {
type: Boolean
},
/*
* Rendering type of this layer.
* (fill, line, symbol, circle, fill-extrusion, raster, background)
*/
renderingType: {
type: String
},
/*
* Name of a source description to be used for this layer.
*/
source: {
type: String
},
/*
* Define the source data directly inside this layer. `type` is usually
* `geojson`, where `data` is the GeoJSON object or an URL to the GeoJSON.
*
* @type {{type: string, data: object}}
*/
sourceData: {
type: Object,
observer: '_sourceDataChanged'
},
/*
* Layer to use from a vector tile source. Required if the source
* supports multiple layers.
*/
sourceLayer: {
type: String
},
/*
* The minimum zoom level on which the layer gets parsed and appears on.
*/
minZoom: {
type: Number,
value: 0
},
/*
* The maximum zoom level on which the layer gets parsed and appears on.
*/
maxZoom: {
type: Number,
value: 22
},
/*
* A expression specifying conditions on source features. Only features
* that match the filter are displayed.
*
* e.g. `["==", "$type", "Polygon"]` to render Polygon only.
*/
filter: Array,
/*
* Optional number. Defaults to 1.
* The opacity of the layer.
*/
opacity: Number,
/*
* Optional color. Defaults to #000000. This color can be specified
* as rgba with an alpha component and the color's opacity will not
* affect the opacity of the 1px stroke, if it is used.
*/
color: String,
/*
* Optional array. Units in pixels. Defaults to 0,0.
* The geometry's offset. Values are [x, y] where negatives indicate
* left and up, respectively.
*/
translate: Array,
/*
* Optional enum. One of `map` or `viewport`. Defaults to map.
* Requires `fill-translate`.
* Controls the translation reference point.
* - `map`: The fill is translated relative to the map.
* - `viewport`: The fill is translated relative to the viewport.
*/
translateAnchor: String,
/*
* Optional string.
* Name of image in sprite to use for drawing image fills or lines. For
* seamless patterns, image width and height must be a factor of two
* (2, 4, 8, ..., 512).
*/
pattern: String,
/*
* no antialiasing for fill if set to true.
*/
noAntialias: Boolean,
/*
* The outline color of the fill. Matches the value of color if
* unspecified.
* Disabled if `pattern` or `no-antialias` is set.
*/
outlineColor: String,
/*
* Optional enum. One of `butt`, `round`, `square`. Defaults to `butt`.
* The display of line endings.
* - `butt`: A cap with a squared-off end which is drawn to the exact
* endpoint of the line.
* - `round`: A cap with a rounded end which is drawn beyond the endpoint
* of the line at a radius of one-half of the line's width and centered
* on the endpoint of the line.
* - `square`: A cap with a squared-off end which is drawn beyond the
* endpoint of the line at a distance of one-half of the line's width.
*
*/
lineCap: String,
/*
* Optional enum. One of `bevel`, `round`, `miter`. Defaults to `miter`.
* The display of lines when joining.
* - `bevel`:
* A join with a squared-off end which is drawn beyond the endpoint of the
* line at a distance of one-half of the line's width.
* - `round`:
* A join with a rounded end which is drawn beyond the endpoint of the
* line at a radius of one-half of the line's width and centered on the
* endpoint of the line.
* - `miter`:
* A join with a sharp, angled corner which is drawn with the outer sides
* beyond the endpoint of the path until they meet.
*/
lineJoin: String,
/*
* Optional number. Defaults to 2. Requires line-join = `miter`.
* Used to automatically convert `miter` joins to bevel joins for sharp
* angles.
*/
lineMiterLimit: Number,
/*
* Optional number. Defaults to 1.05. Requires line-join = `round`.
* Used to automatically convert round joins to miter joins for shallow
* angles.
*/
lineRoundLimit: Number,
/*
* Optional number. Units in pixels. Defaults to 1.
* Stroke thickness.
*/
lineWidth: Number,
/*
* Optional number. Units in pixels. Defaults to 0.
* The line's offset. For linear features, a positive value offsets the
* line to the right, relative to the direction of the line, and a
* negative value to the left. For polygon features, a positive value
* results in an inset, and a negative value results in an outset.
*/
lineOffset: Number,
/*
* Optional number. Units in pixels. Defaults to 0.
* Blur applied to the `rendering-type` = `line` or `circle`, in pixels.
* 1 blurs the circle such that only the centerpoint is full opacity.
*/
blur: Number,
/*
* Optional array. Units in line widths. Disabled by line-pattern.
* Specifies the lengths of the alternating dashes and gaps that form the
* dash pattern. The lengths are later scaled by the line width. To
* convert a dash length to pixels, multiply the length by the current
* line width.
*/
lineDasharray: Array,
/*
* Optional number. Units in pixels. Defaults to 0.
* Draws a line casing outside of a line's actual path. Value indicates
* the width of the inner gap.
*/
lineGapWidth: Number,
/*
* Optional number. Units in pixels. Defaults to 5.
* Circle radius.
*/
radius: Number,
/*
* Optional enum. One of `map` or `viewport`. Defaults to map.
* Controls the scaling behavior of the circle when the map is pitched.
* - `map`: Circles are scaled according to their apparent distance to
* the camera.
* - `viewport`: Circles are not scaled.
*/
circlePitchScale: String,
/*
* Optional number. Units in pixels. Defaults to 0.
* The width of the circle's stroke. Strokes are placed outside of the
* circle-radius.
*/
circleStrokeWidth: Number,
/*
* Optional color. Defaults to #000000.
* The stroke color of the circle.
*/
circleStrokeColor: String,
/*
* Optional number. Defaults to 1.
* The opacity of the circle's stroke.
*/
circleStrokeOpacity: Number,
/*
* requires `rendering-type` to be `fill-extrusion`.
* The opacity of the entire fill extrusion layer. This is rendered on
* a per-layer, not per-feature, basis, and data-driven styling is not
* available.
*/
fillExtrusionOpacity: {
type: Number
},
/*
* requires `rendering-type` to be `fill-extrusion`.
* The base color of the extruded fill. The extrusion's surfaces will be
* shaded differently based on this color in combination with the root
* light settings. If this color is specified as rgba with an alpha
* component, the alpha component will be ignored; use
* fill-extrusion-opacity to set layer opacity.
*/
fillExtrusionColor: {
type: String
},
/*
* requires `rendering-type` to be `fill-extrusion`.
* The geometry's offset. Values are [x, y] where negatives indicate
* left and up (on the flat plane), respectively.
*/
fillExtrusionTranslate: {
type: Array
},
/*
* requires `rendering-type` to be `fill-extrusion`.
* Controls the translation reference point (map or viewport).
*/
fillExtrusionTranslateAnchor: {
type: String
},
/*
* requires `rendering-type` to be `fill-extrusion`.
* Name of image in sprite to use for drawing images on extruded fills.
* For seamless patterns, image width and height must be a factor of
* two (2, 4, 8, ..., 512).
*/
fillExtrusionPattern: {
type: String
},
/*
* requires `rendering-type` to be `fill-extrusion`.
* The height (in meters) with which to extrude this layer.
*/
fillExtrusionHeight: {
type: Number
},
/*
* requires `rendering-type` to be `fill-extrusion`.
* The height with which to extrude the base of this layer.
* Must be less than or equal to fill-extrusion-height
*/
fillExtrusionBase: {
type: Number
},
/*
* Requires renderingType to be 'symbol'. Value to use for a text label.
* Feature properties are specified using
* tokens like {field_name}. (Token replacement is only supported for
* literal text-field values--not for property functions.)
*/
textField: String,
/*
* Requires renderingType to be 'symbol'.
* Font stack to use for displaying text.
*/
textFont: String,
/*
* Requires renderingType to be 'symbol'.
* Font size. Defaults to 16px;
*/
textSize: Number,
/*
* Requires renderingType to be 'symbol'.
* The maximum line width for text wrapping. Defaults to 10;
*/
textMaxWidth: Number,
/*
* Requires renderingType to be 'symbol'.
* Text leading value for multi-line text. Defaults to 1.2;
*/
textLineHeight: Number,
/*
* Requires renderingType to be 'symbol'.
* text justification options. "left", "right", or "center" (default).
*/
textJustify: String,
/*
* Requires renderingType to be 'symbol'.
* Part of the text placed closest to the anchor.
* One of center, left, right, top, bottom, top-left, top-right,
* bottom-left, bottom-right. Defaults to center.
*/
textAnchor: String,
/*
* Requires renderingType to be 'symbol'.
* Offset distance of text from its anchor.
* Positive values indicate right and down, while negative values
* indicate left and up.
*/
textOffset: Array,
/*
* Requires renderingType to be 'symbol'.
* The color with which the text will be drawn. Defaults to #000000
*/
textColor: String,
/*
* A string with space delimited
* (map events)[https://www.mapbox.com/mapbox-gl-js/api/#Map] to
* listen to.
* The corresponding event with be prefix with `mapbox-layer-`.
* e.g. `click` will result with `mapbox-layer-click` event to be fired.
*
* ```html
* <mapbox-gl events-to-watch="rotate zoomend"
* on-mapbox-gl-move="handleMove"
* on-mapbox-gl-rotate="handleRotate"
* on-mapbox-gl-zoomend="handleZoomend"></mapbox-gl>
* ```
*
*/
eventsToWatch: {
type: String,
observer: '_eventsToWatchChanged'
},
_antialias: {
type: Boolean,
computed: '_not(noAntialias)'
},
_visibility: {
type: String,
computed: '_if(hidden, "none", "visible")'
},
_added: {
type: String
},
_layer: Object,
_clearEvents: Array,
_watchEvents: Array
},
observers: [
'_generateLayerProp(layerId, renderingType, minZoom, maxZoom)',
'_updateLayerProp("source", source, _layer)',
'_updateLayerProp("source", sourceData, _layer)',
'_updateLayerProp("source-layer", sourceLayer, _layer)',
'_updateLayerProp("filter", filter, _layer)',
'_setFilter(map, layerId, filter)',
'_setPaintProperty(map, layerId, "color", color, renderingType, _added)',
'_setPaintProperty(map, layerId, "opacity", opacity, renderingType, _added)',
'_setPaintProperty(map, layerId, "translate", translate, renderingType, _added)',
'_setPaintProperty(map, layerId, "translate-anchor", translateAnchor, renderingType, _added)',
'_setPaintProperty(map, layerId, "pattern", pattern, renderingType, _added)',
'_setPaintProperty(map, layerId, "blur", blur, renderingType, _added)',
'_setPaintProperty(map, layerId, "circle-radius", radius, null, _added)',
'_setPaintProperty(map, layerId, "circle-pitch-scale", circlePitchScale, null, _added)',
'_setPaintProperty(map, layerId, "circle-stroke-width", circleStrokeWidth, null, _added)',
'_setPaintProperty(map, layerId, "circle-stroke-color", circleStrokeColor, null, _added)',
'_setPaintProperty(map, layerId, "circle-stroke-opacity", circleStrokeOpacity, null, _added)',
'_setPaintProperty(map, layerId, "line-dasharray", lineDasharray, null, _added)',
'_setPaintProperty(map, layerId, "line-width", lineWidth, null, _added)',
'_setPaintProperty(map, layerId, "line-gap-width", lineGapWidth, null, _added)',
'_setPaintProperty(map, layerId, "line-offset", lineOffset, null, _added)',
'_setPaintProperty(map, layerId, "fill-antialias", _antialias, null, _added)',
'_setPaintProperty(map, layerId, "fill-outline-color", outlineColor, null, _added)',
'_setPaintProperty(map, layerId, "fill-extrusion-translate-anchor", fillExtrusionTranslateAnchor, null, _added)',
'_setPaintProperty(map, layerId, "fill-extrusion-pattern", fillExtrusionPattern, null, _added)',
'_setPaintProperty(map, layerId, "fill-extrusion-opacity", fillExtrusionOpacity, null, _added)',
'_setPaintProperty(map, layerId, "fill-extrusion-color", fillExtrusionColor, null, _added)',
'_setPaintProperty(map, layerId, "fill-extrusion-translate", fillExtrusionTranslate, null, _added)',
'_setPaintProperty(map, layerId, "fill-extrusion-height", fillExtrusionHeight, null, _added)',
'_setPaintProperty(map, layerId, "fill-extrusion-base", fillExtrusionBase, null, _added)',
'_setPaintProperty(map, layerId, "text-color", textColor, null, _added)',
'_setLayoutProperty(map, layerId, "text-field", textField, null, _added)',
'_setLayoutProperty(map, layerId, "text-font", textFont, null, _added)',
'_setLayoutProperty(map, layerId, "text-size", textSize, null, _added)',
'_setLayoutProperty(map, layerId, "text-line-height", textLineHeight, null, _added)',
'_setLayoutProperty(map, layerId, "text-max-width", textMaxWidth, null, _added)',
'_setLayoutProperty(map, layerId, "text-justify", textJustify, null, _added)',
'_setLayoutProperty(map, layerId, "text-anchor", textAnchor, null, _added)',
'_setLayoutProperty(map, layerId, "text-offset", textOffset, null, _added)',
'_setLayoutProperty(map, layerId, "visibility", _visibility, null, _added)',
'_setLayoutProperty(map, layerId, "line-cap", lineCap, null, _added)',
'_setLayoutProperty(map, layerId, "line-join", lineJoin, null, _added)',
'_setLayoutProperty(map, layerId, "line-miter-limit", lineMiterLimit, null, _added)',
'_setLayoutProperty(map, layerId, "line-round-limit", lineRoundLimit, null, _added)',
'_clearListeners(map, _clearEvents)',
'_forwardEvents(map, _watchEvents, _added)'
],
_not: function(val) {
return !val;
},
_if: function(predicate, valtrue, valfalse) {
return predicate ? valtrue : valfalse;
},
_setFilter: function(map, layerId, filter) {
if (!map || !layerId || !this._added) return;
return map.setFilter(layerId, filter);
},
_sourceDataChanged(v) {
if (this.map) {
var src = this.map.getSource(this.layerId);
var l = this.map.getLayer(this.layerId);
src && src.setData(v.data);
}
},
_eventsToWatchChanged: function(newstr, oldstr) {
if (oldstr) {
this._clearEvents = oldstr.trim().split(' ');
}
if (newstr) {
this._watchEvents = newstr.trim().split(' ');
}
},
_clearListeners: function(map, _clearEvents) {
if (!_clearEvents || !map) return;
for (var i = 0, len = _clearEvents.length; i < len; ++i) {
this._clearListener(_clearEvents[i].trim());
}
},
_clearListener: function(name) {
if (this._listeners[name]) {
this.map.off(name, this.layerId, this._listeners[name]);
this._listeners[name] = null;
}
},
_forwardEvents: function(map, _watchEvents, _attached) {
if (!_watchEvents || !map || !_attached) return;
this._listeners = this._listeners || Object.create(null);
for (var i = 0, len = _watchEvents.length; i < len; ++i) {
this._forwardEvent(_watchEvents[i].trim());
}
},
_forwardEvent: function(name, fn) {
if (fn) {
this._listeners[name] = function(event) {
fn(event);
this.dispatchEvent(
new CustomEvent('mapbox-layer-' + name, { detail: event })
);
}.bind(this);
} else {
this._listeners[name] = function(event) {
this.dispatchEvent(
new CustomEvent('mapbox-layer-' + name, { detail: event })
);
}.bind(this);
}
this.map.on(name, this.layerId, this._listeners[name]);
},
_layerIdChanged: function(curr, old) {
if (!this.map) return;
if (this.map.getLayer(old)) {
this.map.removeLayer(old);
this._added = false;
}
},
_generateLayerProp: function(layerId, renderingType, minZoom, maxZoom) {
if (!layerId || !renderingType) return;
this._layer = {
id: layerId,
type: renderingType,
minzoom: minZoom,
maxzoom: maxZoom
};
},
_setPaintProperty: function(map, layerId, field, value, prefix) {
if (!this._added || !this.map || !value || !this.map.getLayer(layerId)) return;
var _field = typeof prefix == 'string' ? prefix + '-' + field : field;
this.map.setPaintProperty(layerId, _field, value);
},
_setLayoutProperty: function(map, layerId, field, value, prefix) {
if (!this._added || !this.map || !value || !this.map.getLayer(layerId)) return;
var _field = typeof prefix == 'string' ? prefix + '-' + field : field;
this.map.setLayoutProperty(layerId, _field, value);
},
_updateLayerProp: function(key, value, _layer) {
if (!_layer) return;
if (value) {
_layer[key] = value;
this.notifyPath(`_layer.${key}`);
} else if (_layer[key]) {
delete _layer[key];
}
},
_updateLayerPropField: function(key, field, value, _layer, renderingType) {
if (!_layer) return;
_layer[key] = _layer[key] || {};
var _field = renderingType + '-' + field ? renderingType : field;
if (value) {
_layer[key][_field] = value;
} else if (_layer[key][_field]) {
delete _layer[key][_field];
}
}
};
})((window.MapboxGLPolymer = window.MapboxGLPolymer || {}));
</script>