Skip to content

armgit / core

Public Imported from GitHub
0

100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps

Some refactoring of or-map and or-icon components to improve customisation

Rich Turnerd8e25a72dcf3
diff --git a/model/src/main/java/org/openremote/model/asset/Asset.java b/model/src/main/java/org/openremote/model/asset/Asset.java
index 2b63fd2b5..60738d981 100644
--- a/model/src/main/java/org/openremote/model/asset/Asset.java
+++ b/model/src/main/java/org/openremote/model/asset/Asset.java
@@ -241,7 +241,7 @@ public class Asset implements IdentifiableEntity {
protected Date createdOn;
@NotNull(message = "{Asset.name.NotNull}")
- @Size(min = 3, max = 1023, message = "{Asset.name.Size}")
+ @Size(min = 1, max = 1023, message = "{Asset.name.Size}")
@Column(name = "NAME", nullable = false, length = 1023)
protected String name;
diff --git a/ui/component/or-icon/src/icon-set-svg.ts b/ui/component/or-icon/src/icon-set-svg.ts
index 2fb87174d..02bd37fae 100644
--- a/ui/component/or-icon/src/icon-set-svg.ts
+++ b/ui/component/or-icon/src/icon-set-svg.ts
@@ -29,6 +29,7 @@ export class IconSetSvg implements IconSet {
} else {
const path = document.createElementNS("http://www.w3.org/2000/svg", "path");
path.setAttribute("d", iconData);
+ path.style.pointerEvents = "pointer-events: var(--or-icon-pointer-events, none);";
svg.appendChild(path);
}
return svg;
diff --git a/ui/component/or-icon/src/or-icons.ts b/ui/component/or-icon/src/or-icons.ts
index 3317a5b73..dfad4de68 100644
--- a/ui/component/or-icon/src/or-icons.ts
+++ b/ui/component/or-icon/src/or-icons.ts
@@ -1,9 +1,9 @@
import {IconSetSvg} from "./icon-set-svg";
let orIcons: IconSetSvg = new IconSetSvg(24, {
- "logo": "<path fill=\"#C4D600\" d=\"m11.96125,23.85096c-6.56773,0 -11.90971,-5.34202 -11.90971,-11.90862l2.49411,0c0,5.19176 4.22389,9.41568 9.41561,9.41568c5.19053,0 9.41561,-4.22392 9.41561,-9.41568c0,-5.19176 -4.22389,-9.41568 -9.41561,-9.41568l0,-2.49413c6.56655,0 11.90971,5.34439 11.90971,11.9098c0,6.5666 -5.34316,11.90862 -11.90971,11.90862l0,0z\" /><path fill=\"#4E9D2D\" d=\"m10.25094,20.38647c-2.26848,-0.47231 -4.21822,-1.80199 -5.48741,-3.74009s-1.70892,-4.25777 -1.23545,-6.52625c0.47347,-2.26965 1.80315,-4.21822 3.74242,-5.48857c1.93926,-1.27035 4.25661,-1.70892 6.52625,-1.23545c4.68471,0.97719 7.70121,5.5828 6.72286,10.26635l-2.39994,-0.49907c0.70149,-3.36085 -1.4623,-6.66585 -4.82431,-7.36734c-1.62866,-0.33969 -3.29105,-0.02559 -4.68238,0.88645c-1.39134,0.91088 -2.34526,2.3092 -2.68495,3.93786c-0.33853,1.62633 -0.02443,3.28988 0.88645,4.68122c0.91088,1.39134 2.3092,2.34526 3.93786,2.68379l-0.50139,2.4011l0,0z\"/><path fill=\"#1D5632\"d=\"m12.03064,17.49616c-0.09524,0 -0.19048,-0.00116 -0.28457,-0.00465c-1.49833,-0.0755 -2.87818,-0.72942 -3.88636,-1.84097c-1.00818,-1.11271 -1.52156,-2.54948 -1.44722,-4.05013l2.44378,0.12196c-0.04181,0.84557 0.24856,1.65745 0.81769,2.28466c0.56681,0.62721 1.34733,0.99656 2.1929,1.03838c0.84208,0.03833 1.65629,-0.2474 2.2835,-0.81653c0.62837,-0.56797 0.99656,-1.34617 1.03954,-2.19174c0.08711,-1.74573 -1.26371,-3.23708 -3.00943,-3.3242l0.12196,-2.44378c3.09422,0.15332 5.4869,2.79455 5.33358,5.88877c-0.07434,1.49949 -0.72942,2.87818 -1.84097,3.8852c-1.04186,0.94313 -2.36945,1.45303 -3.7644,1.45303l0,0z\"/>",
+ "logo": "<path style='pointer-events: var(--or-icon-pointer-events, none);' fill=\"#C4D600\" d=\"m11.96125,23.85096c-6.56773,0 -11.90971,-5.34202 -11.90971,-11.90862l2.49411,0c0,5.19176 4.22389,9.41568 9.41561,9.41568c5.19053,0 9.41561,-4.22392 9.41561,-9.41568c0,-5.19176 -4.22389,-9.41568 -9.41561,-9.41568l0,-2.49413c6.56655,0 11.90971,5.34439 11.90971,11.9098c0,6.5666 -5.34316,11.90862 -11.90971,11.90862l0,0z\" /><path fill=\"#4E9D2D\" d=\"m10.25094,20.38647c-2.26848,-0.47231 -4.21822,-1.80199 -5.48741,-3.74009s-1.70892,-4.25777 -1.23545,-6.52625c0.47347,-2.26965 1.80315,-4.21822 3.74242,-5.48857c1.93926,-1.27035 4.25661,-1.70892 6.52625,-1.23545c4.68471,0.97719 7.70121,5.5828 6.72286,10.26635l-2.39994,-0.49907c0.70149,-3.36085 -1.4623,-6.66585 -4.82431,-7.36734c-1.62866,-0.33969 -3.29105,-0.02559 -4.68238,0.88645c-1.39134,0.91088 -2.34526,2.3092 -2.68495,3.93786c-0.33853,1.62633 -0.02443,3.28988 0.88645,4.68122c0.91088,1.39134 2.3092,2.34526 3.93786,2.68379l-0.50139,2.4011l0,0z\"/><path fill=\"#1D5632\"d=\"m12.03064,17.49616c-0.09524,0 -0.19048,-0.00116 -0.28457,-0.00465c-1.49833,-0.0755 -2.87818,-0.72942 -3.88636,-1.84097c-1.00818,-1.11271 -1.52156,-2.54948 -1.44722,-4.05013l2.44378,0.12196c-0.04181,0.84557 0.24856,1.65745 0.81769,2.28466c0.56681,0.62721 1.34733,0.99656 2.1929,1.03838c0.84208,0.03833 1.65629,-0.2474 2.2835,-0.81653c0.62837,-0.56797 0.99656,-1.34617 1.03954,-2.19174c0.08711,-1.74573 -1.26371,-3.23708 -3.00943,-3.3242l0.12196,-2.44378c3.09422,0.15332 5.4869,2.79455 5.33358,5.88877c-0.07434,1.49949 -0.72942,2.87818 -1.84097,3.8852c-1.04186,0.94313 -2.36945,1.45303 -3.7644,1.45303l0,0z\"/>",
"logo-plain": "m11.9925,23.91349c-6.58498,0 -11.94099,-5.35605 -11.94099,-11.9399l2.50066,0c0,5.20539 4.23498,9.44041 9.44033,9.44041c5.20416,0 9.44033,-4.23502 9.44033,-9.44041c0,-5.20539 -4.23498,-9.44041 -9.44033,-9.44041l0,-2.50068c6.5838,0 11.94099,5.35842 11.94099,11.94108c0,6.58385 -5.35719,11.9399 -11.94099,11.9399l0,0zm-1.80787,-3.27769c-2.31323,-0.48163 -4.30141,-1.83755 -5.59563,-3.81389s-1.74263,-4.34178 -1.25982,-6.65502c0.48281,-2.31443 1.83872,-4.30145 3.81623,-5.59686c1.97751,-1.29542 4.34056,-1.74264 6.65497,-1.25983c4.77711,0.99647 7.8531,5.69295 6.85545,10.46891l-2.44727,-0.50891c0.71532,-3.42716 -1.49114,-6.79738 -4.91946,-7.5127c-1.66078,-0.34639 -3.35596,-0.0261 -4.77474,0.90394c-1.41878,0.92886 -2.39152,2.35476 -2.73791,4.01555c-0.3452,1.65842 -0.02491,3.3548 0.90394,4.77359c0.92885,1.41879 2.35475,2.39154 4.01552,2.73675l-0.51128,2.44848l0,0zm1.81499,-2.92536c-0.09727,0 -0.19455,-0.00119 -0.29064,-0.00475c-1.53029,-0.07711 -2.93958,-0.74498 -3.96926,-1.88025c-1.02968,-1.13646 -1.55401,-2.60388 -1.47809,-4.13655l2.49591,0.12456c-0.04271,0.86361 0.25386,1.69282 0.83513,2.33341c0.5789,0.64059 1.37607,1.01783 2.23968,1.06053c0.86005,0.03915 1.69162,-0.25268 2.33221,-0.83395c0.64177,-0.58009 1.01782,-1.3749 1.06171,-2.23851c0.08897,-1.78298 -1.29066,-3.30616 -3.07362,-3.39513l0.12456,-2.49593c3.16022,0.15659 5.60394,2.85419 5.44735,6.01443c-0.07592,1.53149 -0.74498,2.9396 -1.88024,3.9681c-1.06408,0.96326 -2.41999,1.48403 -3.8447,1.48403l0,0z",
- "marker": "<ellipse fill=\"#000\" opacity=\"0.2\" ry=\"1.5\" rx=\"4\" cy=\"22.5\" cx=\"12\" /><path d=\"m11.999999,0a8.437501,8.344057 0 0 1 8.437501,8.344057c0,6.500779 -5.798864,6.280799 -8.437501,14.412461c-2.638637,-8.131662 -8.437501,-7.911683 -8.437501,-14.412461a8.437501,8.344057 0 0 1 8.437501,-8.344057z\" />"
+ "marker": "<ellipse fill=\"#000\" opacity=\"0.2\" ry=\"1.5\" rx=\"4\" cy=\"22.5\" cx=\"12\" /><path style='pointer-events: var(--or-icon-pointer-events, none);' d=\"m11.999999,0a8.437501,8.344057 0 0 1 8.437501,8.344057c0,6.500779 -5.798864,6.280799 -8.437501,14.412461c-2.638637,-8.131662 -8.437501,-7.911683 -8.437501,-14.412461a8.437501,8.344057 0 0 1 8.437501,-8.344057z\" />"
});
export default orIcons;
\ No newline at end of file
diff --git a/ui/component/or-map/src/index.ts b/ui/component/or-map/src/index.ts
index d40d08f5e..84af225d2 100644
--- a/ui/component/or-map/src/index.ts
+++ b/ui/component/or-map/src/index.ts
@@ -1,10 +1,10 @@
import openremote, {EventCallback, OREvent} from "@openremote/core";
import {FlattenedNodesObserver} from "@polymer/polymer/lib/utils/flattened-nodes-observer.js";
-import {css, customElement, html, LitElement, property, query, PropertyValues} from "lit-element";
+import {css, customElement, html, LitElement, property, query, PropertyValues, CSSResult} from "lit-element";
import {LngLat, LngLatBoundsLike, LngLatLike} from "mapbox-gl";
import {MapWidget} from "./mapwidget";
import {
- OrMapMarker, OrMapMarkerEvent, OrMapMarkerChangedEvent
+ OrMapMarker, OrMapMarkerEvent, OrMapMarkerChangedEvent, MarkerStyle
} from "./markers/or-map-marker";
import {getLngLat} from "./util";
@@ -25,16 +25,20 @@ export interface ViewSettings {
@customElement("or-map")
export class OrMap extends LitElement {
- public static styles = css`
+ public static styles = [
+ MarkerStyle,
+ css`
:host {
display: block;
overflow: hidden;
}
+
#map {
position: relative;
width: 100%;
height: 100%;
}
+
slot {
display: none;
}
@@ -46,48 +50,13 @@ export class OrMap extends LitElement {
.leaflet-marker-icon, .mapboxgl-marker {
pointer-events: none !important;
}
-
- /* Offset margin set by leaflet-marker-icon */
- .leaflet-marker-icon .or-map-marker {
- margin-left: 6px;
- margin-top: 6px;
- }
-
- .or-map-marker {
- position: absolute; /* This makes mapboxJS behave like mapboxGL */
- cursor: grab;
- }
-
- .or-map-marker.interactive {
- cursor: pointer;
- }
-
- .or-map-marker>*{
- pointer-events: visible;
- }
-
- .or-map-marker-default {
- position: relative;
- transform: var(--or-map-marker-transform, translate(-24px, -45px));
- --or-icon-width: var(--or-map-marker-width, 48px);
- --or-icon-height: var(--or-map-marker-height, 48px);
- --or-icon-fill-color: var(--or-map-marker-fill, #1D5632);
- }
-
- .or-map-marker-default .marker-icon {
- position: absolute;
- left: 50%;
- top: 0px;
- --or-icon-fill-color: var(--or-map-marker-icon-fill, #FFF);
- --or-icon-width: var(--or-map-marker-icon-width, 24px);
- --or-icon-height: var(--or-map-marker-icon-height, 24px);
- transform: var(--or-map-marker-icon-transform, translate(-50%, 5px));
- }
- `;
+ `];
@property({type: String})
public type: Type = Type.VECTOR;
+ protected _markerStyles: string[] = [];
+
@property({type: String, converter: {
fromAttribute(value: string | null, type?: String): LngLatLike | undefined {
if (!value) {
@@ -122,6 +91,7 @@ export class OrMap extends LitElement {
protected _map?: MapWidget;
protected _loaded: boolean = false;
protected _observer?: FlattenedNodesObserver;
+ protected _markers: OrMapMarker[] = [];
@query("#map")
protected _mapContainer?: HTMLElement;
@@ -134,6 +104,10 @@ export class OrMap extends LitElement {
this.addEventListener(OrMapMarkerEvent.CHANGED, this._onMarkerChangedEvent);
}
+ public get markers(): OrMapMarker[] {
+ return this._markers;
+ }
+
public disconnectedCallback() {
super.disconnectedCallback();
this._observer!.disconnect();
@@ -192,6 +166,36 @@ export class OrMap extends LitElement {
}
if (node instanceof OrMapMarker) {
+
+ this._markers.push(node);
+
+ // Add styles of marker class to the shadow root if not already added
+ const className = node.constructor.name;
+ if (this._markerStyles.indexOf(className) < 0) {
+ const styles = (node.constructor as any).styles;
+ let stylesArr: CSSResult[] = [];
+
+ if (styles) {
+ if (!Array.isArray(styles)) {
+ stylesArr.push(styles as CSSResult);
+ } else {
+ stylesArr = styles as CSSResult[];
+ }
+
+ stylesArr.forEach((styleItem) => {
+ const styleElem = document.createElement("style");
+ styleElem.textContent = styleItem.toString();
+ if (this._mapContainer!.children.length > 0) {
+ this._mapContainer!.insertBefore(styleElem, this._mapContainer!.children[0]);
+ } else {
+ this._mapContainer!.appendChild(styleElem);
+ }
+ })
+ }
+
+ this._markerStyles.push(className);
+ }
+
this._map.addMarker(node);
}
});
@@ -204,6 +208,10 @@ export class OrMap extends LitElement {
}
if (node instanceof OrMapMarker) {
+ const i = this._markers.indexOf(node);
+ if (i >= 0) {
+ this._markers.splice(i, 1);
+ }
this._map.removeMarker(node);
}
});
diff --git a/ui/component/or-map/src/mapwidget.ts b/ui/component/or-map/src/mapwidget.ts
index 9976e816f..183d0b91f 100644
--- a/ui/component/or-map/src/mapwidget.ts
+++ b/ui/component/or-map/src/mapwidget.ts
@@ -77,8 +77,6 @@ export class MapWidget {
return;
}
- this._loaded = true;
-
if (this._type === Type.RASTER) {
if (!MapWidget._mapboxJsStyle) {
@@ -171,6 +169,8 @@ export class MapWidget {
this._mapGl = new map.Map(options);
}
+
+ this._loaded = true;
}
public addMarker(marker: OrMapMarker) {
@@ -182,17 +182,15 @@ export class MapWidget {
}
public onMarkerChanged(marker: OrMapMarker, prop: string) {
+ if (!this._loaded) {
+ return;
+ }
+
switch (prop) {
- case "icon":
- this._updateMarkerElement(marker, true);
- break;
case "lat":
case "lng":
this._updateMarkerPosition(marker);
break;
- case "visible":
- this._updateMarkerVisibility(marker);
- break;
}
}
@@ -213,68 +211,44 @@ export class MapWidget {
}
}
- protected _updateMarkerVisibility(marker: OrMapMarker) {
- switch (this._type) {
- case Type.RASTER:
- const m: MarkerJS | undefined = this._markersJs.get(marker);
- if (m && m.getElement()) {
- if (marker.visible) {
- m.getElement().removeAttribute("hidden");
- } else {
- m.getElement().setAttribute("hidden", "true");
- }
- }
- break;
- case Type.VECTOR:
- const mGl: MarkerGL | undefined = this._markersGl.get(marker);
- if (mGl) {
- if (marker.visible) {
- mGl.getElement().removeAttribute("hidden");
- } else {
- mGl.getElement().setAttribute("hidden", "true");
- }
- }
- break;
- }
- }
-
protected _updateMarkerElement(marker: OrMapMarker, doAdd: boolean) {
switch (this._type) {
case Type.RASTER:
let m = this._markersJs.get(marker);
if (m) {
+ this._removeMarkerClickHandler(marker, marker.markerContainer as HTMLElement);
+ marker._actualMarkerElement = undefined;
m.removeFrom(this._mapJs!);
this._markersJs.delete(marker);
- this._removeMarkerClickHandler(marker, m.getElement());
}
if (doAdd) {
- const elem = marker.createMarkerElement();
+ const elem = marker._createMarkerElement();
if (elem) {
const icon = L.divIcon({html: elem.outerHTML, className: "or-marker-raster"});
m = L.marker([marker.lat, marker.lng], {icon: icon, clickable: marker.interactive});
m.addTo(this._mapJs!);
-
+ marker._actualMarkerElement = m.getElement() ? m.getElement().firstElementChild as HTMLDivElement : undefined;
if (marker.interactive) {
- this._addMarkerClickHandler(marker, m.getElement());
+ this._addMarkerClickHandler(marker, marker.markerContainer as HTMLElement);
}
this._markersJs.set(marker, m);
- this._updateMarkerVisibility(marker);
}
}
break;
case Type.VECTOR:
let mGl = this._markersGl.get(marker);
if (mGl) {
+ marker._actualMarkerElement = undefined;
+ this._removeMarkerClickHandler(marker, mGl.getElement());
mGl.remove();
this._markersGl.delete(marker);
- this._removeMarkerClickHandler(marker, mGl.getElement());
}
if (doAdd) {
- const elem = marker.createMarkerElement();
+ const elem = marker._createMarkerElement();
if (elem) {
mGl = new MarkerGL({
@@ -285,7 +259,8 @@ export class MapWidget {
.addTo(this._mapGl!);
this._markersGl.set(marker, mGl);
- this._updateMarkerVisibility(marker);
+
+ marker._actualMarkerElement = mGl.getElement() as HTMLDivElement;
if (marker.interactive) {
this._addMarkerClickHandler(marker, mGl.getElement());
@@ -297,17 +272,19 @@ export class MapWidget {
}
protected _addMarkerClickHandler(marker: OrMapMarker, elem: HTMLElement) {
- const handler = (ev: MouseEvent) => {
- ev.stopPropagation();
- marker._onClick(ev);
- };
- this._clickHandlers.set(marker, handler);
- elem.addEventListener("click", handler);
+ if (elem) {
+ const handler = (ev: MouseEvent) => {
+ ev.stopPropagation();
+ marker._onClick(ev);
+ };
+ this._clickHandlers.set(marker, handler);
+ elem.addEventListener("click", handler);
+ }
}
protected _removeMarkerClickHandler(marker: OrMapMarker, elem: HTMLElement) {
const handler = this._clickHandlers.get(marker);
- if (handler) {
+ if (handler && elem) {
elem.removeEventListener("click", handler);
this._clickHandlers.delete(marker);
}
diff --git a/ui/component/or-map/src/markers/or-map-marker-asset.ts b/ui/component/or-map/src/markers/or-map-marker-asset.ts
index 64cfc3384..485a59e6a 100644
--- a/ui/component/or-map/src/markers/or-map-marker-asset.ts
+++ b/ui/component/or-map/src/markers/or-map-marker-asset.ts
@@ -10,6 +10,8 @@ export class OrMapMarkerAsset extends OrMapMarker {
@property({type: String})
public asset?: string;
+ public assetTypeAsIcon: boolean = true;
+
constructor() {
super();
}
@@ -61,11 +63,13 @@ export class OrMapMarkerAsset extends OrMapMarker {
// Model d.ts is clearly not perfect need to sort out Jackson annotations
this.lat = (location.coordinates as any)[1] || 0;
this.lng = (location.coordinates as any)[0] || 0;
- if (asset.type) {
- // TODO: Get hold of icon from asset type
- this.icon = "office-building";
- } else {
- this.icon = undefined;
+ if (this.assetTypeAsIcon) {
+ if (asset.type) {
+ // TODO: Get hold of icon from asset type
+ this.icon = "office-building";
+ } else {
+ this.icon = undefined;
+ }
}
this.visible = true;
}).catch(() => {
diff --git a/ui/component/or-map/src/markers/or-map-marker.ts b/ui/component/or-map/src/markers/or-map-marker.ts
index 64656a99f..7e48af8e0 100644
--- a/ui/component/or-map/src/markers/or-map-marker.ts
+++ b/ui/component/or-map/src/markers/or-map-marker.ts
@@ -1,5 +1,6 @@
import {css, customElement, LitElement, property, PropertyValues, query, html} from "lit-element";
-import {html as litHtml, render} from "lit-html";
+import {Type} from "../index";
+import {Marker as MarkerJS} from "mapbox.js";
export enum OrMapMarkerEvent {
CLICKED = "or-map-marker-clicked",
@@ -48,6 +49,41 @@ export interface MarkerChangedEventDetail extends MarkerEventDetail {
property: string;
}
+export const MarkerStyle = css`
+ .or-map-marker {
+ position: absolute; /* This makes mapboxJS behave like mapboxGL */
+ }
+
+ .marker-container {
+ position: relative;
+ transform: var(--or-map-marker-transform, translate(-24px, -45px));
+ cursor: pointer;
+ --or-icon-width: var(--or-map-marker-width, 48px);
+ --or-icon-height: var(--or-map-marker-height, 48px);
+ --or-icon-fill-color: var(--or-map-marker-fill, #1D5632);
+ }
+
+ .or-map-marker.interactive .marker-container {
+ pointer-events: all;
+ }
+
+ .or-map-marker-default.interactive .marker-container {
+ pointer-events: none;
+ --or-icon-pointer-events: visible;
+ }
+
+ .or-map-marker .marker-icon {
+ position: absolute;
+ left: 50%;
+ top: 50%;
+ z-index: 1000;
+ --or-icon-fill-color: var(--or-map-marker-icon-fill, #FFF);
+ --or-icon-width: var(--or-map-marker-icon-width, 24px);
+ --or-icon-height: var(--or-map-marker-icon-height, 24px);
+ transform: var(--or-map-marker-icon-transform, translate(-50%, -19px));
+ }
+`;
+
/**
* Base class for all map markers.
*
@@ -56,22 +92,10 @@ export interface MarkerChangedEventDetail extends MarkerEventDetail {
@customElement("or-map-marker")
export class OrMapMarker extends LitElement {
- public static styles = css`
- :host {
- display: none;
- }
-
- slot {
- display: none;
- }
- `;
-
protected static _defaultTemplate = (icon: string | undefined) => `
- <div class="or-map-marker-default">
- <or-icon icon="or:marker"></or-icon>
- <or-icon class="marker-icon" icon="${icon || ""}"></or-icon>
- </div>
- `;
+ <or-icon icon="or:marker"></or-icon>
+ <or-icon class="marker-icon" icon="${icon || ""}"></or-icon>
+ `
@property({type: Number})
public lat: number = 0;
@@ -88,46 +112,138 @@ export class OrMapMarker extends LitElement {
@property({type: Boolean})
public interactive: boolean = true;
+ @property({type: Boolean})
+ active: boolean = false;
+
+ // This is the actual map marker element not the same element as returned from createMarkerElement when using raster map
+ public _actualMarkerElement?: HTMLDivElement;
+
@query("slot")
protected _slot?: HTMLSlotElement;
+ public get markerContainer(): HTMLDivElement | undefined {
+ if (this._actualMarkerElement) {
+ return this._actualMarkerElement.firstElementChild as HTMLDivElement;
+ }
+ }
+
public _onClick(e: MouseEvent) {
this.dispatchEvent(new OrMapMarkerClickedEvent(this));
}
+ public _createMarkerElement(): HTMLDivElement {
+ const markerElem = document.createElement("div");
+ const markerContainerElem = document.createElement("div");
+ markerElem.appendChild(markerContainerElem);
+ this.addMarkerClassNames(markerElem);
+ this.addMarkerContainerClassNames(markerContainerElem);
+ let content = this.createMarkerContent();
+ if (!content) {
+ // Append default marker
+ markerElem.classList.add("or-map-marker-default");
+ content = this.createDefaultMarkerContent();
+ }
+ markerContainerElem.appendChild(content);
+ this.updateInteractive(markerElem);
+ this.updateVisibility(markerElem);
+ this.updateActive(markerElem);
+ return markerElem;
+ }
+
/**
- * Override in sub types to return custom marker HTML
+ * Override in sub types to customise the look of the marker. If undefined returned then a default marker will
+ * be used instead.
*/
- public createMarkerElement(): HTMLElement {
-
- const ele = document.createElement("div");
- this.setMarkerElementClassNames(ele);
+ public createMarkerContent(): HTMLElement | undefined {
// Append child elements
let hasChildren = false;
-
+ let container = document.createElement("div");
+
if (this._slot) {
this._slot.assignedNodes({flatten: true}).forEach((child) => {
if (child instanceof HTMLElement) {
- ele.appendChild(child.cloneNode(true));
+ container.appendChild(child.cloneNode(true));
hasChildren = true;
}
});
}
if (!hasChildren) {
- // Append default marker
- this.addDefaultMarkerContent(ele);
+ return;
}
- return ele;
+ return container;
}
protected shouldUpdate(_changedProperties: PropertyValues): boolean {
+ if (_changedProperties.has("icon")) {
+ this.refreshMarkerContent();
+ }
+
+ if (_changedProperties.has("visible")) {
+ this.updateVisibility(this._actualMarkerElement);
+ }
+
+ if (_changedProperties.has("interactive")) {
+ this.updateInteractive(this._actualMarkerElement);
+ }
+
+ if (_changedProperties.has("active")) {
+ this.updateActive(this._actualMarkerElement);
+ }
+
_changedProperties.forEach((oldValue, prop) => this._raisePropertyChange(prop as string));
return false;
}
+ protected updateVisibility(container?: HTMLDivElement) {
+ if (container) {
+ if (this.visible) {
+ container.removeAttribute("hidden");
+ } else {
+ container.setAttribute("hidden", "true");
+ }
+ }
+ }
+
+ protected updateActive(container?: HTMLDivElement) {
+ if (container) {
+ if (this.active) {
+ container.classList.add("active");
+ } else {
+ container.classList.remove("active");
+ }
+ }
+ }
+
+ protected updateInteractive(container?: HTMLDivElement) {
+ if (container) {
+ if (this.interactive) {
+ container.classList.add("interactive");
+ } else {
+ container.classList.remove("interactive");
+ }
+ }
+ }
+
+ protected refreshMarkerContent() {
+ if (this.markerContainer) {
+ let content = this.createMarkerContent();
+ if (!content) {
+ // Append default marker
+ this._actualMarkerElement!.classList.add("or-map-marker-default");
+ content = this.createDefaultMarkerContent();
+ } else {
+ this._actualMarkerElement!.classList.remove("or-map-marker-default");
+ }
+ if (this.markerContainer.children.length > 0) {
+ this.markerContainer.removeChild(this.markerContainer.children[0]);
+ }
+ this.markerContainer.appendChild(content);
+ }
+ }
+
protected render() {
return html`
<slot></slot>
@@ -138,16 +254,17 @@ export class OrMapMarker extends LitElement {
this.dispatchEvent(new OrMapMarkerChangedEvent(this, prop));
}
- protected setMarkerElementClassNames(markerElement: HTMLElement) {
- let classes = "or-map-marker ";
- if (this.interactive) {
- classes += "interactive ";
- }
- const className = (classes + this.className).trim();
- markerElement.className = className;
+ protected addMarkerClassNames(markerElement: HTMLElement) {
+ markerElement.classList.add("or-map-marker");
+ }
+
+ protected addMarkerContainerClassNames(markerContainer: HTMLElement) {
+ markerContainer.classList.add("marker-container");
}
- protected addDefaultMarkerContent(markerElement: HTMLElement) {
- markerElement.innerHTML = OrMapMarker._defaultTemplate(this.icon);
+ protected createDefaultMarkerContent(): HTMLElement {
+ const div = document.createElement("div");
+ div.innerHTML = OrMapMarker._defaultTemplate(this.icon);
+ return div;
}
}
diff --git a/ui/demo/demo-or-map/index.html b/ui/demo/demo-or-map/index.html
index 5064661ba..b8c922b56 100644
--- a/ui/demo/demo-or-map/index.html
+++ b/ui/demo/demo-or-map/index.html
@@ -25,7 +25,7 @@
--or-map-marker-fill: blue;
--or-map-marker-icon-width: 38px;
--or-map-marker-icon-height: 38px;
- --or-map-marker-icon-transform: translate(-50%, 9px);
+ --or-map-marker-icon-transform: translate(-50%, -31px);
--or-map-marker-icon-fill: red;
}
</style>
@@ -37,10 +37,10 @@
<demo-snippet>
<template>
<or-map type="RASTER" center="5.454250, 51.445990" style="height: 500px; width: 100%;">
- <or-map-marker lat="51.445990" lng="5.454250" click-action="showCard" class="marker" icon="or:logo-plain"></or-map-marker>
+ <or-map-marker lat="51.445990" lng="5.454250" class="marker" icon="or:logo-plain"></or-map-marker>
</or-map>
<or-map id="vector" center="5.454250, 51.445990" style="height: 500px; width: 100%;">
- <or-map-marker lat="51.445990" lng="5.454250" click-action="showCard" class="marker" icon="or:logo-plain"></or-map-marker>
+ <or-map-marker lat="51.445990" lng="5.454250" class="marker" icon="or:logo-plain"></or-map-marker>
</or-map>
</template>
</demo-snippet>