Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"intl-messageformat": "^2.2.0",
"js-yaml": "^3.13.1",
"leaflet": "^1.4.0",
"leaflet-draw": "^1.0.4",
"lit-element": "^2.2.1",
"lit-html": "^1.1.0",
"lit-virtualizer": "^0.4.2",
Expand Down Expand Up @@ -123,6 +124,7 @@
"@types/hls.js": "^0.12.3",
"@types/js-yaml": "^3.12.1",
"@types/leaflet": "^1.4.3",
"@types/leaflet-draw": "^1.0.1",
"@types/memoize-one": "4.1.0",
"@types/mocha": "^5.2.6",
"@types/webspeechapi": "^0.0.29",
Expand Down
8 changes: 7 additions & 1 deletion src/common/dom/setup-leaflet-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { Map } from "leaflet";

// Sets up a Leaflet map on the provided DOM element
export type LeafletModuleType = typeof import("leaflet");
export type LeafletDrawModuleType = typeof import("leaflet-draw");

export const setupLeafletMap = async (
mapElement: HTMLElement,
darkMode = false
darkMode = false,
draw = false
): Promise<[Map, LeafletModuleType]> => {
if (!mapElement.parentNode) {
throw new Error("Cannot setup Leaflet map on disconnected element");
Expand All @@ -16,6 +18,10 @@ export const setupLeafletMap = async (
)) as LeafletModuleType;
Leaflet.Icon.Default.imagePath = "/static/images/leaflet/images/";

if (draw) {
await import(/* webpackChunkName: "leaflet-draw" */ "leaflet-draw");
}

const map = Leaflet.map(mapElement);
const style = document.createElement("link");
style.setAttribute("href", "/static/images/leaflet/leaflet.css");
Expand Down
161 changes: 141 additions & 20 deletions src/components/map/ha-location-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,35 @@ import {
customElement,
PropertyValues,
} from "lit-element";
import { Marker, Map, LeafletMouseEvent, DragEndEvent, LatLng } from "leaflet";
import {
Marker,
Map,
LeafletMouseEvent,
DragEndEvent,
LatLng,
Circle,
DivIcon,
} from "leaflet";
import {
setupLeafletMap,
LeafletModuleType,
} from "../../common/dom/setup-leaflet-map";
import { fireEvent } from "../../common/dom/fire_event";
import { nextRender } from "../../common/util/render-status";

@customElement("ha-location-editor")
class LocationEditor extends LitElement {
@property() public location?: [number, number];
@property() public radius?: number;
@property() public icon?: string;
public fitZoom = 16;

private _iconEl?: DivIcon;
private _ignoreFitToMap?: [number, number];

// tslint:disable-next-line
private Leaflet?: LeafletModuleType;
private _leafletMap?: Map;
private _locationMarker?: Marker;
private _locationMarker?: Marker | Circle;

public fitMap(): void {
if (!this._leafletMap || !this.location) {
Expand Down Expand Up @@ -53,30 +64,48 @@ class LocationEditor extends LitElement {
return;
}

this._updateMarker();
if (!this._ignoreFitToMap || this._ignoreFitToMap !== this.location) {
this.fitMap();
if (changedProps.has("location")) {
this._updateMarker();
if (
this.location &&
(!this._ignoreFitToMap ||
this._ignoreFitToMap[0] !== this.location[0] ||
this._ignoreFitToMap[1] !== this.location[1])
) {
this.fitMap();
}
this._ignoreFitToMap = undefined;
}
if (changedProps.has("radius")) {
this._updateRadius();
Comment thread
bramkragten marked this conversation as resolved.
}
if (changedProps.has("icon")) {
this._updateIcon();
}
this._ignoreFitToMap = undefined;
}

private get _mapEl(): HTMLDivElement {
return this.shadowRoot!.querySelector("div")!;
}

private async _initMap(): Promise<void> {
[this._leafletMap, this.Leaflet] = await setupLeafletMap(this._mapEl);
[this._leafletMap, this.Leaflet] = await setupLeafletMap(
this._mapEl,
false,
Boolean(this.radius)
);
this._leafletMap.addEventListener(
"click",
// @ts-ignore
(ev: LeafletMouseEvent) => this._updateLocation(ev.latlng)
(ev: LeafletMouseEvent) => this._locationUpdated(ev.latlng)
);
this._updateIcon();
this._updateMarker();
this.fitMap();
this._leafletMap.invalidateSize();
}

private _updateLocation(latlng: LatLng) {
private _locationUpdated(latlng: LatLng) {
let longitude = latlng.lng;
if (Math.abs(longitude) > 180.0) {
// Normalize longitude if map provides values beyond -180 to +180 degrees.
Expand All @@ -86,7 +115,68 @@ class LocationEditor extends LitElement {
fireEvent(this, "change", undefined, { bubbles: false });
}

private _updateMarker(): void {
private _radiusUpdated() {
this._ignoreFitToMap = this.location;
this.radius = (this._locationMarker as Circle).getRadius();
fireEvent(this, "change", undefined, { bubbles: false });
}

private _updateIcon() {
if (!this.icon) {
this._iconEl = undefined;
return;
}

// create icon
let iconHTML = "";
const el = document.createElement("ha-icon");
el.setAttribute("icon", this.icon);
iconHTML = el.outerHTML;

this._iconEl = this.Leaflet!.divIcon({
html: iconHTML,
iconSize: [24, 24],
className: "light leaflet-edit-move",
});
this._setIcon();
}

private _setIcon() {
if (!this._locationMarker || !this._iconEl) {
return;
}

if (!this.radius) {
(this._locationMarker as Marker).setIcon(this._iconEl);
return;
}

// @ts-ignore
const moveMarker = this._locationMarker.editing._moveMarker;
moveMarker.setIcon(this._iconEl);
}

private _setupEdit() {
// @ts-ignore
this._locationMarker.editing.enable();
// @ts-ignore
const moveMarker = this._locationMarker.editing._moveMarker;
// @ts-ignore
const resizeMarker = this._locationMarker.editing._resizeMarkers[0];
this._setIcon();
moveMarker.addEventListener(
"dragend",
// @ts-ignore
(ev: DragEndEvent) => this._locationUpdated(ev.target.getLatLng())
);
resizeMarker.addEventListener(
"dragend",
// @ts-ignore
(ev: DragEndEvent) => this._radiusUpdated(ev)
);
}

private async _updateMarker(): Promise<void> {
if (!this.location) {
if (this._locationMarker) {
this._locationMarker.remove();
Expand All @@ -97,17 +187,41 @@ class LocationEditor extends LitElement {

if (this._locationMarker) {
this._locationMarker.setLatLng(this.location);
if (this.radius) {
// @ts-ignore
this._locationMarker.editing.disable();
await nextRender();
this._setupEdit();
}
return;
}
this._locationMarker = this.Leaflet!.marker(this.location, {
draggable: true,
});
this._locationMarker.addEventListener(
"dragend",
// @ts-ignore
(ev: DragEndEvent) => this._updateLocation(ev.target.getLatLng())
);
this._leafletMap!.addLayer(this._locationMarker);

if (!this.radius) {
this._locationMarker = this.Leaflet!.marker(this.location, {
draggable: true,
});
this._setIcon();
this._locationMarker.addEventListener(
"dragend",
// @ts-ignore
(ev: DragEndEvent) => this._locationUpdated(ev.target.getLatLng())
);
this._leafletMap!.addLayer(this._locationMarker);
} else {
this._locationMarker = this.Leaflet!.circle(this.location, {
color: "#FF9800",
radius: this.radius,
});
this._leafletMap!.addLayer(this._locationMarker);
this._setupEdit();
}
}

private _updateRadius(): void {
if (!this._locationMarker || !this.radius) {
return;
}
(this._locationMarker as Circle).setRadius(this.radius);
}

static get styles(): CSSResult {
Expand All @@ -119,6 +233,13 @@ class LocationEditor extends LitElement {
#map {
height: 100%;
}
.leaflet-edit-move {
cursor: move !important;
}
.leaflet-edit-resize {
border-radius: 50%;
cursor: nesw-resize !important;
}
`;
}
}
Expand Down
Loading