Commit 8e78589b64e588f97cec424dde728ca6f126baf5
Committed by
GitHub
Merge pull request #3272 from vvlladd28/improvement/map/calculate-function2
[3.0] Fix calculation value in function for map widget
Showing
3 changed files
with
37 additions
and
17 deletions
... | ... | @@ -20,7 +20,15 @@ import { Datasource, DatasourceData } from '@app/shared/models/widget.models'; |
20 | 20 | import _ from 'lodash'; |
21 | 21 | import { Observable, Observer, of } from 'rxjs'; |
22 | 22 | import { map } from 'rxjs/operators'; |
23 | -import { createLabelFromDatasource, hashCode, isDefinedAndNotNull, isNumber, isUndefined, padValue } from '@core/utils'; | |
23 | +import { | |
24 | + createLabelFromDatasource, | |
25 | + hashCode, | |
26 | + isDefined, | |
27 | + isDefinedAndNotNull, isFunction, | |
28 | + isNumber, | |
29 | + isUndefined, | |
30 | + padValue | |
31 | +} from '@core/utils'; | |
24 | 32 | |
25 | 33 | export function createTooltip(target: L.Layer, |
26 | 34 | settings: MarkerSettings | PolylineSettings | PolygonSettings, |
... | ... | @@ -403,6 +411,24 @@ export function safeExecute(func: (...args: any[]) => any, params = []) { |
403 | 411 | return res; |
404 | 412 | } |
405 | 413 | |
414 | +export function functionValueCalculator(useFunction: boolean, func: (...args: any[]) => any, params = [], defaultValue: any) { | |
415 | + let res; | |
416 | + if (useFunction && isDefined(func) && isFunction(func)) { | |
417 | + try { | |
418 | + res = func(...params); | |
419 | + if (!isDefinedAndNotNull(res) || res === '') { | |
420 | + res = defaultValue; | |
421 | + } | |
422 | + } catch (err) { | |
423 | + res = defaultValue; | |
424 | + console.log('error in external function:', err); | |
425 | + } | |
426 | + } else { | |
427 | + res = defaultValue; | |
428 | + } | |
429 | + return res; | |
430 | +} | |
431 | + | |
406 | 432 | export function calculateNewPointCoordinate(coordinate: number, imageSize: number): number { |
407 | 433 | let pointCoordinate = coordinate / imageSize; |
408 | 434 | if (pointCoordinate < 0) { | ... | ... |
... | ... | @@ -15,7 +15,7 @@ |
15 | 15 | /// |
16 | 16 | |
17 | 17 | import L, { LatLngExpression, LeafletMouseEvent } from 'leaflet'; |
18 | -import { createTooltip, parseWithTranslation, safeExecute } from './maps-utils'; | |
18 | +import { createTooltip, functionValueCalculator, parseWithTranslation, safeExecute } from './maps-utils'; | |
19 | 19 | import { FormattedData, PolygonSettings } from './map-models'; |
20 | 20 | |
21 | 21 | export class Polygon { |
... | ... | @@ -97,10 +97,7 @@ export class Polygon { |
97 | 97 | } |
98 | 98 | |
99 | 99 | private getPolygonColor(settings: PolygonSettings): string | null { |
100 | - if (settings.usePolygonColorFunction) { | |
101 | - return safeExecute(settings.polygonColorFunction, [this.data, this.dataSources, this.data.dsIndex]); | |
102 | - } else { | |
103 | - return settings.polygonColor; | |
104 | - } | |
100 | + return functionValueCalculator(settings.usePolygonColorFunction, settings.polygonColorFunction, | |
101 | + [this.data, this.dataSources, this.data.dsIndex], settings.polygonColor); | |
105 | 102 | } |
106 | 103 | } | ... | ... |
... | ... | @@ -18,7 +18,7 @@ import L, { PolylineDecoratorOptions } from 'leaflet'; |
18 | 18 | import 'leaflet-polylinedecorator'; |
19 | 19 | |
20 | 20 | import { FormattedData, PolylineSettings } from './map-models'; |
21 | -import { safeExecute } from '@home/components/widget/lib/maps/maps-utils'; | |
21 | +import { functionValueCalculator, safeExecute } from '@home/components/widget/lib/maps/maps-utils'; | |
22 | 22 | |
23 | 23 | export class Polyline { |
24 | 24 | |
... | ... | @@ -72,15 +72,12 @@ export class Polyline { |
72 | 72 | getPolyStyle(settings: PolylineSettings): L.PolylineOptions { |
73 | 73 | return { |
74 | 74 | interactive: false, |
75 | - color: settings.useColorFunction ? | |
76 | - safeExecute(settings.colorFunction, | |
77 | - [this.data, this.dataSources, this.data.dsIndex]) : settings.color, | |
78 | - opacity: settings.useStrokeOpacityFunction ? | |
79 | - safeExecute(settings.strokeOpacityFunction, | |
80 | - [this.data, this.dataSources, this.data.dsIndex]) : settings.strokeOpacity, | |
81 | - weight: settings.useStrokeWeightFunction ? | |
82 | - safeExecute(settings.strokeWeightFunction, | |
83 | - [this.data, this.dataSources, this.data.dsIndex]) : settings.strokeWeight, | |
75 | + color: functionValueCalculator(settings.useColorFunction, settings.colorFunction, | |
76 | + [this.data, this.dataSources, this.data.dsIndex], settings.color), | |
77 | + opacity: functionValueCalculator(settings.useStrokeOpacityFunction, settings.strokeOpacityFunction, | |
78 | + [this.data, this.dataSources, this.data.dsIndex], settings.strokeOpacity), | |
79 | + weight: functionValueCalculator(settings.useStrokeWeightFunction, settings.strokeWeightFunction, | |
80 | + [this.data, this.dataSources, this.data.dsIndex], settings.strokeWeight) | |
84 | 81 | } |
85 | 82 | } |
86 | 83 | ... | ... |