PageSourceSearch

http://somomap.es/pl/Leaflet.StyledLayerControl-master/src/styledLayerControl.js

js somomap.es collected 2026-10-03 18:43:03 UTC 10,612 bytes, 396 lines download raw bytes

1L.Control.StyledLayerControl = L.Control.Layers.extend({
2		options : {
3			collapsed : true,
4			position : 'topright',
5			autoZIndex : true
6		},
7
8		initialize : function (baseLayers, groupedOverlays, options) {
9			var i,
10			j;
11			L.Util.setOptions(this, options);
12
13			this._layers = {};
14			this._lastZIndex = 0;
15			this._handlingClick = false;
16			this._groupList = [];
17			this._domGroups = [];
18
19			for (i in baseLayers) {
20				for (var j in baseLayers[i].layers) {
21					this._addLayer(baseLayers[i].layers[j], j, baseLayers[i], false);
22				}
23			}
24
25			for (i in groupedOverlays) {
26				for (var j in groupedOverlays[i].layers) {
27					this._addLayer(groupedOverlays[i].layers[j], j, groupedOverlays[i], true);
28				}
29			}
30			
31			
32		},
33
34		onAdd : function (map) {
35			this._initLayout();
36			this._update();
37
38			map
39			.on('layeradd', this._onLayerChange, this)
40			.on('layerremove', this._onLayerChange, this);
41
42			return this._container;
43		},
44
45		onRemove : function (map) {
46			map
47			.off('layeradd', this._onLayerChange)
48			.off('layerremove', this._onLayerChange);
49		},
50
51		addBaseLayer : function (layer, name, group) {
52			this._addLayer(layer, name, group, false);
53			this._update();
54			return this;
55		},
56
57		addOverlay : function (layer, name, group) {
58			this._addLayer(layer, name, group, true);
59			this._update();
60			return this;
61		},
62
63		removeLayer : function (layer) {
64			var id = L.Util.stamp(layer);
65			delete this._layers[id];
66			this._update();
67			return this;
68		},
69			
70		removeGroup : function (group_Name){
71			for(group in this._groupList){
72				if( this._groupList[group].groupName == group_Name ){
73					for(layer in this._layers){
74						if( this._layers[layer].group && this._layers[layer].group.name == group_Name ){
75							delete this._layers[layer];
76						}
77					}
78					delete this._groupList[group];
79					this._update();
80					break;
81				}
82			}
83		},	
84
85		_initLayout : function () {
86			var className = 'leaflet-control-layers',
87			container = this._container = L.DomUtil.create('div', className);
88
89			//Makes this work on IE10 Touch devices by stopping it from firing a mouseout event when the touch is released
90			container.setAttribute('aria-haspopup', true);
91
92			if (!L.Browser.touch) {
93				L.DomEvent.disableClickPropagation(container);
94				L.DomEvent.on(container, 'wheel', L.DomEvent.stopPropagation);
95			} else {
96				L.DomEvent.on(container, 'click', L.DomEvent.stopPropagation);
97			}
98			
99			var section = document.createElement('section');
100			section.className = 'ac-container ' + className + '-list';
101			
102			var form = this._form = L.DomUtil.create('form');
103			
104			section.appendChild( form );
105
106			if (this.options.collapsed) {
107				if (!L.Browser.android) {
108					L.DomEvent
109					.on(container, 'mouseover', this._expand, this)
110					.on(container, 'mouseout', this._collapse, this);
111				}
112				var link = this._layersLink = L.DomUtil.create('a', className + '-toggle', container);
113				link.href = '#';
114				link.title = 'Layers';
115
116				if (L.Browser.touch) {
117					L.DomEvent
118					.on(link, 'click', L.DomEvent.stop)
119					.on(link, 'click', this._expand, this);
120				} else {
121					L.DomEvent.on(link, 'focus', this._expand, this);
122				}
123
124				this._map.on('click', this._collapse, this);
125				// TODO keyboard accessibility
126				
127			} else {
128				this._expand();
129			}
130
131			this._baseLayersList = L.DomUtil.create('div', className + '-base', form);
132			this._overlaysList = L.DomUtil.create('div', className + '-overlays', form);
133
134			container.appendChild(section);
135			
136			// process options of ac-container css class - to options.container_width and options.container_maxHeight
137			for(var c = 0; c < (containers = container.getElementsByClassName('ac-container')).length; c++ ){
138				if (this.options.container_width) {
139					containers[c].style.width = this.options.container_width;
140				}	
141					
142				// set the max-height of control to y value of map object
143				this._default_maxHeight = this.options.container_maxHeight ?  this.options.container_maxHeight : (this._map._size.y - 70);
144				containers[c].style.maxHeight = this._default_maxHeight + "px";
145				
146			}
147			
148			window.onresize = this._on_resize_window.bind(this);
149			
150		},
151			
152		_on_resize_window : function(){
153			// listen to resize of screen to reajust de maxHeight of container
154			for(var c = 0; c < containers.length; c++ ){
155				// input the new value to height
156				containers[c].style.maxHeight = (window.innerHeight-90) < this._default_maxHeight ? (window.innerHeight - 90) + "px" : this._default_maxHeight + "px";
157			}
158		},
159
160		_addLayer : function (layer, name, group, overlay) {
161			var id = L.Util.stamp(layer);
162
163			this._layers[id] = {
164				layer : layer,
165				name : name,
166				overlay : overlay
167			};
168
169			if (group) {
170				var groupId = this._groupList.indexOf(group);
171				
172				// if not find the group search for the name
173				if( groupId === -1 ){
174					for( g in this._groupList){
175						if( this._groupList[g].groupName == group.groupName ){
176							groupId = g;
177							break;
178						}
179					}
180				}
181
182				if (groupId === -1) {
183					groupId = this._groupList.push(group) - 1;
184				}
185
186				this._layers[id].group = {
187					name : group.groupName,
188					id : groupId,
189					expanded : group.expanded
190				};
191			}
192
193			if (this.options.autoZIndex && layer.setZIndex) {
194				this._lastZIndex++;
195				layer.setZIndex(this._lastZIndex);
196			}
197		},
198
199		_update : function () {
200			if (!this._container) {
201				return;
202			}
203
204			this._baseLayersList.innerHTML = '';
205			this._overlaysList.innerHTML = '';
206			this._domGroups.length = 0;
207
208			var baseLayersPresent = false,
209			overlaysPresent = false,
210			i,
211			obj;
212
213			for (i in this._layers) {
214				obj = this._layers[i];
215				this._addItem(obj);
216				overlaysPresent = overlaysPresent || obj.overlay;
217				baseLayersPresent = baseLayersPresent || !obj.overlay;
218			}
219		
220		},
221
222		_onLayerChange : function (e) {
223			var obj = this._layers[L.Util.stamp(e.layer)];
224
225			if (!obj) {
226				return;
227			}
228
229			if (!this._handlingClick) {
230				this._update();
231			}
232
233			var type = obj.overlay ?
234				(e.type === 'layeradd' ? 'overlayadd' : 'overlayremove') :
235				(e.type === 'layeradd' ? 'baselayerchange' : null);
236
237			if (type) {
238				this._map.fire(type, obj);
239			}
240		},
241
242		// IE7 bugs out if you create a radio dynamically, so you have to do it this hacky way (see http://bit.ly/PqYLBe)
243		_createRadioElement : function (name, checked) {
244
245			var radioHtml = '<input type="radio" class="leaflet-control-layers-selector" name="' + name + '"';
246			if (checked) {
247				radioHtml += ' checked="checked"';
248			}
249			radioHtml += '/>';
250
251			var radioFragment = document.createElement('div');
252			radioFragment.innerHTML = radioHtml;
253
254			return radioFragment.firstChild;
255		},
256
257		_addItem : function (obj) {
258			var label = document.createElement('div'),
259			input,
260			checked = this._map.hasLayer(obj.layer),
261			container;
262		
263
264			if (obj.overlay) {
265				input = document.createElement('input');
266				input.type = 'checkbox';
267				input.className = 'leaflet-control-layers-selector';
268				input.defaultChecked = checked;
269				
270				label.className = "menu-item-checkbox"; 
271				
272			} else {
273				input = this._createRadioElement('leaflet-base-layers', checked);
274				
275				label.className = "menu-item-radio"; 
276			}
277			
278			
279			input.layerId = L.Util.stamp(obj.layer);
280
281			L.DomEvent.on(input, 'click', this._onInputClick, this);
282
283			var name = document.createElement('span');
284			name.innerHTML = ' ' + obj.name;
285
286			label.appendChild(input);
287			label.appendChild(name);
288			
289			// configure the delete button for layers with attribute removable = true
290			if( obj.layer.StyledLayerControl && obj.layer.StyledLayerControl.removable ){
291				var bt_delete = document.createElement("input");
292				bt_delete.type = "button";
293				bt_delete.className = "bt_delete";
294				L.DomEvent.on(bt_delete, 'click', this._onDeleteClick, this);
295				label.appendChild(bt_delete);
296			}	
297
298			if (obj.overlay) {
299				container = this._overlaysList;
300			} else {
301				container = this._baseLayersList;
302			}
303						
304			var groupContainer = this._domGroups[obj.group.id];
305
306			if (!groupContainer) {
307				
308				groupContainer = document.createElement('div');
309				groupContainer.id = 'leaflet-control-accordion-layers-' + obj.group.id;
310				
311				// verify if group is expanded
312				var s_expanded = obj.group.expanded ? ' checked = "true" ' : '';
313				
314				// verify if type is exclusive
315				var s_type_exclusive = this.options.exclusive ? ' type="radio" ' : ' type="checkbox" ';
316				
317				inputElement = '<input id="ac' + obj.group.id + '" name="accordion-1" class="menu" ' + s_expanded + s_type_exclusive + '/>';
318				inputLabel   = '<label for="ac' + obj.group.id + '">' + obj.group.name + '</label>';
319				
320				article = document.createElement('article');
321				article.className = 'ac-large';
322				article.appendChild( label );
323				
324				// process options of ac-large css class - to options.group_maxHeight property
325				if(this.options.group_maxHeight){
326					article.style.maxHeight = this.options.group_maxHeight;
327				}
328				
329				groupContainer.innerHTML = inputElement + inputLabel;
330				groupContainer.appendChild( article );
331				container.appendChild(groupContainer); 
332
333				this._domGroups[obj.group.id] = groupContainer;
334			} else {
335				groupContainer.lastElementChild.appendChild( label );
336			}	
337
338			return label;
339		},
340
341		_onInputClick : function () {
342			var i,
343			input,
344			obj,
345			inputs = this._form.getElementsByTagName('input'),
346			inputsLen = inputs.length;
347
348			this._handlingClick = true;
349
350			for (i = 0; i < inputsLen; i++) {
351				input = inputs[i];
352				obj = this._layers[input.layerId];
353				
354			    if ( !obj ) { continue; }
355
356				if (input.checked && !this._map.hasLayer(obj.layer)) {
357					this._map.addLayer(obj.layer);
358
359				} else if (!input.checked && this._map.hasLayer(obj.layer)) {
360					this._map.removeLayer(obj.layer);
361				}
362			}
363
364			this._handlingClick = false;
365		},
366			
367		_onDeleteClick : function(obj){
368			var node = obj.target.parentElement.childNodes[0];
369			n_obj = this._layers[node.layerId];
370			
371			// verify if obj is a basemap and checked to not remove
372			if( !n_obj.overlay && node.checked ){
373				return false;
374			}	
375			
376			if( this._map.hasLayer(n_obj.layer) ){
377				this._map.removeLayer(n_obj.layer);
378			}
379			
380			obj.target.parentNode.remove();
381			
382			return false;
383		},
384
385		_expand : function () {
386			L.DomUtil.addClass(this._container, 'leaflet-control-layers-expanded');
387		},
388
389		_collapse : function () {
390			this._container.className = this._container.className.replace(' leaflet-control-layers-expanded', '');
391		}
392	});
393
394L.Control.styledLayerControl = function (baseLayers, overlays, options) {
395	return new L.Control.StyledLayerControl(baseLayers, overlays, options);
396};

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.