Fix calculation value in function for map widget
This commit is contained in:
parent
a0ae28f2be
commit
31d0425cd8
@ -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) {
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user