nakarte

Source code of https://map.sikmir.ru (fork)
git clone git://git.sikmir.ru/nakarte
Log | Files | Refs | LICENSE

commit 7dff74910c924e5511bbfc4cbf8a8c7611ae8055
parent 167ba25c4f265c57a2846cb7f0045b2c0f9afc8f
Author: Sergej Orlov <wladimirych@gmail.com>
Date:   Tue, 29 Nov 2016 02:36:51 +0300

[layers control] adapt size to map size, with any control position

Diffstat:
Msrc/App.js | 9+++------
Asrc/lib/leaflet.control.layers.adaptive-height/adaptive-height.js | 44++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 47 insertions(+), 6 deletions(-)

diff --git a/src/App.js b/src/App.js @@ -15,7 +15,7 @@ import './adaptive.css'; import 'lib/leaflet.control.panoramas/panoramas'; import 'lib/leaflet.control.track-list/track-list'; import 'lib/leaflet.control.track-list/track-list.hash-state'; - +import enableLayersControlAdaptiveHeight from 'lib/leaflet.control.layers.adaptive-height/adaptive-height'; function autoSizeControl(map, control) { // для контрола Layers есть аналогичная функция при разворачивании из кнопки. @@ -69,21 +69,18 @@ function setUp() { .addTo(map) .enableHashState('l'); enableLayersControlHotKeys (layersControl); - + enableLayersControlAdaptiveHeight(layersControl); new L.Control.PrintPages({position: 'bottomleft'}).addTo(map); new L.Control.Coordinates().addTo(map); new L.Control.Panoramas(document.getElementById('street-view')) .addTo(map) .enableHashState('n'); + const tracksControl = new L.Control.TrackList() .addTo(map) .enableHashState('nktk'); - - map.on('resize', autoSizeControl.bind(null, map, layersControl)); - autoSizeControl(map, layersControl); - map.on('resize', autoSizeControl.bind(null, map, tracksControl)); autoSizeControl(map, tracksControl); diff --git a/src/lib/leaflet.control.layers.adaptive-height/adaptive-height.js b/src/lib/leaflet.control.layers.adaptive-height/adaptive-height.js @@ -0,0 +1,44 @@ +import L from 'leaflet'; + +function enableAdaptiveHeight(control) { + if (control._adaptiveHeightEnabled) { + return; + } + const originalOnAdd = control.onAdd; + + L.Util.extend(control, { + _adaptiveHeightEnabled: true, + + onAdd: function(map) { + const result = originalOnAdd.call(this, map); + this.__setupResizeEventsHandler(); + this.__setAdaptiveHeight(); + return result; + }, + + __setupResizeEventsHandler: function() { + this._map.on('resize', this.__setAdaptiveHeight, this); + }, + + __setAdaptiveHeight: function() { + const mapHeight = this._map.getSize().y; + let maxHeight; + maxHeight = (mapHeight + - this._container.offsetTop // controls above + - (this._container.parentNode.offsetHeight - this._container.offsetTop - this._container.offsetHeight) //controls below + - 30); // margin + this._form.style.maxHeight = maxHeight + 'px'; + } + } + ); + + if (control._map) { + control.__setupResizeEventsHandler(); + setTimeout(() => control.__setAdaptiveHeight(), 0); + + } + + return control; +} + +export default enableAdaptiveHeight;