Commit 19b0da39cacf3a28119809c701c698bafdb2b013

Authored by Artem Halushko
1 parent 3ed29a84

provide entity to custom actions

@@ -19,6 +19,7 @@ import { Observable, Subject, fromEvent, of } from 'rxjs'; @@ -19,6 +19,7 @@ import { Observable, Subject, fromEvent, of } from 'rxjs';
19 import { finalize, share, map } from 'rxjs/operators'; 19 import { finalize, share, map } from 'rxjs/operators';
20 import base64js from 'base64-js'; 20 import base64js from 'base64-js';
21 import { Datasource } from '@app/shared/models/widget.models'; 21 import { Datasource } from '@app/shared/models/widget.models';
  22 +import { FormattedData } from '@app/modules/home/components/widget/lib/maps/map-models';
22 23
23 export function onParentScrollOrWindowResize(el: Node): Observable<Event> { 24 export function onParentScrollOrWindowResize(el: Node): Observable<Event> {
24 const scrollSubject = new Subject<Event>(); 25 const scrollSubject = new Subject<Event>();
@@ -504,12 +505,12 @@ export function parseArray(input: any[]): any[] { @@ -504,12 +505,12 @@ export function parseArray(input: any[]): any[] {
504 ); 505 );
505 } 506 }
506 507
507 -export function parseData(input: any[]): any[] { 508 +export function parseData(input: any[]): FormattedData[] {
508 return _(input).groupBy(el => el?.datasource?.entityName) 509 return _(input).groupBy(el => el?.datasource?.entityName)
509 .values().value().map((entityArray, i) => { 510 .values().value().map((entityArray, i) => {
510 const obj = { 511 const obj = {
511 entityName: entityArray[0]?.datasource?.entityName, 512 entityName: entityArray[0]?.datasource?.entityName,
512 - $datasource: entityArray[0]?.datasource, 513 + $datasource: entityArray[0]?.datasource as Datasource,
513 dsIndex: i, 514 dsIndex: i,
514 deviceType: null 515 deviceType: null
515 }; 516 };
@@ -568,7 +569,7 @@ export function parseTemplate(template: string, data: { $datasource?: Datasource @@ -568,7 +569,7 @@ export function parseTemplate(template: string, data: { $datasource?: Datasource
568 formatted.forEach(value => { 569 formatted.forEach(value => {
569 const [variable, digits] = value.replace('${', '').replace('}', '').split(':'); 570 const [variable, digits] = value.replace('${', '').replace('}', '').split(':');
570 data[variable] = padValue(data[variable], +digits); 571 data[variable] = padValue(data[variable], +digits);
571 - if (isNaN(data[variable])) data[value] = ''; 572 + if (data[variable] === 'NaN') data[variable] = '';
572 template = template.replace(value, '${' + variable + '}'); 573 template = template.replace(value, '${' + variable + '}');
573 }); 574 });
574 const variables = template.match(/\$\{.*?\}/g); 575 const variables = template.match(/\$\{.*?\}/g);
@@ -15,6 +15,7 @@ @@ -15,6 +15,7 @@
15 /// 15 ///
16 16
17 import { LatLngTuple, LeafletMouseEvent } from 'leaflet'; 17 import { LatLngTuple, LeafletMouseEvent } from 'leaflet';
  18 +import { Datasource } from '@app/shared/models/widget.models';
18 19
19 export type GenericFunction = (data: FormattedData, dsData: FormattedData[], dsIndex: number) => string; 20 export type GenericFunction = (data: FormattedData, dsData: FormattedData[], dsIndex: number) => string;
20 export type MarkerImageFunction = (data: FormattedData, dsData: FormattedData[], dsIndex: number) => string; 21 export type MarkerImageFunction = (data: FormattedData, dsData: FormattedData[], dsIndex: number) => string;
@@ -96,11 +97,11 @@ export type MarkerSettings = { @@ -96,11 +97,11 @@ export type MarkerSettings = {
96 } 97 }
97 98
98 export interface FormattedData { 99 export interface FormattedData {
99 - aliasName: string; 100 + $datasource: Datasource;
100 entityName: string; 101 entityName: string;
101 - $datasource: string;  
102 dsIndex: number; 102 dsIndex: number;
103 - deviceType: string 103 + deviceType: string;
  104 + [key: string]: any
104 } 105 }
105 106
106 export type PolygonSettings = { 107 export type PolygonSettings = {
@@ -151,6 +152,6 @@ export interface HistorySelectSettings { @@ -151,6 +152,6 @@ export interface HistorySelectSettings {
151 buttonColor: string; 152 buttonColor: string;
152 } 153 }
153 154
154 -export type actionsHandler = ($event: Event | LeafletMouseEvent) => void; 155 +export type actionsHandler = ($event: Event | LeafletMouseEvent, datasource: Datasource) => void;
155 156
156 export type UnitedMapSettings = MapSettings & PolygonSettings & MarkerSettings & PolylineSettings; 157 export type UnitedMapSettings = MapSettings & PolygonSettings & MarkerSettings & PolylineSettings;
@@ -36,7 +36,7 @@ import { initSchema, addToSchema, mergeSchemes, addCondition, addGroupInfo } fro @@ -36,7 +36,7 @@ import { initSchema, addToSchema, mergeSchemes, addCondition, addGroupInfo } fro
36 import { of, Subject } from 'rxjs'; 36 import { of, Subject } from 'rxjs';
37 import { WidgetContext } from '@app/modules/home/models/widget-component.models'; 37 import { WidgetContext } from '@app/modules/home/models/widget-component.models';
38 import { getDefCenterPosition } from './maps-utils'; 38 import { getDefCenterPosition } from './maps-utils';
39 -import { JsonSettingsSchema, WidgetActionDescriptor, DatasourceType, widgetType } from '@shared/models/widget.models'; 39 +import { JsonSettingsSchema, WidgetActionDescriptor, DatasourceType, widgetType, Datasource } from '@shared/models/widget.models';
40 import { EntityId } from '@shared/models/id/entity-id'; 40 import { EntityId } from '@shared/models/id/entity-id';
41 import { AttributeScope, DataKeyType, LatestTelemetry } from '@shared/models/telemetry/telemetry.models'; 41 import { AttributeScope, DataKeyType, LatestTelemetry } from '@shared/models/telemetry/telemetry.models';
42 import { AttributeService } from '@core/http/attribute.service'; 42 import { AttributeService } from '@core/http/attribute.service';
@@ -142,7 +142,7 @@ export class MapWidgetController implements MapWidgetInterface { @@ -142,7 +142,7 @@ export class MapWidgetController implements MapWidgetInterface {
142 const descriptors = this.ctx.actionsApi.getActionDescriptors(name); 142 const descriptors = this.ctx.actionsApi.getActionDescriptors(name);
143 const actions = {}; 143 const actions = {};
144 descriptors.forEach(descriptor => { 144 descriptors.forEach(descriptor => {
145 - actions[descriptor.name] = ($event: Event) => this.onCustomAction(descriptor, $event); 145 + actions[descriptor.name] = ($event: Event, datasource: Datasource) => this.onCustomAction(descriptor, $event, datasource);
146 }, actions); 146 }, actions);
147 return actions; 147 return actions;
148 } 148 }
@@ -150,15 +150,15 @@ export class MapWidgetController implements MapWidgetInterface { @@ -150,15 +150,15 @@ export class MapWidgetController implements MapWidgetInterface {
150 onInit() { 150 onInit() {
151 } 151 }
152 152
153 - private onCustomAction(descriptor: WidgetActionDescriptor, $event: any) { 153 + private onCustomAction(descriptor: WidgetActionDescriptor, $event: any, entityInfo: Datasource) {
154 if ($event && $event.stopPropagation) { 154 if ($event && $event.stopPropagation) {
155 $event?.stopPropagation(); 155 $event?.stopPropagation();
156 } 156 }
157 - // safeExecute(parseFunction(descriptor.customFunction, ['$event', 'widgetContext']), [$event, this.ctx])  
158 - const entityInfo = this.ctx.actionsApi.getActiveEntityInfo();  
159 - const entityId = entityInfo ? entityInfo.entityId : null;  
160 - const entityName = entityInfo ? entityInfo.entityName : null;  
161 - const entityLabel = entityInfo ? entityInfo.entityLabel : null; 157 + const { id, entityName, entityLabel, entityType } = entityInfo;
  158 + const entityId: EntityId = {
  159 + entityType,
  160 + id
  161 + };
162 this.ctx.actionsApi.handleWidgetAction($event, descriptor, entityId, entityName, null, entityLabel); 162 this.ctx.actionsApi.handleWidgetAction($event, descriptor, entityId, entityName, null, entityLabel);
163 } 163 }
164 164
@@ -16,19 +16,22 @@ @@ -16,19 +16,22 @@
16 16
17 import L from 'leaflet'; 17 import L from 'leaflet';
18 import { MarkerSettings, PolygonSettings, PolylineSettings } from './map-models'; 18 import { MarkerSettings, PolygonSettings, PolylineSettings } from './map-models';
  19 +import { Datasource } from '@app/shared/models/widget.models';
19 20
20 export function createTooltip(target: L.Layer, 21 export function createTooltip(target: L.Layer,
21 settings: MarkerSettings | PolylineSettings | PolygonSettings, 22 settings: MarkerSettings | PolylineSettings | PolygonSettings,
22 - content?: string | HTMLElement): L.Popup { 23 + datasource: Datasource,
  24 + content?: string | HTMLElement
  25 +): L.Popup {
23 const popup = L.popup(); 26 const popup = L.popup();
24 popup.setContent(content); 27 popup.setContent(content);
25 target.bindPopup(popup, { autoClose: settings.autocloseTooltip, closeOnClick: false }); 28 target.bindPopup(popup, { autoClose: settings.autocloseTooltip, closeOnClick: false });
26 if (settings.showTooltipAction === 'hover') { 29 if (settings.showTooltipAction === 'hover') {
27 target.off('click'); 30 target.off('click');
28 - target.on('mouseover', function () { 31 + target.on('mouseover', () => {
29 target.openPopup(); 32 target.openPopup();
30 }); 33 });
31 - target.on('mouseout', function () { 34 + target.on('mouseout', () => {
32 target.closePopup(); 35 target.closePopup();
33 }); 36 });
34 } 37 }
@@ -37,7 +40,7 @@ export function createTooltip(target: L.Layer, @@ -37,7 +40,7 @@ export function createTooltip(target: L.Layer,
37 Array.from(actions).forEach( 40 Array.from(actions).forEach(
38 (element: HTMLElement) => { 41 (element: HTMLElement) => {
39 if (element && settings.tooltipAction[element.id]) { 42 if (element && settings.tooltipAction[element.id]) {
40 - element.addEventListener('click', settings.tooltipAction[element.id]) 43 + element.addEventListener('click', ($event) => settings.tooltipAction[element.id]($event, datasource));
41 } 44 }
42 }); 45 });
43 }); 46 });
@@ -42,7 +42,7 @@ export class Marker { @@ -42,7 +42,7 @@ export class Marker {
42 }); 42 });
43 43
44 if (settings.showTooltip) { 44 if (settings.showTooltip) {
45 - this.tooltip = createTooltip(this.leafletMarker, settings); 45 + this.tooltip = createTooltip(this.leafletMarker, settings, data.$datasource);
46 this.updateMarkerTooltip(data); 46 this.updateMarkerTooltip(data);
47 } 47 }
48 48
@@ -50,7 +50,7 @@ export class Marker { @@ -50,7 +50,7 @@ export class Marker {
50 this.leafletMarker.on('click', (event: LeafletMouseEvent) => { 50 this.leafletMarker.on('click', (event: LeafletMouseEvent) => {
51 for (const action in this.settings.markerClick) { 51 for (const action in this.settings.markerClick) {
52 if (typeof (this.settings.markerClick[action]) === 'function') { 52 if (typeof (this.settings.markerClick[action]) === 'function') {
53 - this.settings.markerClick[action](event); 53 + this.settings.markerClick[action](event, this.data.$datasource);
54 } 54 }
55 } 55 }
56 }); 56 });
@@ -14,7 +14,7 @@ @@ -14,7 +14,7 @@
14 /// limitations under the License. 14 /// limitations under the License.
15 /// 15 ///
16 16
17 -import L, { LatLngExpression, LatLngTuple } from 'leaflet'; 17 +import L, { LatLngExpression, LatLngTuple, LeafletMouseEvent } from 'leaflet';
18 import { createTooltip } from './maps-utils'; 18 import { createTooltip } from './maps-utils';
19 import { PolygonSettings, FormattedData } from './map-models'; 19 import { PolygonSettings, FormattedData } from './map-models';
20 import { DatasourceData } from '@app/shared/models/widget.models'; 20 import { DatasourceData } from '@app/shared/models/widget.models';
@@ -27,7 +27,7 @@ export class Polygon { @@ -27,7 +27,7 @@ export class Polygon {
27 data; 27 data;
28 dataSources; 28 dataSources;
29 29
30 - constructor(public map, polyData: DatasourceData, dataSources, private settings: PolygonSettings, onClickListener?) { 30 + constructor(public map, polyData: DatasourceData, dataSources, private settings: PolygonSettings) {
31 this.leafletPoly = L.polygon(polyData.data, { 31 this.leafletPoly = L.polygon(polyData.data, {
32 fill: true, 32 fill: true,
33 fillColor: settings.polygonColor, 33 fillColor: settings.polygonColor,
@@ -39,11 +39,17 @@ export class Polygon { @@ -39,11 +39,17 @@ export class Polygon {
39 this.dataSources = dataSources; 39 this.dataSources = dataSources;
40 this.data = polyData; 40 this.data = polyData;
41 if (settings.showPolygonTooltip) { 41 if (settings.showPolygonTooltip) {
42 - this.tooltip = createTooltip(this.leafletPoly, settings); 42 + this.tooltip = createTooltip(this.leafletPoly, settings, polyData.datasource);
43 this.updateTooltip(polyData); 43 this.updateTooltip(polyData);
44 } 44 }
45 - if (onClickListener) {  
46 - this.leafletPoly.on('click', onClickListener); 45 + if (settings.polygonClick) {
  46 + this.leafletPoly.on('click', (event: LeafletMouseEvent) => {
  47 + for (const action in this.settings.polygonClick) {
  48 + if (typeof (this.settings.polygonClick[action]) === 'function') {
  49 + this.settings.polygonClick[action](event, polyData.datasource);
  50 + }
  51 + }
  52 + });
47 } 53 }
48 } 54 }
49 55