UNPKG

maplair

Version:

JavaScript library for mobile-friendly interactive maps

79 lines (78 loc) • 2.61 kB
import { disableClickPropagation } from "../dom/DomEvent.js"; import { create } from "../dom/DomUtil.js"; import { Control } from "./Control.js"; import { I18n } from "../core/I18n.js"; //#region src/control/AttributionControl.js const mapleLeaf = "<span aria-hidden=\"true\" class=\"maplair-attribution-leaf\">&#x1F341;</span>"; var AttributionControl = class AttributionControl extends Control { static { this.setDefaultOptions({ position: "bottomright", prefix: null }); } constructor(options) { super(options); if (this.options.prefix === null) { const title = I18n.translate("A JavaScript library for interactive maps"); this.options.prefix = `<a target="_blank" href="https://maplair.org" title="${title}">${mapleLeaf}Maplair</a>`; } this._attributions = {}; this.callInitHooks(AttributionControl); } onAdd(map) { map.attributionControl = this; this._container = create("div", "maplair-control-attribution"); disableClickPropagation(this._container); for (const layer of Object.values(map._layers)) if (layer.getAttribution) this.addAttribution(layer.getAttribution()); this._update(); map.on("layeradd", this._addAttribution, this); return this._container; } onRemove(map) { map.off("layeradd", this._addAttribution, this); } _addAttribution(ev) { if (ev.layer.getAttribution) { this.addAttribution(ev.layer.getAttribution()); ev.layer.once("remove", () => this.removeAttribution(ev.layer.getAttribution())); } } setPrefix(prefix) { this.options.prefix = prefix; this._update(); return this; } addAttribution(text) { if (!text) return this; if (!this._attributions[text]) this._attributions[text] = 0; this._attributions[text]++; this._update(); return this; } removeAttribution(text) { if (!text) return this; if (this._attributions[text]) { this._attributions[text]--; this._update(); } return this; } _update() { if (!this._map) return; const attribs = Object.keys(this._attributions).filter((i) => this._attributions[i]); const prefixAndAttribs = []; if (this.options.prefix) prefixAndAttribs.push(this.options.prefix); if (attribs.length) prefixAndAttribs.push(attribs.join(", ")); this._container.innerHTML = prefixAndAttribs.join(" <span aria-hidden=\"true\">|</span> "); } static register(MaplairMap) { MaplairMap.mergeOptions({ attributionControl: true }); MaplairMap.addInitHook(function() { if (this.options.attributionControl) new AttributionControl().addTo(this); }); } }; //#endregion export { AttributionControl }; //# sourceMappingURL=AttributionControl.js.map