maplair
Version:
JavaScript library for mobile-friendly interactive maps
119 lines (118 loc) • 3.55 kB
JavaScript
import { stamp } from "../core/Util.js";
import { Evented } from "../core/Events.js";
//#region src/layer/Layer.js
var Layer = class extends Evented {
static {
this.setDefaultOptions({
pane: "overlayPane",
attribution: null,
bubblingPointerEvents: true
});
}
addTo(map) {
map.addLayer(this);
return this;
}
remove() {
return this.removeFrom(this._map || this._mapToAdd);
}
removeFrom(obj) {
obj?.removeLayer(this);
return this;
}
getPane(name) {
return this._map.getPane(name ? this.options[name] || name : this.options.pane);
}
addInteractiveTarget(targetEl) {
this._map._targets[stamp(targetEl)] = this;
return this;
}
removeInteractiveTarget(targetEl) {
delete this._map._targets[stamp(targetEl)];
return this;
}
getAttribution() {
return this.options.attribution;
}
_layerAdd(e) {
const map = e.target;
if (!map.hasLayer(this)) return;
this._map = map;
this._zoomAnimated = map._zoomAnimated;
if (this.getEvents) {
const events = this.getEvents();
map.on(events, this);
this.once("remove", () => map.off(events, this));
}
this.onAdd(map);
this.fire("add");
map.fire("layeradd", { layer: this });
}
static register(MaplairMap) {
MaplairMap.include({
addLayer(layer) {
if (!layer._layerAdd) throw new Error("The provided object is not a Layer.");
const id = stamp(layer);
if (this._layers[id]) return this;
this._layers[id] = layer;
layer._mapToAdd = this;
if (layer.beforeAdd) layer.beforeAdd(this);
this.whenReady(layer._layerAdd, layer);
return this;
},
removeLayer(layer) {
const id = stamp(layer);
if (!this._layers[id]) return this;
if (this._loaded) layer.onRemove(this);
delete this._layers[id];
if (this._loaded) {
this.fire("layerremove", { layer });
layer.fire("remove");
}
layer._map = layer._mapToAdd = null;
return this;
},
hasLayer(layer) {
return stamp(layer) in this._layers;
},
eachLayer(method, context) {
for (const layer of Object.values(this._layers)) method.call(context, layer);
return this;
},
_addLayers(layers) {
layers = layers ? Array.isArray(layers) ? layers : [layers] : [];
for (const layer of layers) this.addLayer(layer);
},
_addZoomLimit(layer) {
if (!isNaN(layer.options.maxZoom) || !isNaN(layer.options.minZoom)) {
this._zoomBoundLayers[stamp(layer)] = layer;
this._updateZoomLevels();
}
},
_removeZoomLimit(layer) {
const id = stamp(layer);
if (this._zoomBoundLayers[id]) {
delete this._zoomBoundLayers[id];
this._updateZoomLevels();
}
},
_updateZoomLevels() {
let minZoom = Infinity, maxZoom = -Infinity;
const oldZoomSpan = this._getZoomSpan();
for (const l of Object.values(this._zoomBoundLayers)) {
const options = l.options;
minZoom = Math.min(minZoom, options.minZoom ?? Infinity);
maxZoom = Math.max(maxZoom, options.maxZoom ?? -Infinity);
}
this._layersMaxZoom = maxZoom === -Infinity ? void 0 : maxZoom;
this._layersMinZoom = minZoom === Infinity ? void 0 : minZoom;
if (oldZoomSpan !== this._getZoomSpan()) this.fire("zoomlevelschange");
if (this.options.maxZoom === void 0 && this._layersMaxZoom && this.getZoom() > this._layersMaxZoom) this.setZoom(this._layersMaxZoom);
if (this.options.minZoom === void 0 && this._layersMinZoom && this.getZoom() < this._layersMinZoom) this.setZoom(this._layersMinZoom);
}
});
}
};
//#endregion
export { Layer };
//# sourceMappingURL=Layer.js.map