Every component is now two modules sharing a basename: - src/elements/leaflet-foo.ts -- `export default class LeafletFooElement extends WithProps(...)`, the class only, no customElements.define, no side effects. src/elements/index.ts is an order-free barrel. - src/components/leaflet-foo.ts -- three lines: import the class, define the tag, re-export. Importing this (or src/index.ts) registers the tag. Plugin authors can now import a class without triggering the built-in define, to subclass it or register it under a different tag name. Package subpath exports `leaflet-components/elements`, `leaflet-components/elements/leaflet-foo.js`, and `leaflet-components/components/leaflet-foo.js` map onto dist/; jsr.json gains an `./elements` entry. The load-bearing ordering moves from the export statements in src/index.ts to its `./components/*` side-effect imports (the elements barrel carries no define, so its order is free). Classes are renamed LeafletFoo -> LeafletFooElement, including in the HTMLElementTagNameMap augmentation. Docs (CLAUDE.md, docs/, README.md) updated for the new layout.
52 lines
1.7 KiB
TypeScript
52 lines
1.7 KiB
TypeScript
import { Control, type ControlPosition } from 'leaflet';
|
|
import { bool, choice } from '../core/props.ts';
|
|
import { WithProps } from '../core/with-props.ts';
|
|
import type { LeafletRegisterEvent } from '../core/register.ts';
|
|
|
|
export default class LeafletControlLayersElement extends WithProps(
|
|
{
|
|
position: choice<ControlPosition>('topright'),
|
|
collapsed: bool(true),
|
|
autoZIndex: bool(true),
|
|
hideSingleBase: bool(),
|
|
sortLayers: bool(),
|
|
},
|
|
{ attach: 'self' },
|
|
) {
|
|
declare readonly leafletObject?: Control.Layers;
|
|
|
|
createLeafletObject(options: Control.LayersOptions): Control.Layers {
|
|
return new Control.Layers({}, {}, options);
|
|
}
|
|
|
|
connectedCallback(): void {
|
|
super.connectedCallback();
|
|
this.addEventListener('leaflet-register', this.#onChildRegister);
|
|
}
|
|
|
|
disconnectedCallback(): void {
|
|
this.removeEventListener('leaflet-register', this.#onChildRegister);
|
|
super.disconnectedCallback();
|
|
}
|
|
|
|
// Every child -- present at construction or added later -- announces
|
|
// itself this way: a parent's connectedCallback always finishes before a
|
|
// child's does (even for an already-built subtree attached in one go), so
|
|
// there is no "read the children that are already here" case to handle
|
|
// separately.
|
|
#onChildRegister = (e: LeafletRegisterEvent) => {
|
|
e.stopPropagation();
|
|
const el = e.detail.element;
|
|
const layer = e.detail.leafletObject;
|
|
const name = el.getAttribute('name');
|
|
if (!name) return;
|
|
if (el.getAttribute('type') === 'base') this.leafletObject?.addBaseLayer(layer, name);
|
|
else this.leafletObject?.addOverlay(layer, name);
|
|
if (el.hasAttribute('active')) {
|
|
this.dispatchEvent(
|
|
new CustomEvent('leaflet-add-layer', { bubbles: true, detail: { layer } }),
|
|
);
|
|
}
|
|
};
|
|
}
|