...
|
...
|
@@ -20,7 +20,7 @@ import { Datasource } 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, padValue } from '@core/utils';
|
|
23
|
+import { createLabelFromDatasource, hashCode, isNumber, padValue } from '@core/utils';
|
24
|
24
|
|
25
|
25
|
export function createTooltip(target: L.Layer,
|
26
|
26
|
settings: MarkerSettings | PolylineSettings | PolygonSettings,
|
...
|
...
|
@@ -43,8 +43,9 @@ export function createTooltip(target: L.Layer, |
43
|
43
|
const actions = document.getElementsByClassName('tb-custom-action');
|
44
|
44
|
Array.from(actions).forEach(
|
45
|
45
|
(element: HTMLElement) => {
|
46
|
|
- if (element && settings.tooltipAction[element.id]) {
|
47
|
|
- element.addEventListener('click', ($event) => settings.tooltipAction[element.id]($event, datasource));
|
|
46
|
+ const actionName = element.getAttribute('data-action-name');
|
|
47
|
+ if (element && settings.tooltipAction[actionName]) {
|
|
48
|
+ element.addEventListener('click', ($event) => settings.tooltipAction[actionName]($event, datasource));
|
48
|
49
|
}
|
49
|
50
|
});
|
50
|
51
|
});
|
...
|
...
|
@@ -111,38 +112,81 @@ export function aspectCache(imageUrl: string): Observable<number> { |
111
|
112
|
|
112
|
113
|
export type TranslateFunc = (key: string, defaultTranslation?: string) => string;
|
113
|
114
|
|
|
115
|
+const varsRegex = /\${([^}]*)}/g;
|
|
116
|
+const linkActionRegex = /<link-act name=['"]([^['"]*)['"]>([^<]*)<\/link-act>/g;
|
|
117
|
+const buttonActionRegex = /<button-act name=['"]([^['"]*)['"]>([^<]*)<\/button-act>/g;
|
|
118
|
+
|
|
119
|
+function createLinkElement(actionName: string, actionText: string): string {
|
|
120
|
+ return `<a href="#" class="tb-custom-action" data-action-name=${actionName}>${actionText}</a>`;
|
|
121
|
+}
|
|
122
|
+
|
|
123
|
+function createButtonElement(actionName: string, actionText: string) {
|
|
124
|
+ return `<button mat-button class="tb-custom-action" data-action-name=${actionName}>${actionText}</button>`;
|
|
125
|
+}
|
|
126
|
+
|
114
|
127
|
function parseTemplate(template: string, data: { $datasource?: Datasource, [key: string]: any },
|
115
|
|
- translateFn?: TranslateFunc) {
|
|
128
|
+ translateFn?: TranslateFunc) {
|
116
|
129
|
let res = '';
|
117
|
130
|
try {
|
118
|
|
- if (template.match(/<link-act/g)) {
|
119
|
|
- template = template.replace(/<link-act/g, '<a href="#"').replace(/link-act>/g, 'a>')
|
120
|
|
- .replace(/name=(['"])(.*?)(['"])/g, `class='tb-custom-action' id='$2'`);
|
121
|
|
- }
|
122
|
131
|
if (translateFn) {
|
123
|
132
|
template = translateFn(template);
|
124
|
133
|
}
|
125
|
134
|
template = createLabelFromDatasource(data.$datasource, template);
|
126
|
|
- const formatted = template.match(/\${([^}]*):\d*}/g);
|
127
|
|
- if (formatted)
|
128
|
|
- formatted.forEach(value => {
|
129
|
|
- const [variable, digits] = value.replace('${', '').replace('}', '').split(':');
|
130
|
|
- data[variable] = padValue(data[variable], +digits);
|
131
|
|
- if (data[variable] === 'NaN') data[variable] = '';
|
132
|
|
- template = template.replace(value, '${' + variable + '}');
|
133
|
|
- });
|
134
|
|
- const variables = template.match(/\${.*?}/g);
|
135
|
|
- if (variables) {
|
136
|
|
- variables.forEach(variable => {
|
137
|
|
- variable = variable.replace('${', '').replace('}', '');
|
138
|
|
- if (!data[variable])
|
139
|
|
- data[variable] = '';
|
140
|
|
- })
|
|
135
|
+
|
|
136
|
+ let match = varsRegex.exec(template);
|
|
137
|
+ while (match !== null) {
|
|
138
|
+ const variable = match[0];
|
|
139
|
+ let label = match[1];
|
|
140
|
+ let valDec = 2;
|
|
141
|
+ const splitValues = label.split(':');
|
|
142
|
+ if (splitValues.length > 1) {
|
|
143
|
+ label = splitValues[0];
|
|
144
|
+ valDec = parseFloat(splitValues[1]);
|
|
145
|
+ }
|
|
146
|
+
|
|
147
|
+ if (label.startsWith('#')) {
|
|
148
|
+ const keyIndexStr = label.substring(1);
|
|
149
|
+ const n = Math.floor(Number(keyIndexStr));
|
|
150
|
+ if (String(n) === keyIndexStr && n >= 0) {
|
|
151
|
+ label = data.$datasource.dataKeys[n].label;
|
|
152
|
+ }
|
|
153
|
+ }
|
|
154
|
+
|
|
155
|
+ const value = data[label];
|
|
156
|
+ let textValue: string;
|
|
157
|
+ if (isNumber(value)) {
|
|
158
|
+ textValue = padValue(value, valDec);
|
|
159
|
+ } else {
|
|
160
|
+ textValue = value;
|
|
161
|
+ }
|
|
162
|
+ template = template.split(variable).join(textValue);
|
|
163
|
+ match = varsRegex.exec(template);
|
|
164
|
+ }
|
|
165
|
+
|
|
166
|
+ let actionTags: string;
|
|
167
|
+ let actionText: string;
|
|
168
|
+ let actionName: string;
|
|
169
|
+ let action: string;
|
|
170
|
+
|
|
171
|
+ match = linkActionRegex.exec(template);
|
|
172
|
+ while (match !== null) {
|
|
173
|
+ [actionTags, actionName, actionText] = match;
|
|
174
|
+ action = createLinkElement(actionName, actionText);
|
|
175
|
+ template = template.split(actionTags).join(action);
|
|
176
|
+ match = linkActionRegex.exec(template);
|
141
|
177
|
}
|
|
178
|
+
|
|
179
|
+ match = buttonActionRegex.exec(template);
|
|
180
|
+ while (match !== null) {
|
|
181
|
+ [actionTags, actionName, actionText] = match;
|
|
182
|
+ action = createButtonElement(actionName, actionText);
|
|
183
|
+ template = template.split(actionTags).join(action);
|
|
184
|
+ match = buttonActionRegex.exec(template);
|
|
185
|
+ }
|
|
186
|
+
|
142
|
187
|
const compiled = _.template(template);
|
143
|
188
|
res = compiled(data);
|
144
|
|
- }
|
145
|
|
- catch (ex) {
|
|
189
|
+ } catch (ex) {
|
146
|
190
|
console.log(ex, template)
|
147
|
191
|
}
|
148
|
192
|
return res;
|
...
|
...
|
|