Debian Codegraph @

public_html/ol3/ol3-layerswitcher.js

raw
1 /**
2 * OpenLayers 3 Layer Switcher Control.
3 * See [the examples](./examples) for usage.
4 * @constructor
5 * @extends {ol.control.Control}
6 * @param {Object} opt_options Control options, extends olx.control.ControlOptions adding:
7 * **`tipLabel`** `String` - the button tooltip.
8 */
9 ol.control.LayerSwitcher = function(opt_options) {
10
11 var options = opt_options || {};
12
13 var tipLabel = options.tipLabel ?
14 options.tipLabel : 'Legend';
15
16 this.mapListeners = [];
17
18 this.hiddenClassName = 'ol-unselectable ol-control layer-switcher';
19 if (ol.control.LayerSwitcher.isTouchDevice_()) {
20 this.hiddenClassName += ' touch';
21 }
22 this.shownClassName = this.hiddenClassName + ' shown';
23
24 var element = document.createElement('div');
25 element.className = this.hiddenClassName;
26
27 var button = document.createElement('button');
28 button.setAttribute('title', tipLabel);
29 element.appendChild(button);
30
31 this.panel = document.createElement('div');
32 this.panel.className = 'panel';
33 element.appendChild(this.panel);
34 ol.control.LayerSwitcher.enableTouchScroll_(this.panel);
35
36 var this_ = this;
37
38 button.onmouseover = function(e) {
39 this_.showPanel();
40 };
41
42 button.onclick = function(e) {
43 e = e || window.event;
44 this_.showPanel();
45 e.preventDefault();
46 };
47
48 this_.panel.onmouseout = function(e) {
49 e = e || window.event;
50 if (!this_.panel.contains(e.toElement || e.relatedTarget)) {
51 this_.hidePanel();
52 }
53 };
54
55 ol.control.Control.call(this, {
56 element: element,
57 target: options.target
58 });
59
60 };
61
62 ol.inherits(ol.control.LayerSwitcher, ol.control.Control);
63
64 /**
65 * Show the layer panel.
66 */
67 ol.control.LayerSwitcher.prototype.showPanel = function() {
68 if (this.element.className != this.shownClassName) {
69 this.element.className = this.shownClassName;
70 this.renderPanel();
71 }
72 };
73
74 /**
75 * Hide the layer panel.
76 */
77 ol.control.LayerSwitcher.prototype.hidePanel = function() {
78 if (this.element.className != this.hiddenClassName) {
79 this.element.className = this.hiddenClassName;
80 }
81 };
82
83 /**
84 * Re-draw the layer panel to represent the current state of the layers.
85 */
86 ol.control.LayerSwitcher.prototype.renderPanel = function() {
87
88 this.ensureTopVisibleBaseLayerShown_();
89
90 while(this.panel.firstChild) {
91 this.panel.removeChild(this.panel.firstChild);
92 }
93
94 var ul = document.createElement('ul');
95 this.panel.appendChild(ul);
96 this.renderLayers_(this.getMap(), ul);
97
98 };
99
100 /**
101 * Set the map instance the control is associated with.
102 * @param {ol.Map} map The map instance.
103 */
104 ol.control.LayerSwitcher.prototype.setMap = function(map) {
105 // Clean up listeners associated with the previous map
106 for (var i = 0, key; i < this.mapListeners.length; i++) {
107 this.getMap().unByKey(this.mapListeners[i]);
108 }
109 this.mapListeners.length = 0;
110 // Wire up listeners etc. and store reference to new map
111 ol.control.Control.prototype.setMap.call(this, map);
112 if (map) {
113 var this_ = this;
114 this.mapListeners.push(map.on('pointerdown', function() {
115 this_.hidePanel();
116 }));
117 this.renderPanel();
118 }
119 };
120
121 /**
122 * Ensure only the top-most base layer is visible if more than one is visible.
123 * @private
124 */
125 ol.control.LayerSwitcher.prototype.ensureTopVisibleBaseLayerShown_ = function() {
126 var lastVisibleBaseLyr;
127 ol.control.LayerSwitcher.forEachRecursive(this.getMap(), function(l, idx, a) {
128 if (l.get('type') === 'base' && l.getVisible()) {
129 lastVisibleBaseLyr = l;
130 }
131 });
132 if (lastVisibleBaseLyr) this.setVisible_(lastVisibleBaseLyr, true);
133 };
134
135 /**
136 * Toggle the visible state of a layer.
137 * Takes care of hiding other layers in the same exclusive group if the layer
138 * is toggle to visible.
139 * @private
140 * @param {ol.layer.Base} The layer whos visibility will be toggled.
141 */
142 ol.control.LayerSwitcher.prototype.setVisible_ = function(lyr, visible) {
143 var map = this.getMap();
144 lyr.setVisible(visible);
145 if (visible && lyr.get('type') === 'base') {
146 // Hide all other base layers regardless of grouping
147 ol.control.LayerSwitcher.forEachRecursive(map, function(l, idx, a) {
148 if (l != lyr && l.get('type') === 'base') {
149 l.setVisible(false);
150 }
151 });
152 }
153 };
154
155 /**
156 * Render all layers that are children of a group.
157 * @private
158 * @param {ol.layer.Base} lyr Layer to be rendered (should have a title property).
159 * @param {Number} idx Position in parent group list.
160 */
161 ol.control.LayerSwitcher.prototype.renderLayer_ = function(lyr, idx) {
162
163 var this_ = this;
164
165 var li = document.createElement('li');
166
167 var lyrTitle = lyr.get('title');
168 var lyrId = ol.control.LayerSwitcher.uuid();
169
170 var label = document.createElement('label');
171
172 if (lyr.getLayers && !lyr.get('combine')) {
173
174 li.className = 'group';
175 label.innerHTML = lyrTitle;
176 li.appendChild(label);
177 var ul = document.createElement('ul');
178 li.appendChild(ul);
179
180 this.renderLayers_(lyr, ul);
181
182 } else {
183
184 li.className = 'layer';
185 var input = document.createElement('input');
186 if (lyr.get('type') === 'base') {
187 input.type = 'radio';
188 input.name = 'base';
189 } else {
190 input.type = 'checkbox';
191 }
192 input.id = lyrId;
193 input.checked = lyr.get('visible');
194 input.onchange = function(e) {
195 this_.setVisible_(lyr, e.target.checked);
196 };
197 li.appendChild(input);
198
199 label.htmlFor = lyrId;
200 label.innerHTML = lyrTitle;
201 li.appendChild(label);
202
203 }
204
205 return li;
206
207 };
208
209 /**
210 * Render all layers that are children of a group.
211 * @private
212 * @param {ol.layer.Group} lyr Group layer whos children will be rendered.
213 * @param {Element} elm DOM element that children will be appended to.
214 */
215 ol.control.LayerSwitcher.prototype.renderLayers_ = function(lyr, elm) {
216 var lyrs = lyr.getLayers().getArray().slice();
217 for (var i = 0, l; i < lyrs.length; i++) {
218 l = lyrs[i];
219 if (l.get('title')) {
220 elm.appendChild(this.renderLayer_(l, i));
221 }
222 }
223 };
224
225 /**
226 * **Static** Call the supplied function for each layer in the passed layer group
227 * recursing nested groups.
228 * @param {ol.layer.Group} lyr The layer group or collection/array to start iterating from.
229 * @param {Function} fn Callback which will be called for each `ol.layer.Base`
230 * found under `lyr`. The signature for `fn` is the same as `ol.Collection#forEach`
231 */
232 ol.control.LayerSwitcher.forEachRecursive = function(lyr, fn) {
233 var traverse = function(lyr, idx, a) {
234 fn(lyr, idx, a);
235 if (lyr.getLayers) {
236 ol.control.LayerSwitcher.forEachRecursive(lyr, fn);
237 }
238 };
239
240 if (lyr.getLayers) {
241 lyr.getLayers().forEach(traverse);
242 } else {
243 lyr.forEach(traverse);
244 }
245 };
246
247 /**
248 * Generate a UUID
249 * @returns {String} UUID
250 *
251 * Adapted from http://stackoverflow.com/a/2117523/526860
252 */
253 ol.control.LayerSwitcher.uuid = function() {
254 return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
255 var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
256 return v.toString(16);
257 });
258 }
259
260 /**
261 * @private
262 * @desc Apply workaround to enable scrolling of overflowing content within an
263 * element. Adapted from https://gist.github.com/chrismbarr/4107472
264 */
265 ol.control.LayerSwitcher.enableTouchScroll_ = function(elm) {
266 if(ol.control.LayerSwitcher.isTouchDevice_()){
267 var scrollStartPos = 0;
268 elm.addEventListener("touchstart", function(event) {
269 scrollStartPos = this.scrollTop + event.touches[0].pageY;
270 }, false);
271 elm.addEventListener("touchmove", function(event) {
272 this.scrollTop = scrollStartPos - event.touches[0].pageY;
273 }, false);
274 }
275 };
276
277 /**
278 * @private
279 * @desc Determine if the current browser supports touch events. Adapted from
280 * https://gist.github.com/chrismbarr/4107472
281 */
282 ol.control.LayerSwitcher.isTouchDevice_ = function() {
283 try {
284 document.createEvent("TouchEvent");
285 return true;
286 } catch(e) {
287 return false;
288 }
289 };