index.js (1849B)
1 import L from 'leaflet'; 2 import './style.css'; 3 import '~/lib/controls-styles/controls-styles.css'; 4 import enableTopRow from '~/lib/leaflet.control.layers.top-row'; 5 6 function enableMinimize(control) { 7 const originalOnAdd = control.onAdd; 8 if (control._minimizeEnabled) { 9 return; 10 } 11 enableTopRow(control); 12 13 L.Util.extend(control, { 14 _minimizeEnabled: true, 15 16 onAdd: function(map) { 17 const container = originalOnAdd.call(this, map); 18 setTimeout(() => this.__injectMinimizeButtons(), 0); 19 return container; 20 }, 21 22 __injectMinimizeButtons: function() { 23 const container = this._container; 24 const contentsWrapper = L.DomUtil.create('div', 'leaflet-control-content'); 25 while (container.childNodes.length) { 26 contentsWrapper.appendChild(container.childNodes[0]); 27 } 28 container.appendChild(contentsWrapper); 29 const minimizeButton = L.DomUtil.create('div', 'button-minimize'); 30 this._topRow.appendChild(minimizeButton); 31 const expandButton = L.DomUtil.create('div', 'leaflet-control-button-toggle', container); 32 expandButton.title = 'Select layers'; 33 L.DomEvent.on(expandButton, 'click', this.setExpanded, this); 34 L.DomEvent.on(minimizeButton, 'click', this.setMinimized, this); 35 }, 36 37 setExpanded: function() { 38 L.DomUtil.removeClass(this._container, 'minimized'); 39 }, 40 41 setMinimized: function() { 42 L.DomUtil.addClass(this._container, 'minimized'); 43 } 44 } 45 ); 46 if (control._map) { 47 control.__injectMinimizeButtons(); 48 } 49 } 50 51 export default enableMinimize;