2023-04-12 07:26:45 +00:00
|
|
|
// import * as util from '../core/utilities';
|
2024-03-14 19:25:27 +00:00
|
|
|
import { EventEmitter } from './EventEmitter.js';
|
2023-04-12 07:26:45 +00:00
|
|
|
import type { Map } from 'leaflet/src/map/index';
|
|
|
|
|
|
|
|
export abstract class Control<T> extends EventEmitter<T> {
|
|
|
|
// @section
|
|
|
|
// @aka Control options
|
2023-07-17 17:13:30 +00:00
|
|
|
public options = {
|
2023-04-12 07:26:45 +00:00
|
|
|
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);
|
|
|
|
// }
|
|
|
|
|
2023-07-17 17:13:30 +00:00
|
|
|
public getPosition() {
|
2023-04-12 07:26:45 +00:00
|
|
|
return this.options.position;
|
|
|
|
}
|
|
|
|
|
2023-07-17 17:13:30 +00:00
|
|
|
public getContainer() {
|
2023-04-12 07:26:45 +00:00
|
|
|
return this._container;
|
|
|
|
}
|
|
|
|
|
2023-07-17 17:13:30 +00:00
|
|
|
public abstract onRemove(map): void;
|
2023-06-27 16:23:18 +00:00
|
|
|
|
2023-07-17 17:13:30 +00:00
|
|
|
public abstract onAdd(map: any): HTMLElement;
|
2023-04-12 07:26:45 +00:00
|
|
|
|
2023-07-17 17:13:30 +00:00
|
|
|
public addTo(map: Map): Control<T> {
|
2023-04-12 07:26:45 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-07-17 17:13:30 +00:00
|
|
|
public removeFrom(map) {
|
|
|
|
let pos = this.getPosition();
|
|
|
|
let corner = map._controlCorners[pos];
|
2023-04-12 07:26:45 +00:00
|
|
|
|
|
|
|
corner.removeChild(this._container);
|
|
|
|
this._map = null;
|
|
|
|
|
|
|
|
if (this.onRemove) {
|
|
|
|
this.onRemove(map);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|