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.
70 lines
1.8 KiB
TypeScript
70 lines
1.8 KiB
TypeScript
import { CircleMarker } from 'leaflet';
|
|
import { defineProps, num, str, on } from '../core/props.ts';
|
|
import { registerChildren, unregisterChildren } from '../core/register.ts';
|
|
import {
|
|
WithProps,
|
|
buildOptions,
|
|
isPathStyleAttr,
|
|
numAttr,
|
|
updatePathStyle,
|
|
} from '../core/utils.ts';
|
|
|
|
const PROPS = defineProps({
|
|
lat: num(),
|
|
lng: num(),
|
|
radius: num(10),
|
|
color: str('#3388ff'),
|
|
weight: num(3),
|
|
opacity: num(1.0),
|
|
fill: on(),
|
|
fillColor: str('#3388ff'),
|
|
fillOpacity: num(0.2),
|
|
});
|
|
|
|
export class LeafletCircleMarker extends WithProps(HTMLElement, PROPS) {
|
|
#obj?: CircleMarker;
|
|
#syncing = false;
|
|
|
|
connectedCallback() {
|
|
this.#obj = new CircleMarker(
|
|
[numAttr(this, PROPS, 'lat'), numAttr(this, PROPS, 'lng')],
|
|
buildOptions(this, PROPS, ['lat', 'lng']),
|
|
);
|
|
this.#obj.on('move', this.#onMove, this);
|
|
registerChildren(this, this.#obj);
|
|
}
|
|
|
|
disconnectedCallback() {
|
|
this.#obj?.off('move', this.#onMove, this);
|
|
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 || this.#syncing) return;
|
|
if (name === 'lat' || name === 'lng') {
|
|
this.#obj.setLatLng([numAttr(this, PROPS, 'lat'), numAttr(this, PROPS, 'lng')]);
|
|
} else if (name === 'radius') {
|
|
this.#obj.setRadius(numAttr(this, PROPS, 'radius'));
|
|
} else if (isPathStyleAttr(name)) {
|
|
updatePathStyle(this.#obj, name, val);
|
|
}
|
|
}
|
|
|
|
#onMove() {
|
|
if (!this.#obj || this.#syncing) return;
|
|
this.#syncing = true;
|
|
const pos = this.#obj.getLatLng();
|
|
this.setAttribute('lat', `${pos.lat}`);
|
|
this.setAttribute('lng', `${pos.lng}`);
|
|
this.#syncing = false;
|
|
}
|
|
}
|
|
|
|
customElements.define('leaflet-circle-marker', LeafletCircleMarker);
|