2
2
* @module olcs.AbstractSynchronizer
3
3
*/
4
4
import { unByKey as olObservableUnByKey } from 'ol/Observable.js' ;
5
- import olLayerGroup from 'ol/layer/Group.js' ;
6
- import { olcsListen , getUid } from './util.js' ;
7
-
8
-
9
- class AbstractSynchronizer {
5
+ import LayerGroup from 'ol/layer/Group.js' ;
6
+ import { getUid , olcsListen } from './util.js' ;
7
+ import Map from 'ol/Map'
8
+ import type { Scene , ImageryLayer } from 'cesium' ;
9
+ import View from 'ol/View' ;
10
+ import Collection from 'ol/Collection' ;
11
+ import BaseLayer from 'ol/layer/Base' ;
12
+ import { EventsKey } from 'ol/events' ;
13
+ import { LayerWithParents } from './core' ;
14
+ import VectorLayerCounterpart from './core/VectorLayerCounterpart' ;
15
+
16
+
17
+ abstract class AbstractSynchronizer < T extends ImageryLayer | VectorLayerCounterpart > {
18
+ protected map : Map ;
19
+ protected view : View ;
20
+ protected scene : Scene ;
21
+ protected olLayers : Collection < BaseLayer > ;
22
+ mapLayerGroup : LayerGroup ;
10
23
/**
11
- * @param {!ol.Map } map
12
- * @param {!Cesium.Scene } scene
13
- * @template T
14
- * @abstract
15
- * @api
24
+ * Map of OpenLayers layer ids (from getUid) to the Cesium ImageryLayers.
25
+ * Null value means, that we are unable to create equivalent layers.
16
26
*/
17
- constructor ( map , scene ) {
18
- /**
19
- * @type {!ol.Map }
20
- * @protected
21
- */
22
- this . map = map ;
27
+ protected layerMap : Record < string , Array < T > > = { } ;
28
+ /**
29
+ * Map of listen keys for OpenLayers layer layers ids (from getUid).
30
+ */
31
+ protected olLayerListenKeys : Record < string , Array < EventsKey > > = { } ;
32
+ /**
33
+ * Map of listen keys for OpenLayers layer groups ids (from getUid).
34
+ */
35
+ private olGroupListenKeys_ : Record < string , Array < EventsKey > > = { } ;
23
36
24
- /**
25
- * @type {ol.View }
26
- * @protected
27
- */
37
+ protected constructor ( map : Map , scene : Scene ) {
38
+ this . map = map ;
28
39
this . view = map . getView ( ) ;
29
-
30
- /**
31
- * @type {!Cesium.Scene }
32
- * @protected
33
- */
34
40
this . scene = scene ;
35
-
36
- /**
37
- * @type {ol.Collection.<ol.layer.Base> }
38
- * @protected
39
- */
40
41
this . olLayers = map . getLayerGroup ( ) . getLayers ( ) ;
41
-
42
- /**
43
- * @type {ol.layer.Group }
44
- */
45
42
this . mapLayerGroup = map . getLayerGroup ( ) ;
46
-
47
- /**
48
- * Map of OpenLayers layer ids (from getUid) to the Cesium ImageryLayers.
49
- * Null value means, that we are unable to create equivalent layers.
50
- * @type {Object.<string, ?Array.<T>> }
51
- * @protected
52
- */
53
- this . layerMap = { } ;
54
-
55
- /**
56
- * Map of listen keys for OpenLayers layer layers ids (from getUid).
57
- * @type {!Object.<string, Array<ol.EventsKey>> }
58
- * @protected
59
- */
60
- this . olLayerListenKeys = { } ;
61
-
62
- /**
63
- * Map of listen keys for OpenLayers layer groups ids (from getUid).
64
- * @type {!Object.<string, !Array.<ol.EventsKey>> }
65
- * @private
66
- */
67
- this . olGroupListenKeys_ = { } ;
68
43
}
69
44
70
45
/**
71
46
* Destroy all and perform complete synchronization of the layers.
72
- * @api
73
47
*/
74
48
synchronize ( ) {
75
49
this . destroyAll ( ) ;
@@ -79,20 +53,16 @@ class AbstractSynchronizer {
79
53
/**
80
54
* Order counterparts using the same algorithm as the Openlayers renderer:
81
55
* z-index then original sequence order.
82
- * @protected
83
56
*/
84
- orderLayers ( ) {
57
+ protected orderLayers ( ) {
85
58
// Ordering logics is handled in subclasses.
86
59
}
87
60
88
61
/**
89
62
* Add a layer hierarchy.
90
- * @param {ol.layer.Base } root
91
- * @private
92
63
*/
93
- addLayers_ ( root ) {
94
- /** @type {Array<import('olsc/core.js').LayerWithParents> } */
95
- const fifo = [ {
64
+ private addLayers_ ( root : BaseLayer ) {
65
+ const fifo : LayerWithParents [ ] = [ {
96
66
layer : root ,
97
67
parents : [ ]
98
68
} ] ;
@@ -104,15 +74,15 @@ class AbstractSynchronizer {
104
74
console . assert ( ! this . layerMap [ olLayerId ] ) ;
105
75
106
76
let cesiumObjects = null ;
107
- if ( olLayer instanceof olLayerGroup ) {
77
+ if ( olLayer instanceof LayerGroup ) {
108
78
this . listenForGroupChanges_ ( olLayer ) ;
109
79
if ( olLayer !== this . mapLayerGroup ) {
110
80
cesiumObjects = this . createSingleLayerCounterparts ( olLayerWithParents ) ;
111
81
}
112
82
if ( ! cesiumObjects ) {
113
83
olLayer . getLayers ( ) . forEach ( ( l ) => {
114
84
if ( l ) {
115
- const newOlLayerWithParents = {
85
+ const newOlLayerWithParents : LayerWithParents = {
116
86
layer : l ,
117
87
parents : olLayer === this . mapLayerGroup ?
118
88
[ ] :
@@ -129,7 +99,7 @@ class AbstractSynchronizer {
129
99
// for example when a source is set after the layer is added to the map
130
100
const layerId = olLayerId ;
131
101
const layerWithParents = olLayerWithParents ;
132
- const onLayerChange = ( e ) => {
102
+ const onLayerChange = ( ) => {
133
103
const cesiumObjs = this . createSingleLayerCounterparts ( layerWithParents ) ;
134
104
if ( cesiumObjs ) {
135
105
// unsubscribe event listener
@@ -152,12 +122,8 @@ class AbstractSynchronizer {
152
122
153
123
/**
154
124
* Add Cesium objects.
155
- * @param {Array.<T> } cesiumObjects
156
- * @param {string } layerId
157
- * @param {ol.layer.Base } layer
158
- * @private
159
125
*/
160
- addCesiumObjects_ ( cesiumObjects , layerId , layer ) {
126
+ private addCesiumObjects_ ( cesiumObjects : Array < T > , layerId : string , layer : BaseLayer ) {
161
127
this . layerMap [ layerId ] = cesiumObjects ;
162
128
this . olLayerListenKeys [ layerId ] . push ( olcsListen ( layer , 'change:zIndex' , ( ) => this . orderLayers ( ) ) ) ;
163
129
cesiumObjects . forEach ( ( cesiumObject ) => {
@@ -169,9 +135,8 @@ class AbstractSynchronizer {
169
135
* Remove and destroy a single layer.
170
136
* @param {ol.layer.Layer } layer
171
137
* @return {boolean } counterpart destroyed
172
- * @private
173
138
*/
174
- removeAndDestroySingleLayer_ ( layer ) {
139
+ private removeAndDestroySingleLayer_ ( layer : BaseLayer ) : boolean {
175
140
const uid = getUid ( layer ) . toString ( ) ;
176
141
const counterparts = this . layerMap [ uid ] ;
177
142
if ( ! ! counterparts ) {
@@ -188,10 +153,8 @@ class AbstractSynchronizer {
188
153
189
154
/**
190
155
* Unlisten a single layer group.
191
- * @param {ol.layer.Group } group
192
- * @private
193
156
*/
194
- unlistenSingleGroup_ ( group ) {
157
+ private unlistenSingleGroup_ ( group : LayerGroup ) {
195
158
if ( group === this . mapLayerGroup ) {
196
159
return ;
197
160
}
@@ -206,16 +169,14 @@ class AbstractSynchronizer {
206
169
207
170
/**
208
171
* Remove layer hierarchy.
209
- * @param {ol.layer.Base } root
210
- * @private
211
172
*/
212
- removeLayer_ ( root ) {
173
+ private removeLayer_ ( root : BaseLayer ) {
213
174
if ( ! ! root ) {
214
175
const fifo = [ root ] ;
215
176
while ( fifo . length > 0 ) {
216
177
const olLayer = fifo . splice ( 0 , 1 ) [ 0 ] ;
217
178
const done = this . removeAndDestroySingleLayer_ ( olLayer ) ;
218
- if ( olLayer instanceof olLayerGroup ) {
179
+ if ( olLayer instanceof LayerGroup ) {
219
180
this . unlistenSingleGroup_ ( olLayer ) ;
220
181
if ( ! done ) {
221
182
// No counterpart for the group itself so removing
@@ -231,19 +192,17 @@ class AbstractSynchronizer {
231
192
232
193
/**
233
194
* Register listeners for single layer group change.
234
- * @param {ol.layer.Group } group
235
- * @private
236
195
*/
237
- listenForGroupChanges_ ( group ) {
196
+ private listenForGroupChanges_ ( group : LayerGroup ) {
238
197
const uuid = getUid ( group ) . toString ( ) ;
239
198
240
199
console . assert ( this . olGroupListenKeys_ [ uuid ] === undefined ) ;
241
200
242
- const listenKeyArray = [ ] ;
201
+ const listenKeyArray : EventsKey [ ] = [ ] ;
243
202
this . olGroupListenKeys_ [ uuid ] = listenKeyArray ;
244
203
245
204
// only the keys that need to be relistened when collection changes
246
- let contentKeys = [ ] ;
205
+ let contentKeys : EventsKey [ ] = [ ] ;
247
206
const listenAddRemove = ( function ( ) {
248
207
const collection = group . getLayers ( ) ;
249
208
if ( collection ) {
@@ -275,9 +234,8 @@ class AbstractSynchronizer {
275
234
276
235
/**
277
236
* Destroys all the created Cesium objects.
278
- * @protected
279
237
*/
280
- destroyAll ( ) {
238
+ protected destroyAll ( ) {
281
239
this . removeAllCesiumObjects ( true ) ; // destroy
282
240
let objKey ;
283
241
for ( objKey in this . olGroupListenKeys_ ) {
@@ -294,43 +252,19 @@ class AbstractSynchronizer {
294
252
295
253
/**
296
254
* Adds a single Cesium object to the collection.
297
- * @param {!T } object
298
- * @abstract
299
- * @protected
300
255
*/
301
- addCesiumObject ( object ) { }
256
+ protected abstract addCesiumObject ( object : T ) : void ;
302
257
303
- /**
304
- * @param {!T } object
305
- * @abstract
306
- * @protected
307
- */
308
- destroyCesiumObject ( object ) { }
258
+ protected abstract destroyCesiumObject ( object : T ) : void ;
309
259
310
260
/**
311
261
* Remove single Cesium object from the collection.
312
- * @param {!T } object
313
- * @param {boolean } destroy
314
- * @abstract
315
- * @protected
316
262
*/
317
- removeSingleCesiumObject ( object , destroy ) { }
263
+ protected abstract removeSingleCesiumObject ( object : T , destroy : boolean ) : void ;
318
264
319
- /**
320
- * Remove all Cesium objects from the collection.
321
- * @param {boolean } destroy
322
- * @abstract
323
- * @protected
324
- */
325
- removeAllCesiumObjects ( destroy ) { }
265
+ protected abstract removeAllCesiumObjects ( destroy : boolean ) : void ;
326
266
327
- /**
328
- * @param {import('olsc/core.js').LayerWithParents } olLayerWithParents
329
- * @return {?Array.<T> }
330
- * @abstract
331
- * @protected
332
- */
333
- createSingleLayerCounterparts ( olLayerWithParents ) { }
267
+ protected abstract createSingleLayerCounterparts ( olLayerWithParents : LayerWithParents ) : Array < T > ;
334
268
}
335
269
336
270
0 commit comments