Showing
3 changed files
with
43 additions
and
42 deletions
... | ... | @@ -20,7 +20,15 @@ |
20 | 20 | import { Inject, Injectable, NgZone } from '@angular/core'; |
21 | 21 | import { WINDOW } from '@core/services/window.service'; |
22 | 22 | import { ExceptionData } from '@app/shared/models/error.models'; |
23 | -import { deepClone, deleteNullProperties, guid, isDefined, isDefinedAndNotNull, isUndefined, createLabelFromDatasource } from '@core/utils'; | |
23 | +import { | |
24 | + createLabelFromDatasource, | |
25 | + deepClone, | |
26 | + deleteNullProperties, | |
27 | + guid, | |
28 | + isDefined, | |
29 | + isDefinedAndNotNull, | |
30 | + isUndefined | |
31 | +} from '@core/utils'; | |
24 | 32 | import { WindowMessage } from '@shared/models/window-message.model'; |
25 | 33 | import { TranslateService } from '@ngx-translate/core'; |
26 | 34 | import { customTranslationsPrefix } from '@app/shared/models/constants'; |
... | ... | @@ -34,8 +42,6 @@ import jsonSchemaDefaults from 'json-schema-defaults'; |
34 | 42 | import materialIconsCodepoints from '!raw-loader!material-design-icons/iconfont/codepoints'; |
35 | 43 | import { Observable, of, ReplaySubject } from 'rxjs'; |
36 | 44 | |
37 | -const varsRegex = /\$\{([^}]*)\}/g; | |
38 | - | |
39 | 45 | const predefinedFunctions: { [func: string]: string } = { |
40 | 46 | Sin: 'return Math.round(1000*Math.sin(time/5000));', |
41 | 47 | Cos: 'return Math.round(1000*Math.cos(time/5000));', |
... | ... | @@ -215,7 +221,7 @@ export class UtilsService { |
215 | 221 | } |
216 | 222 | |
217 | 223 | public customTranslation(translationValue: string, defaultValue: string): string { |
218 | - let result = ''; | |
224 | + let result: string; | |
219 | 225 | const translationId = customTranslationsPrefix + translationValue; |
220 | 226 | const translation = this.translate.instant(translationId); |
221 | 227 | if (translation !== translationId) { |
... | ... | @@ -226,20 +232,6 @@ export class UtilsService { |
226 | 232 | return result; |
227 | 233 | } |
228 | 234 | |
229 | - public insertVariable(pattern: string, name: string, value: any): string { | |
230 | - let result = deepClone(pattern); | |
231 | - let match = varsRegex.exec(pattern); | |
232 | - while (match !== null) { | |
233 | - const variable = match[0]; | |
234 | - const variableName = match[1]; | |
235 | - if (variableName === name) { | |
236 | - result = result.split(variable).join(value); | |
237 | - } | |
238 | - match = varsRegex.exec(pattern); | |
239 | - } | |
240 | - return result; | |
241 | - } | |
242 | - | |
243 | 235 | public guid(): string { |
244 | 236 | return guid(); |
245 | 237 | } |
... | ... | @@ -353,10 +345,6 @@ export class UtilsService { |
353 | 345 | return additionalDataKey; |
354 | 346 | } |
355 | 347 | |
356 | - public createLabelFromDatasource(datasource: Datasource, pattern: string) { | |
357 | - return createLabelFromDatasource(datasource, pattern); | |
358 | - } | |
359 | - | |
360 | 348 | public generateColors(datasources: Array<Datasource>) { |
361 | 349 | let index = 0; |
362 | 350 | datasources.forEach((datasource) => { | ... | ... |
... | ... | @@ -20,6 +20,8 @@ import { finalize, share } from 'rxjs/operators'; |
20 | 20 | import base64js from 'base64-js'; |
21 | 21 | import { Datasource } from '@app/shared/models/widget.models'; |
22 | 22 | |
23 | +const varsRegex = /\${([^}]*)}/g; | |
24 | + | |
23 | 25 | export function onParentScrollOrWindowResize(el: Node): Observable<Event> { |
24 | 26 | const scrollSubject = new Subject<Event>(); |
25 | 27 | const scrollParentNodes = scrollParents(el); |
... | ... | @@ -103,6 +105,15 @@ export function isString(value: any): boolean { |
103 | 105 | return typeof value === 'string'; |
104 | 106 | } |
105 | 107 | |
108 | +export function isEmpty(obj: any): boolean { | |
109 | + for (const key of Object.keys(obj)) { | |
110 | + if (Object.prototype.hasOwnProperty.call(obj, key)) { | |
111 | + return false; | |
112 | + } | |
113 | + } | |
114 | + return true; | |
115 | +} | |
116 | + | |
106 | 117 | export function formatValue(value: any, dec?: number, units?: string, showZeroDecimals?: boolean): string | undefined { |
107 | 118 | if (isDefined(value) && |
108 | 119 | value !== null && isNumeric(value)) { |
... | ... | @@ -435,8 +446,21 @@ export function getDescendantProp(obj: any, path: string): any { |
435 | 446 | return path.split('.').reduce((acc, part) => acc && acc[part], obj); |
436 | 447 | } |
437 | 448 | |
449 | +export function insertVariable(pattern: string, name: string, value: any): string { | |
450 | + let result = pattern; | |
451 | + let match = varsRegex.exec(pattern); | |
452 | + while (match !== null) { | |
453 | + const variable = match[0]; | |
454 | + const variableName = match[1]; | |
455 | + if (variableName === name) { | |
456 | + result = result.split(variable).join(value); | |
457 | + } | |
458 | + match = varsRegex.exec(pattern); | |
459 | + } | |
460 | + return result; | |
461 | +} | |
462 | + | |
438 | 463 | export function createLabelFromDatasource(datasource: Datasource, pattern: string) { |
439 | - const varsRegex = /\${([^}]*)}/g; | |
440 | 464 | let label = pattern; |
441 | 465 | if (!datasource) { |
442 | 466 | return label; | ... | ... |
... | ... | @@ -23,7 +23,7 @@ import { StateControllerComponent } from './state-controller.component'; |
23 | 23 | import { StatesControllerService } from '@home/pages/dashboard/states/states-controller.service'; |
24 | 24 | import { EntityId } from '@app/shared/models/id/entity-id'; |
25 | 25 | import { UtilsService } from '@core/services/utils.service'; |
26 | -import { base64toObj, objToBase64 } from '@app/core/utils'; | |
26 | +import { base64toObj, insertVariable, isEmpty, objToBase64 } from '@app/core/utils'; | |
27 | 27 | import { DashboardUtilsService } from '@core/services/dashboard-utils.service'; |
28 | 28 | import { EntityService } from '@core/http/entity.service'; |
29 | 29 | import { EntityType } from '@shared/models/entity-type.models'; |
... | ... | @@ -123,11 +123,10 @@ export class EntityStateControllerComponent extends StateControllerComponent imp |
123 | 123 | if (this.states && this.states[id]) { |
124 | 124 | this.resolveEntity(params).subscribe( |
125 | 125 | () => { |
126 | - const newState: StateObject = { | |
126 | + this.stateObject[this.stateObject.length - 1] = { | |
127 | 127 | id, |
128 | 128 | params |
129 | 129 | }; |
130 | - this.stateObject[this.stateObject.length - 1] = newState; | |
131 | 130 | this.gotoState(this.stateObject[this.stateObject.length - 1].id, true, openRightLayout); |
132 | 131 | } |
133 | 132 | ); |
... | ... | @@ -203,14 +202,14 @@ export class EntityStateControllerComponent extends StateControllerComponent imp |
203 | 202 | const targetParams = params && params.targetEntityParamName ? params[params.targetEntityParamName] : params; |
204 | 203 | const entityName = targetParams && targetParams.entityName ? targetParams.entityName : ''; |
205 | 204 | const entityLabel = targetParams && targetParams.entityLabel ? targetParams.entityLabel : ''; |
206 | - result = this.utils.insertVariable(stateName, 'entityName', entityName); | |
207 | - result = this.utils.insertVariable(result, 'entityLabel', entityLabel); | |
205 | + result = insertVariable(stateName, 'entityName', entityName); | |
206 | + result = insertVariable(result, 'entityLabel', entityLabel); | |
208 | 207 | for (const prop of Object.keys(params)) { |
209 | 208 | if (params[prop] && params[prop].entityName) { |
210 | - result = this.utils.insertVariable(result, prop + ':entityName', params[prop].entityName); | |
209 | + result = insertVariable(result, prop + ':entityName', params[prop].entityName); | |
211 | 210 | } |
212 | 211 | if (params[prop] && params[prop].entityLabel) { |
213 | - result = this.utils.insertVariable(result, prop + ':entityLabel', params[prop].entityLabel); | |
212 | + result = insertVariable(result, prop + ':entityLabel', params[prop].entityLabel); | |
214 | 213 | } |
215 | 214 | } |
216 | 215 | } |
... | ... | @@ -276,20 +275,13 @@ export class EntityStateControllerComponent extends StateControllerComponent imp |
276 | 275 | if (this.stateObject.length === 1) { |
277 | 276 | const state = this.stateObject[0]; |
278 | 277 | const rootStateId = this.dashboardUtils.getRootStateId(this.states); |
279 | - if (state.id === rootStateId && (!state.params || this.isEmpty(state.params))) { | |
278 | + if (state.id === rootStateId && (!state.params || isEmpty(state.params))) { | |
280 | 279 | return true; |
281 | 280 | } |
282 | 281 | } |
283 | 282 | return false; |
284 | 283 | } |
285 | 284 | |
286 | - private isEmpty(obj: any): boolean { | |
287 | - for (const key of Object.keys(obj)) { | |
288 | - return !Object.prototype.hasOwnProperty.call(obj, key); | |
289 | - } | |
290 | - return true; | |
291 | - } | |
292 | - | |
293 | 285 | private resolveEntity(params: StateParams): Observable<void> { |
294 | 286 | if (params && params.targetEntityParamName) { |
295 | 287 | params = params[params.targetEntityParamName]; |
... | ... | @@ -313,10 +305,7 @@ export class EntityStateControllerComponent extends StateControllerComponent imp |
313 | 305 | } |
314 | 306 | |
315 | 307 | private isEntityResolved(params: StateParams): boolean { |
316 | - if (!params.entityName || !params.entityName.length) { | |
317 | - return false; | |
318 | - } | |
319 | - return true; | |
308 | + return !(!params.entityName || !params.entityName.length); | |
320 | 309 | } |
321 | 310 | |
322 | 311 | private getStateObjById(id: string): StateObject { | ... | ... |