-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathlayers.js
211 lines (167 loc) · 5.48 KB
/
layers.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
/**
* Copyright 2012-2018, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var Lib = require('../../lib');
var convertTextOpts = require('./convert_text_opts');
function MapboxLayer(mapbox, index) {
this.mapbox = mapbox;
this.map = mapbox.map;
this.uid = mapbox.uid + '-' + 'layer' + index;
this.idSource = this.uid + '-source';
this.idLayer = this.uid + '-layer';
// some state variable to check if a remove/add step is needed
this.sourceType = null;
this.source = null;
this.layerType = null;
this.below = null;
// is layer currently visible
this.visible = false;
}
var proto = MapboxLayer.prototype;
proto.update = function update(opts) {
if(!this.visible) {
// IMPORTANT: must create source before layer to not cause errors
this.updateSource(opts);
this.updateLayer(opts);
} else if(this.needsNewSource(opts)) {
// IMPORTANT: must delete layer before source to not cause errors
this.updateLayer(opts);
this.updateSource(opts);
} else if(this.needsNewLayer(opts)) {
this.updateLayer(opts);
} else {
this.updateStyle(opts);
}
this.visible = isVisible(opts);
};
proto.needsNewSource = function(opts) {
// for some reason changing layer to 'fill' or 'symbol'
// w/o changing the source throws an exception in mapbox-gl 0.18 ;
// stay safe and make new source on type changes
return (
this.sourceType !== opts.sourcetype ||
this.source !== opts.source ||
this.layerType !== opts.type
);
};
proto.needsNewLayer = function(opts) {
return (
this.layerType !== opts.type ||
this.below !== opts.below
);
};
proto.updateSource = function(opts) {
var map = this.map;
if(map.getSource(this.idSource)) map.removeSource(this.idSource);
this.sourceType = opts.sourcetype;
this.source = opts.source;
if(!isVisible(opts)) return;
var sourceOpts = convertSourceOpts(opts);
map.addSource(this.idSource, sourceOpts);
};
proto.updateLayer = function(opts) {
var map = this.map;
var convertedOpts = convertOpts(opts);
if(map.getLayer(this.idLayer)) map.removeLayer(this.idLayer);
this.layerType = opts.type;
if(isVisible(opts)) {
map.addLayer({
id: this.idLayer,
source: this.idSource,
'source-layer': opts.sourcelayer || '',
type: opts.type,
layout: convertedOpts.layout,
paint: convertedOpts.paint
}, opts.below);
}
};
proto.updateStyle = function(opts) {
if(isVisible(opts)) {
var convertedOpts = convertOpts(opts);
this.mapbox.setOptions(this.idLayer, 'setLayoutProperty', convertedOpts.layout);
this.mapbox.setOptions(this.idLayer, 'setPaintProperty', convertedOpts.paint);
}
};
proto.dispose = function dispose() {
var map = this.map;
map.removeLayer(this.idLayer);
map.removeSource(this.idSource);
};
function isVisible(opts) {
var source = opts.source;
return (
Lib.isPlainObject(source) ||
(typeof source === 'string' && source.length > 0)
);
}
function convertOpts(opts) {
var layout = {},
paint = {};
switch(opts.type) {
case 'circle':
Lib.extendFlat(paint, {
'circle-radius': opts.circle.radius,
'circle-color': opts.color,
'circle-opacity': opts.opacity
});
break;
case 'line':
Lib.extendFlat(paint, {
'line-width': opts.line.width,
'line-color': opts.color,
'line-opacity': opts.opacity
});
break;
case 'fill':
Lib.extendFlat(paint, {
'fill-color': opts.color,
'fill-outline-color': opts.fill.outlinecolor,
'fill-opacity': opts.opacity
// no way to pass specify outline width at the moment
});
break;
case 'symbol':
var symbol = opts.symbol,
textOpts = convertTextOpts(symbol.textposition, symbol.iconsize);
Lib.extendFlat(layout, {
'icon-image': symbol.icon + '-15',
'icon-size': symbol.iconsize / 10,
'text-field': symbol.text,
'text-size': symbol.textfont.size,
'text-anchor': textOpts.anchor,
'text-offset': textOpts.offset
// TODO font family
// 'text-font': symbol.textfont.family.split(', '),
});
Lib.extendFlat(paint, {
'icon-color': opts.color,
'text-color': symbol.textfont.color,
'text-opacity': opts.opacity
});
break;
}
return { layout: layout, paint: paint };
}
function convertSourceOpts(opts) {
var sourceType = opts.sourcetype;
var source = opts.source;
var sourceOpts = {type: sourceType};
var field;
if(sourceType === 'geojson') {
field = 'data';
} else if(sourceType === 'vector') {
field = typeof source === 'string' ? 'url' : 'tiles';
}
sourceOpts[field] = source;
return sourceOpts;
}
module.exports = function createMapboxLayer(mapbox, index, opts) {
var mapboxLayer = new MapboxLayer(mapbox, index);
mapboxLayer.update(opts);
return mapboxLayer;
};