refactor map to lit
This commit is contained in:
parent
ebe6c71c08
commit
90813656a9
|
|
@ -3,18 +3,8 @@ import { LatLng, LatLngExpression, LeafletEvent, map, Map, tileLayer } from 'lea
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
// Vite specific import :(
|
// Vite specific import :(
|
||||||
import leafletCSS from 'leaflet/dist/leaflet.css?inline';
|
import leafletCSS from 'leaflet/dist/leaflet.css?inline';
|
||||||
import { css } from './common/tags';
|
import { FolkElement } from './common/folk-element';
|
||||||
const styles = css`
|
import { css, PropertyValues, unsafeCSS } from '@lit/reactive-element';
|
||||||
${leafletCSS}
|
|
||||||
:host {
|
|
||||||
display: block;
|
|
||||||
}
|
|
||||||
|
|
||||||
:host > div {
|
|
||||||
height: 100%;
|
|
||||||
width: 100%;
|
|
||||||
}
|
|
||||||
`;
|
|
||||||
|
|
||||||
export class RecenterEvent extends Event {
|
export class RecenterEvent extends Event {
|
||||||
constructor() {
|
constructor() {
|
||||||
|
|
@ -22,23 +12,54 @@ export class RecenterEvent extends Event {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class FolkMap extends HTMLElement {
|
export class FolkMap extends FolkElement {
|
||||||
static tagName = 'folk-map';
|
static override tagName = 'folk-map';
|
||||||
|
|
||||||
static define() {
|
static override styles = css`
|
||||||
if (customElements.get(this.tagName)) return;
|
${unsafeCSS(leafletCSS)}
|
||||||
customElements.define(this.tagName, this);
|
:host {
|
||||||
}
|
display: block;
|
||||||
|
}
|
||||||
|
|
||||||
|
:host > div {
|
||||||
|
height: 100%;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
#container = document.createElement('div');
|
#container = document.createElement('div');
|
||||||
#map!: Map;
|
#map = map(this.#container);
|
||||||
|
|
||||||
constructor() {
|
override firstUpdated(changedProperties: PropertyValues): void {
|
||||||
super();
|
super.firstUpdated(changedProperties);
|
||||||
|
|
||||||
const shadow = this.attachShadow({ mode: 'open' });
|
this.renderRoot.appendChild(this.#container);
|
||||||
shadow.adoptedStyleSheets.push(styles);
|
|
||||||
shadow.appendChild(this.#container);
|
this.#map.addLayer(
|
||||||
|
tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
|
||||||
|
maxZoom: 19,
|
||||||
|
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
this.#map.setView(
|
||||||
|
(this.getAttribute('coordinates') || '0, 0').split(',').map(Number) as LatLngExpression,
|
||||||
|
Number(this.getAttribute('zoom') || 13)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
connectedCallback(): void {
|
||||||
|
super.connectedCallback();
|
||||||
|
|
||||||
|
// Move end includes changes to zoom
|
||||||
|
this.#map.on('moveend', this.handleEvent);
|
||||||
|
}
|
||||||
|
|
||||||
|
disconnectedCallback(): void {
|
||||||
|
super.disconnectedCallback();
|
||||||
|
|
||||||
|
// Move end includes changes to zoom
|
||||||
|
this.#map.off('moveend', this.handleEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
get lat() {
|
get lat() {
|
||||||
|
|
@ -69,24 +90,6 @@ export class FolkMap extends HTMLElement {
|
||||||
this.#map.setZoom(zoom);
|
this.#map.setZoom(zoom);
|
||||||
}
|
}
|
||||||
|
|
||||||
connectedCallback() {
|
|
||||||
this.#map = map(this.#container);
|
|
||||||
this.#map.addLayer(
|
|
||||||
tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
|
|
||||||
maxZoom: 19,
|
|
||||||
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
// Move end includes changes to zoom
|
|
||||||
this.#map.on('moveend', this.handleEvent);
|
|
||||||
|
|
||||||
this.#map.setView(
|
|
||||||
(this.getAttribute('coordinates') || '0, 0').split(',').map(Number) as LatLngExpression,
|
|
||||||
Number(this.getAttribute('zoom') || 13)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
handleEvent = (event: LeafletEvent) => {
|
handleEvent = (event: LeafletEvent) => {
|
||||||
switch (event.type) {
|
switch (event.type) {
|
||||||
case 'moveend': {
|
case 'moveend': {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue