/
composite-layer.js
228 lines (204 loc) · 7.26 KB
/
composite-layer.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright (c) 2015 - 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import Layer from './layer';
import log from '../utils/log';
import {flatten} from '../utils/flatten';
export default class CompositeLayer extends Layer {
get isComposite() {
return true;
}
getSubLayers() {
return (this.internalState && this.internalState.subLayers) || [];
}
// initializeState is usually not needed for composite layers
// Provide empty definition to disable check for missing definition
initializeState() {}
// Updates selected state members and marks the composite layer to need rerender
setState(updateObject) {
super.setState(updateObject);
// Trigger a layer update
// Although conceptually layer.draw and compositeLayer.renderLayers are equivalent,
// they are executed during different lifecycles.
// draw can be called without calling updateState (e.g. most viewport changes),
// while renderLayers can only be called during a recursive layer update.
this.setNeedsUpdate();
}
// called to augment the info object that is bubbled up from a sublayer
// override Layer.getPickingInfo() because decoding / setting uniform do
// not apply to a composite layer.
// @return null to cancel event
getPickingInfo({info}) {
const {object} = info;
const isDataWrapped =
object && object.__source && object.__source.parent && object.__source.parent.id === this.id;
if (!isDataWrapped) {
return info;
}
return Object.assign(info, {
// override object with picked data
object: object.__source.object,
index: object.__source.index
});
}
// Implement to generate subLayers
renderLayers() {
return null;
}
// Returns true if sub layer needs to be rendered
shouldRenderSubLayer(id, data) {
const {_subLayerProps: overridingProps} = this.props;
return (data && data.length) || (overridingProps && overridingProps[id]);
}
// Returns sub layer class for a specific sublayer
getSubLayerClass(id, DefaultLayerClass) {
const {_subLayerProps: overridingProps} = this.props;
return (
(overridingProps && overridingProps[id] && overridingProps[id].type) || DefaultLayerClass
);
}
// When casting user data into another format to pass to sublayers,
// add reference to the original object and object index
getSubLayerRow(row, sourceObject, sourceObjectIndex) {
row.__source = {
parent: this,
object: sourceObject,
index: sourceObjectIndex
};
return row;
}
// Some composite layers cast user data into another format before passing to sublayers
// We need to unwrap them before calling the accessor so that they see the original data
// objects
getSubLayerAccessor(accessor) {
if (typeof accessor === 'function') {
const objectInfo = {
data: this.props.data,
target: []
};
return (x, i) => {
if (x.__source) {
objectInfo.index = x.__source.index;
return accessor(x.__source.object, objectInfo);
}
return accessor(x, i);
};
}
return accessor;
}
// Returns sub layer props for a specific sublayer
getSubLayerProps(sublayerProps = {}) {
const {
opacity,
pickable,
visible,
parameters,
getPolygonOffset,
highlightedObjectIndex,
autoHighlight,
highlightColor,
coordinateSystem,
coordinateOrigin,
wrapLongitude,
positionFormat,
modelMatrix,
extensions,
_subLayerProps: overridingProps
} = this.props;
const newProps = {
opacity,
pickable,
visible,
parameters,
getPolygonOffset,
highlightedObjectIndex,
autoHighlight,
highlightColor,
coordinateSystem,
coordinateOrigin,
wrapLongitude,
positionFormat,
modelMatrix,
extensions
};
const overridingSublayerProps =
overridingProps && sublayerProps.id && overridingProps[sublayerProps.id];
const overridingSublayerTriggers =
overridingSublayerProps && overridingSublayerProps.updateTriggers;
const sublayerId = sublayerProps.id || 'sublayer';
if (overridingSublayerProps) {
const propTypes = this.constructor._propTypes;
for (const key in overridingSublayerProps) {
const propType = propTypes[key];
// eslint-disable-next-line
if (propType && propType.type === 'accessor') {
overridingSublayerProps[key] = this.getSubLayerAccessor(overridingSublayerProps[key]);
}
}
}
Object.assign(
newProps,
sublayerProps,
// experimental feature that allows users to override sublayer props via parent layer prop
overridingSublayerProps,
{
id: `${this.props.id}-${sublayerId}`,
updateTriggers: Object.assign(
{
all: this.props.updateTriggers.all
},
sublayerProps.updateTriggers,
overridingSublayerTriggers
)
}
);
// Pass through extension props
for (const extension of extensions) {
const passThroughProps = extension.getSubLayerProps.call(this, extension);
Object.assign(newProps, passThroughProps, {
updateTriggers: Object.assign(newProps.updateTriggers, passThroughProps.updateTriggers)
});
}
return newProps;
}
_getAttributeManager() {
return null;
}
// Called by layer manager to render subLayers
_renderLayers() {
let {subLayers} = this.internalState;
if (subLayers && !this.needsUpdate()) {
log.log(3, `Composite layer reused subLayers ${this}`, this.internalState.subLayers)();
} else {
subLayers = this.renderLayers();
// Flatten the returned array, removing any null, undefined or false
// this allows layers to render sublayers conditionally
// (see CompositeLayer.renderLayers docs)
subLayers = flatten(subLayers, {filter: Boolean});
this.internalState.subLayers = subLayers;
log.log(2, `Composite layer rendered new subLayers ${this}`, subLayers)();
}
// populate reference to parent layer (this layer)
// NOTE: needs to be done even when reusing layers as the parent may have changed
for (const layer of subLayers) {
layer.parent = this;
}
}
}
CompositeLayer.layerName = 'CompositeLayer';