-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathLayerControl.js
More file actions
167 lines (158 loc) · 4.93 KB
/
LayerControl.js
File metadata and controls
167 lines (158 loc) · 4.93 KB
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
export var LayerControl = L.Control.Layers.extend({
/* removes 'base' layers as a concept */
options: {
autoZIndex: false,
sortLayers: true,
sortFunction: function (layerA, layerB) {
return layerA.options.zIndex < layerB.options.zIndex
? -1
: layerA.options.zIndex > layerB.options.zIndex
? 1
: 0;
}
},
initialize: function (overlays, options) {
L.setOptions(this, options);
// the _layers array contains objects like {layer: layer, name: "name", overlay: true}
// the array index is the id of the layer returned by L.stamp(layer) which I guess is a unique hash
this._layerControlInputs = [];
this._layers = [];
this._lastZIndex = 0;
this._handlingClick = false;
for (var i in overlays) {
this._addLayer(overlays[i], i, true);
}
},
onAdd: function () {
this._initLayout();
// Adding event on layer control button
L.DomEvent.on(
this._container.getElementsByTagName('a')[0],
'keydown',
this._focusFirstLayer,
this._container
);
L.DomEvent.on(
this._container,
'contextmenu',
this._preventDefaultContextMenu,
this
);
this._update();
if (this._layers.length < 1 && !this._map._showControls) {
this._container.setAttribute('hidden', '');
} else {
this._map._showControls = true;
}
return this._container;
},
onRemove: function (map) {
L.DomEvent.off(
this._container.getElementsByTagName('a')[0],
'keydown',
this._focusFirstLayer,
this._container
);
},
addOrUpdateOverlay: function (layer, name) {
var alreadyThere = false;
for (var i = 0; i < this._layers.length; i++) {
if (this._layers[i].layer === layer) {
alreadyThere = true;
this._layers[i].name = name;
// replace the controls with updated controls if necessary.
break;
}
}
if (!alreadyThere) {
this.addOverlay(layer, name);
}
if (this._layers.length > 0) {
this._container.removeAttribute('hidden');
this._map._showControls = true;
}
return this._map ? this._update() : this;
},
removeLayer: function (layer) {
L.Control.Layers.prototype.removeLayer.call(this, layer);
if (this._layers.length === 0) {
this._container.setAttribute('hidden', '');
}
},
_checkDisabledLayers: function () {},
// focus the first layer in the layer control when enter is pressed
_focusFirstLayer: function (e) {
if (
e.key === 'Enter' &&
this.className ===
'leaflet-control-layers leaflet-control leaflet-control-layers-expanded'
) {
var elem =
this.children[1].children[2].children[0].children[0].children[0]
.children[0];
if (elem) setTimeout(() => elem.focus(), 0);
}
},
_withinZoomBounds: function (zoom, range) {
return range.min <= zoom && zoom <= range.max;
},
_addItem: function (obj) {
var layercontrols = obj.layer._layerEl._layerControlHTML;
// the input is required by Leaflet...
obj.input = layercontrols.querySelector(
'input.leaflet-control-layers-selector'
);
this._layerControlInputs.push(obj.input);
obj.input.layerId = L.stamp(obj.layer);
this._overlaysList.appendChild(layercontrols);
return layercontrols;
},
//overrides collapse and conditionally collapses the panel
collapse: function (e) {
// if layer control is not expanded, return
if (!this._container.className.includes('expanded')) {
return;
}
// return if layer contextmenu is still open
if (
!this._map.contextMenu._extentLayerMenu.hidden ||
!this._map.contextMenu._layerMenu.hidden
) {
return;
}
if (
e.target.tagName === 'SELECT' ||
(e.relatedTarget &&
e.relatedTarget.parentElement &&
(e.relatedTarget.className === 'mapml-contextmenu mapml-layer-menu' ||
e.relatedTarget.parentElement.className ===
'mapml-contextmenu mapml-layer-menu')) ||
(this._map && this._map.contextMenu._layerMenu.style.display === 'block')
)
return this;
L.DomUtil.removeClass(this._container, 'leaflet-control-layers-expanded');
if (e.originalEvent?.pointerType === 'touch') {
this._container._isExpanded = false;
}
return this;
},
_preventDefaultContextMenu: function (e) {
let latlng = this._map.mouseEventToLatLng(e);
let containerPoint = this._map.mouseEventToContainerPoint(e);
e.preventDefault();
// for touch devices, when the layer control is not expanded,
// the layer context menu should not show on map
if (!this._container._isExpanded && e.pointerType === 'touch') {
this._container._isExpanded = true;
return;
}
this._map.fire('contextmenu', {
originalEvent: e,
containerPoint: containerPoint,
latlng: latlng
});
}
});
export var layerControl = function (layers, options) {
return new LayerControl(layers, options);
};