2023-04-12 07:26:45 +00:00
|
|
|
// import * as util from '../core/utilities';
|
|
|
|
import { EventEmitter } from './EventEmitter';
|
|
|
|
import type { Map } from 'leaflet/src/map/index';
|
|
|
|
|
|
|
|
export abstract class Control<T> extends EventEmitter<T> {
|
|
|
|
// @section
|
|
|
|
// @aka Control options
|
|
|
|
options = {
|
|
|
|
position: 'topright',
|
|
|
|
};
|
|
|
|
protected _map;
|
|
|
|
protected _container;
|
|
|
|
|
|
|
|
// constructor(defaults?) {
|
|
|
|
// super();
|
|
|
|
// if (!(this instanceof Control)) {
|
|
|
|
// throw new TypeError("Control constructor cannot be called as a function.");
|
2023-06-27 16:23:18 +00:00
|
|
|
// }
|
|
|
|
// // properties
|
2023-04-12 07:26:45 +00:00
|
|
|
// // util.setOptions(this, defaults);
|
|
|
|
// }
|
|
|
|
|
|
|
|
getPosition() {
|
|
|
|
return this.options.position;
|
|
|
|
}
|
|
|
|
|
|
|
|
getContainer() {
|
|
|
|
return this._container;
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract onRemove(map): void;
|
2023-06-27 16:23:18 +00:00
|
|
|
|
|
|
|
abstract onAdd(map: any): HTMLElement;
|
2023-04-12 07:26:45 +00:00
|
|
|
|
|
|
|
addTo(map: Map): Control<T> {
|
|
|
|
this._map = map;
|
|
|
|
|
2023-06-27 16:23:18 +00:00
|
|
|
let container = (this._container = this.onAdd(map));
|
|
|
|
let pos = this.getPosition(); //"topright"
|
2023-04-12 07:26:45 +00:00
|
|
|
let corner = map.controlCorners[pos];
|
|
|
|
if (container) {
|
|
|
|
// $(container).addClass('gba-control');
|
2023-06-27 16:23:18 +00:00
|
|
|
container.classList.add('gba-control');
|
2023-04-12 07:26:45 +00:00
|
|
|
|
|
|
|
if (pos.indexOf('bottom') !== -1) {
|
|
|
|
corner.insertBefore(container, corner.firstChild);
|
2023-06-27 16:23:18 +00:00
|
|
|
} else {
|
2023-04-12 07:26:45 +00:00
|
|
|
corner.appendChild(container);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
removeFrom(map) {
|
|
|
|
let pos = this.getPosition(),
|
|
|
|
corner = map._controlCorners[pos];
|
|
|
|
|
|
|
|
corner.removeChild(this._container);
|
|
|
|
this._map = null;
|
|
|
|
|
|
|
|
if (this.onRemove) {
|
|
|
|
this.onRemove(map);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|