-
Notifications
You must be signed in to change notification settings - Fork 5
/
BaseLayerMixin.js
62 lines (56 loc) · 1.51 KB
/
BaseLayerMixin.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
import {GroupLayer} from './GroupLayer'
/**
* @typedef {object} BaseLayerMixinOptions
* @property {GroupLayer} groupLayer the group this baseLayer belongs to
*/
/**
* This mixin provides baselayer functionality to any layer
*/
export class BaseLayerMixin {
/**
* @param {BaseLayerMixinOptions} options
*/
initialize (options) {
/**
* @type {GroupLayer}
* @private
*/
this.groupLayer_ = options.groupLayer
this.on('change:available', this.onChangeAvailable.bind(this))
this.on('change:visible', this.onChangeVisible.bind(this))
}
/**
* This method sets the next available layer visible if this layer gets unavailable and was the visible baselayer
*/
onChangeAvailable () {
if (!this.get('available') && this.getVisible()) {
this.groupLayer_.recursiveForEach(layer => {
if (!(layer instanceof GroupLayer) && layer.get('available')) {
layer.setVisible(true)
}
})
}
}
/**
* This method sets all other baselayers not visible if set true.
*/
onChangeVisible () {
if (this.getVisible()) {
this.groupLayer_.recursiveForEach(layer => {
if (!(layer instanceof GroupLayer) && layer !== this) {
layer.setVisible(false)
}
})
} else {
let found = false
this.groupLayer_.recursiveForEach(layer => {
if (!(layer instanceof GroupLayer) && layer.getVisible()) {
found = true
}
})
if (!found) {
this.setVisible(true)
}
}
}
}