UNPKG

maplair

Version:

JavaScript library for mobile-friendly interactive maps

81 lines (80 loc) • 2.32 kB
import { setOptions } from "../core/Util.js"; import { Class } from "../core/Class.js"; import { create } from "../dom/DomUtil.js"; //#region src/control/Control.js var Control = class Control extends Class { static { this.setDefaultOptions({ position: "topright" }); } constructor(options) { super(); setOptions(this, options); this.callInitHooks(Control); } getPosition() { return this.options.position; } setPosition(position) { const map = this._map; map?.removeControl(this); this.options.position = position; map?.addControl(this); return this; } getContainer() { return this._container; } addTo(map) { this.remove(); this._map = map; const container = this._container = this.onAdd(map), pos = this.getPosition(), corner = map._controlCorners[pos]; container.classList.add("maplair-control"); if (pos.includes("bottom")) corner.insertBefore(container, corner.firstChild); else corner.appendChild(container); this._map.on("unload", this.remove, this); return this; } remove() { if (!this._map) return this; this._container.remove(); if (this.onRemove) this.onRemove(this._map); this._map.off("unload", this.remove, this); this._map = null; return this; } _refocusOnMap(e) { if (this._map && e && !(e.screenX === 0 && e.screenY === 0)) this._map.getContainer().focus(); } static register(MaplairMap) { MaplairMap.include({ addControl(control) { control.addTo(this); return this; }, removeControl(control) { control.remove(); return this; }, _initControlPos() { const corners = this._controlCorners = {}, l = "maplair-", container = this._controlContainer = create("div", `${l}control-container`, this._container); function createCorner(vSide, hSide) { const className = `${l + vSide} ${l}${hSide}`; corners[vSide + hSide] = create("div", className, container); } createCorner("top", "left"); createCorner("top", "right"); createCorner("bottom", "left"); createCorner("bottom", "right"); }, _clearControlPos() { for (const c of Object.values(this._controlCorners)) c.remove(); this._controlContainer.remove(); delete this._controlCorners; delete this._controlContainer; } }); } }; //#endregion export { Control }; //# sourceMappingURL=Control.js.map