buildOptions was passing default values for absent attributes, including empty-string defaults like pane:'', which caused Leaflet to look up this._panes[''] and return undefined, crashing in appendChild. - Skip empty-string defaults in buildOptions so Leaflet uses its own - Broaden createChildRegisterHandler param from LayerGroup to Layer so non-container layers (marker, circle, etc.) can intercept popup/tooltip children and call bindPopup/bindTooltip instead of bubbling to the map - Add child-registration handler to every layer component (all 12) - Fix disconnectedCallback in container components to unbind popups and tooltips and remove child layers properly
101 lines
3.7 KiB
TypeScript
101 lines
3.7 KiB
TypeScript
import { VideoOverlay, LatLngBounds, LatLngBoundsExpression, LatLngExpression } from 'leaflet';
|
|
import { registerWithParent, buildOptions, parseAttributeValue } from '../core/utils.js';
|
|
import { createChildRegisterHandler, type ChildEntry } from '../core/register.js';
|
|
import type { PropDef, PropTypesFromTable } from '../types/props.js';
|
|
|
|
const PROPS = {
|
|
url: { kind: 'str', attr: 'url', default: '' },
|
|
bounds: { kind: 'str', attr: 'bounds', default: '' },
|
|
opacity: { kind: 'num', attr: 'opacity', default: 1.0 },
|
|
alt: { kind: 'str', attr: 'alt', default: '' },
|
|
interactive: { kind: 'bool-on', attr: 'interactive' },
|
|
crossOrigin: { kind: 'str', attr: 'cross-origin', default: '' },
|
|
loop: { kind: 'bool-on', attr: 'loop' },
|
|
autoplay: { kind: 'bool-on', attr: 'autoplay' },
|
|
muted: { kind: 'bool-on', attr: 'muted' },
|
|
playsInline: { kind: 'bool-on', attr: 'playsinline' },
|
|
} satisfies Record<string, PropDef>;
|
|
|
|
const PROP_BY_ATTR = new Map<string, string>(
|
|
Object.entries(PROPS).map(([name, spec]) => [spec.attr, name]),
|
|
);
|
|
|
|
type PropTypes = PropTypesFromTable<typeof PROPS>;
|
|
const TypedBase = HTMLElement as unknown as new () => HTMLElement & PropTypes;
|
|
|
|
export class LeafletVideoOverlay extends TypedBase {
|
|
#obj?: VideoOverlay;
|
|
#children = new Map<HTMLElement, ChildEntry>();
|
|
#childHandler?: EventListener;
|
|
|
|
static get observedAttributes() {
|
|
return Object.values(PROPS).map((s) => s.attr);
|
|
}
|
|
|
|
static {
|
|
for (const [name, spec] of Object.entries(PROPS)) {
|
|
Object.defineProperty(LeafletVideoOverlay.prototype, name, {
|
|
get(this: LeafletVideoOverlay) {
|
|
const val = this.getAttribute(spec.attr);
|
|
if (spec.kind === 'num') return val !== null ? Number(val) : spec.default;
|
|
if (spec.kind === 'bool-on') return this.hasAttribute(spec.attr);
|
|
return val ?? spec.default;
|
|
},
|
|
set(this: LeafletVideoOverlay, v: number | string | boolean) {
|
|
if (spec.kind === 'bool-on') this.toggleAttribute(spec.attr, !!v);
|
|
else this.setAttribute(spec.attr, String(v));
|
|
},
|
|
configurable: true,
|
|
enumerable: true,
|
|
});
|
|
}
|
|
}
|
|
|
|
connectedCallback() {
|
|
const url = this.getAttribute('url') || '';
|
|
this.#obj = new VideoOverlay(
|
|
url,
|
|
this.#parsedBounds(),
|
|
buildOptions(this, PROPS, ['url', 'bounds']),
|
|
);
|
|
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);
|
|
this.#children.clear();
|
|
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 if (name === 'bounds') {
|
|
this.#obj.setBounds(new LatLngBounds(this.#parsedBounds() as LatLngExpression[]));
|
|
} else {
|
|
const propName = PROP_BY_ATTR.get(name);
|
|
if (!propName) return;
|
|
const setter =
|
|
`set${propName.charAt(0).toUpperCase()}${propName.slice(1)}` as keyof VideoOverlay;
|
|
if (typeof this.#obj[setter] === 'function') {
|
|
(this.#obj[setter] as (v: unknown) => void)(parseAttributeValue(val));
|
|
}
|
|
}
|
|
}
|
|
|
|
#parsedBounds(): LatLngBoundsExpression {
|
|
const raw = this.getAttribute('bounds');
|
|
return raw ? (JSON.parse(raw) as LatLngBoundsExpression) : [];
|
|
}
|
|
|
|
getElement(): HTMLVideoElement | undefined {
|
|
return this.#obj?.getElement();
|
|
}
|
|
}
|
|
|
|
customElements.define('leaflet-video-overlay', LeafletVideoOverlay);
|