Commit 9953853f71f8c3a33d09eb646d889a5bda6cb1e8

Authored by Igor Kulikov
1 parent 98a15cd0

Refactoring

@@ -20,7 +20,15 @@ @@ -20,7 +20,15 @@
20 import { Inject, Injectable, NgZone } from '@angular/core'; 20 import { Inject, Injectable, NgZone } from '@angular/core';
21 import { WINDOW } from '@core/services/window.service'; 21 import { WINDOW } from '@core/services/window.service';
22 import { ExceptionData } from '@app/shared/models/error.models'; 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 import { WindowMessage } from '@shared/models/window-message.model'; 32 import { WindowMessage } from '@shared/models/window-message.model';
25 import { TranslateService } from '@ngx-translate/core'; 33 import { TranslateService } from '@ngx-translate/core';
26 import { customTranslationsPrefix } from '@app/shared/models/constants'; 34 import { customTranslationsPrefix } from '@app/shared/models/constants';
@@ -34,8 +42,6 @@ import jsonSchemaDefaults from 'json-schema-defaults'; @@ -34,8 +42,6 @@ import jsonSchemaDefaults from 'json-schema-defaults';
34 import materialIconsCodepoints from '!raw-loader!material-design-icons/iconfont/codepoints'; 42 import materialIconsCodepoints from '!raw-loader!material-design-icons/iconfont/codepoints';
35 import { Observable, of, ReplaySubject } from 'rxjs'; 43 import { Observable, of, ReplaySubject } from 'rxjs';
36 44
37 -const varsRegex = /\$\{([^}]*)\}/g;  
38 -  
39 const predefinedFunctions: { [func: string]: string } = { 45 const predefinedFunctions: { [func: string]: string } = {
40 Sin: 'return Math.round(1000*Math.sin(time/5000));', 46 Sin: 'return Math.round(1000*Math.sin(time/5000));',
41 Cos: 'return Math.round(1000*Math.cos(time/5000));', 47 Cos: 'return Math.round(1000*Math.cos(time/5000));',
@@ -215,7 +221,7 @@ export class UtilsService { @@ -215,7 +221,7 @@ export class UtilsService {
215 } 221 }
216 222
217 public customTranslation(translationValue: string, defaultValue: string): string { 223 public customTranslation(translationValue: string, defaultValue: string): string {
218 - let result = ''; 224 + let result: string;
219 const translationId = customTranslationsPrefix + translationValue; 225 const translationId = customTranslationsPrefix + translationValue;
220 const translation = this.translate.instant(translationId); 226 const translation = this.translate.instant(translationId);
221 if (translation !== translationId) { 227 if (translation !== translationId) {
@@ -226,20 +232,6 @@ export class UtilsService { @@ -226,20 +232,6 @@ export class UtilsService {
226 return result; 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 public guid(): string { 235 public guid(): string {
244 return guid(); 236 return guid();
245 } 237 }
@@ -353,10 +345,6 @@ export class UtilsService { @@ -353,10 +345,6 @@ export class UtilsService {
353 return additionalDataKey; 345 return additionalDataKey;
354 } 346 }
355 347
356 - public createLabelFromDatasource(datasource: Datasource, pattern: string) {  
357 - return createLabelFromDatasource(datasource, pattern);  
358 - }  
359 -  
360 public generateColors(datasources: Array<Datasource>) { 348 public generateColors(datasources: Array<Datasource>) {
361 let index = 0; 349 let index = 0;
362 datasources.forEach((datasource) => { 350 datasources.forEach((datasource) => {
@@ -20,6 +20,8 @@ import { finalize, share } from 'rxjs/operators'; @@ -20,6 +20,8 @@ import { finalize, share } from 'rxjs/operators';
20 import base64js from 'base64-js'; 20 import base64js from 'base64-js';
21 import { Datasource } from '@app/shared/models/widget.models'; 21 import { Datasource } from '@app/shared/models/widget.models';
22 22
  23 +const varsRegex = /\${([^}]*)}/g;
  24 +
23 export function onParentScrollOrWindowResize(el: Node): Observable<Event> { 25 export function onParentScrollOrWindowResize(el: Node): Observable<Event> {
24 const scrollSubject = new Subject<Event>(); 26 const scrollSubject = new Subject<Event>();
25 const scrollParentNodes = scrollParents(el); 27 const scrollParentNodes = scrollParents(el);
@@ -103,6 +105,15 @@ export function isString(value: any): boolean { @@ -103,6 +105,15 @@ export function isString(value: any): boolean {
103 return typeof value === 'string'; 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 export function formatValue(value: any, dec?: number, units?: string, showZeroDecimals?: boolean): string | undefined { 117 export function formatValue(value: any, dec?: number, units?: string, showZeroDecimals?: boolean): string | undefined {
107 if (isDefined(value) && 118 if (isDefined(value) &&
108 value !== null && isNumeric(value)) { 119 value !== null && isNumeric(value)) {
@@ -435,8 +446,21 @@ export function getDescendantProp(obj: any, path: string): any { @@ -435,8 +446,21 @@ export function getDescendantProp(obj: any, path: string): any {
435 return path.split('.').reduce((acc, part) => acc && acc[part], obj); 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 export function createLabelFromDatasource(datasource: Datasource, pattern: string) { 463 export function createLabelFromDatasource(datasource: Datasource, pattern: string) {
439 - const varsRegex = /\${([^}]*)}/g;  
440 let label = pattern; 464 let label = pattern;
441 if (!datasource) { 465 if (!datasource) {
442 return label; 466 return label;
@@ -23,7 +23,7 @@ import { StateControllerComponent } from './state-controller.component'; @@ -23,7 +23,7 @@ import { StateControllerComponent } from './state-controller.component';
23 import { StatesControllerService } from '@home/pages/dashboard/states/states-controller.service'; 23 import { StatesControllerService } from '@home/pages/dashboard/states/states-controller.service';
24 import { EntityId } from '@app/shared/models/id/entity-id'; 24 import { EntityId } from '@app/shared/models/id/entity-id';
25 import { UtilsService } from '@core/services/utils.service'; 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 import { DashboardUtilsService } from '@core/services/dashboard-utils.service'; 27 import { DashboardUtilsService } from '@core/services/dashboard-utils.service';
28 import { EntityService } from '@core/http/entity.service'; 28 import { EntityService } from '@core/http/entity.service';
29 import { EntityType } from '@shared/models/entity-type.models'; 29 import { EntityType } from '@shared/models/entity-type.models';
@@ -123,11 +123,10 @@ export class EntityStateControllerComponent extends StateControllerComponent imp @@ -123,11 +123,10 @@ export class EntityStateControllerComponent extends StateControllerComponent imp
123 if (this.states && this.states[id]) { 123 if (this.states && this.states[id]) {
124 this.resolveEntity(params).subscribe( 124 this.resolveEntity(params).subscribe(
125 () => { 125 () => {
126 - const newState: StateObject = { 126 + this.stateObject[this.stateObject.length - 1] = {
127 id, 127 id,
128 params 128 params
129 }; 129 };
130 - this.stateObject[this.stateObject.length - 1] = newState;  
131 this.gotoState(this.stateObject[this.stateObject.length - 1].id, true, openRightLayout); 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,14 +202,14 @@ export class EntityStateControllerComponent extends StateControllerComponent imp
203 const targetParams = params && params.targetEntityParamName ? params[params.targetEntityParamName] : params; 202 const targetParams = params && params.targetEntityParamName ? params[params.targetEntityParamName] : params;
204 const entityName = targetParams && targetParams.entityName ? targetParams.entityName : ''; 203 const entityName = targetParams && targetParams.entityName ? targetParams.entityName : '';
205 const entityLabel = targetParams && targetParams.entityLabel ? targetParams.entityLabel : ''; 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 for (const prop of Object.keys(params)) { 207 for (const prop of Object.keys(params)) {
209 if (params[prop] && params[prop].entityName) { 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 if (params[prop] && params[prop].entityLabel) { 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,20 +275,13 @@ export class EntityStateControllerComponent extends StateControllerComponent imp
276 if (this.stateObject.length === 1) { 275 if (this.stateObject.length === 1) {
277 const state = this.stateObject[0]; 276 const state = this.stateObject[0];
278 const rootStateId = this.dashboardUtils.getRootStateId(this.states); 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 return true; 279 return true;
281 } 280 }
282 } 281 }
283 return false; 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 private resolveEntity(params: StateParams): Observable<void> { 285 private resolveEntity(params: StateParams): Observable<void> {
294 if (params && params.targetEntityParamName) { 286 if (params && params.targetEntityParamName) {
295 params = params[params.targetEntityParamName]; 287 params = params[params.targetEntityParamName];
@@ -313,10 +305,7 @@ export class EntityStateControllerComponent extends StateControllerComponent imp @@ -313,10 +305,7 @@ export class EntityStateControllerComponent extends StateControllerComponent imp
313 } 305 }
314 306
315 private isEntityResolved(params: StateParams): boolean { 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 private getStateObjById(id: string): StateObject { 311 private getStateObjById(id: string): StateObject {