UNPKG

mobility-toolbox-js

Version:

Toolbox for JavaScript applications in the domains of mobility and logistics.

74 lines (73 loc) 2.36 kB
import { getMapGlCopyrights } from '../../common/utils'; /** * @private */ const DEFAULT_SEPARATOR = ' | '; /** * Display layer's attributions trying to remove duplicated ones. * * @example * import { Map } from 'maplibre-gl'; * import { CopyrightControl } from 'mobility-toolbox-js/maplibre'; * * const map = new Map({ * container: 'map', * style: `https://maps.geops.io/styles/travic_v2/style.json?key=${window.apiKey}`, * }); * * const control = new CopyrightControl(); * map.addControl(control); * * * @see <a href="/example/mb-realtime>MapLibre Realtime layer example</a> * * @implements {maplibregl.IControl} * * @public */ class CopyrightControl { constructor(options = {}) { this.options = options; } getDefaultPosition() { return 'bottom-right'; } onAdd(map) { this.map = map; if (!this.container) { this.container = document.createElement('div'); } this.render = this.render.bind(this); this.map.on('idle', this.render); this.map.on('sourcedata', this.render); this.map.on('styledata', this.render); this.render(); return this.container; } onRemove() { var _a, _b; if ((_a = this.container) === null || _a === void 0 ? void 0 : _a.parentElement) { (_b = this.container.parentElement) === null || _b === void 0 ? void 0 : _b.removeChild(this.container); } if (this.map) { this.map.off('sourcedata', this.render); this.map.off('styledata', this.render); this.map.off('idle', this.render); } this.map = undefined; return this.container; } render() { var _a, _b; if (this.map && this.container) { const separator = ((_a = this.options) === null || _a === void 0 ? void 0 : _a.separator) || DEFAULT_SEPARATOR; const attribs = ((_b = this.options) === null || _b === void 0 ? void 0 : _b.customAttribution) || getMapGlCopyrights(this.map); const content = (Array.isArray(attribs) ? attribs : [attribs]).join(separator); if (this.container.innerHTML !== content) { this.content = content; this.container.innerHTML = this.content; } } } } export default CopyrightControl;