Skip to content

armgit / core

Public Imported from GitHub
0

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

UI updates: or-map

Rich Turnerad5440e22ac9
diff --git a/ui/component/model/build.gradle b/ui/component/model/build.gradle
index 9b46e7407..f5b7bd21a 100644
--- a/ui/component/model/build.gradle
+++ b/ui/component/model/build.gradle
@@ -25,7 +25,7 @@ generateTypeScript {
]
excludeClassPatterns = [
"org.openremote.model.http.*Constraint**",
- "org.openremote.model.event.**",
+ "org.openremote.model.event.shared.*Filter**",
"org.openremote.model.util.**",
"org.openremote.model.flow.**",
"org.openremote.model.interop.**",
diff --git a/ui/component/or-map/@types/mapbox.js.d.ts b/ui/component/or-map/@types/mapbox.js.d.ts
index 85e95e141..43d82faef 100644
--- a/ui/component/or-map/@types/mapbox.js.d.ts
+++ b/ui/component/or-map/@types/mapbox.js.d.ts
@@ -1,17 +1,18 @@
declare module "mapbox.js" {
- import {LngLatBoundsLike, LngLatLike} from "mapbox-gl";
- import {DivIconOptions} from "mapbox.js";
export default L;
type Point2d = [number, number];
type Point3d = [number, number, number];
+ type LatLngLike = Point2d | Point3d | LatLng;
+ type LatLngBoundsLike = [LatLngLike, LatLngLike] | LatLngBounds;
+
export interface MapOptions {
zoom: number,
boxZoom?: boolean;
- center?: LngLatLike;
- maxBounds?: LngLatBoundsLike;
+ center?: LatLngLike;
+ maxBounds?: LatLngBoundsLike;
maxZoom?: number;
minZoom?: number;
}
@@ -36,7 +37,7 @@ declare module "mapbox.js" {
}
export interface MarkerOptions {
- icon?: L.Icon;
+ icon?: Icon;
keyboard?: boolean;
title?: string;
alt?: string;
@@ -44,6 +45,40 @@ declare module "mapbox.js" {
opacity?: number;
}
+ export class LatLng {
+ private constructor();
+
+ lat: number;
+ lng: number;
+ alt?: number;
+ }
+
+ export class LatLngBounds {
+ private constructor();
+ getSouthEast(): LatLng;
+ getNorthWest(): LatLng;
+ getNorthEast(): LatLng;
+ getSouthWest(): LatLng;
+ getCenter(): LatLng;
+ }
+
+ export class Icon {
+
+ }
+
+ export class DivIcon extends Icon {
+
+ }
+
+ export class Marker extends Layer {
+ setLatLng(latLng: LatLngLike): this;
+ }
+
+ class Layer {
+ addTo(addTo: L.mapbox.map): this;
+ removeFrom(removeFrom: L.mapbox.map): this;
+ }
+
export namespace L {
namespace mapbox {
export class map {
@@ -57,31 +92,12 @@ declare module "mapbox.js" {
function latLng(lat: number, lng: number, alt?: number): LatLng;
- function latLng(pos: Point2d | Point3d | LatLng): LatLng;
+ function latLng(pos: LatLngLike): LatLng;
- function marker(pos: Point2d | Point3d | LatLng, options: MarkerOptions): Marker;
-
- export class LatLng {
- lat: number;
- lng: number;
- alt?: number;
- }
-
- export class Icon {
-
- }
+ function latLngBounds(corner1: LatLngLike, corner2: LatLngLike): LatLngBounds;
- export class DivIcon extends Icon {
+ function latLngBounds(bounds: LatLngBoundsLike): LatLngBounds;
- }
-
- export class Marker extends Layer {
-
- }
-
- class Layer {
- addTo(addTo: L.mapbox.map): this;
- removeFrom(removeFrom: L.mapbox.map): this;
- }
+ function marker(pos: LatLngLike, options: MarkerOptions): Marker;
}
}
\ No newline at end of file
diff --git a/ui/component/or-map/markers/or-map-marker-asset.ts b/ui/component/or-map/markers/or-map-marker-asset.ts
new file mode 100644
index 000000000..16177b9b5
--- /dev/null
+++ b/ui/component/or-map/markers/or-map-marker-asset.ts
@@ -0,0 +1,77 @@
+import {html} from "@polymer/polymer";
+import {customElement, property, observe} from "@polymer/decorators";
+import {OrMapMarker} from "./or-map-marker";
+import rest from "@openremote/rest";
+import {Asset, AssetAttribute, GeoJSONPoint} from "@openremote/model";
+import {GenericAxiosResponse} from "axios";
+
+/**
+ * `or-map-marker-asset`
+ * Asset linked map marker
+ */
+
+@customElement('or-map-marker-asset')
+export class OrMapMarkerAsset extends OrMapMarker {
+
+ @property({type: String, observer: "_assetChanged"})
+ asset?: string;
+
+ constructor() {
+ super();
+ }
+
+ _createMarkerElement(): HTMLElement {
+ return document.createElement("div");
+ }
+
+ refreshMarker() {
+ this.getAsset().then((asset) => {
+ const attrs: Array<AssetAttribute> = <Array<AssetAttribute>>asset.attributes;
+ const attr: AssetAttribute | undefined = attrs.find(attr => {
+ return attr.name === "location"
+ });
+
+ if (!attr) {
+ this.visible = false;
+ return;
+ }
+
+ const location: GeoJSONPoint | null = attr.valueAsObject as GeoJSONPoint;
+
+ if (!location) {
+ this.visible = false;
+ return;
+ }
+
+ this._createAssetMarker();
+ this.lat = location.y || 0;
+ this.lng = location.x || 0;
+ this.visible = true;
+ }).catch(() => {
+ this.visible = false;
+ });
+ }
+
+ async getAsset(): Promise<Asset> {
+ const response: GenericAxiosResponse<Asset> = await rest.api.AssetResource.get(this.asset!);
+ return response.data;
+ }
+
+ _assetChanged() {
+ this.visible = false;
+
+ if (this.asset) {
+ this.refreshMarker();
+ }
+ }
+
+ _elementChanged() {
+ this._updateMarker();
+ }
+
+ private _createAssetMarker() {
+ this._ele = document.createElement("div");
+ this._ele.className = "marker";
+ }
+}
+
diff --git a/ui/component/or-map/markers/or-map-marker-basic.ts b/ui/component/or-map/markers/or-map-marker-basic.ts
deleted file mode 100644
index d23b72b82..000000000
--- a/ui/component/or-map/markers/or-map-marker-basic.ts
+++ /dev/null
@@ -1,40 +0,0 @@
-import {html} from "@polymer/polymer";
-import {customElement, property} from "@polymer/decorators";
-import {OrMapMarker} from "./or-map-marker";
-
-
-/**
- * `or-map-marker-basic`
- * Basic map marker
- */
-
-@customElement('or-map-marker-basic')
-export class OrMapMarkerBasic extends OrMapMarker {
- @property({type: Number})
- lat: number = 0;
-
- @property({type: Number})
- lng: number = 0;
-
- @property({type: String})
- icon: string = '';
-
- ready() {
- super.ready();
- this._lat = this.lat;
- this._lng = this.lng;
- const div = document.createElement('div');
- div.innerHTML = `<style>
- .marker {
- position: absolute;
- width: 32px;
- height: 32px;
- background-size: 32px;
- }
- </style>
- <div style="background-image: url('${this.icon}');" class="marker"></div>`;
- this._html = div;
- }
-
-}
-
diff --git a/ui/component/or-map/markers/or-map-marker.ts b/ui/component/or-map/markers/or-map-marker.ts
index 891ab2081..4dc1a17c5 100644
--- a/ui/component/or-map/markers/or-map-marker.ts
+++ b/ui/component/or-map/markers/or-map-marker.ts
@@ -1,22 +1,112 @@
import {html, PolymerElement} from "@polymer/polymer";
+import {property, query, observe, customElement} from "@polymer/decorators";
+import {MapWidget} from "../src/mapwidget";
/**
* Base class for all map markers
*/
-export abstract class OrMapMarker extends PolymerElement {
- protected _lat: number = 0;
- protected _lng: number = 0;
- protected _html?: HTMLElement = undefined;
+@customElement("or-map-marker")
+export class OrMapMarker extends PolymerElement {
+ protected _added: boolean = false;
+ protected _attached: boolean = false;
- get latitude(): number {
- return this._lat;
+ @property({type: Number})
+ lat: number = 0;
+
+ @property({type: Number})
+ lng: number = 0;
+
+ @property({type: Boolean})
+ visible: boolean = true;
+
+ @property({type: Object})
+ _ele!: HTMLElement;
+
+ @property({type: Object})
+ map?: MapWidget;
+
+ @query("slot")
+ _slot!: HTMLSlotElement;
+
+ static get template() {
+ return html`
+ <style>
+ :host {
+ display: block;
+ overflow: hidden;
+ }
+ #map {
+ position: relative;
+ width: 100%;
+ height: 100%;
+ }
+ slot {
+ display: none;
+ }
+ </style>
+ <div id="map"></div>
+ <slot></slot>
+ `;
+ }
+
+ connectedCallback() {
+ super.connectedCallback();
+ this._attached = true;
+ }
+
+ disconnectedCallback() {
+ super.disconnectedCallback();
+ this._removeMarker();
+ this._attached = false;
}
- get longitude(): number {
- return this._lng;
+ @observe("visible", "lat", "lng", "map")
+ _updateMarker() {
+ if (!this._attached || !this.map) return;
+
+ if (!this._ele) {
+ this._ele = this._createMarkerElement();
+ }
+
+ if (!this._ele) {
+ return;
+ }
+
+ if (!this.visible) {
+ this._removeMarker();
+ } else {
+ this._addMarker();
+ this.map!.updateMarkerPosition(this);
+ }
}
- get html(): HTMLElement | undefined {
- return this._html;
+ _addMarker() {
+ if (!this._added) {
+ this.map!.addMarker(this);
+ this._added = true;
+ }
+ }
+
+ _removeMarker() {
+ if (this._added) {
+ this.map!.removeMarker(this);
+ this._added = false;
+ }
+ }
+
+ _createMarkerElement(): HTMLElement {
+ let children = this._slot.assignedNodes({flatten:true});
+ let len = children.length;
+ let className = ("or-map-marker " + this.className).trim();
+ let ele = document.createElement("div");
+ ele.className = className;
+
+ if (len > 0) {
+ // if more than 1 ele put inside wrapper
+ for (var i=0; i<len; ++i) {
+ ele.appendChild(children[i]);
+ }
+ }
+ return ele;
}
}
\ 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 90c7f492f..24845f088 100644
--- a/ui/component/or-map/src/index.ts
+++ b/ui/component/or-map/src/index.ts
@@ -38,6 +38,13 @@ export class OrMap extends PolymerElement {
slot {
display: none;
}
+ .marker {
+ background-image: url('http://pngimg.com/uploads/smiley/smiley_PNG36233.png');
+ position: absolute;
+ width: 32px;
+ height: 32px;
+ background-size: 32px;
+ }
</style>
<div id="map"></div>
<slot id="markers-slot"></slot>
@@ -58,7 +65,7 @@ export class OrMap extends PolymerElement {
}
if(node instanceof OrMapMarker) {
- this._map.addMarker(node);
+ node.map = this._map;
}
});
}
@@ -69,8 +76,9 @@ export class OrMap extends PolymerElement {
return;
}
+
if(node instanceof OrMapMarker) {
- this._map.removeMarker(node);
+ node.map = undefined;
}
});
}
diff --git a/ui/component/or-map/src/mapwidget.ts b/ui/component/or-map/src/mapwidget.ts
index 1a23aa7ad..025254605 100644
--- a/ui/component/or-map/src/mapwidget.ts
+++ b/ui/component/or-map/src/mapwidget.ts
@@ -1,4 +1,4 @@
-import L, {MapOptions} from "mapbox.js";
+import L, {MapOptions, Marker} from "mapbox.js";
import {Type} from "./index";
import rest from "@openremote/rest";
import openremote from "@openremote/core";
@@ -14,7 +14,7 @@ export class MapWidget {
protected _styleParent: Node;
protected _mapContainer: HTMLElement;
protected _loaded: boolean = false;
- protected _markersJs?: Map<OrMapMarker, L.Marker>;
+ protected _markersJs?: Map<OrMapMarker, Marker>;
protected _markersGl?: Map<OrMapMarker, MarkerGL>;
constructor(type: Type, styleParent: Node, mapContainer: HTMLElement) {
@@ -24,7 +24,7 @@ export class MapWidget {
switch(type) {
case Type.RASTER:
- this._markersJs = new Map<OrMapMarker, L.Marker>();
+ this._markersJs = new Map<OrMapMarker, Marker>();
break;
case Type.VECTOR:
this._markersGl = new Map<OrMapMarker, MarkerGL>();
@@ -107,11 +107,11 @@ export class MapWidget {
}
addMarker(marker: OrMapMarker) {
- if (marker.html) {
+ if (marker._ele) {
switch (this._type) {
case Type.RASTER:
- let icon = L.divIcon({className: 'map-marker', html: marker.html.innerHTML});
- let m: L.Marker = L.marker([marker.latitude, marker.longitude], {icon: icon});
+ let icon = L.divIcon({className: 'map-marker', html: marker._ele.outerHTML});
+ let m: Marker = L.marker([marker.lat, marker.lng], {icon: icon});
m.addTo(this._mapJs!);
this._markersJs!.set(marker, m);
break;
@@ -121,7 +121,6 @@ export class MapWidget {
.setLngLat([marker.latitude, marker.longitude])
.addTo(this._mapGl);
break;
-
}
}
}
@@ -129,7 +128,7 @@ export class MapWidget {
removeMarker(marker: OrMapMarker) {
switch (this._type) {
case Type.RASTER:
- let m: L.Marker | undefined = this._markersJs!.get(marker);
+ let m: Marker | undefined = this._markersJs!.get(marker);
if (m) {
this._markersJs!.delete(marker);
m.removeFrom(this._mapJs!);
@@ -137,7 +136,19 @@ export class MapWidget {
break;
case Type.VECTOR:
break;
+ }
+ }
+ updateMarkerPosition(marker: OrMapMarker) {
+ switch (this._type) {
+ case Type.RASTER:
+ let m: Marker | undefined = this._markersJs!.get(marker);
+ if (m) {
+ m.setLatLng([marker.lat, marker.lng]);
+ }
+ break;
+ case Type.VECTOR:
+ break;
}
}
}
\ No newline at end of file
diff --git a/ui/component/rest/build.gradle b/ui/component/rest/build.gradle
index c7a1c29b0..a6e53bf3d 100644
--- a/ui/component/rest/build.gradle
+++ b/ui/component/rest/build.gradle
@@ -49,7 +49,8 @@ generateTypeScript {
clean {
doLast {
- delete "dist"
+ def dir = new File("${projectDir}/dist")
+ dir.deleteDir()
}
}
diff --git a/ui/component/rest/src/index.ts b/ui/component/rest/src/index.ts
index 933d7a066..679ef1ca0 100644
--- a/ui/component/rest/src/index.ts
+++ b/ui/component/rest/src/index.ts
@@ -3,7 +3,7 @@ import axios, {AxiosInstance} from "axios";
import {ApiClient} from "../build/restclient";
import {Promise} from "es6-promise";
-export class RestApi {
+class RestApi {
get api() {
return this._client;