Fix tooltip data

This commit is contained in:
Vladyslav_Prykhodko 2020-05-12 17:24:36 +03:00
parent 2d235cf9eb
commit 1b9cf7c527
2 changed files with 70 additions and 26 deletions

View File

@ -496,7 +496,7 @@ export function padValue(val: any, dec: number): string {
val = Math.abs(val); val = Math.abs(val);
if (dec > 0) { if (dec > 0) {
strVal = val.toFixed(dec).toString() strVal = val.toFixed(dec);
} else { } else {
strVal = Math.round(val).toString(); strVal = Math.round(val).toString();
} }

View File

@ -20,7 +20,7 @@ import { Datasource } from '@app/shared/models/widget.models';
import _ from 'lodash'; import _ from 'lodash';
import { Observable, Observer, of } from 'rxjs'; import { Observable, Observer, of } from 'rxjs';
import { map } from 'rxjs/operators'; import { map } from 'rxjs/operators';
import { createLabelFromDatasource, hashCode, padValue } from '@core/utils'; import { createLabelFromDatasource, hashCode, isNumber, padValue } from '@core/utils';
export function createTooltip(target: L.Layer, export function createTooltip(target: L.Layer,
settings: MarkerSettings | PolylineSettings | PolygonSettings, settings: MarkerSettings | PolylineSettings | PolygonSettings,
@ -43,8 +43,9 @@ export function createTooltip(target: L.Layer,
const actions = document.getElementsByClassName('tb-custom-action'); const actions = document.getElementsByClassName('tb-custom-action');
Array.from(actions).forEach( Array.from(actions).forEach(
(element: HTMLElement) => { (element: HTMLElement) => {
if (element && settings.tooltipAction[element.id]) { const actionName = element.getAttribute('data-action-name');
element.addEventListener('click', ($event) => settings.tooltipAction[element.id]($event, datasource)); if (element && settings.tooltipAction[actionName]) {
element.addEventListener('click', ($event) => settings.tooltipAction[actionName]($event, datasource));
} }
}); });
}); });
@ -111,38 +112,81 @@ export function aspectCache(imageUrl: string): Observable<number> {
export type TranslateFunc = (key: string, defaultTranslation?: string) => string; export type TranslateFunc = (key: string, defaultTranslation?: string) => string;
const varsRegex = /\${([^}]*)}/g;
const linkActionRegex = /<link-act name=['"]([^['"]*)['"]>([^<]*)<\/link-act>/g;
const buttonActionRegex = /<button-act name=['"]([^['"]*)['"]>([^<]*)<\/button-act>/g;
function createLinkElement(actionName: string, actionText: string): string {
return `<a href="#" class="tb-custom-action" data-action-name=${actionName}>${actionText}</a>`;
}
function createButtonElement(actionName: string, actionText: string) {
return `<button mat-button class="tb-custom-action" data-action-name=${actionName}>${actionText}</button>`;
}
function parseTemplate(template: string, data: { $datasource?: Datasource, [key: string]: any }, function parseTemplate(template: string, data: { $datasource?: Datasource, [key: string]: any },
translateFn?: TranslateFunc) { translateFn?: TranslateFunc) {
let res = ''; let res = '';
try { try {
if (template.match(/<link-act/g)) {
template = template.replace(/<link-act/g, '<a href="#"').replace(/link-act>/g, 'a>')
.replace(/name=(['"])(.*?)(['"])/g, `class='tb-custom-action' id='$2'`);
}
if (translateFn) { if (translateFn) {
template = translateFn(template); template = translateFn(template);
} }
template = createLabelFromDatasource(data.$datasource, template); template = createLabelFromDatasource(data.$datasource, template);
const formatted = template.match(/\${([^}]*):\d*}/g);
if (formatted) let match = varsRegex.exec(template);
formatted.forEach(value => { while (match !== null) {
const [variable, digits] = value.replace('${', '').replace('}', '').split(':'); const variable = match[0];
data[variable] = padValue(data[variable], +digits); let label = match[1];
if (data[variable] === 'NaN') data[variable] = ''; let valDec = 2;
template = template.replace(value, '${' + variable + '}'); const splitValues = label.split(':');
}); if (splitValues.length > 1) {
const variables = template.match(/\${.*?}/g); label = splitValues[0];
if (variables) { valDec = parseFloat(splitValues[1]);
variables.forEach(variable => { }
variable = variable.replace('${', '').replace('}', '');
if (!data[variable]) if (label.startsWith('#')) {
data[variable] = ''; const keyIndexStr = label.substring(1);
}) const n = Math.floor(Number(keyIndexStr));
if (String(n) === keyIndexStr && n >= 0) {
label = data.$datasource.dataKeys[n].label;
}
}
const value = data[label];
let textValue: string;
if (isNumber(value)) {
textValue = padValue(value, valDec);
} else {
textValue = value;
}
template = template.split(variable).join(textValue);
match = varsRegex.exec(template);
} }
let actionTags: string;
let actionText: string;
let actionName: string;
let action: string;
match = linkActionRegex.exec(template);
while (match !== null) {
[actionTags, actionName, actionText] = match;
action = createLinkElement(actionName, actionText);
template = template.split(actionTags).join(action);
match = linkActionRegex.exec(template);
}
match = buttonActionRegex.exec(template);
while (match !== null) {
[actionTags, actionName, actionText] = match;
action = createButtonElement(actionName, actionText);
template = template.split(actionTags).join(action);
match = buttonActionRegex.exec(template);
}
const compiled = _.template(template); const compiled = _.template(template);
res = compiled(data); res = compiled(data);
} } catch (ex) {
catch (ex) {
console.log(ex, template) console.log(ex, template)
} }
return res; return res;