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-control-zoom.ts

57 lines
1.9 KiB
TypeScript

import { Control, ControlPosition } from 'leaflet';
import { registerWithParent } from '../core/utils.js';
import type { PropDef, PropTypesFromTable } from '../types/props.js';
const PROPS = {
position: { kind: 'str', attr: 'position', default: 'topleft' },
zoomInText: { kind: 'str', attr: 'zoom-in-text', default: '+' },
zoomInTitle: { kind: 'str', attr: 'zoom-in-title', default: 'Zoom in' },
zoomOutText: { kind: 'str', attr: 'zoom-out-text', default: '-' },
zoomOutTitle: { kind: 'str', attr: 'zoom-out-title', default: 'Zoom out' },
} satisfies Record<string, PropDef>;
type PropTypes = PropTypesFromTable<typeof PROPS>;
const TypedBase = HTMLElement as unknown as new () => HTMLElement & PropTypes;
export class LeafletControlZoom extends TypedBase {
#obj?: Control.Zoom;
static get observedAttributes() {
return Object.values(PROPS).map((s) => s.attr);
}
static {
for (const [name, spec] of Object.entries(PROPS)) {
Object.defineProperty(LeafletControlZoom.prototype, name, {
get(this: LeafletControlZoom) {
const val = this.getAttribute(spec.attr);
return val ?? (spec as { default: string }).default;
},
set(this: LeafletControlZoom, v: number | string) {
this.setAttribute(spec.attr, String(v));
},
configurable: true,
enumerable: true,
});
}
}
connectedCallback() {
this.#obj = new Control.Zoom({
position: this.getAttribute('position') as ControlPosition | undefined,
zoomInText: this.getAttribute('zoom-in-text') ?? '+',
zoomInTitle: this.getAttribute('zoom-in-title') ?? 'Zoom in',
zoomOutText: this.getAttribute('zoom-out-text') ?? '-',
zoomOutTitle: this.getAttribute('zoom-out-title') ?? 'Zoom out',
});
registerWithParent(this, this.#obj);
}
disconnectedCallback() {
this.#obj?.remove();
this.#obj = undefined;
}
}
customElements.define('leaflet-control-zoom', LeafletControlZoom);