-
Notifications
You must be signed in to change notification settings - Fork 1
/
layerCollection.ts
293 lines (260 loc) · 8.77 KB
/
layerCollection.ts
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
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { check } from '@vcsuite/check';
import IndexedCollection from './indexedCollection.js';
import ExclusiveManager from './exclusiveManager.js';
import LayerState from '../layer/layerState.js';
import VcsEvent from '../vcsEvent.js';
import GlobalHider from '../layer/globalHider.js';
// eslint-disable-next-line import/no-named-default
import type { default as Layer, SplitLayer } from '../layer/layer.js';
/**
* The largest integer zindex which can be safely assigned to a layer (equal to Number.MAX_SAFE_INTEGER)
* You should use this to ensure layers are always rendered on top.
*/
export const maxZIndex = Number.MAX_SAFE_INTEGER;
/**
* A collection of layers. Manages rendering order and layer exclusivity. Emits state changes for convenience. Passed to
* {@link Map} for layers available to said map. Layers must have unique names.
*/
// ignored do to static issues, see https://github.com/microsoft/TypeScript/issues/4628
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
class LayerCollection extends IndexedCollection<Layer> {
/**
* Creates a LayerCollection from an iterable of layers, such as an Array.
* @param iterable
*/
static from(iterable: Iterable<Layer>): LayerCollection {
const collection = new LayerCollection();
if (iterable) {
// eslint-disable-next-line no-restricted-syntax
for (const layer of iterable) {
collection.add(layer);
}
}
return collection;
}
private _layerEventListeners: Record<string, (() => void)[]>;
private _zIndexSymbol: symbol;
/**
* Event raised, when a layer of this collection changes its state. Passed the layer.
*/
stateChanged: VcsEvent<Layer>;
/**
* The exclusive manager for this collection. Layers within this collection are automatically added and tracked.
*/
exclusiveManager: ExclusiveManager;
/**
* The global hider for this collection.
*/
private _globalHider: GlobalHider;
/**
* Locale for this layerCollection, will be synchronized by the vcsApp, if part of an vcsApp.
* This Locale will be set on all Member Layers. On setting the Locale this will trigger a reload of all locale
* aware layers.
*/
private _locale: string;
constructor() {
super();
this._layerEventListeners = {};
this._zIndexSymbol = Symbol('zIndex');
this.stateChanged = new VcsEvent();
this.exclusiveManager = new ExclusiveManager();
this._globalHider = new GlobalHider();
this._locale = 'en';
}
/**
* A symbol to describe the local z index of a layer. The local z index must not equal the layers z index, but is
* always consistent in comparison to the neighbouring layers. If a layer is moved other then by z index, the collection
* ensures consistency by setting a new local z index if needed.
*/
get zIndexSymbol(): symbol {
return this._zIndexSymbol;
}
/**
* The current global hider of these layers
*/
get globalHider(): GlobalHider {
return this._globalHider;
}
/**
* The current global hider of these layers
* @param globalHider
*/
set globalHider(globalHider: GlobalHider) {
check(globalHider, GlobalHider);
this._globalHider = globalHider;
this._array.forEach((layer) => {
layer.setGlobalHider(this._globalHider);
});
}
get locale(): string {
return this._locale;
}
set locale(value: string) {
check(value, String);
if (this._locale !== value) {
this._locale = value;
[...this].forEach((layer) => {
layer.locale = this._locale;
});
}
}
private _listenToLayerEvents(layer: Layer): void {
const stateListener = layer.stateChanged.addEventListener(
(state: LayerState) => {
if (state === LayerState.ACTIVE) {
this.exclusiveManager.handleLayerActivated(layer);
}
this.stateChanged.raiseEvent(layer);
},
);
const zIndexListener = layer.zIndexChanged.addEventListener(() => {
this._zIndexChanged(layer);
});
const exclusiveGroupsListener =
layer.exclusiveGroupsChanged.addEventListener(() => {
this.exclusiveManager.handleExclusiveGroupsChanged(layer);
});
const listeners = [stateListener, zIndexListener, exclusiveGroupsListener];
if ((layer as Layer & SplitLayer).splitDirectionChanged) {
listeners.push(
(layer as Layer & SplitLayer).splitDirectionChanged.addEventListener(
() => {
this.exclusiveManager.handleSplitDirectionChanged(layer);
},
),
);
}
this._layerEventListeners[layer.name] = listeners;
}
/**
* Determines the location in the array before the first entry with a higher local z index or null if there is no such position
* @param zIndex
* @private
*/
private _findZIndexPosition(zIndex: number): number | null {
const usedIndex = this._array.findIndex(
// @ts-ignore
(l) => l[this._zIndexSymbol] > zIndex,
);
return usedIndex > -1 ? usedIndex : null;
}
/**
* This is callback for a layers zIndex changed. It reevaluates the array given the new zIndex
* an moves the specified layer to its new location determined by findeZIndexPosition or the end of the array failing that.
* @param layer
* @private
*/
private _zIndexChanged(layer: Layer): void {
const currentIndex = this.indexOf(layer);
if (currentIndex > -1) {
// @ts-ignore
layer[this._zIndexSymbol] = layer.zIndex;
let zIndexPosition = this._findZIndexPosition(layer.zIndex);
if (
zIndexPosition != null &&
zIndexPosition > 0 &&
zIndexPosition > currentIndex
) {
zIndexPosition -= 1; // remove self from count
}
zIndexPosition =
zIndexPosition != null ? zIndexPosition : this._array.length - 1;
this._move(layer, currentIndex, zIndexPosition);
this._ensureLocalZIndex(layer);
}
}
/**
* Ensures the local z index is consistent with the neighbours of a given layer.
* e.g. the layer on elower must have a lower or equal zIndex
* and the one higher a higher or equal zIndex.
*/
private _ensureLocalZIndex(layer: Layer): void {
const currentIndex = this.indexOf(layer);
// @ts-ignore
const currentLayerZIndex = layer[this._zIndexSymbol] as number;
if (currentIndex > 0) {
// @ts-ignore
const below: number = this._array[currentIndex - 1][
this._zIndexSymbol
] as number;
if (below > currentLayerZIndex) {
// @ts-ignore
layer[this._zIndexSymbol] = below;
}
}
if (currentIndex < this._array.length - 1) {
// @ts-ignore
const above = this._array[currentIndex + 1][this._zIndexSymbol] as number;
if (above < currentLayerZIndex) {
// @ts-ignore
layer[this._zIndexSymbol] = above;
}
}
}
/**
* Adds a layer to the collection. Can optionally be passed an index at which to insert the layer.
* The layer locale will be set to the same locale of the layerCollection. This will trigger a forceRedraw
* of the layer if the layer locale is different and the layer is locale aware.
* @param layer
* @param index
* @returns returns the layer index or null, if the layers name is not unique
*/
add(layer: Layer, index?: number): number | null {
let usedIndex: number | null | undefined = index;
if (index == null) {
usedIndex = this._findZIndexPosition(layer.zIndex);
}
const insertedAt = super.add(layer, usedIndex);
if (insertedAt != null) {
// @ts-ignore
layer[this._zIndexSymbol] = layer.zIndex;
layer.setGlobalHider(this._globalHider);
layer.locale = this.locale;
this._ensureLocalZIndex(layer);
this._listenToLayerEvents(layer);
this.exclusiveManager.registerLayer(layer);
}
return insertedAt;
}
protected _remove(layer: Layer): number {
if (this._layerEventListeners[layer.name]) {
this._layerEventListeners[layer.name].forEach((cb) => {
cb();
});
delete this._layerEventListeners[layer.name];
}
// @ts-ignore
delete layer[this._zIndexSymbol];
layer.setGlobalHider();
this.exclusiveManager.unregisterLayer(layer);
return super._remove(layer);
}
clear(): void {
Object.values(this._layerEventListeners)
.flat()
.forEach((r) => {
r();
});
this._array.forEach((l) => {
// @ts-ignore
delete l[this._zIndexSymbol];
});
this.exclusiveManager.clear();
this._layerEventListeners = {};
super.clear();
}
destroy(): void {
Object.values(this._layerEventListeners)
.flat()
.forEach((r) => {
r();
});
this._layerEventListeners = {};
this.exclusiveManager.destroy();
this._globalHider.destroy();
super.destroy();
}
}
export default LayerCollection;