|
|
|
|
|
import {Class} from '../core/Class';
|
|
|
import {Map} from '../map/Map';
|
|
|
import * as Util from '../core/Util';
|
|
|
import * as DomUtil from '../dom/DomUtil';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export var Control = Class.extend({
|
|
|
|
|
|
|
|
|
options: {
|
|
|
|
|
|
|
|
|
|
|
|
position: 'topright'
|
|
|
},
|
|
|
|
|
|
initialize: function (options) {
|
|
|
Util.setOptions(this, options);
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getPosition: function () {
|
|
|
return this.options.position;
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
setPosition: function (position) {
|
|
|
var map = this._map;
|
|
|
|
|
|
if (map) {
|
|
|
map.removeControl(this);
|
|
|
}
|
|
|
|
|
|
this.options.position = position;
|
|
|
|
|
|
if (map) {
|
|
|
map.addControl(this);
|
|
|
}
|
|
|
|
|
|
return this;
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
getContainer: function () {
|
|
|
return this._container;
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
addTo: function (map) {
|
|
|
this.remove();
|
|
|
this._map = map;
|
|
|
|
|
|
var container = this._container = this.onAdd(map),
|
|
|
pos = this.getPosition(),
|
|
|
corner = map._controlCorners[pos];
|
|
|
|
|
|
DomUtil.addClass(container, 'leaflet-control');
|
|
|
|
|
|
if (pos.indexOf('bottom') !== -1) {
|
|
|
corner.insertBefore(container, corner.firstChild);
|
|
|
} else {
|
|
|
corner.appendChild(container);
|
|
|
}
|
|
|
|
|
|
this._map.on('unload', this.remove, this);
|
|
|
|
|
|
return this;
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
remove: function () {
|
|
|
if (!this._map) {
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
DomUtil.remove(this._container);
|
|
|
|
|
|
if (this.onRemove) {
|
|
|
this.onRemove(this._map);
|
|
|
}
|
|
|
|
|
|
this._map.off('unload', this.remove, this);
|
|
|
this._map = null;
|
|
|
|
|
|
return this;
|
|
|
},
|
|
|
|
|
|
_refocusOnMap: function (e) {
|
|
|
|
|
|
if (this._map && e && e.screenX > 0 && e.screenY > 0) {
|
|
|
this._map.getContainer().focus();
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
|
|
|
export var control = function (options) {
|
|
|
return new Control(options);
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Map.include({
|
|
|
|
|
|
|
|
|
addControl: function (control) {
|
|
|
control.addTo(this);
|
|
|
return this;
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
removeControl: function (control) {
|
|
|
control.remove();
|
|
|
return this;
|
|
|
},
|
|
|
|
|
|
_initControlPos: function () {
|
|
|
var corners = this._controlCorners = {},
|
|
|
l = 'leaflet-',
|
|
|
container = this._controlContainer =
|
|
|
DomUtil.create('div', l + 'control-container', this._container);
|
|
|
|
|
|
function createCorner(vSide, hSide) {
|
|
|
var className = l + vSide + ' ' + l + hSide;
|
|
|
|
|
|
corners[vSide + hSide] = DomUtil.create('div', className, container);
|
|
|
}
|
|
|
|
|
|
createCorner('top', 'left');
|
|
|
createCorner('top', 'right');
|
|
|
createCorner('bottom', 'left');
|
|
|
createCorner('bottom', 'right');
|
|
|
},
|
|
|
|
|
|
_clearControlPos: function () {
|
|
|
for (var i in this._controlCorners) {
|
|
|
DomUtil.remove(this._controlCorners[i]);
|
|
|
}
|
|
|
DomUtil.remove(this._controlContainer);
|
|
|
delete this._controlCorners;
|
|
|
delete this._controlContainer;
|
|
|
}
|
|
|
});
|
|
|
|