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.
37 lines
1.1 KiB
TypeScript
37 lines
1.1 KiB
TypeScript
import { LayerGroup } from 'leaflet';
|
|
import { buildOptions, registerWithParent } from '../core/utils.js';
|
|
import { createChildRegisterHandler, type ChildEntry } from '../core/register.js';
|
|
|
|
const PROPS = {} as const satisfies Record<string, never>;
|
|
|
|
export class LeafletLayerGroup extends HTMLElement {
|
|
#obj?: LayerGroup;
|
|
#children = new Map<HTMLElement, ChildEntry>();
|
|
#childHandler?: (e: Event) => void;
|
|
|
|
static get observedAttributes() {
|
|
return [];
|
|
}
|
|
|
|
connectedCallback() {
|
|
this.#obj = new LayerGroup([], buildOptions(this, PROPS));
|
|
this.#childHandler = createChildRegisterHandler(this.#obj, this.#children) as EventListener;
|
|
this.addEventListener('leaflet-register', this.#childHandler);
|
|
registerWithParent(this, this.#obj);
|
|
}
|
|
|
|
disconnectedCallback() {
|
|
if (this.#childHandler) {
|
|
this.removeEventListener('leaflet-register', this.#childHandler);
|
|
}
|
|
for (const [el] of this.#children) {
|
|
this.#obj?.removeLayer(el as unknown as LayerGroup);
|
|
}
|
|
this.#children.clear();
|
|
this.#obj?.remove();
|
|
this.#obj = undefined;
|
|
}
|
|
}
|
|
|
|
customElements.define('leaflet-layer-group', LeafletLayerGroup);
|