-
Notifications
You must be signed in to change notification settings - Fork 504
Expand file tree
/
Copy pathlayer-manager.js
More file actions
401 lines (354 loc) · 12.6 KB
/
layer-manager.js
File metadata and controls
401 lines (354 loc) · 12.6 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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import $ from "./global/jquery";
import L from "./global/leaflet";
import { asArray } from "./util";
export default class LayerManager {
constructor(map) {
this._map = map;
// BEGIN layer indices
// {<groupname>: {<stamp>: layer}}
this._byGroup = {};
// {<categoryName>: {<stamp>: layer}}
this._byCategory = {};
// {<categoryName_layerId>: layer}
this._byLayerId = {};
// {<stamp>: {
// "group": <groupname>,
// "layerId": <layerId>,
// "category": <category>,
// "container": <container>
// }
// }
this._byStamp = {};
// {<crosstalkGroupName>: {<key>: [<stamp>, <stamp>, ...], ...}}
this._byCrosstalkGroup = {};
// END layer indices
// {<categoryName>: L.layerGroup}
this._categoryContainers = {};
// {<groupName>: L.layerGroup}
this._groupContainers = {};
}
addLayer(layer, category, layerId, group, ctGroup, ctKey) {
// Was a group provided?
let hasId = typeof(layerId) === "string";
let grouped = typeof(group) === "string";
let stamp = L.Util.stamp(layer);
// This will be the default layer group to add the layer to.
// We may overwrite this let before using it (i.e. if a group is assigned).
// This one liner creates the _categoryContainers[category] entry if it
// doesn't already exist.
let container = this._categoryContainers[category] =
this._categoryContainers[category] || L.layerGroup().addTo(this._map);
let oldLayer = null;
if (hasId) {
// First, remove any layer with the same category and layerId
let prefixedLayerId = this._layerIdKey(category, layerId);
oldLayer = this._byLayerId[prefixedLayerId];
if (oldLayer) {
this._removeLayer(oldLayer);
}
// Update layerId index
this._byLayerId[prefixedLayerId] = layer;
}
// Update group index
if (grouped) {
this._byGroup[group] = this._byGroup[group] || {};
this._byGroup[group][stamp] = layer;
// Since a group is assigned, don't add the layer to the category's layer
// group; instead, use the group's layer group.
// This one liner creates the _groupContainers[group] entry if it doesn't
// already exist.
container = this.getLayerGroup(group, true);
}
// Update category index
this._byCategory[category] = this._byCategory[category] || {};
this._byCategory[category][stamp] = layer;
// Update stamp index
let layerInfo = this._byStamp[stamp] = {
layer: layer,
group: group,
ctGroup: ctGroup,
ctKey: ctKey,
layerId: layerId,
category: category,
container: container,
hidden: false
};
// Update crosstalk group index
if (ctGroup) {
if (layer.setStyle) {
// Need to save this info so we know what to set opacity to later
layer.options.origOpacity = typeof(layer.options.opacity) !== "undefined" ? layer.options.opacity : 0.5;
layer.options.origFillOpacity = typeof(layer.options.fillOpacity) !== "undefined" ? layer.options.fillOpacity : 0.2;
layer.options.origColor = typeof(layer.options.color) !== "undefined" ? layer.options.color : "#03F";
layer.options.origFillColor = typeof(layer.options.fillColor) !== "undefined" ? layer.options.fillColor : layer.options.origColor;
}
let ctg = this._byCrosstalkGroup[ctGroup];
if (!ctg) {
ctg = this._byCrosstalkGroup[ctGroup] = {};
let crosstalk = global.crosstalk;
let handleFilter = (e) => {
if (!e.value) {
let groupKeys = Object.keys(ctg);
for (let i = 0; i < groupKeys.length; i++) {
let key = groupKeys[i];
let layerInfo = this._byStamp[ctg[key]];
this._setVisibility(layerInfo, true);
}
} else {
let selectedKeys = {};
for (let i = 0; i < e.value.length; i++) {
selectedKeys[e.value[i]] = true;
}
let groupKeys = Object.keys(ctg);
for (let i = 0; i < groupKeys.length; i++) {
let key = groupKeys[i];
let layerInfo = this._byStamp[ctg[key]];
this._setVisibility(layerInfo, selectedKeys[groupKeys[i]]);
}
}
};
let filterHandle = new crosstalk.FilterHandle(ctGroup);
filterHandle.on("change", handleFilter);
let handleSelection = (e) => {
if (!e.value || !e.value.length) {
let groupKeys = Object.keys(ctg);
for (let i = 0; i < groupKeys.length; i++) {
let key = groupKeys[i];
let layerInfo = this._byStamp[ctg[key]];
// reset the crosstalk style params
layerInfo.layer.options.ctOpacity = undefined;
layerInfo.layer.options.ctFillOpacity = undefined;
layerInfo.layer.options.ctColor = undefined;
layerInfo.layer.options.ctFillColor = undefined;
this._setStyle(layerInfo);
}
} else {
let selectedKeys = {};
for (let i = 0; i < e.value.length; i++) {
selectedKeys[e.value[i]] = true;
}
let groupKeys = Object.keys(ctg);
// for compatability with plotly's ability to colour selections
// https://github.com/jcheng5/plotly/blob/71cf8a/R/crosstalk.R#L96-L100
let selectionColour = crosstalk.group(ctGroup).var("plotlySelectionColour").get();
let ctOpts = crosstalk.var("plotlyCrosstalkOpts").get() || {opacityDim: 0.2};
let persist = ctOpts.persistent === true;
for (let i = 0; i < groupKeys.length; i++) {
let key = groupKeys[i];
let layerInfo = this._byStamp[ctg[key]];
let selected = selectedKeys[groupKeys[i]];
let opts = layerInfo.layer.options;
// remember "old" selection colors if this is persistent selection
layerInfo.layer.options.ctColor =
selected ? selectionColour :
persist ? opts.ctColor : opts.origColor;
layerInfo.layer.options.ctFillColor =
selected ? selectionColour :
persist ? opts.ctFillColor : opts.origFillColor;
layerInfo.layer.options.ctOpacity =
selected ? opts.origOpacity :
(persist && opts.origOpacity == opts.ctOpacity) ? opts.origOpacity :
ctOpts.opacityDim * opts.origOpacity;
layerInfo.layer.options.ctFillOpacity =
selected ? opts.origFillOpacity :
(persist && opts.origFillOpacity == opts.ctFillOpacity) ? opts.origFillOpacity :
ctOpts.opacityDim * opts.origFillOpacity;
this._setStyle(layerInfo);
}
}
};
let selHandle = new crosstalk.SelectionHandle(ctGroup);
selHandle.on("change", handleSelection);
setTimeout(() => {
handleFilter({value: filterHandle.filteredKeys});
handleSelection({value: selHandle.value});
}, 100);
}
if (!ctg[ctKey])
ctg[ctKey] = [];
ctg[ctKey].push(stamp);
}
// Add to container
if (!layerInfo.hidden)
container.addLayer(layer);
return oldLayer;
}
brush(bounds, extraInfo) {
/* eslint-disable no-console */
// For each Crosstalk group...
Object.keys(this._byCrosstalkGroup).forEach(ctGroupName => {
let ctg = this._byCrosstalkGroup[ctGroupName];
let selection = [];
// ...iterate over each Crosstalk key (each of which may have multiple
// layers)...
Object.keys(ctg).forEach(ctKey => {
// ...and for each layer...
ctg[ctKey].forEach(stamp => {
let layerInfo = this._byStamp[stamp];
// ...if it's something with a point...
if (layerInfo.layer.getLatLng) {
// ... and it's inside the selection bounds...
// TODO: Use pixel containment, not lat/lng containment
if (bounds.contains(layerInfo.layer.getLatLng())) {
// ...add the key to the selection.
selection.push(ctKey);
}
}
});
});
new global.crosstalk.SelectionHandle(ctGroupName).set(selection, extraInfo);
});
}
unbrush(extraInfo) {
Object.keys(this._byCrosstalkGroup).forEach(ctGroupName => {
new global.crosstalk.SelectionHandle(ctGroupName).clear(extraInfo);
});
}
_setVisibility(layerInfo, visible) {
if (layerInfo.hidden ^ visible) {
return;
} else if (visible) {
layerInfo.container.addLayer(layerInfo.layer);
layerInfo.hidden = false;
} else {
layerInfo.container.removeLayer(layerInfo.layer);
layerInfo.hidden = true;
}
}
_setStyle(layerInfo) {
let opts = layerInfo.layer.options;
if (!layerInfo.layer.setStyle) {
return;
}
layerInfo.layer.setStyle({
opacity: opts.ctOpacity || opts.origOpacity,
fillOpacity: opts.ctFillOpacity || opts.origFillOpacity,
color: opts.ctColor || opts.origColor,
fillColor: opts.ctFillColor || opts.origFillColor
});
}
getLayer(category, layerId) {
return this._byLayerId[this._layerIdKey(category, layerId)];
}
removeLayer(category, layerIds) {
// Find layer info
$.each(asArray(layerIds), (i, layerId) => {
let layer = this._byLayerId[this._layerIdKey(category, layerId)];
if (layer) {
this._removeLayer(layer);
}
});
}
clearLayers(category) {
// Find all layers in _byCategory[category]
let catTable = this._byCategory[category];
if (!catTable) {
return false;
}
// Remove all layers. Make copy of keys to avoid mutating the collection
// behind the iterator you're accessing.
let stamps = [];
$.each(catTable, (k, v) => {
stamps.push(k);
});
$.each(stamps, (i, stamp) => {
this._removeLayer(stamp);
});
}
getLayerGroup(group, ensureExists) {
let g = this._groupContainers[group];
if (ensureExists && !g) {
this._byGroup[group] = this._byGroup[group] || {};
g = this._groupContainers[group] = L.featureGroup();
g.groupname = group;
g.addTo(this._map);
}
return g;
}
getGroupNameFromLayerGroup(layerGroup) {
return layerGroup.groupname;
}
getVisibleGroups() {
let result = [];
$.each(this._groupContainers, (k, v) => {
if (this._map.hasLayer(v)) {
result.push(k);
}
});
return result;
}
getAllGroupNames() {
let result = [];
$.each(this._groupContainers, (k, v) => {
result.push(k);
});
return result;
}
clearGroup(group) {
// Find all layers in _byGroup[group]
let groupTable = this._byGroup[group];
if (!groupTable) {
return false;
}
// Remove all layers. Make copy of keys to avoid mutating the collection
// behind the iterator you're accessing.
let stamps = [];
$.each(groupTable, (k, v) => {
stamps.push(k);
});
$.each(stamps, (i, stamp) => {
this._removeLayer(stamp);
});
}
clear() {
function clearLayerGroup(key, layerGroup) {
layerGroup.clearLayers();
}
// Clear all indices and layerGroups
this._byGroup = {};
this._byCategory = {};
this._byLayerId = {};
this._byStamp = {};
this._byCrosstalkGroup = {};
$.each(this._categoryContainers, clearLayerGroup);
this._categoryContainers = {};
$.each(this._groupContainers, clearLayerGroup);
this._groupContainers = {};
}
_removeLayer(layer) {
let stamp;
if (typeof(layer) === "string") {
stamp = layer;
} else {
stamp = L.Util.stamp(layer);
}
let layerInfo = this._byStamp[stamp];
if (!layerInfo) {
return false;
}
layerInfo.container.removeLayer(stamp);
if (typeof(layerInfo.group) === "string") {
delete this._byGroup[layerInfo.group][stamp];
}
if (typeof(layerInfo.layerId) === "string") {
delete this._byLayerId[this._layerIdKey(layerInfo.category, layerInfo.layerId)];
}
delete this._byCategory[layerInfo.category][stamp];
delete this._byStamp[stamp];
if (layerInfo.ctGroup) {
let ctGroup = this._byCrosstalkGroup[layerInfo.ctGroup];
let layersForKey = ctGroup[layerInfo.ctKey];
let idx = layersForKey ? layersForKey.indexOf(+stamp) : -1;
if (idx >= 0) {
if (layersForKey.length === 1) {
delete ctGroup[layerInfo.ctKey];
} else {
layersForKey.splice(idx, 1);
}
}
}
}
_layerIdKey(category, layerId) {
return category + "\n" + layerId;
}
}