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