refactor: extract child-registration boilerplate into WeakMap helpers
Adds registerChildren / unregisterChildren / getChildren to register.ts using module-scoped WeakMaps, removing the duplicated #children / #childHandler fields + lifecycle wiring from 15 components. registerChildren(el, layer) creates the children Map + handler, stores both in WeakMaps, wires the event listener, and calls registerWithParent — all in one call. unregisterChildren(el) tears it down. getChildren(el) gives access to the map for components that need custom child iteration in disconnectedCallback (geojson, layer-group, feature-group). Net: -101 lines, 15 components lose 2-4 fields + 4-8 lifecycle lines each. Components with extra lifecycle work (marker -> dragend, polygon/polyline -> observer + syncCoords) keep their custom additions inline, just the register boilerplate is replaced.
This commit is contained in:
parent
c18829a0e3
commit
e2752aa7e0
@ -1,12 +1,8 @@
|
|||||||
import { CircleMarker } from 'leaflet';
|
import { CircleMarker } from 'leaflet';
|
||||||
import { registerWithParent, buildOptions, numAttr } from '../core/utils.js';
|
import { buildOptions, numAttr } from '../core/utils.js';
|
||||||
import { withProps } from '../core/with-props.js';
|
import { withProps } from '../core/with-props.js';
|
||||||
|
|
||||||
import {
|
import { registerChildren, unregisterChildren } from '../core/register.js';
|
||||||
createChildRegisterHandler,
|
|
||||||
type ChildEntry,
|
|
||||||
LeafletRegisterEvent,
|
|
||||||
} from '../core/register.js';
|
|
||||||
import { isPathStyleAttr, updatePathStyle } from '../core/path-style.js';
|
import { isPathStyleAttr, updatePathStyle } from '../core/path-style.js';
|
||||||
import type { PropDef } from '../types/props.js';
|
import type { PropDef } from '../types/props.js';
|
||||||
|
|
||||||
@ -24,22 +20,17 @@ const PROPS = {
|
|||||||
|
|
||||||
export class LeafletCircleMarker extends withProps(HTMLElement, PROPS) {
|
export class LeafletCircleMarker extends withProps(HTMLElement, PROPS) {
|
||||||
#obj?: CircleMarker;
|
#obj?: CircleMarker;
|
||||||
#children = new Map<HTMLElement, ChildEntry>();
|
|
||||||
#childHandler?: (e: LeafletRegisterEvent) => void;
|
|
||||||
|
|
||||||
connectedCallback() {
|
connectedCallback() {
|
||||||
this.#obj = new CircleMarker(
|
this.#obj = new CircleMarker(
|
||||||
[numAttr(this, PROPS, 'lat'), numAttr(this, PROPS, 'lng')],
|
[numAttr(this, PROPS, 'lat'), numAttr(this, PROPS, 'lng')],
|
||||||
buildOptions(this, PROPS, ['lat', 'lng']),
|
buildOptions(this, PROPS, ['lat', 'lng']),
|
||||||
);
|
);
|
||||||
this.#childHandler = createChildRegisterHandler(this.#obj, this.#children);
|
registerChildren(this, this.#obj);
|
||||||
this.addEventListener('leaflet-register', this.#childHandler);
|
|
||||||
registerWithParent(this, this.#obj);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
disconnectedCallback() {
|
disconnectedCallback() {
|
||||||
if (this.#childHandler) this.removeEventListener('leaflet-register', this.#childHandler);
|
unregisterChildren(this);
|
||||||
this.#children.clear();
|
|
||||||
this.#obj?.remove();
|
this.#obj?.remove();
|
||||||
this.#obj = undefined;
|
this.#obj = undefined;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,12 +1,8 @@
|
|||||||
import { Circle } from 'leaflet';
|
import { Circle } from 'leaflet';
|
||||||
import { registerWithParent, buildOptions, numAttr } from '../core/utils.js';
|
import { buildOptions, numAttr } from '../core/utils.js';
|
||||||
import { withProps } from '../core/with-props.js';
|
import { withProps } from '../core/with-props.js';
|
||||||
|
|
||||||
import {
|
import { registerChildren, unregisterChildren } from '../core/register.js';
|
||||||
createChildRegisterHandler,
|
|
||||||
type ChildEntry,
|
|
||||||
type LeafletRegisterEvent,
|
|
||||||
} from '../core/register.js';
|
|
||||||
import { isPathStyleAttr, updatePathStyle } from '../core/path-style.js';
|
import { isPathStyleAttr, updatePathStyle } from '../core/path-style.js';
|
||||||
import type { PropDef } from '../types/props.js';
|
import type { PropDef } from '../types/props.js';
|
||||||
|
|
||||||
@ -24,22 +20,17 @@ const PROPS = {
|
|||||||
|
|
||||||
export class LeafletCircle extends withProps(HTMLElement, PROPS) {
|
export class LeafletCircle extends withProps(HTMLElement, PROPS) {
|
||||||
#obj?: Circle;
|
#obj?: Circle;
|
||||||
#children = new Map<HTMLElement, ChildEntry>();
|
|
||||||
#childHandler?: (e: LeafletRegisterEvent) => void;
|
|
||||||
|
|
||||||
connectedCallback() {
|
connectedCallback() {
|
||||||
this.#obj = new Circle(
|
this.#obj = new Circle(
|
||||||
[numAttr(this, PROPS, 'lat'), numAttr(this, PROPS, 'lng')],
|
[numAttr(this, PROPS, 'lat'), numAttr(this, PROPS, 'lng')],
|
||||||
buildOptions(this, PROPS, ['lat', 'lng']),
|
buildOptions(this, PROPS, ['lat', 'lng']),
|
||||||
);
|
);
|
||||||
this.#childHandler = createChildRegisterHandler(this.#obj, this.#children);
|
registerChildren(this, this.#obj);
|
||||||
this.addEventListener('leaflet-register', this.#childHandler);
|
|
||||||
registerWithParent(this, this.#obj);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
disconnectedCallback() {
|
disconnectedCallback() {
|
||||||
if (this.#childHandler) this.removeEventListener('leaflet-register', this.#childHandler);
|
unregisterChildren(this);
|
||||||
this.#children.clear();
|
|
||||||
this.#obj?.remove();
|
this.#obj?.remove();
|
||||||
this.#obj = undefined;
|
this.#obj = undefined;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,15 +1,9 @@
|
|||||||
import { FeatureGroup, Layer } from 'leaflet';
|
import { FeatureGroup, Layer } from 'leaflet';
|
||||||
import { buildOptions, registerWithParent } from '../core/utils.js';
|
import { buildOptions } from '../core/utils.js';
|
||||||
import {
|
import { registerChildren, unregisterChildren, getChildren } from '../core/register.js';
|
||||||
createChildRegisterHandler,
|
|
||||||
type ChildEntry,
|
|
||||||
type LeafletRegisterEvent,
|
|
||||||
} from '../core/register.js';
|
|
||||||
|
|
||||||
export class LeafletFeatureGroup extends HTMLElement {
|
export class LeafletFeatureGroup extends HTMLElement {
|
||||||
#obj?: FeatureGroup;
|
#obj?: FeatureGroup;
|
||||||
#children = new Map<HTMLElement, ChildEntry>();
|
|
||||||
#childHandler?: (e: LeafletRegisterEvent) => void;
|
|
||||||
|
|
||||||
static get observedAttributes() {
|
static get observedAttributes() {
|
||||||
return [];
|
return [];
|
||||||
@ -17,19 +11,16 @@ export class LeafletFeatureGroup extends HTMLElement {
|
|||||||
|
|
||||||
connectedCallback() {
|
connectedCallback() {
|
||||||
this.#obj = new FeatureGroup([], buildOptions(this, {}));
|
this.#obj = new FeatureGroup([], buildOptions(this, {}));
|
||||||
this.#childHandler = createChildRegisterHandler(this.#obj, this.#children);
|
registerChildren(this, this.#obj);
|
||||||
this.addEventListener('leaflet-register', this.#childHandler);
|
|
||||||
registerWithParent(this, this.#obj);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
disconnectedCallback() {
|
disconnectedCallback() {
|
||||||
if (this.#childHandler) this.removeEventListener('leaflet-register', this.#childHandler);
|
for (const [el, entry] of getChildren(this) ?? []) {
|
||||||
for (const [el, entry] of this.#children) {
|
|
||||||
if (entry.type === 'popup') this.#obj?.unbindPopup();
|
if (entry.type === 'popup') this.#obj?.unbindPopup();
|
||||||
else if (entry.type === 'tooltip') this.#obj?.unbindTooltip();
|
else if (entry.type === 'tooltip') this.#obj?.unbindTooltip();
|
||||||
else if (entry.type === 'layer') this.#obj?.removeLayer(el as unknown as Layer);
|
else if (entry.type === 'layer') this.#obj?.removeLayer(el as unknown as Layer);
|
||||||
}
|
}
|
||||||
this.#children.clear();
|
unregisterChildren(this);
|
||||||
this.#obj?.remove();
|
this.#obj?.remove();
|
||||||
this.#obj = undefined;
|
this.#obj = undefined;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,12 +1,8 @@
|
|||||||
import { GeoJSON, PathOptions, Layer } from 'leaflet';
|
import { GeoJSON, PathOptions, Layer } from 'leaflet';
|
||||||
import { buildOptions, registerWithParent } from '../core/utils.js';
|
import { buildOptions } from '../core/utils.js';
|
||||||
import { withProps } from '../core/with-props.js';
|
import { withProps } from '../core/with-props.js';
|
||||||
|
|
||||||
import {
|
import { registerChildren, unregisterChildren, getChildren } from '../core/register.js';
|
||||||
createChildRegisterHandler,
|
|
||||||
type ChildEntry,
|
|
||||||
LeafletRegisterEvent,
|
|
||||||
} from '../core/register.js';
|
|
||||||
import type { PropDef } from '../types/props.js';
|
import type { PropDef } from '../types/props.js';
|
||||||
|
|
||||||
const PROPS = {
|
const PROPS = {
|
||||||
@ -31,8 +27,6 @@ const PROP_BY_ATTR = new Map<string, string>(
|
|||||||
|
|
||||||
export class LeafletGeoJSON extends withProps(HTMLElement, PROPS) {
|
export class LeafletGeoJSON extends withProps(HTMLElement, PROPS) {
|
||||||
#obj?: GeoJSON;
|
#obj?: GeoJSON;
|
||||||
#children = new Map<HTMLElement, ChildEntry>();
|
|
||||||
#childHandler?: (e: LeafletRegisterEvent) => void;
|
|
||||||
|
|
||||||
connectedCallback() {
|
connectedCallback() {
|
||||||
const raw = this.getAttribute('data');
|
const raw = this.getAttribute('data');
|
||||||
@ -41,19 +35,16 @@ export class LeafletGeoJSON extends withProps(HTMLElement, PROPS) {
|
|||||||
Object.entries(buildOptions(this, PROPS, ['data'])).filter(([, v]) => v !== ''),
|
Object.entries(buildOptions(this, PROPS, ['data'])).filter(([, v]) => v !== ''),
|
||||||
);
|
);
|
||||||
this.#obj = new GeoJSON(data, { style: styleOpts as PathOptions });
|
this.#obj = new GeoJSON(data, { style: styleOpts as PathOptions });
|
||||||
this.#childHandler = createChildRegisterHandler(this.#obj, this.#children);
|
registerChildren(this, this.#obj);
|
||||||
this.addEventListener('leaflet-register', this.#childHandler);
|
|
||||||
registerWithParent(this, this.#obj);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
disconnectedCallback() {
|
disconnectedCallback() {
|
||||||
if (this.#childHandler) this.removeEventListener('leaflet-register', this.#childHandler);
|
for (const [el, entry] of getChildren(this) ?? []) {
|
||||||
for (const [el, entry] of this.#children) {
|
|
||||||
if (entry.type === 'popup') this.#obj?.unbindPopup();
|
if (entry.type === 'popup') this.#obj?.unbindPopup();
|
||||||
else if (entry.type === 'tooltip') this.#obj?.unbindTooltip();
|
else if (entry.type === 'tooltip') this.#obj?.unbindTooltip();
|
||||||
else if (entry.type === 'layer') this.#obj?.removeLayer(el as unknown as Layer);
|
else if (entry.type === 'layer') this.#obj?.removeLayer(el as unknown as Layer);
|
||||||
}
|
}
|
||||||
this.#children.clear();
|
unregisterChildren(this);
|
||||||
this.#obj?.remove();
|
this.#obj?.remove();
|
||||||
this.#obj = undefined;
|
this.#obj = undefined;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,12 +1,8 @@
|
|||||||
import { ImageOverlay, LatLngBounds, LatLngBoundsExpression, LatLngExpression } from 'leaflet';
|
import { ImageOverlay, LatLngBounds, LatLngBoundsExpression, LatLngExpression } from 'leaflet';
|
||||||
import { registerWithParent, buildOptions, parseAttributeValue } from '../core/utils.js';
|
import { buildOptions, parseAttributeValue } from '../core/utils.js';
|
||||||
import { withProps } from '../core/with-props.js';
|
import { withProps } from '../core/with-props.js';
|
||||||
|
|
||||||
import {
|
import { registerChildren, unregisterChildren } from '../core/register.js';
|
||||||
createChildRegisterHandler,
|
|
||||||
type ChildEntry,
|
|
||||||
LeafletRegisterEvent,
|
|
||||||
} from '../core/register.js';
|
|
||||||
import type { PropDef } from '../types/props.js';
|
import type { PropDef } from '../types/props.js';
|
||||||
|
|
||||||
const PROPS = {
|
const PROPS = {
|
||||||
@ -27,8 +23,6 @@ const PROP_BY_ATTR = new Map<string, string>(
|
|||||||
|
|
||||||
export class LeafletImageOverlay extends withProps(HTMLElement, PROPS) {
|
export class LeafletImageOverlay extends withProps(HTMLElement, PROPS) {
|
||||||
#obj?: ImageOverlay;
|
#obj?: ImageOverlay;
|
||||||
#children = new Map<HTMLElement, ChildEntry>();
|
|
||||||
#childHandler?: (e: LeafletRegisterEvent) => void;
|
|
||||||
|
|
||||||
connectedCallback() {
|
connectedCallback() {
|
||||||
const url = this.getAttribute('url') || '';
|
const url = this.getAttribute('url') || '';
|
||||||
@ -37,14 +31,11 @@ export class LeafletImageOverlay extends withProps(HTMLElement, PROPS) {
|
|||||||
this.#parsedBounds(),
|
this.#parsedBounds(),
|
||||||
buildOptions(this, PROPS, ['url', 'bounds']),
|
buildOptions(this, PROPS, ['url', 'bounds']),
|
||||||
);
|
);
|
||||||
this.#childHandler = createChildRegisterHandler(this.#obj, this.#children);
|
registerChildren(this, this.#obj);
|
||||||
this.addEventListener('leaflet-register', this.#childHandler);
|
|
||||||
registerWithParent(this, this.#obj);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
disconnectedCallback() {
|
disconnectedCallback() {
|
||||||
if (this.#childHandler) this.removeEventListener('leaflet-register', this.#childHandler);
|
unregisterChildren(this);
|
||||||
this.#children.clear();
|
|
||||||
this.#obj?.remove();
|
this.#obj?.remove();
|
||||||
this.#obj = undefined;
|
this.#obj = undefined;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,15 +1,9 @@
|
|||||||
import { LayerGroup, Layer } from 'leaflet';
|
import { LayerGroup, Layer } from 'leaflet';
|
||||||
import { buildOptions, registerWithParent } from '../core/utils.js';
|
import { buildOptions } from '../core/utils.js';
|
||||||
import {
|
import { registerChildren, unregisterChildren, getChildren } from '../core/register.js';
|
||||||
createChildRegisterHandler,
|
|
||||||
type ChildEntry,
|
|
||||||
LeafletRegisterEvent,
|
|
||||||
} from '../core/register.js';
|
|
||||||
|
|
||||||
export class LeafletLayerGroup extends HTMLElement {
|
export class LeafletLayerGroup extends HTMLElement {
|
||||||
#obj?: LayerGroup;
|
#obj?: LayerGroup;
|
||||||
#children = new Map<HTMLElement, ChildEntry>();
|
|
||||||
#childHandler?: (e: LeafletRegisterEvent) => void;
|
|
||||||
|
|
||||||
static get observedAttributes() {
|
static get observedAttributes() {
|
||||||
return [];
|
return [];
|
||||||
@ -17,19 +11,16 @@ export class LeafletLayerGroup extends HTMLElement {
|
|||||||
|
|
||||||
connectedCallback() {
|
connectedCallback() {
|
||||||
this.#obj = new LayerGroup([], buildOptions(this, {}));
|
this.#obj = new LayerGroup([], buildOptions(this, {}));
|
||||||
this.#childHandler = createChildRegisterHandler(this.#obj, this.#children);
|
registerChildren(this, this.#obj);
|
||||||
this.addEventListener('leaflet-register', this.#childHandler);
|
|
||||||
registerWithParent(this, this.#obj);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
disconnectedCallback() {
|
disconnectedCallback() {
|
||||||
if (this.#childHandler) this.removeEventListener('leaflet-register', this.#childHandler);
|
for (const [el, entry] of getChildren(this) ?? []) {
|
||||||
for (const [el, entry] of this.#children) {
|
|
||||||
if (entry.type === 'popup') this.#obj?.unbindPopup();
|
if (entry.type === 'popup') this.#obj?.unbindPopup();
|
||||||
else if (entry.type === 'tooltip') this.#obj?.unbindTooltip();
|
else if (entry.type === 'tooltip') this.#obj?.unbindTooltip();
|
||||||
else if (entry.type === 'layer') this.#obj?.removeLayer(el as unknown as Layer);
|
else if (entry.type === 'layer') this.#obj?.removeLayer(el as unknown as Layer);
|
||||||
}
|
}
|
||||||
this.#children.clear();
|
unregisterChildren(this);
|
||||||
this.#obj?.remove();
|
this.#obj?.remove();
|
||||||
this.#obj = undefined;
|
this.#obj = undefined;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,12 +1,8 @@
|
|||||||
import { Marker } from 'leaflet';
|
import { Marker } from 'leaflet';
|
||||||
import { registerWithParent, buildOptions, numAttr, parseAttributeValue } from '../core/utils.js';
|
import { buildOptions, numAttr, parseAttributeValue } from '../core/utils.js';
|
||||||
import { withProps } from '../core/with-props.js';
|
import { withProps } from '../core/with-props.js';
|
||||||
|
|
||||||
import {
|
import { registerChildren, unregisterChildren } from '../core/register.js';
|
||||||
createChildRegisterHandler,
|
|
||||||
type ChildEntry,
|
|
||||||
LeafletRegisterEvent,
|
|
||||||
} from '../core/register.js';
|
|
||||||
import type { PropDef } from '../types/props.js';
|
import type { PropDef } from '../types/props.js';
|
||||||
|
|
||||||
const PROPS = {
|
const PROPS = {
|
||||||
@ -26,8 +22,6 @@ const PROP_BY_ATTR = new Map<string, string>(
|
|||||||
export class LeafletMarker extends withProps(HTMLElement, PROPS) {
|
export class LeafletMarker extends withProps(HTMLElement, PROPS) {
|
||||||
#obj?: Marker;
|
#obj?: Marker;
|
||||||
#syncing = false;
|
#syncing = false;
|
||||||
#children = new Map<HTMLElement, ChildEntry>();
|
|
||||||
#childHandler?: (e: LeafletRegisterEvent) => void;
|
|
||||||
|
|
||||||
connectedCallback() {
|
connectedCallback() {
|
||||||
this.#obj = new Marker(
|
this.#obj = new Marker(
|
||||||
@ -35,15 +29,12 @@ export class LeafletMarker extends withProps(HTMLElement, PROPS) {
|
|||||||
buildOptions(this, PROPS, ['lat', 'lng']),
|
buildOptions(this, PROPS, ['lat', 'lng']),
|
||||||
);
|
);
|
||||||
this.#obj.on('dragend', this.#onDragEnd);
|
this.#obj.on('dragend', this.#onDragEnd);
|
||||||
this.#childHandler = createChildRegisterHandler(this.#obj, this.#children);
|
registerChildren(this, this.#obj);
|
||||||
this.addEventListener('leaflet-register', this.#childHandler);
|
|
||||||
registerWithParent(this, this.#obj);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
disconnectedCallback() {
|
disconnectedCallback() {
|
||||||
if (this.#childHandler) this.removeEventListener('leaflet-register', this.#childHandler);
|
|
||||||
this.#obj?.off('dragend', this.#onDragEnd);
|
this.#obj?.off('dragend', this.#onDragEnd);
|
||||||
this.#children.clear();
|
unregisterChildren(this);
|
||||||
this.#obj?.remove();
|
this.#obj?.remove();
|
||||||
this.#obj = undefined;
|
this.#obj = undefined;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,12 +1,8 @@
|
|||||||
import { Polygon } from 'leaflet';
|
import { Polygon } from 'leaflet';
|
||||||
import { registerWithParent, buildOptions } from '../core/utils.js';
|
import { buildOptions } from '../core/utils.js';
|
||||||
import { withProps } from '../core/with-props.js';
|
import { withProps } from '../core/with-props.js';
|
||||||
|
|
||||||
import {
|
import { registerChildren, unregisterChildren } from '../core/register.js';
|
||||||
createChildRegisterHandler,
|
|
||||||
type ChildEntry,
|
|
||||||
LeafletRegisterEvent,
|
|
||||||
} from '../core/register.js';
|
|
||||||
import { isPathStyleAttr, updatePathStyle } from '../core/path-style.js';
|
import { isPathStyleAttr, updatePathStyle } from '../core/path-style.js';
|
||||||
import type { PropDef } from '../types/props.js';
|
import type { PropDef } from '../types/props.js';
|
||||||
import type { LeafletLine } from './leaflet-line.js';
|
import type { LeafletLine } from './leaflet-line.js';
|
||||||
@ -23,14 +19,10 @@ const PROPS = {
|
|||||||
export class LeafletPolygon extends withProps(HTMLElement, PROPS) {
|
export class LeafletPolygon extends withProps(HTMLElement, PROPS) {
|
||||||
#obj?: Polygon;
|
#obj?: Polygon;
|
||||||
#observer?: MutationObserver;
|
#observer?: MutationObserver;
|
||||||
#children = new Map<HTMLElement, ChildEntry>();
|
|
||||||
#childHandler?: (e: LeafletRegisterEvent) => void;
|
|
||||||
|
|
||||||
connectedCallback() {
|
connectedCallback() {
|
||||||
this.#obj = new Polygon(this.#getCoords(), buildOptions(this, PROPS));
|
this.#obj = new Polygon(this.#getCoords(), buildOptions(this, PROPS));
|
||||||
this.#childHandler = createChildRegisterHandler(this.#obj, this.#children);
|
registerChildren(this, this.#obj);
|
||||||
this.addEventListener('leaflet-register', this.#childHandler);
|
|
||||||
registerWithParent(this, this.#obj);
|
|
||||||
|
|
||||||
this.addEventListener('line-updated', this.#syncCoords);
|
this.addEventListener('line-updated', this.#syncCoords);
|
||||||
this.#observer = new MutationObserver(() => this.#syncCoords());
|
this.#observer = new MutationObserver(() => this.#syncCoords());
|
||||||
@ -41,8 +33,7 @@ export class LeafletPolygon extends withProps(HTMLElement, PROPS) {
|
|||||||
this.#observer?.disconnect();
|
this.#observer?.disconnect();
|
||||||
this.#observer = undefined;
|
this.#observer = undefined;
|
||||||
this.removeEventListener('line-updated', this.#syncCoords);
|
this.removeEventListener('line-updated', this.#syncCoords);
|
||||||
if (this.#childHandler) this.removeEventListener('leaflet-register', this.#childHandler);
|
unregisterChildren(this);
|
||||||
this.#children.clear();
|
|
||||||
this.#obj?.remove();
|
this.#obj?.remove();
|
||||||
this.#obj = undefined;
|
this.#obj = undefined;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,12 +1,8 @@
|
|||||||
import { Polyline } from 'leaflet';
|
import { Polyline } from 'leaflet';
|
||||||
import { registerWithParent, buildOptions } from '../core/utils.js';
|
import { buildOptions } from '../core/utils.js';
|
||||||
import { withProps } from '../core/with-props.js';
|
import { withProps } from '../core/with-props.js';
|
||||||
|
|
||||||
import {
|
import { registerChildren, unregisterChildren } from '../core/register.js';
|
||||||
createChildRegisterHandler,
|
|
||||||
type ChildEntry,
|
|
||||||
LeafletRegisterEvent,
|
|
||||||
} from '../core/register.js';
|
|
||||||
import { isPathStyleAttr, updatePathStyle } from '../core/path-style.js';
|
import { isPathStyleAttr, updatePathStyle } from '../core/path-style.js';
|
||||||
import type { PropDef } from '../types/props.js';
|
import type { PropDef } from '../types/props.js';
|
||||||
import type { LeafletLine } from './leaflet-line.js';
|
import type { LeafletLine } from './leaflet-line.js';
|
||||||
@ -23,14 +19,10 @@ const PROPS = {
|
|||||||
export class LeafletPolyline extends withProps(HTMLElement, PROPS) {
|
export class LeafletPolyline extends withProps(HTMLElement, PROPS) {
|
||||||
#obj?: Polyline;
|
#obj?: Polyline;
|
||||||
#observer?: MutationObserver;
|
#observer?: MutationObserver;
|
||||||
#children = new Map<HTMLElement, ChildEntry>();
|
|
||||||
#childHandler?: (e: LeafletRegisterEvent) => void;
|
|
||||||
|
|
||||||
connectedCallback() {
|
connectedCallback() {
|
||||||
this.#obj = new Polyline(this.#getCoords(), buildOptions(this, PROPS));
|
this.#obj = new Polyline(this.#getCoords(), buildOptions(this, PROPS));
|
||||||
this.#childHandler = createChildRegisterHandler(this.#obj, this.#children);
|
registerChildren(this, this.#obj);
|
||||||
this.addEventListener('leaflet-register', this.#childHandler);
|
|
||||||
registerWithParent(this, this.#obj);
|
|
||||||
|
|
||||||
this.addEventListener('line-updated', this.#syncCoords);
|
this.addEventListener('line-updated', this.#syncCoords);
|
||||||
this.#observer = new MutationObserver(() => this.#syncCoords());
|
this.#observer = new MutationObserver(() => this.#syncCoords());
|
||||||
@ -41,8 +33,7 @@ export class LeafletPolyline extends withProps(HTMLElement, PROPS) {
|
|||||||
this.#observer?.disconnect();
|
this.#observer?.disconnect();
|
||||||
this.#observer = undefined;
|
this.#observer = undefined;
|
||||||
this.removeEventListener('line-updated', this.#syncCoords);
|
this.removeEventListener('line-updated', this.#syncCoords);
|
||||||
if (this.#childHandler) this.removeEventListener('leaflet-register', this.#childHandler);
|
unregisterChildren(this);
|
||||||
this.#children.clear();
|
|
||||||
this.#obj?.remove();
|
this.#obj?.remove();
|
||||||
this.#obj = undefined;
|
this.#obj = undefined;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,12 +1,8 @@
|
|||||||
import { Rectangle, LatLngBoundsExpression } from 'leaflet';
|
import { Rectangle, LatLngBoundsExpression } from 'leaflet';
|
||||||
import { registerWithParent, buildOptions } from '../core/utils.js';
|
import { buildOptions } from '../core/utils.js';
|
||||||
import { withProps } from '../core/with-props.js';
|
import { withProps } from '../core/with-props.js';
|
||||||
|
|
||||||
import {
|
import { registerChildren, unregisterChildren } from '../core/register.js';
|
||||||
createChildRegisterHandler,
|
|
||||||
type ChildEntry,
|
|
||||||
LeafletRegisterEvent,
|
|
||||||
} from '../core/register.js';
|
|
||||||
import { isPathStyleAttr, updatePathStyle } from '../core/path-style.js';
|
import { isPathStyleAttr, updatePathStyle } from '../core/path-style.js';
|
||||||
import type { PropDef } from '../types/props.js';
|
import type { PropDef } from '../types/props.js';
|
||||||
|
|
||||||
@ -22,19 +18,14 @@ const PROPS = {
|
|||||||
|
|
||||||
export class LeafletRectangle extends withProps(HTMLElement, PROPS) {
|
export class LeafletRectangle extends withProps(HTMLElement, PROPS) {
|
||||||
#obj?: Rectangle;
|
#obj?: Rectangle;
|
||||||
#children = new Map<HTMLElement, ChildEntry>();
|
|
||||||
#childHandler?: (e: LeafletRegisterEvent) => void;
|
|
||||||
|
|
||||||
connectedCallback() {
|
connectedCallback() {
|
||||||
this.#obj = new Rectangle(this.#parsedBounds(), buildOptions(this, PROPS, ['bounds']));
|
this.#obj = new Rectangle(this.#parsedBounds(), buildOptions(this, PROPS, ['bounds']));
|
||||||
this.#childHandler = createChildRegisterHandler(this.#obj, this.#children);
|
registerChildren(this, this.#obj);
|
||||||
this.addEventListener('leaflet-register', this.#childHandler);
|
|
||||||
registerWithParent(this, this.#obj);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
disconnectedCallback() {
|
disconnectedCallback() {
|
||||||
if (this.#childHandler) this.removeEventListener('leaflet-register', this.#childHandler);
|
unregisterChildren(this);
|
||||||
this.#children.clear();
|
|
||||||
this.#obj?.remove();
|
this.#obj?.remove();
|
||||||
this.#obj = undefined;
|
this.#obj = undefined;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,12 +1,8 @@
|
|||||||
import { SVGOverlay, LatLngBounds, LatLngBoundsExpression, LatLngExpression } from 'leaflet';
|
import { SVGOverlay, LatLngBounds, LatLngBoundsExpression, LatLngExpression } from 'leaflet';
|
||||||
import { registerWithParent, buildOptions, parseAttributeValue } from '../core/utils.js';
|
import { buildOptions, parseAttributeValue } from '../core/utils.js';
|
||||||
import { withProps } from '../core/with-props.js';
|
import { withProps } from '../core/with-props.js';
|
||||||
|
|
||||||
import {
|
import { registerChildren, unregisterChildren } from '../core/register.js';
|
||||||
createChildRegisterHandler,
|
|
||||||
type ChildEntry,
|
|
||||||
LeafletRegisterEvent,
|
|
||||||
} from '../core/register.js';
|
|
||||||
import type { PropDef } from '../types/props.js';
|
import type { PropDef } from '../types/props.js';
|
||||||
|
|
||||||
const PROPS = {
|
const PROPS = {
|
||||||
@ -24,8 +20,6 @@ const PROP_BY_ATTR = new Map<string, string>(
|
|||||||
|
|
||||||
export class LeafletSVGOverlay extends withProps(HTMLElement, PROPS) {
|
export class LeafletSVGOverlay extends withProps(HTMLElement, PROPS) {
|
||||||
#obj?: SVGOverlay;
|
#obj?: SVGOverlay;
|
||||||
#children = new Map<HTMLElement, ChildEntry>();
|
|
||||||
#childHandler?: (e: LeafletRegisterEvent) => void;
|
|
||||||
|
|
||||||
connectedCallback() {
|
connectedCallback() {
|
||||||
const svg = this.querySelector('svg');
|
const svg = this.querySelector('svg');
|
||||||
@ -35,14 +29,11 @@ export class LeafletSVGOverlay extends withProps(HTMLElement, PROPS) {
|
|||||||
this.#parsedBounds(),
|
this.#parsedBounds(),
|
||||||
buildOptions(this, PROPS, ['bounds']),
|
buildOptions(this, PROPS, ['bounds']),
|
||||||
);
|
);
|
||||||
this.#childHandler = createChildRegisterHandler(this.#obj, this.#children);
|
registerChildren(this, this.#obj);
|
||||||
this.addEventListener('leaflet-register', this.#childHandler);
|
|
||||||
registerWithParent(this, this.#obj);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
disconnectedCallback() {
|
disconnectedCallback() {
|
||||||
if (this.#childHandler) this.removeEventListener('leaflet-register', this.#childHandler);
|
unregisterChildren(this);
|
||||||
this.#children.clear();
|
|
||||||
this.#obj?.remove();
|
this.#obj?.remove();
|
||||||
this.#obj = undefined;
|
this.#obj = undefined;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,12 +1,8 @@
|
|||||||
import { TileLayer } from 'leaflet';
|
import { TileLayer } from 'leaflet';
|
||||||
import { registerWithParent, buildOptions, parseAttributeValue } from '../core/utils.js';
|
import { buildOptions, parseAttributeValue } from '../core/utils.js';
|
||||||
import { withProps } from '../core/with-props.js';
|
import { withProps } from '../core/with-props.js';
|
||||||
|
|
||||||
import {
|
import { registerChildren, unregisterChildren } from '../core/register.js';
|
||||||
createChildRegisterHandler,
|
|
||||||
type ChildEntry,
|
|
||||||
LeafletRegisterEvent,
|
|
||||||
} from '../core/register.js';
|
|
||||||
import type { PropDef } from '../types/props.js';
|
import type { PropDef } from '../types/props.js';
|
||||||
|
|
||||||
const PROPS = {
|
const PROPS = {
|
||||||
@ -25,8 +21,6 @@ const PROP_BY_ATTR = new Map<string, string>(
|
|||||||
|
|
||||||
export class LeafletTileLayerWMS extends withProps(HTMLElement, PROPS) {
|
export class LeafletTileLayerWMS extends withProps(HTMLElement, PROPS) {
|
||||||
#obj?: TileLayer.WMS;
|
#obj?: TileLayer.WMS;
|
||||||
#children = new Map<HTMLElement, ChildEntry>();
|
|
||||||
#childHandler?: (e: LeafletRegisterEvent) => void;
|
|
||||||
|
|
||||||
connectedCallback() {
|
connectedCallback() {
|
||||||
const url = this.getAttribute('url') || '';
|
const url = this.getAttribute('url') || '';
|
||||||
@ -34,14 +28,11 @@ export class LeafletTileLayerWMS extends withProps(HTMLElement, PROPS) {
|
|||||||
url,
|
url,
|
||||||
buildOptions(this, PROPS, ['url']) as Record<string, string>,
|
buildOptions(this, PROPS, ['url']) as Record<string, string>,
|
||||||
);
|
);
|
||||||
this.#childHandler = createChildRegisterHandler(this.#obj, this.#children);
|
registerChildren(this, this.#obj);
|
||||||
this.addEventListener('leaflet-register', this.#childHandler);
|
|
||||||
registerWithParent(this, this.#obj);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
disconnectedCallback() {
|
disconnectedCallback() {
|
||||||
if (this.#childHandler) this.removeEventListener('leaflet-register', this.#childHandler);
|
unregisterChildren(this);
|
||||||
this.#children.clear();
|
|
||||||
this.#obj?.remove();
|
this.#obj?.remove();
|
||||||
this.#obj = undefined;
|
this.#obj = undefined;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,12 +1,8 @@
|
|||||||
import { TileLayer } from 'leaflet';
|
import { TileLayer } from 'leaflet';
|
||||||
import { registerWithParent, buildOptions, parseAttributeValue } from '../core/utils.js';
|
import { buildOptions, parseAttributeValue } from '../core/utils.js';
|
||||||
import { withProps } from '../core/with-props.js';
|
import { withProps } from '../core/with-props.js';
|
||||||
|
|
||||||
import {
|
import { registerChildren, unregisterChildren } from '../core/register.js';
|
||||||
createChildRegisterHandler,
|
|
||||||
type ChildEntry,
|
|
||||||
LeafletRegisterEvent,
|
|
||||||
} from '../core/register.js';
|
|
||||||
import type { PropDef } from '../types/props.js';
|
import type { PropDef } from '../types/props.js';
|
||||||
|
|
||||||
const PROPS = {
|
const PROPS = {
|
||||||
@ -24,20 +20,15 @@ const PROP_BY_ATTR = new Map<string, string>(
|
|||||||
|
|
||||||
export class LeafletTileLayer extends withProps(HTMLElement, PROPS) {
|
export class LeafletTileLayer extends withProps(HTMLElement, PROPS) {
|
||||||
#obj?: TileLayer;
|
#obj?: TileLayer;
|
||||||
#children = new Map<HTMLElement, ChildEntry>();
|
|
||||||
#childHandler?: (e: LeafletRegisterEvent) => void;
|
|
||||||
|
|
||||||
connectedCallback() {
|
connectedCallback() {
|
||||||
const url = this.getAttribute('url') || '';
|
const url = this.getAttribute('url') || '';
|
||||||
this.#obj = new TileLayer(url, buildOptions(this, PROPS, ['url']));
|
this.#obj = new TileLayer(url, buildOptions(this, PROPS, ['url']));
|
||||||
this.#childHandler = createChildRegisterHandler(this.#obj, this.#children);
|
registerChildren(this, this.#obj);
|
||||||
this.addEventListener('leaflet-register', this.#childHandler);
|
|
||||||
registerWithParent(this, this.#obj);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
disconnectedCallback() {
|
disconnectedCallback() {
|
||||||
if (this.#childHandler) this.removeEventListener('leaflet-register', this.#childHandler);
|
unregisterChildren(this);
|
||||||
this.#children.clear();
|
|
||||||
this.#obj?.remove();
|
this.#obj?.remove();
|
||||||
this.#obj = undefined;
|
this.#obj = undefined;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,12 +1,8 @@
|
|||||||
import { VideoOverlay, LatLngBounds, LatLngBoundsExpression, LatLngExpression } from 'leaflet';
|
import { VideoOverlay, LatLngBounds, LatLngBoundsExpression, LatLngExpression } from 'leaflet';
|
||||||
import { registerWithParent, buildOptions, parseAttributeValue } from '../core/utils.js';
|
import { buildOptions, parseAttributeValue } from '../core/utils.js';
|
||||||
import { withProps } from '../core/with-props.js';
|
import { withProps } from '../core/with-props.js';
|
||||||
|
|
||||||
import {
|
import { registerChildren, unregisterChildren } from '../core/register.js';
|
||||||
createChildRegisterHandler,
|
|
||||||
type ChildEntry,
|
|
||||||
LeafletRegisterEvent,
|
|
||||||
} from '../core/register.js';
|
|
||||||
import type { PropDef } from '../types/props.js';
|
import type { PropDef } from '../types/props.js';
|
||||||
|
|
||||||
const PROPS = {
|
const PROPS = {
|
||||||
@ -28,8 +24,6 @@ const PROP_BY_ATTR = new Map<string, string>(
|
|||||||
|
|
||||||
export class LeafletVideoOverlay extends withProps(HTMLElement, PROPS) {
|
export class LeafletVideoOverlay extends withProps(HTMLElement, PROPS) {
|
||||||
#obj?: VideoOverlay;
|
#obj?: VideoOverlay;
|
||||||
#children = new Map<HTMLElement, ChildEntry>();
|
|
||||||
#childHandler?: (e: LeafletRegisterEvent) => void;
|
|
||||||
|
|
||||||
connectedCallback() {
|
connectedCallback() {
|
||||||
const url = this.getAttribute('url') || '';
|
const url = this.getAttribute('url') || '';
|
||||||
@ -38,14 +32,11 @@ export class LeafletVideoOverlay extends withProps(HTMLElement, PROPS) {
|
|||||||
this.#parsedBounds(),
|
this.#parsedBounds(),
|
||||||
buildOptions(this, PROPS, ['url', 'bounds']),
|
buildOptions(this, PROPS, ['url', 'bounds']),
|
||||||
);
|
);
|
||||||
this.#childHandler = createChildRegisterHandler(this.#obj, this.#children);
|
registerChildren(this, this.#obj);
|
||||||
this.addEventListener('leaflet-register', this.#childHandler);
|
|
||||||
registerWithParent(this, this.#obj);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
disconnectedCallback() {
|
disconnectedCallback() {
|
||||||
if (this.#childHandler) this.removeEventListener('leaflet-register', this.#childHandler);
|
unregisterChildren(this);
|
||||||
this.#children.clear();
|
|
||||||
this.#obj?.remove();
|
this.#obj?.remove();
|
||||||
this.#obj = undefined;
|
this.#obj = undefined;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
import { Layer, LayerGroup, Popup, Tooltip } from 'leaflet';
|
import { Layer, LayerGroup, Popup, Tooltip } from 'leaflet';
|
||||||
|
import { registerWithParent } from './utils.js';
|
||||||
|
|
||||||
export type LeafletRegisterEvent = CustomEvent<{
|
export type LeafletRegisterEvent = CustomEvent<{
|
||||||
leafletObject: Layer;
|
leafletObject: Layer;
|
||||||
@ -34,3 +35,27 @@ export function createChildRegisterHandler(layer: Layer, children: Map<HTMLEleme
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const childrenMap = new WeakMap<HTMLElement, Map<HTMLElement, ChildEntry>>();
|
||||||
|
const handlerMap = new WeakMap<HTMLElement, (e: LeafletRegisterEvent) => void>();
|
||||||
|
|
||||||
|
export function registerChildren(el: HTMLElement, layer: Layer): Map<HTMLElement, ChildEntry> {
|
||||||
|
const children = new Map<HTMLElement, ChildEntry>();
|
||||||
|
const handler = createChildRegisterHandler(layer, children);
|
||||||
|
childrenMap.set(el, children);
|
||||||
|
handlerMap.set(el, handler);
|
||||||
|
el.addEventListener('leaflet-register', handler);
|
||||||
|
registerWithParent(el, layer);
|
||||||
|
return children;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function unregisterChildren(el: HTMLElement): void {
|
||||||
|
const handler = handlerMap.get(el);
|
||||||
|
if (handler) el.removeEventListener('leaflet-register', handler);
|
||||||
|
childrenMap.delete(el);
|
||||||
|
handlerMap.delete(el);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getChildren(el: HTMLElement): Map<HTMLElement, ChildEntry> | undefined {
|
||||||
|
return childrenMap.get(el);
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user