import { TileLayer } from 'leaflet'; import { buildOptions, buildAttrMap, setLayerAttr } from '../core/utils.ts'; import { withProps } from '../core/with-props.ts'; import { registerChildren, unregisterChildren } from '../core/register.ts'; import { defineProps, num, str } from '../core/props.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 default 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; } 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);