100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
UI updates: Map component WIP
Rich Turner6f3b0d95710e
diff --git a/ui/component/or-map/@types/mapbox.js.d.ts b/ui/component/or-map/@types/mapbox.js.d.tsindex f2842e489..85e95e141 100644--- a/ui/component/or-map/@types/mapbox.js.d.ts+++ b/ui/component/or-map/@types/mapbox.js.d.ts@@ -1,22 +1,87 @@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];++ export interface MapOptions {+ zoom: number,+ boxZoom?: boolean;+ center?: LngLatLike;+ maxBounds?: LngLatBoundsLike;+ maxZoom?: number;+ minZoom?: number;+ }++ export interface IconOptions {+ iconUrl?: string,+ iconRetinaUrl?: string,+ iconSize?: Point2d,+ iconAnchor?: Point2d,+ popupAnchor?: Point2d,+ tooltipAnchor?: Point2d,+ shadowUrl?: string,+ shadowRetinaUrl?: string,+ shadowSize?: Point2d,+ shadowAnchor?: Point2d,+ className?: string+ }++ export interface DivIconOptions extends IconOptions {+ html?: string,+ bgPos?: Point2d+ }++ export interface MarkerOptions {+ icon?: L.Icon;+ keyboard?: boolean;+ title?: string;+ alt?: string;+ zIndexOffset?: number;+ opacity?: number;+ }+export namespace L {- export namespace mapbox {+ namespace mapbox {export class map {constructor(element: string | Element, tilejson: object, options?: MapOptions);}+ }- export interface MapOptions {- zoom: number,- boxZoom?: boolean;- center?: LngLatLike;- maxBounds?: LngLatBoundsLike;- maxZoom?: number;- minZoom?: number;- }+ function icon(options: IconOptions): Icon;++ function divIcon(options: DivIconOptions): DivIcon;++ function latLng(lat: number, lng: number, alt?: number): LatLng;++ function latLng(pos: Point2d | Point3d | LatLng): LatLng;++ function marker(pos: Point2d | Point3d | LatLng, options: MarkerOptions): Marker;++ export class LatLng {+ lat: number;+ lng: number;+ alt?: number;+ }++ export class Icon {++ }++ export class DivIcon extends Icon {++ }++ export class Marker extends Layer {++ }++ class Layer {+ addTo(addTo: L.mapbox.map): this;+ removeFrom(removeFrom: L.mapbox.map): this;}}}\ No newline at end of filediff --git a/ui/component/or-map/markers/or-map-marker-basic.ts b/ui/component/or-map/markers/or-map-marker-basic.tsindex 038b20986..d23b72b82 100644--- a/ui/component/or-map/markers/or-map-marker-basic.ts+++ b/ui/component/or-map/markers/or-map-marker-basic.ts@@ -4,12 +4,8 @@ import {OrMapMarker} from "./or-map-marker";/**- * `or-map-marker`- * Displays marker on OpenRemote Map- *- * @customElement- * @polymer- * @demo demo/index.html+ * `or-map-marker-basic`+ * Basic map marker*/@customElement('or-map-marker-basic')diff --git a/ui/component/or-map/markers/or-map-marker.ts b/ui/component/or-map/markers/or-map-marker.tsindex 9bb51041f..891ab2081 100644--- a/ui/component/or-map/markers/or-map-marker.ts+++ b/ui/component/or-map/markers/or-map-marker.ts@@ -1,12 +1,7 @@import {html, PolymerElement} from "@polymer/polymer";/**- * `or-map-marker`- * Displays marker on OpenRemote Map- *- * @customElement- * @polymer- * @demo demo/index.html+ * Base class for all map markers*/export abstract class OrMapMarker extends PolymerElement {protected _lat: number = 0;diff --git a/ui/component/or-map/src/index.ts b/ui/component/or-map/src/index.tsindex db88e5e8b..90c7f492f 100644--- a/ui/component/or-map/src/index.ts+++ b/ui/component/or-map/src/index.ts@@ -2,10 +2,9 @@ import openremote, {EventCallback, OREvent} from "@openremote/core";import {html, PolymerElement} from "@polymer/polymer";import { FlattenedNodesObserver } from '@polymer/polymer/lib/utils/flattened-nodes-observer.js';import {customElement, property} from '@polymer/decorators';-import rest from "@openremote/rest";import {OrMapMarker} from "../markers/or-map-marker";-import {Map, MapboxOptions, Style as MapboxStyle} from "mapbox-gl";-import L from "mapbox.js";++import {MapWidget} from "./mapwidget";export enum Type {VECTOR = "VECTOR",@@ -20,9 +19,7 @@ export enum Type {@customElement('or-map')export class OrMap extends PolymerElement {protected _initCallback?: EventCallback;- protected _map?: any;- protected static _mapboxGlStyle?: any;- protected static _mapboxJsStyle?: any;+ protected _map?: MapWidget;protected _loaded: boolean = false;protected _observer?: FlattenedNodesObserver;@@ -56,21 +53,26 @@ export class OrMap extends PolymerElement {_processNewMarkers(nodes : Element[]) {nodes.forEach((node) => {- // node tagName should include marker to pass this check+ if (!this._map) {+ return;+ }+if(node instanceof OrMapMarker) {- const marker = <OrMapMarker>node;- if (marker.html) {- // @ts-ignore- let leafletMarket = L.divIcon({className: 'map-marker', html: marker.html.innerHTML});- // @ts-ignore- L.marker([marker.latitude, marker.longitude], {icon: leafletMarket}).addTo(this._map);- }+ this._map.addMarker(node);}});}_processRemovedMarkers(nodes : Element[]) {+ nodes.forEach((node) => {+ if (!this._map) {+ return;+ }+ if(node instanceof OrMapMarker) {+ this._map.removeMarker(node);+ }+ });}disconnectedCallback() {@@ -94,7 +96,7 @@ export class OrMap extends PolymerElement {}}- async loadMap() {+ loadMap() {if (this._loaded) {return;@@ -102,83 +104,18 @@ export class OrMap extends PolymerElement {if (this.shadowRoot) {const mapElement: HTMLElement = this.shadowRoot.getElementById('map')!;- if (this.type === Type.RASTER) {-- if (!OrMap._mapboxJsStyle) {- // @ts-ignore- OrMap._mapboxJsStyle = await import("mapbox.js/theme/style.css");- }-- // Add style to shadow root- var style = document.createElement('style');- style.id = "mapboxJsStyle";- style.textContent = OrMap._mapboxJsStyle.default.toString();- this.shadowRoot.appendChild(style);-- const settingsResponse = await rest.api.MapResource.getSettingsJs();- const settings = <any> settingsResponse.data;- const options: L.mapbox.MapOptions = {- zoom: <number>settings.zoom+1, // JS zoom is out by one compared to GL- minZoom: settings.minZoom || 0,- maxZoom: settings.maxZoom || 22,- boxZoom: settings.boxZoom || false,- };- if (settings.bounds) {- let b = settings.bounds;- options.maxBounds = [- [b[1], b[0]],- [b[3], b[2]],- ];- }-- const m: L.mapbox.map = new L.mapbox.map(mapElement, settings, options);- this._map = m;-- } else {- if (!OrMap._mapboxGlStyle) {- // @ts-ignore- OrMap._mapboxGlStyle = await import("mapbox-gl/dist/mapbox-gl.css");- }-- // Add style to shadow root- var style = document.createElement('style');- style.id = "mapboxGlStyle";- style.textContent = OrMap._mapboxGlStyle.default.toString();- this.shadowRoot.appendChild(style);-- const map: typeof import("mapbox-gl") = await import("mapbox-gl");- const settingsResponse = await rest.api.MapResource.getSettings();- const settings = <any> settingsResponse.data;-- const options: MapboxOptions = {- container: mapElement,- style: <MapboxStyle> settings,- attributionControl: true,- minZoom: settings.minZoom || 0,- maxZoom: settings.maxZoom || 0,- maxBounds: settings.maxBounds || null,- boxZoom: settings.boxZoom || false,- transformRequest: (url, resourceType) => {- return {- url: url,- headers: {'Authorization': openremote.getAuthorizationHeader()}- }- }- };-- this._map = new map.Map(options);- }- // Get markers from slot- let slotElement = this.shadowRoot.getElementById('markers-slot');- this._observer = new FlattenedNodesObserver(slotElement!, (info) => {- this._processNewMarkers(info.addedNodes);- this._processRemovedMarkers(info.removedNodes);+ this._map = new MapWidget(this.type, this.shadowRoot, mapElement);+ this._map.load().then(() => {+ // Get markers from slot+ let slotElement = this.shadowRoot!.getElementById('markers-slot');+ this._observer = new FlattenedNodesObserver(slotElement!, (info) => {+ this._processNewMarkers(info.addedNodes);+ this._processRemovedMarkers(info.removedNodes);+ });});}--this._loaded = true;}}diff --git a/ui/component/or-map/src/mapwidget.ts b/ui/component/or-map/src/mapwidget.tsnew file mode 100644index 000000000..b71468376--- /dev/null+++ b/ui/component/or-map/src/mapwidget.ts@@ -0,0 +1,140 @@+import L, {MapOptions} from "mapbox.js";+import {Type} from "./index";+import rest from "@openremote/rest";+import openremote from "@openremote/core";+import {Map as MapGL, MapboxOptions, Style as MapboxStyle, Marker as MarkerGL} from "mapbox-gl";+import {OrMapMarker} from "../markers/or-map-marker";++export class MapWidget {+ protected _mapJs?: L.mapbox.map;+ protected _mapGl?: MapGL;+ protected static _mapboxGlStyle?: any;+ protected static _mapboxJsStyle?: any;+ protected _type: Type;+ protected _styleParent: Node;+ protected _mapContainer: HTMLElement;+ protected _loaded: boolean = false;+ protected _markersJs?: Map<OrMapMarker, L.Marker>;+ protected _markersGl?: Map<OrMapMarker, MarkerGL>;++ constructor(type: Type, styleParent: Node, mapContainer: HTMLElement) {+ this._type = type;+ this._styleParent = styleParent;+ this._mapContainer = mapContainer;++ switch(type) {+ case Type.RASTER:+ this._markersJs = new Map<OrMapMarker, L.Marker>();+ break;+ case Type.VECTOR:+ this._markersGl = new Map<OrMapMarker, MarkerGL>();+ break;+ }+ }++ async load(): Promise<void> {+ if (this._loaded) {+ return;+ }++ this._loaded = true;++ if (this._type === Type.RASTER) {++ if (!MapWidget._mapboxJsStyle) {+ // @ts-ignore+ MapWidget._mapboxJsStyle = await import("mapbox.js/theme/style.css");+ }++ // Add style to shadow root+ var style = document.createElement('style');+ style.id = "mapboxJsStyle";+ style.textContent = MapWidget._mapboxJsStyle.default.toString();+ this._styleParent.appendChild(style);++ const settingsResponse = await rest.api.MapResource.getSettingsJs();+ const settings = <any> settingsResponse.data;+ const options: MapOptions = {+ zoom: <number>settings.zoom + 1, // JS zoom is out by one compared to GL+ minZoom: settings.minZoom || 0,+ maxZoom: settings.maxZoom || 22,+ boxZoom: settings.boxZoom || false,+ };+ if (settings.bounds) {+ let b = settings.bounds;+ options.maxBounds = [+ [b[1], b[0]],+ [b[3], b[2]],+ ];+ }++ this._mapJs = new L.mapbox.map(this._mapContainer, settings, options);++ } else {+ if (!MapWidget._mapboxGlStyle) {+ // @ts-ignore+ MapWidget._mapboxGlStyle = await import("mapbox-gl/dist/mapbox-gl.css");+ }++ // Add style to shadow root+ var style = document.createElement('style');+ style.id = "mapboxGlStyle";+ style.textContent = MapWidget._mapboxGlStyle.default.toString();+ this._styleParent.appendChild(style);++ const map: typeof import("mapbox-gl") = await import("mapbox-gl");+ const settingsResponse = await rest.api.MapResource.getSettings();+ const settings = <any> settingsResponse.data;++ const options: MapboxOptions = {+ container: this._mapContainer,+ style: <MapboxStyle> settings,+ attributionControl: true,+ minZoom: settings.minZoom || 0,+ maxZoom: settings.maxZoom || 0,+ maxBounds: settings.maxBounds || null,+ boxZoom: settings.boxZoom || false,+ transformRequest: (url, resourceType) => {+ return {+ url: url,+ headers: {'Authorization': openremote.getAuthorizationHeader()}+ }+ }+ };++ this._mapGl = new map.Map(options);+ }+ }++ addMarker(marker: OrMapMarker) {+ if (marker.html) {+ 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});+ m.addTo(this._mapJs!);+ this._markersJs!.set(marker, m);+ break;+ case Type.VECTOR:++ break;++ }+ }+ }++ removeMarker(marker: OrMapMarker) {+ switch (this._type) {+ case Type.RASTER:+ let m: L.Marker | undefined = this._markersJs!.get(marker);+ if (m) {+ this._markersJs!.delete(marker);+ m.removeFrom(this._mapJs!);+ }+ break;+ case Type.VECTOR:+ break;++ }+ }+}\ No newline at end of filediff --git a/ui/demo/demo-or-map/tsconfig.json b/ui/demo/demo-or-map/tsconfig.jsonindex 6f139ca97..5bfc1025d 100644--- a/ui/demo/demo-or-map/tsconfig.json+++ b/ui/demo/demo-or-map/tsconfig.json@@ -1,7 +1,8 @@{"extends": "../../tsconfig","compilerOptions": {- "outDir": "./dist/"+ "outDir": "./dist/",+ "declaration": false},"include": ["./src",