UNPKG

maplair

Version:

JavaScript library for mobile-friendly interactive maps

207 lines (176 loc) • 5.81 kB
import {Class} from '../core/Class.js'; import * as Util from '../core/Util.js'; import * as DomUtil from '../dom/DomUtil.js'; /** @import {MaplairMap} from '../map/Map.js' */ /** * Options of `Control`, inherited by all controls. * @typedef {object} ControlOptions * @property {string} [position='topright'] The position of the control (one of the map * corners or the middle of the top or bottom edge). Possible values are `'topleft'`, * `'topcenter'`, `'topright'`, `'bottomleft'`, `'bottomcenter'` or `'bottomright'`. * @property {number} [order=0] The position of the control within its corner: controls * with a lower order are closer to the corner of the map, those with the same order in * the order of adding. */ /** * Methods of `MaplairMap` for controls. * @lends MaplairMap.prototype */ const mapControlMethods = { /** * Adds the given control to the map. * @group Methods for layers and controls * @param {Control} control * @returns {this} */ addControl(control) { control.addTo(this); return this; }, /** * Removes the given control from the map. * @group Methods for layers and controls * @param {Control} control * @returns {this} */ removeControl(control) { control.remove(); return this; }, _initControlPos() { const corners = this._controlCorners = {}, l = 'maplair-', container = this._controlContainer = DomUtil.create('div', `${l}control-container`, this._container); for (const v of ['top', 'bottom']) { for (const h of ['left', 'center', 'right']) { corners[v + h] = DomUtil.create('div', `${l + v} ${l}${h}`, container); } } }, _clearControlPos() { this._controlContainer.remove(); delete this._controlCorners; delete this._controlContainer; } }; /** @typedef {typeof mapControlMethods} MapControlMethods */ /** * The methods which controls implement, see `Control`. * @typedef {object} ControlExtensionMethods * @property {(map: MaplairMap) => HTMLElement} [onAdd] Should return the container DOM * element for the control and add listeners on relevant map events. Called on * [`control.addTo(map)`](#control-addto). * @property {(map: MaplairMap) => void} [onRemove] Should contain all clean up code that * removes the listeners previously added in `onAdd`. Called on * [`control.remove()`](#control-remove). * @property {() => Record<string, Function>} [getEvents] Should return an object like * `{zoomend: this._update}` for [`on`](#evented-on). The event handlers in this object * will be automatically added and removed from the map with your control. * @lends Control */ // The `order` option of the containers of added controls const controlOrders = new WeakMap(); /** * Base class for map controls such as `ZoomControl`, `AttributionControl`, * `LayersControl` and `ScaleControl`, which handles positioning them. Extend it to * create your own controls. * * Every control should extend from `Control` and (re-)implement the following methods: * * * `onAdd(map: MaplairMap): HTMLElement` should return the container DOM element for * the control and add listeners on relevant map events. Called on * [`control.addTo(map)`](#control-addTo). * * `onRemove(map: MaplairMap)` (optional) should contain all clean up code that removes * the listeners previously added in [`onAdd`](#control-onadd). Called on * [`control.remove()`](#control-remove). * * `getEvents(): Object` (optional) should return an object like * `{zoomend: this._update}` for [`on`](#evented-on). The event handlers in this * object will be automatically added and removed from the map with your control. */ export class Control extends Class { static defaultOptions = { position: 'topright', order: 0 }; /** * @param {ControlOptions} [options] */ constructor(options) { super(); Util.setOptions(this, options); this.callInitHooks(Control); } /** * Returns the position of the control. * @returns {string} */ getPosition() { return this.options.position; } /** * Sets the position of the control. * @param {string} position * @returns {this} */ setPosition(position) { const map = this._map; map?.removeControl(this); this.options.position = position; map?.addControl(this); return this; } /** * Returns the HTMLElement that contains the control. * @returns {HTMLElement} */ getContainer() { return this._container; } /** * Adds the control to the given map. * @param {MaplairMap} map * @returns {this} */ 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'); // sorted by order from the corner of the map (bottom corners are filled upwards), // farther than the controls with the same order const order = this.options.order ?? 0, bottom = pos.includes('bottom'), children = [...corner.children]; controlOrders.set(container, order); corner.insertBefore(container, children.find(el => bottom ? (controlOrders.get(el) ?? 0) <= order : (controlOrders.get(el) ?? 0) > order) ?? null); this._mapEvents = {...this.getEvents?.(), unload: this.remove}; map.on(this._mapEvents, this); return this; } /** * Removes the control from the map it is currently active on. * @returns {this} */ remove() { if (!this._map) { return this; } this._container.remove(); this._map.off(this._mapEvents, this); this.onRemove?.(this._map); this._map = null; return this; } /** * Registers this default feature on the map class, called by `MaplairMap` itself. * @internal * @param {typeof MaplairMap} MaplairMap */ static register(MaplairMap) { MaplairMap.include(mapControlMethods); } }