You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
leaflet-components/src/components/leaflet-tile-layer.ts

47 lines
1.2 KiB
TypeScript

import { TileLayer } from 'leaflet';
import { registerChildren, unregisterChildren } from '../core/register.ts';
import { WithProps, defineProps, num, str } from '../core/props.ts';
import { buildAttrMap, buildOptions, setLayerAttr } from '../core/utils.ts';
const PROPS = defineProps({
url: str(),
attribution: str(),
minZoom: num(),
maxZoom: num(18),
opacity: num(1.0),
zIndex: num(),
});
const PROP_BY_ATTR = buildAttrMap(PROPS);
export class LeafletTileLayer extends WithProps(HTMLElement, PROPS) {
#obj?: TileLayer;
connectedCallback() {
const url = this.getAttribute('url') || '';
this.#obj = new TileLayer(url, buildOptions(this, PROPS, ['url']));
registerChildren(this, this.#obj);
}
disconnectedCallback() {
unregisterChildren(this);
this.#obj?.remove();
this.#obj = undefined;
}
get leafletObject() {
return this.#obj;
}
attributeChangedCallback(name: string, _old: string | null, val: string | null) {
if (!this.#obj) return;
if (name === 'url') {
if (val) this.#obj.setUrl(val);
} else {
setLayerAttr(this.#obj!, PROPS, PROP_BY_ATTR, name, val);
}
}
}
customElements.define('leaflet-tile-layer', LeafletTileLayer);