1 | import StyleLayer from './style_layer';
|
2 | import createStyleLayer from './create_style_layer';
|
3 |
|
4 | import featureFilter from '../style-spec/feature_filter';
|
5 | import groupByLayout from '../style-spec/group_by_layout';
|
6 |
|
7 | import type {TypedStyleLayer} from './style_layer/typed_style_layer';
|
8 | import type {LayerSpecification} from '../style-spec/types.g';
|
9 |
|
10 | export type LayerConfigs = {[_: string]: LayerSpecification};
|
11 | export type Family<Layer extends TypedStyleLayer> = Array<Layer>;
|
12 |
|
13 | class StyleLayerIndex {
|
14 | familiesBySource: {
|
15 | [source: string]: {
|
16 | [sourceLayer: string]: Array<Family<any>>;
|
17 | };
|
18 | };
|
19 | keyCache: {[source: string]: string};
|
20 |
|
21 | _layerConfigs: LayerConfigs;
|
22 | _layers: {[_: string]: StyleLayer};
|
23 |
|
24 | constructor(layerConfigs?: Array<LayerSpecification> | null) {
|
25 | this.keyCache = {};
|
26 | if (layerConfigs) {
|
27 | this.replace(layerConfigs);
|
28 | }
|
29 | }
|
30 |
|
31 | replace(layerConfigs: Array<LayerSpecification>) {
|
32 | this._layerConfigs = {};
|
33 | this._layers = {};
|
34 | this.update(layerConfigs, []);
|
35 | }
|
36 |
|
37 | update(layerConfigs: Array<LayerSpecification>, removedIds: Array<string>) {
|
38 | for (const layerConfig of layerConfigs) {
|
39 | this._layerConfigs[layerConfig.id] = layerConfig;
|
40 |
|
41 | const layer = this._layers[layerConfig.id] = createStyleLayer(layerConfig);
|
42 | layer._featureFilter = featureFilter(layer.filter);
|
43 | if (this.keyCache[layerConfig.id])
|
44 | delete this.keyCache[layerConfig.id];
|
45 | }
|
46 | for (const id of removedIds) {
|
47 | delete this.keyCache[id];
|
48 | delete this._layerConfigs[id];
|
49 | delete this._layers[id];
|
50 | }
|
51 |
|
52 | this.familiesBySource = {};
|
53 |
|
54 | const groups = groupByLayout(Object.values(this._layerConfigs), this.keyCache);
|
55 |
|
56 | for (const layerConfigs of groups) {
|
57 | const layers = layerConfigs.map((layerConfig) => this._layers[layerConfig.id]);
|
58 |
|
59 | const layer = layers[0];
|
60 | if (layer.visibility === 'none') {
|
61 | continue;
|
62 | }
|
63 |
|
64 | const sourceId = layer.source || '';
|
65 | let sourceGroup = this.familiesBySource[sourceId];
|
66 | if (!sourceGroup) {
|
67 | sourceGroup = this.familiesBySource[sourceId] = {};
|
68 | }
|
69 |
|
70 | const sourceLayerId = layer.sourceLayer || '_geojsonTileLayer';
|
71 | let sourceLayerFamilies = sourceGroup[sourceLayerId];
|
72 | if (!sourceLayerFamilies) {
|
73 | sourceLayerFamilies = sourceGroup[sourceLayerId] = [];
|
74 | }
|
75 |
|
76 | sourceLayerFamilies.push(layers);
|
77 | }
|
78 | }
|
79 | }
|
80 |
|
81 | export default StyleLayerIndex;
|