X-Git-Url: https://git.openstreetmap.org/rails.git/blobdiff_plain/6f793e1b21a534b0331a5874613c4f3c05853a45..b9f7f39140e942df4dc08d4167d3c07202628840:/vendor/assets/leaflet/leaflet.zoom.js diff --git a/vendor/assets/leaflet/leaflet.zoom.js b/vendor/assets/leaflet/leaflet.zoom.js index de634040d..fe7e2f5d2 100644 --- a/vendor/assets/leaflet/leaflet.zoom.js +++ b/vendor/assets/leaflet/leaflet.zoom.js @@ -8,29 +8,45 @@ L.Control.Zoomslider = L.Control.extend({ onAdd: function (map) { var className = 'leaflet-control-zoomslider', container = L.DomUtil.create('div', className); - - this._map = map; - this._createButton('Zoom in', className + '-in' - , container, this._zoomIn , this); + L.DomEvent.disableClickPropagation(container); + + this._map = map; + + this._zoomInButton = this._createButton('Zoom in', className + '-in' + , container, this._zoomIn , this); this._createSlider(className + '-slider', container, map); - this._createButton('Zoom out', className + '-out' - , container, this._zoomOut, this); + this._zoomOutButton = this._createButton('Zoom out', className + '-out' + , container, this._zoomOut, this); - - - this._map.on('zoomend', this._snapToSliderValue, this); + map.on('layeradd layerremove', this._refresh, this); + + map.whenReady(function(){ + this._snapToSliderValue(); + map.on('zoomend', this._snapToSliderValue, this); + }, this); - this._snapToSliderValue(); return container; }, onRemove: function(map){ map.off('zoomend', this._snapToSliderValue); + map.off('layeradd layerremove', this._refresh); + }, + + _refresh: function(){ + this._map + .removeControl(this) + .addControl(this); }, - + _createSlider: function (className, container, map) { var zoomLevels = map.getMaxZoom() - map.getMinZoom(); + // This means we have no tilelayers (or that they are setup in a strange way). + // Either way we don't want to add a slider here. + if(zoomLevels == Infinity){ + return undefined; + } this._sliderHeight = this.options.stepHeight * zoomLevels; var wrapper = L.DomUtil.create('div', className + '-wrap', container); @@ -40,11 +56,8 @@ L.Control.Zoomslider = L.Control.extend({ this._draggable = this._createDraggable(); this._draggable.enable(); - - L.DomEvent - .on(slider, 'click', L.DomEvent.stopPropagation) - .on(slider, 'click', L.DomEvent.preventDefault) - .on(slider, 'click', this._onSliderClick, this); + + L.DomEvent.on(slider, 'click', this._onSliderClick, this); return slider; }, @@ -63,31 +76,26 @@ L.Control.Zoomslider = L.Control.extend({ link.title = title; L.DomEvent - .on(link, 'click', L.DomEvent.stopPropagation) - .on(link, 'click', L.DomEvent.preventDefault) - .on(link, 'click', fn, context); - + .on(link, 'click', L.DomEvent.preventDefault) + .on(link, 'click', fn, context); + return link; }, _createDraggable: function() { - L.DomUtil.setPosition(this._knob, new L.Point(0, 0)); - L.DomEvent - .on(this._knob - , L.Draggable.START - , L.DomEvent.stopPropagation) - .on(this._knob, 'click', L.DomEvent.stopPropagation); + L.DomUtil.setPosition(this._knob, L.point(0, 0)); + L.DomEvent.disableClickPropagation(this._knob); var bounds = new L.Bounds( - new L.Point(0, 0), - new L.Point(0, this._sliderHeight) + L.point(0, 0), + L.point(0, this._sliderHeight) ); - var draggable = new L.BoundedDraggable(this._knob, - this._knob, + var draggable = new L.BoundedDraggable(this._knob, + this._knob, bounds) .on('drag', this._snap, this) .on('dragend', this._setZoom, this); - + return draggable; }, @@ -100,9 +108,9 @@ L.Control.Zoomslider = L.Control.extend({ _onSliderClick: function(e){ var first = (e.touches && e.touches.length === 1 ? e.touches[0] : e); - var offset = first.offsetY + var offset = first.offsetY ? first.offsetY - : L.DomEvent.getMousePosition(first).y + : L.DomEvent.getMousePosition(first).y - L.DomUtil.getViewportOffset(this._knob).y; var value = this._posToSliderValue(offset - this._knob.offsetHeight / 2); this._snapToSliderValue(value); @@ -110,20 +118,21 @@ L.Control.Zoomslider = L.Control.extend({ }, _posToSliderValue: function(pos) { - pos = isNaN(pos) + pos = isNaN(pos) ? L.DomUtil.getPosition(this._knob).y - : pos + : pos; return Math.round( (this._sliderHeight - pos) / this.options.stepHeight); }, _snapToSliderValue: function(sliderValue) { + this._updateDisabled(); if(this._knob) { - sliderValue = isNaN(sliderValue) + sliderValue = isNaN(sliderValue) ? this._getSliderValue() : sliderValue; - var y = this._sliderHeight + var y = this._sliderHeight - (sliderValue * this.options.stepHeight); - L.DomUtil.setPosition(this._knob, new L.Point(0, y)); + L.DomUtil.setPosition(this._knob, L.point(0, y)); } }, _toZoomLevel: function(sliderValue) { @@ -134,6 +143,21 @@ L.Control.Zoomslider = L.Control.extend({ }, _getSliderValue: function(){ return this._toSliderValue(this._map.getZoom()); + }, + + _updateDisabled: function () { + var map = this._map, + className = 'leaflet-control-zoomslider-disabled'; + + L.DomUtil.removeClass(this._zoomInButton, className); + L.DomUtil.removeClass(this._zoomOutButton, className); + + if (map.getZoom() === map.getMinZoom()) { + L.DomUtil.addClass(this._zoomOutButton, className); + } + if (map.getZoom() === map.getMaxZoom()) { + L.DomUtil.addClass(this._zoomInButton, className); + } } }); @@ -144,8 +168,7 @@ L.Map.mergeOptions({ L.Map.addInitHook(function () { if (this.options.zoomsliderControl) { - this.zoomsliderControl = new L.Control.Zoomslider(); - this.addControl(this.zoomsliderControl); + L.control.zoomslider().addTo(this); } }); @@ -163,9 +186,9 @@ L.BoundedDraggable = L.Draggable.extend({ this._newPos = this._fitPoint(this._newPos); } }, this); - }, + }, _fitPoint: function(point){ - var closest = new L.Point( + var closest = L.point( Math.min(point.x, this._bounds.max.x), Math.min(point.y, this._bounds.max.y) );