-
Notifications
You must be signed in to change notification settings - Fork 5
/
GroupLayer.js
243 lines (219 loc) · 5.54 KB
/
GroupLayer.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import ol from 'openlayers'
import {ProvideMapMixin} from './ProvideMapMixin'
import {mixin} from '../utilities'
/**
* This Class is a Wrap around {ol.layer.Group} providing some extra functionality. This class is normally used for a
* category of layers containing them.
*/
export class GroupLayer extends mixin(ol.layer.Group, ProvideMapMixin) {
/**
* @param {object} [options={}]
*/
constructor (options = {}) {
super(options)
this.getLayers().on('add', /** ol.CollectionEvent */ e => {
let layer = e.element
if (layer.provideMap) {
layer.provideMap(this.getProvidedMap())
}
})
this.getLayers().on('remove', /** ol.CollectionEvent */ e => {
let layer = e.element
if (layer.provideMap) {
layer.provideMap(null)
}
})
}
/**
* The provideMap methods of all contained children are called recursively
* @param {G4UMap} map
*/
provideMap (map) {
super.provideMap(map)
this.getLayers().forEach(layer => {
if (layer.provideMap) { layer.provideMap(map) }
})
}
/**
* calls callback for every terminal, non-group Layer
* @param {Function} callback
*/
recursiveForEach (callback) {
this.getLayers().forEach(layer => {
callback(layer, this)
if (layer.recursiveForEach) {
layer.recursiveForEach(callback)
}
})
}
/**
* Attachs a listener to each layer and to each newly added listener and removes the listener if the layer gets
* removed.
* @param eventType
* @param listener
*/
forEachOn (eventType, listener) {
this.recursiveForEach(layer => {
layer.on(eventType, listener)
if (layer instanceof GroupLayer) {
this.getLayers().on('add', (e) => {
e.element.on(eventType, listener)
})
this.getLayers().on('remove', (e) => {
e.element.un(eventType, listener)
})
}
})
}
/**
* Checks how many children are visible. Doesn't check visibility of the group layer
* @returns {number}
*/
getChildrenVisible () {
let array = this.getLayersArray()
let count = 0
for (let i = 0, ii = array.length; i < ii; i++) {
if (array[i] instanceof GroupLayer) {
count += array[i].getChildrenVisible()
} else if (array[i].getVisible()) {
count += 1
}
}
return count
}
/**
* Returns the layers inside as an array.
* @returns {ol.layer.Base[]}
*/
getLayersArray () {
return this.getLayers().getArray()
}
/**
* Returns the number of direct child layers
* @returns {number}
*/
getLength () {
return this.getLayers().getLength()
}
/**
* checks if the given layer is a child of the grouplayer
* @param {ol.layer.Base} layer
* @returns {boolean}
*/
isParentOf (layer) {
let found = false
this.recursiveForEach(function (childLayer) {
if (layer === childLayer) {
found = true
}
})
return found
}
/**
* Returns all ids of all contained layers and its own.
* @returns {number[]}
*/
getIds () {
let ids = [this.get('id')]
let array = this.getLayersArray()
for (let i = 0, ii = array.length; i < ii; i++) {
if (array[i].getIds) {
ids = ids.concat(array[i].getIds())
} else {
ids.push(array[i].get('id'))
}
}
return ids
}
/**
* Returns all attributions of all visible layers.
* @returns {number[]}
*/
getAttributions () {
let attributions = []
this.recursiveForEach(function (layer) {
if (layer.getVisible()) {
if (layer.getSource) {
let atts = layer.getSource().getAttributions()
if (atts) {
attributions = attributions.concat(atts)
}
}
}
})
return attributions
}
/**
* Remove layer by id
* @param {string|number} id
*/
removeLayerById (id) {
let layers = this.getLayers()
for (let i = 0; i < layers.getLength(); i++) {
if (layers.item(i).get('id') === id) {
return layers.removeAt(i)
} else if (layers.item(i).removeLayerById) {
let res = layers.item(i).removeLayerById(id)
if (res) {
return res
}
}
}
}
/**
* Get layer by id
* @param {string|number} id
*/
getLayerById (id) {
let layers = this.getLayers()
for (let i = 0; i < layers.getLength(); i++) {
if (layers.item(i).get('id') === id) {
return layers.item(i)
} else if (layers.item(i).getLayerById) {
let res = layers.item(i).getLayerById(id)
if (res) {
return res
}
}
}
}
/**
* removes layer
* @param {ol.layer.Base} layer
*/
removeLayer (layer) {
let found = this.getLayers().remove(layer)
if (!found) {
this.getLayers().forEach(subLayer => {
if (!found && subLayer.removeLayer) {
found = subLayer.removeLayer(layer)
}
})
}
return found
}
/**
* gives an object with ids as keys and visibility as value
* @returns {Object.<string|number,boolean>}
*/
getIdsVisibilities () {
let visibilities = {}
this.recursiveForEach(layer => {
if (layer.get('id')) {
visibilities[layer.get('id')] = layer.getVisible()
}
})
return visibilities
}
/**
* Sets the visibility of the layers to the given visibility
* @param {Object.<string|number,boolean>} visibilities
*/
setIdsVisibilities (visibilities) {
this.recursiveForEach(layer => {
if (layer.get('id')) {
layer.setVisible(visibilities[layer.get('id')])
}
})
}
}