forked from dvgis/dc-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLayerGroup.js
146 lines (128 loc) · 2.7 KB
/
LayerGroup.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
/**
* @Author: Caven
* @Date: 2020-08-27 19:50:32
*/
import { Util } from '@dc-modules/utils'
import State from '@dc-modules/state/State'
import { LayerGroupEventType, LayerGroupEvent } from '@dc-modules/event'
import Layer from './Layer'
class LayerGroup {
constructor(id) {
this._id = id || Util.uuid()
this._cache = {}
this._show = true
this._viewer = undefined
this._layerGroupEvent = new LayerGroupEvent()
this._layerGroupEvent.on(LayerGroupEventType.ADD, this._onAdd, this)
this._layerGroupEvent.on(LayerGroupEventType.REMOVE, this._onRemove, this)
this._state = State.INITIALIZED
}
get id() {
return this._id
}
get type() {
return Layer.getLayerType('layer_group')
}
set show(show) {
this._show = show
Object.keys(this._cache).forEach(key => {
this._cache[key].show = this._show
})
}
get show() {
return this._show
}
get layerGroupEvent() {
return this._layerGroupEvent
}
get state() {
return this._state
}
/**
*
* @param viewer
* @private
*/
_onAdd(viewer) {
this._viewer = viewer
Object.keys(this._cache).forEach(key => {
this._viewer.addLayer(this._cache[key])
})
this._state = State.ADDED
}
/**
*
* @private
*/
_onRemove() {
Object.keys(this._cache).forEach(key => {
this._viewer && this._viewer.removeLayer(this._cache[key])
})
this._cache = {}
this._state = State.REMOVED
}
/**
* Adds a layer
* @param layer
* @returns {LayerGroup}
*/
addLayer(layer) {
if (!Object(this._cache).hasOwnProperty(layer.id)) {
this._cache[layer.id] = layer
this._viewer && this._viewer.addLayer(layer)
}
return this
}
/**
* Removes a layer
* @param layer
* @returns {LayerGroup}
*/
removeLayer(layer) {
if (Object(this._cache).hasOwnProperty(layer.id)) {
this._viewer && this._viewer.removeLayer(layer)
delete this._cache[layer.id]
}
return this
}
/**
* Returns a layer by id
* @param id
* @returns {*|undefined}
*/
getLayer(id) {
return this._cache[id] || undefined
}
/**
* Returns all layers
* @returns {[]}
*/
getLayers() {
let result = []
Object.keys(this._cache).forEach(key => {
result.push(this._cache[key])
})
return result
}
/**
* Adds to the viewer
* @param viewer
* @returns {LayerGroup}
*/
addTo(viewer) {
if (viewer && viewer.addLayerGroup) {
viewer.addLayerGroup(this)
}
return this
}
/**
*
* @returns {LayerGroup}
*/
remove() {
this._viewer && this._viewer.removeLayerGroup(this)
return this
}
}
Layer.registerType('layer_group')
export default LayerGroup