From 31d0425cd85cb1bbe76b5b96d770466dba6416f8 Mon Sep 17 00:00:00 2001 From: Vladyslav_Prykhodko Date: Mon, 10 Aug 2020 16:09:38 +0300 Subject: [PATCH] Fix calculation value in function for map widget --- .../components/widget/lib/maps/maps-utils.ts | 28 ++++++++++++++++++- .../components/widget/lib/maps/polygon.ts | 9 ++---- .../components/widget/lib/maps/polyline.ts | 17 +++++------ 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/maps/maps-utils.ts b/ui-ngx/src/app/modules/home/components/widget/lib/maps/maps-utils.ts index 87b99d3499..813437a439 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/maps/maps-utils.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/maps/maps-utils.ts @@ -20,7 +20,15 @@ import { Datasource, DatasourceData } from '@app/shared/models/widget.models'; import _ from 'lodash'; import { Observable, Observer, of } from 'rxjs'; import { map } from 'rxjs/operators'; -import { createLabelFromDatasource, hashCode, isDefinedAndNotNull, isNumber, isUndefined, padValue } from '@core/utils'; +import { + createLabelFromDatasource, + hashCode, + isDefined, + isDefinedAndNotNull, isFunction, + isNumber, + isUndefined, + padValue +} from '@core/utils'; export function createTooltip(target: L.Layer, settings: MarkerSettings | PolylineSettings | PolygonSettings, @@ -403,6 +411,24 @@ export function safeExecute(func: (...args: any[]) => any, params = []) { return res; } +export function functionValueCalculator(useFunction: boolean, func: (...args: any[]) => any, params = [], defaultValue: any) { + let res; + if (useFunction && isDefined(func) && isFunction(func)) { + try { + res = func(...params); + if (!isDefinedAndNotNull(res) || res === '') { + res = defaultValue; + } + } catch (err) { + res = defaultValue; + console.log('error in external function:', err); + } + } else { + res = defaultValue; + } + return res; +} + export function calculateNewPointCoordinate(coordinate: number, imageSize: number): number { let pointCoordinate = coordinate / imageSize; if (pointCoordinate < 0) { diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/maps/polygon.ts b/ui-ngx/src/app/modules/home/components/widget/lib/maps/polygon.ts index 2af02b2878..f580e80b1b 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/maps/polygon.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/maps/polygon.ts @@ -15,7 +15,7 @@ /// import L, { LatLngExpression, LeafletMouseEvent } from 'leaflet'; -import { createTooltip, parseWithTranslation, safeExecute } from './maps-utils'; +import { createTooltip, functionValueCalculator, parseWithTranslation, safeExecute } from './maps-utils'; import { FormattedData, PolygonSettings } from './map-models'; export class Polygon { @@ -97,10 +97,7 @@ export class Polygon { } private getPolygonColor(settings: PolygonSettings): string | null { - if (settings.usePolygonColorFunction) { - return safeExecute(settings.polygonColorFunction, [this.data, this.dataSources, this.data.dsIndex]); - } else { - return settings.polygonColor; - } + return functionValueCalculator(settings.usePolygonColorFunction, settings.polygonColorFunction, + [this.data, this.dataSources, this.data.dsIndex], settings.polygonColor); } } diff --git a/ui-ngx/src/app/modules/home/components/widget/lib/maps/polyline.ts b/ui-ngx/src/app/modules/home/components/widget/lib/maps/polyline.ts index 543f4a718a..6521257be5 100644 --- a/ui-ngx/src/app/modules/home/components/widget/lib/maps/polyline.ts +++ b/ui-ngx/src/app/modules/home/components/widget/lib/maps/polyline.ts @@ -18,7 +18,7 @@ import L, { PolylineDecoratorOptions } from 'leaflet'; import 'leaflet-polylinedecorator'; import { FormattedData, PolylineSettings } from './map-models'; -import { safeExecute } from '@home/components/widget/lib/maps/maps-utils'; +import { functionValueCalculator, safeExecute } from '@home/components/widget/lib/maps/maps-utils'; export class Polyline { @@ -72,15 +72,12 @@ export class Polyline { getPolyStyle(settings: PolylineSettings): L.PolylineOptions { return { interactive: false, - color: settings.useColorFunction ? - safeExecute(settings.colorFunction, - [this.data, this.dataSources, this.data.dsIndex]) : settings.color, - opacity: settings.useStrokeOpacityFunction ? - safeExecute(settings.strokeOpacityFunction, - [this.data, this.dataSources, this.data.dsIndex]) : settings.strokeOpacity, - weight: settings.useStrokeWeightFunction ? - safeExecute(settings.strokeWeightFunction, - [this.data, this.dataSources, this.data.dsIndex]) : settings.strokeWeight, + color: functionValueCalculator(settings.useColorFunction, settings.colorFunction, + [this.data, this.dataSources, this.data.dsIndex], settings.color), + opacity: functionValueCalculator(settings.useStrokeOpacityFunction, settings.strokeOpacityFunction, + [this.data, this.dataSources, this.data.dsIndex], settings.strokeOpacity), + weight: functionValueCalculator(settings.useStrokeWeightFunction, settings.strokeWeightFunction, + [this.data, this.dataSources, this.data.dsIndex], settings.strokeWeight) } }