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.
leaflet-components/src/components/leaflet-rectangle.ts

45 lines
1.2 KiB
TypeScript

import { Rectangle } from 'leaflet';
import { WithProps, defineProps, num, str, on } from '../core/props.ts';
import { registerChildren, unregisterChildren } from '../core/register.ts';
import { buildOptions, isPathStyleAttr, parseBoundsAttr, updatePathStyle } from '../core/utils.ts';
const PROPS = defineProps({
bounds: str(),
color: str('#3388ff'),
weight: num(3),
opacity: num(1.0),
fill: on(),
fillColor: str('#3388ff'),
fillOpacity: num(0.2),
});
export class LeafletRectangle extends WithProps(HTMLElement, PROPS) {
#obj?: Rectangle;
connectedCallback() {
this.#obj = new Rectangle(parseBoundsAttr(this), buildOptions(this, PROPS, ['bounds']));
registerChildren(this, this.#obj);
}
disconnectedCallback() {
unregisterChildren(this);
this.#obj?.remove();
this.#obj = undefined;
}
get leafletObject() {
return this.#obj;
}
attributeChangedCallback(name: string, _old: string | null, val: string | null) {
if (!this.#obj) return;
if (name === 'bounds') {
this.#obj.setBounds(parseBoundsAttr(this));
} else if (isPathStyleAttr(name)) {
updatePathStyle(this.#obj, name, val);
}
}
}
customElements.define('leaflet-rectangle', LeafletRectangle);