|
|
|
@ -15,13 +15,12 @@ |
|
|
|
///
|
|
|
|
|
|
|
|
import L from 'leaflet'; |
|
|
|
import { FormattedData, MarkerSettings, PolygonSettings, PolylineSettings } from './map-models'; |
|
|
|
import { FormattedData, MarkerSettings, PolygonSettings, PolylineSettings, ReplaceInfo } from './map-models'; |
|
|
|
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, isNumber, isUndefined, padValue } from '@core/utils'; |
|
|
|
import { Form } from '@angular/forms'; |
|
|
|
import { createLabelFromDatasource, hashCode, isDefinedAndNotNull, isNumber, isUndefined, padValue } from '@core/utils'; |
|
|
|
|
|
|
|
export function createTooltip(target: L.Layer, |
|
|
|
settings: MarkerSettings | PolylineSettings | PolygonSettings, |
|
|
|
@ -185,7 +184,7 @@ function parseTemplate(template: string, data: { $datasource?: Datasource, [key: |
|
|
|
} else { |
|
|
|
textValue = value; |
|
|
|
} |
|
|
|
template = template.split(variable).join(textValue); |
|
|
|
template = template.replace(variable, textValue); |
|
|
|
match = /\${([^}]*)}/g.exec(template); |
|
|
|
} |
|
|
|
|
|
|
|
@ -198,7 +197,7 @@ function parseTemplate(template: string, data: { $datasource?: Datasource, [key: |
|
|
|
while (match !== null) { |
|
|
|
[actionTags, actionName, actionText] = match; |
|
|
|
action = createLinkElement(actionName, actionText); |
|
|
|
template = template.split(actionTags).join(action); |
|
|
|
template = template.replace(actionTags, action); |
|
|
|
match = linkActionRegex.exec(template); |
|
|
|
} |
|
|
|
|
|
|
|
@ -206,7 +205,7 @@ function parseTemplate(template: string, data: { $datasource?: Datasource, [key: |
|
|
|
while (match !== null) { |
|
|
|
[actionTags, actionName, actionText] = match; |
|
|
|
action = createButtonElement(actionName, actionText); |
|
|
|
template = template.split(actionTags).join(action); |
|
|
|
template = template.replace(actionTags, action); |
|
|
|
match = buttonActionRegex.exec(template); |
|
|
|
} |
|
|
|
|
|
|
|
@ -219,6 +218,94 @@ function parseTemplate(template: string, data: { $datasource?: Datasource, [key: |
|
|
|
return res; |
|
|
|
} |
|
|
|
|
|
|
|
export function processPattern(template: string, data: { $datasource?: Datasource, [key: string]: any }): Array<ReplaceInfo> { |
|
|
|
const replaceInfo = []; |
|
|
|
try { |
|
|
|
const reg = /\${([^}]*)}/g; |
|
|
|
let match = reg.exec(template); |
|
|
|
while (match !== null) { |
|
|
|
const variableInfo: ReplaceInfo = { |
|
|
|
dataKeyName: '', |
|
|
|
valDec: 2, |
|
|
|
variable: '' |
|
|
|
}; |
|
|
|
const variable = match[0]; |
|
|
|
let label = match[1]; |
|
|
|
let valDec = 2; |
|
|
|
const splitValues = label.split(':'); |
|
|
|
if (splitValues.length > 1) { |
|
|
|
label = splitValues[0]; |
|
|
|
valDec = parseFloat(splitValues[1]); |
|
|
|
} |
|
|
|
|
|
|
|
variableInfo.variable = variable; |
|
|
|
variableInfo.valDec = valDec; |
|
|
|
|
|
|
|
if (label.startsWith('#')) { |
|
|
|
const keyIndexStr = label.substring(1); |
|
|
|
const n = Math.floor(Number(keyIndexStr)); |
|
|
|
if (String(n) === keyIndexStr && n >= 0) { |
|
|
|
variableInfo.dataKeyName = data.$datasource.dataKeys[n].label; |
|
|
|
} |
|
|
|
} else { |
|
|
|
variableInfo.dataKeyName = label; |
|
|
|
} |
|
|
|
replaceInfo.push(variableInfo); |
|
|
|
|
|
|
|
match = reg.exec(template); |
|
|
|
} |
|
|
|
} catch (ex) { |
|
|
|
console.log(ex, template) |
|
|
|
} |
|
|
|
return replaceInfo; |
|
|
|
} |
|
|
|
|
|
|
|
export function fillPattern(markerLabelText: string, replaceInfoLabelMarker: Array<ReplaceInfo>, data: FormattedData) { |
|
|
|
let text = createLabelFromDatasource(data.$datasource, markerLabelText); |
|
|
|
if (replaceInfoLabelMarker) { |
|
|
|
for(const variableInfo of replaceInfoLabelMarker) { |
|
|
|
let txtVal = ''; |
|
|
|
if (variableInfo.dataKeyName && isDefinedAndNotNull(data[variableInfo.dataKeyName])) { |
|
|
|
const varData = data[variableInfo.dataKeyName]; |
|
|
|
if (isNumber(varData)) { |
|
|
|
txtVal = padValue(varData, variableInfo.valDec); |
|
|
|
} else { |
|
|
|
txtVal = varData; |
|
|
|
} |
|
|
|
} |
|
|
|
text = text.replace(variableInfo.variable, txtVal); |
|
|
|
} |
|
|
|
} |
|
|
|
return text; |
|
|
|
} |
|
|
|
|
|
|
|
function prepareProcessPattern(template: string, translateFn?: TranslateFunc): string { |
|
|
|
if (translateFn) { |
|
|
|
template = translateFn(template); |
|
|
|
} |
|
|
|
let actionTags: string; |
|
|
|
let actionText: string; |
|
|
|
let actionName: string; |
|
|
|
let action: string; |
|
|
|
|
|
|
|
let match = linkActionRegex.exec(template); |
|
|
|
while (match !== null) { |
|
|
|
[actionTags, actionName, actionText] = match; |
|
|
|
action = createLinkElement(actionName, actionText); |
|
|
|
template = template.replace(actionTags, action); |
|
|
|
match = linkActionRegex.exec(template); |
|
|
|
} |
|
|
|
|
|
|
|
match = buttonActionRegex.exec(template); |
|
|
|
while (match !== null) { |
|
|
|
[actionTags, actionName, actionText] = match; |
|
|
|
action = createButtonElement(actionName, actionText); |
|
|
|
template = template.replace(actionTags, action); |
|
|
|
match = buttonActionRegex.exec(template); |
|
|
|
} |
|
|
|
return template; |
|
|
|
} |
|
|
|
|
|
|
|
export const parseWithTranslation = { |
|
|
|
|
|
|
|
translateFn: null, |
|
|
|
@ -233,6 +320,9 @@ export const parseWithTranslation = { |
|
|
|
parseTemplate(template: string, data: object, forceTranslate = false): string { |
|
|
|
return parseTemplate(forceTranslate ? this.translate(template) : template, data, this.translate.bind(this)); |
|
|
|
}, |
|
|
|
prepareProcessPattern(template: string, forceTranslate = false): string { |
|
|
|
return prepareProcessPattern(forceTranslate ? this.translate(template) : template, this.translate.bind(this)); |
|
|
|
}, |
|
|
|
setTranslate(translateFn: TranslateFunc) { |
|
|
|
this.translateFn = translateFn; |
|
|
|
} |
|
|
|
|