diff --git a/src/components/leaflet-circle-marker.ts b/src/components/leaflet-circle-marker.ts index 1818447..6ce7c60 100644 --- a/src/components/leaflet-circle-marker.ts +++ b/src/components/leaflet-circle-marker.ts @@ -1,5 +1,5 @@ import { CircleMarker } from 'leaflet'; -import { registerWithParent, buildOptions } from '../core/utils.js'; +import { registerWithParent, buildOptions, numAttr } from '../core/utils.js'; import { withProps } from '../core/with-props.js'; import { @@ -29,7 +29,7 @@ export class LeafletCircleMarker extends withProps(HTMLElement, PROPS) { connectedCallback() { this.#obj = new CircleMarker( - [this.#num('lat'), this.#num('lng')], + [numAttr(this, PROPS, 'lat'), numAttr(this, PROPS, 'lng')], buildOptions(this, PROPS, ['lat', 'lng']), ); this.#childHandler = createChildRegisterHandler(this.#obj, this.#children); @@ -47,20 +47,13 @@ export class LeafletCircleMarker extends withProps(HTMLElement, PROPS) { attributeChangedCallback(name: string, _old: string | null, val: string | null) { if (!this.#obj) return; if (name === 'lat' || name === 'lng') { - this.#obj.setLatLng([this.#num('lat'), this.#num('lng')]); + this.#obj.setLatLng([numAttr(this, PROPS, 'lat'), numAttr(this, PROPS, 'lng')]); } else if (name === 'radius') { - this.#obj.setRadius(this.#num('radius')); + this.#obj.setRadius(numAttr(this, PROPS, 'radius')); } else if (isPathStyleAttr(name)) { updatePathStyle(this.#obj, name, val); } } - - #num(name: string): number { - const v = this.getAttribute(name); - return v !== null - ? Number(v) - : (PROPS[name as keyof typeof PROPS] as { default: number }).default; - } } customElements.define('leaflet-circle-marker', LeafletCircleMarker); diff --git a/src/components/leaflet-circle.ts b/src/components/leaflet-circle.ts index 6fea28d..e88e388 100644 --- a/src/components/leaflet-circle.ts +++ b/src/components/leaflet-circle.ts @@ -1,5 +1,5 @@ import { Circle } from 'leaflet'; -import { registerWithParent, buildOptions } from '../core/utils.js'; +import { registerWithParent, buildOptions, numAttr } from '../core/utils.js'; import { withProps } from '../core/with-props.js'; import { @@ -29,7 +29,7 @@ export class LeafletCircle extends withProps(HTMLElement, PROPS) { connectedCallback() { this.#obj = new Circle( - [this.#num('lat'), this.#num('lng')], + [numAttr(this, PROPS, 'lat'), numAttr(this, PROPS, 'lng')], buildOptions(this, PROPS, ['lat', 'lng']), ); this.#childHandler = createChildRegisterHandler(this.#obj, this.#children); @@ -47,20 +47,13 @@ export class LeafletCircle extends withProps(HTMLElement, PROPS) { attributeChangedCallback(name: string, _old: string | null, val: string | null) { if (!this.#obj) return; if (name === 'lat' || name === 'lng') { - this.#obj.setLatLng([this.#num('lat'), this.#num('lng')]); + this.#obj.setLatLng([numAttr(this, PROPS, 'lat'), numAttr(this, PROPS, 'lng')]); } else if (name === 'radius') { - this.#obj.setRadius(this.#num('radius')); + this.#obj.setRadius(numAttr(this, PROPS, 'radius')); } else if (isPathStyleAttr(name)) { updatePathStyle(this.#obj, name, val); } } - - #num(name: string): number { - const v = this.getAttribute(name); - return v !== null - ? Number(v) - : (PROPS[name as keyof typeof PROPS] as { default: number }).default; - } } customElements.define('leaflet-circle', LeafletCircle); diff --git a/src/components/leaflet-marker.ts b/src/components/leaflet-marker.ts index e3cb516..3096008 100644 --- a/src/components/leaflet-marker.ts +++ b/src/components/leaflet-marker.ts @@ -1,5 +1,5 @@ import { Marker } from 'leaflet'; -import { registerWithParent, buildOptions, parseAttributeValue } from '../core/utils.js'; +import { registerWithParent, buildOptions, numAttr, parseAttributeValue } from '../core/utils.js'; import { withProps } from '../core/with-props.js'; import { @@ -31,7 +31,7 @@ export class LeafletMarker extends withProps(HTMLElement, PROPS) { connectedCallback() { this.#obj = new Marker( - [this.#num('lat'), this.#num('lng')], + [numAttr(this, PROPS, 'lat'), numAttr(this, PROPS, 'lng')], buildOptions(this, PROPS, ['lat', 'lng']), ); this.#obj.on('dragend', this.#onDragEnd); @@ -51,7 +51,7 @@ export class LeafletMarker extends withProps(HTMLElement, PROPS) { attributeChangedCallback(name: string, _old: string | null, val: string | null) { if (!this.#obj || this.#syncing) return; if (name === 'lat' || name === 'lng') { - this.#obj.setLatLng([this.#num('lat'), this.#num('lng')]); + this.#obj.setLatLng([numAttr(this, PROPS, 'lat'), numAttr(this, PROPS, 'lng')]); } else if (name === 'draggable') { if (val !== null) this.#obj.dragging?.enable(); else this.#obj.dragging?.disable(); @@ -79,13 +79,6 @@ export class LeafletMarker extends withProps(HTMLElement, PROPS) { this.setAttribute('lng', String(pos.lng)); this.#syncing = false; }; - - #num(name: string): number { - const v = this.getAttribute(name); - return v !== null - ? Number(v) - : (PROPS[name as keyof typeof PROPS] as { default: number }).default; - } } customElements.define('leaflet-marker', LeafletMarker); diff --git a/src/components/leaflet-popup.ts b/src/components/leaflet-popup.ts index 7eee2d0..3943339 100644 --- a/src/components/leaflet-popup.ts +++ b/src/components/leaflet-popup.ts @@ -1,5 +1,5 @@ import { Popup } from 'leaflet'; -import { registerWithParent, buildOptions } from '../core/utils.js'; +import { registerWithParent, buildOptions, numAttr } from '../core/utils.js'; import { withProps } from '../core/with-props.js'; import type { PropDef } from '../types/props.js'; @@ -25,7 +25,7 @@ export class LeafletPopup extends withProps(HTMLElement, PROPS) { content: this.innerHTML, }); if (this.hasAttribute('lat') && this.hasAttribute('lng')) { - this.#obj.setLatLng([this.#num('lat'), this.#num('lng')]); + this.#obj.setLatLng([numAttr(this, PROPS, 'lat'), numAttr(this, PROPS, 'lng')]); } registerWithParent(this, this.#obj); @@ -49,16 +49,9 @@ export class LeafletPopup extends withProps(HTMLElement, PROPS) { attributeChangedCallback(name: string) { if (!this.#obj) return; if (name === 'lat' || name === 'lng') { - this.#obj.setLatLng([this.#num('lat'), this.#num('lng')]); + this.#obj.setLatLng([numAttr(this, PROPS, 'lat'), numAttr(this, PROPS, 'lng')]); } } - - #num(name: string): number { - const v = this.getAttribute(name); - return v !== null - ? Number(v) - : (PROPS[name as keyof typeof PROPS] as { default: number }).default; - } } customElements.define('leaflet-popup', LeafletPopup); diff --git a/src/components/leaflet-tooltip.ts b/src/components/leaflet-tooltip.ts index 4132616..cb0720c 100644 --- a/src/components/leaflet-tooltip.ts +++ b/src/components/leaflet-tooltip.ts @@ -1,5 +1,5 @@ import { Tooltip } from 'leaflet'; -import { registerWithParent, buildOptions } from '../core/utils.js'; +import { registerWithParent, buildOptions, numAttr } from '../core/utils.js'; import { withProps } from '../core/with-props.js'; import type { PropDef } from '../types/props.js'; @@ -25,7 +25,7 @@ export class LeafletTooltip extends withProps(HTMLElement, PROPS) { content: this.innerHTML, }); if (this.hasAttribute('lat') && this.hasAttribute('lng')) { - this.#obj.setLatLng([this.#num('lat'), this.#num('lng')]); + this.#obj.setLatLng([numAttr(this, PROPS, 'lat'), numAttr(this, PROPS, 'lng')]); } registerWithParent(this, this.#obj); @@ -49,16 +49,9 @@ export class LeafletTooltip extends withProps(HTMLElement, PROPS) { attributeChangedCallback(name: string) { if (!this.#obj) return; if (name === 'lat' || name === 'lng') { - this.#obj.setLatLng([this.#num('lat'), this.#num('lng')]); + this.#obj.setLatLng([numAttr(this, PROPS, 'lat'), numAttr(this, PROPS, 'lng')]); } } - - #num(name: string): number { - const v = this.getAttribute(name); - return v !== null - ? Number(v) - : (PROPS[name as keyof typeof PROPS] as { default: number }).default; - } } customElements.define('leaflet-tooltip', LeafletTooltip); diff --git a/src/core/utils.ts b/src/core/utils.ts index b0a2629..e432df5 100644 --- a/src/core/utils.ts +++ b/src/core/utils.ts @@ -38,6 +38,12 @@ export function definePropAccessors(proto: object, props: Record, name: string): number { + const v = el.getAttribute(name); + if (v !== null) return Number(v); + return (props[name] as { default: number }).default; +} + export function buildOptions( el: HTMLElement, props: Record,