|
|
|
|
|
import {Control} from './Control';
|
|
|
import {Map} from '../map/Map';
|
|
|
import * as DomUtil from '../dom/DomUtil';
|
|
|
import * as DomEvent from '../dom/DomEvent';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export var Zoom = Control.extend({
|
|
|
|
|
|
|
|
|
options: {
|
|
|
position: 'topleft',
|
|
|
|
|
|
|
|
|
|
|
|
zoomInText: '<span aria-hidden="true">+</span>',
|
|
|
|
|
|
|
|
|
|
|
|
zoomInTitle: 'Zoom in',
|
|
|
|
|
|
|
|
|
|
|
|
zoomOutText: '<span aria-hidden="true">−</span>',
|
|
|
|
|
|
|
|
|
|
|
|
zoomOutTitle: 'Zoom out'
|
|
|
},
|
|
|
|
|
|
onAdd: function (map) {
|
|
|
var zoomName = 'leaflet-control-zoom',
|
|
|
container = DomUtil.create('div', zoomName + ' leaflet-bar'),
|
|
|
options = this.options;
|
|
|
|
|
|
this._zoomInButton = this._createButton(options.zoomInText, options.zoomInTitle,
|
|
|
zoomName + '-in', container, this._zoomIn);
|
|
|
this._zoomOutButton = this._createButton(options.zoomOutText, options.zoomOutTitle,
|
|
|
zoomName + '-out', container, this._zoomOut);
|
|
|
|
|
|
this._updateDisabled();
|
|
|
map.on('zoomend zoomlevelschange', this._updateDisabled, this);
|
|
|
|
|
|
return container;
|
|
|
},
|
|
|
|
|
|
onRemove: function (map) {
|
|
|
map.off('zoomend zoomlevelschange', this._updateDisabled, this);
|
|
|
},
|
|
|
|
|
|
disable: function () {
|
|
|
this._disabled = true;
|
|
|
this._updateDisabled();
|
|
|
return this;
|
|
|
},
|
|
|
|
|
|
enable: function () {
|
|
|
this._disabled = false;
|
|
|
this._updateDisabled();
|
|
|
return this;
|
|
|
},
|
|
|
|
|
|
_zoomIn: function (e) {
|
|
|
if (!this._disabled && this._map._zoom < this._map.getMaxZoom()) {
|
|
|
this._map.zoomIn(this._map.options.zoomDelta * (e.shiftKey ? 3 : 1));
|
|
|
}
|
|
|
},
|
|
|
|
|
|
_zoomOut: function (e) {
|
|
|
if (!this._disabled && this._map._zoom > this._map.getMinZoom()) {
|
|
|
this._map.zoomOut(this._map.options.zoomDelta * (e.shiftKey ? 3 : 1));
|
|
|
}
|
|
|
},
|
|
|
|
|
|
_createButton: function (html, title, className, container, fn) {
|
|
|
var link = DomUtil.create('a', className, container);
|
|
|
link.innerHTML = html;
|
|
|
link.href = '#';
|
|
|
link.title = title;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
link.setAttribute('role', 'button');
|
|
|
link.setAttribute('aria-label', title);
|
|
|
|
|
|
DomEvent.disableClickPropagation(link);
|
|
|
DomEvent.on(link, 'click', DomEvent.stop);
|
|
|
DomEvent.on(link, 'click', fn, this);
|
|
|
DomEvent.on(link, 'click', this._refocusOnMap, this);
|
|
|
|
|
|
return link;
|
|
|
},
|
|
|
|
|
|
_updateDisabled: function () {
|
|
|
var map = this._map,
|
|
|
className = 'leaflet-disabled';
|
|
|
|
|
|
DomUtil.removeClass(this._zoomInButton, className);
|
|
|
DomUtil.removeClass(this._zoomOutButton, className);
|
|
|
this._zoomInButton.setAttribute('aria-disabled', 'false');
|
|
|
this._zoomOutButton.setAttribute('aria-disabled', 'false');
|
|
|
|
|
|
if (this._disabled || map._zoom === map.getMinZoom()) {
|
|
|
DomUtil.addClass(this._zoomOutButton, className);
|
|
|
this._zoomOutButton.setAttribute('aria-disabled', 'true');
|
|
|
}
|
|
|
if (this._disabled || map._zoom === map.getMaxZoom()) {
|
|
|
DomUtil.addClass(this._zoomInButton, className);
|
|
|
this._zoomInButton.setAttribute('aria-disabled', 'true');
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Map.mergeOptions({
|
|
|
zoomControl: true
|
|
|
});
|
|
|
|
|
|
Map.addInitHook(function () {
|
|
|
if (this.options.zoomControl) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.zoomControl = new Zoom();
|
|
|
this.addControl(this.zoomControl);
|
|
|
}
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export var zoom = function (options) {
|
|
|
return new Zoom(options);
|
|
|
};
|
|
|
|