Refactoring
This commit is contained in:
		
							parent
							
								
									98a15cd098
								
							
						
					
					
						commit
						9953853f71
					
				@ -20,7 +20,15 @@
 | 
			
		||||
import { Inject, Injectable, NgZone } from '@angular/core';
 | 
			
		||||
import { WINDOW } from '@core/services/window.service';
 | 
			
		||||
import { ExceptionData } from '@app/shared/models/error.models';
 | 
			
		||||
import { deepClone, deleteNullProperties, guid, isDefined, isDefinedAndNotNull, isUndefined, createLabelFromDatasource } from '@core/utils';
 | 
			
		||||
import {
 | 
			
		||||
  createLabelFromDatasource,
 | 
			
		||||
  deepClone,
 | 
			
		||||
  deleteNullProperties,
 | 
			
		||||
  guid,
 | 
			
		||||
  isDefined,
 | 
			
		||||
  isDefinedAndNotNull,
 | 
			
		||||
  isUndefined
 | 
			
		||||
} from '@core/utils';
 | 
			
		||||
import { WindowMessage } from '@shared/models/window-message.model';
 | 
			
		||||
import { TranslateService } from '@ngx-translate/core';
 | 
			
		||||
import { customTranslationsPrefix } from '@app/shared/models/constants';
 | 
			
		||||
@ -34,8 +42,6 @@ import jsonSchemaDefaults from 'json-schema-defaults';
 | 
			
		||||
import materialIconsCodepoints from '!raw-loader!material-design-icons/iconfont/codepoints';
 | 
			
		||||
import { Observable, of, ReplaySubject } from 'rxjs';
 | 
			
		||||
 | 
			
		||||
const varsRegex = /\$\{([^}]*)\}/g;
 | 
			
		||||
 | 
			
		||||
const predefinedFunctions: { [func: string]: string } = {
 | 
			
		||||
  Sin: 'return Math.round(1000*Math.sin(time/5000));',
 | 
			
		||||
  Cos: 'return Math.round(1000*Math.cos(time/5000));',
 | 
			
		||||
@ -215,7 +221,7 @@ export class UtilsService {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public customTranslation(translationValue: string, defaultValue: string): string {
 | 
			
		||||
    let result = '';
 | 
			
		||||
    let result: string;
 | 
			
		||||
    const translationId = customTranslationsPrefix + translationValue;
 | 
			
		||||
    const translation = this.translate.instant(translationId);
 | 
			
		||||
    if (translation !== translationId) {
 | 
			
		||||
@ -226,20 +232,6 @@ export class UtilsService {
 | 
			
		||||
    return result;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public insertVariable(pattern: string, name: string, value: any): string {
 | 
			
		||||
    let result = deepClone(pattern);
 | 
			
		||||
    let match = varsRegex.exec(pattern);
 | 
			
		||||
    while (match !== null) {
 | 
			
		||||
      const variable = match[0];
 | 
			
		||||
      const variableName = match[1];
 | 
			
		||||
      if (variableName === name) {
 | 
			
		||||
        result = result.split(variable).join(value);
 | 
			
		||||
      }
 | 
			
		||||
      match = varsRegex.exec(pattern);
 | 
			
		||||
    }
 | 
			
		||||
    return result;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public guid(): string {
 | 
			
		||||
    return guid();
 | 
			
		||||
  }
 | 
			
		||||
@ -353,10 +345,6 @@ export class UtilsService {
 | 
			
		||||
    return additionalDataKey;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public createLabelFromDatasource(datasource: Datasource, pattern: string) {
 | 
			
		||||
    return createLabelFromDatasource(datasource, pattern);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public generateColors(datasources: Array<Datasource>) {
 | 
			
		||||
    let index = 0;
 | 
			
		||||
    datasources.forEach((datasource) => {
 | 
			
		||||
 | 
			
		||||
@ -20,6 +20,8 @@ import { finalize, share } from 'rxjs/operators';
 | 
			
		||||
import base64js from 'base64-js';
 | 
			
		||||
import { Datasource } from '@app/shared/models/widget.models';
 | 
			
		||||
 | 
			
		||||
const varsRegex = /\${([^}]*)}/g;
 | 
			
		||||
 | 
			
		||||
export function onParentScrollOrWindowResize(el: Node): Observable<Event> {
 | 
			
		||||
  const scrollSubject = new Subject<Event>();
 | 
			
		||||
  const scrollParentNodes = scrollParents(el);
 | 
			
		||||
@ -103,6 +105,15 @@ export function isString(value: any): boolean {
 | 
			
		||||
  return typeof value === 'string';
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function isEmpty(obj: any): boolean {
 | 
			
		||||
  for (const key of Object.keys(obj)) {
 | 
			
		||||
    if (Object.prototype.hasOwnProperty.call(obj, key)) {
 | 
			
		||||
      return false;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function formatValue(value: any, dec?: number, units?: string, showZeroDecimals?: boolean): string | undefined {
 | 
			
		||||
  if (isDefined(value) &&
 | 
			
		||||
    value !== null && isNumeric(value)) {
 | 
			
		||||
@ -435,8 +446,21 @@ export function getDescendantProp(obj: any, path: string): any {
 | 
			
		||||
  return path.split('.').reduce((acc, part) => acc && acc[part], obj);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function insertVariable(pattern: string, name: string, value: any): string {
 | 
			
		||||
  let result = pattern;
 | 
			
		||||
  let match = varsRegex.exec(pattern);
 | 
			
		||||
  while (match !== null) {
 | 
			
		||||
    const variable = match[0];
 | 
			
		||||
    const variableName = match[1];
 | 
			
		||||
    if (variableName === name) {
 | 
			
		||||
      result = result.split(variable).join(value);
 | 
			
		||||
    }
 | 
			
		||||
    match = varsRegex.exec(pattern);
 | 
			
		||||
  }
 | 
			
		||||
  return result;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function createLabelFromDatasource(datasource: Datasource, pattern: string) {
 | 
			
		||||
  const varsRegex = /\${([^}]*)}/g;
 | 
			
		||||
  let label = pattern;
 | 
			
		||||
  if (!datasource) {
 | 
			
		||||
    return label;
 | 
			
		||||
 | 
			
		||||
@ -23,7 +23,7 @@ import { StateControllerComponent } from './state-controller.component';
 | 
			
		||||
import { StatesControllerService } from '@home/pages/dashboard/states/states-controller.service';
 | 
			
		||||
import { EntityId } from '@app/shared/models/id/entity-id';
 | 
			
		||||
import { UtilsService } from '@core/services/utils.service';
 | 
			
		||||
import { base64toObj, objToBase64 } from '@app/core/utils';
 | 
			
		||||
import { base64toObj, insertVariable, isEmpty, objToBase64 } from '@app/core/utils';
 | 
			
		||||
import { DashboardUtilsService } from '@core/services/dashboard-utils.service';
 | 
			
		||||
import { EntityService } from '@core/http/entity.service';
 | 
			
		||||
import { EntityType } from '@shared/models/entity-type.models';
 | 
			
		||||
@ -123,11 +123,10 @@ export class EntityStateControllerComponent extends StateControllerComponent imp
 | 
			
		||||
    if (this.states && this.states[id]) {
 | 
			
		||||
      this.resolveEntity(params).subscribe(
 | 
			
		||||
        () => {
 | 
			
		||||
          const newState: StateObject = {
 | 
			
		||||
          this.stateObject[this.stateObject.length - 1] = {
 | 
			
		||||
            id,
 | 
			
		||||
            params
 | 
			
		||||
          };
 | 
			
		||||
          this.stateObject[this.stateObject.length - 1] = newState;
 | 
			
		||||
          this.gotoState(this.stateObject[this.stateObject.length - 1].id, true, openRightLayout);
 | 
			
		||||
        }
 | 
			
		||||
      );
 | 
			
		||||
@ -203,14 +202,14 @@ export class EntityStateControllerComponent extends StateControllerComponent imp
 | 
			
		||||
        const targetParams = params && params.targetEntityParamName ? params[params.targetEntityParamName] : params;
 | 
			
		||||
        const entityName = targetParams && targetParams.entityName ? targetParams.entityName : '';
 | 
			
		||||
        const entityLabel = targetParams && targetParams.entityLabel ? targetParams.entityLabel : '';
 | 
			
		||||
        result = this.utils.insertVariable(stateName, 'entityName', entityName);
 | 
			
		||||
        result = this.utils.insertVariable(result, 'entityLabel', entityLabel);
 | 
			
		||||
        result = insertVariable(stateName, 'entityName', entityName);
 | 
			
		||||
        result = insertVariable(result, 'entityLabel', entityLabel);
 | 
			
		||||
        for (const prop of Object.keys(params)) {
 | 
			
		||||
          if (params[prop] && params[prop].entityName) {
 | 
			
		||||
            result = this.utils.insertVariable(result, prop + ':entityName', params[prop].entityName);
 | 
			
		||||
            result = insertVariable(result, prop + ':entityName', params[prop].entityName);
 | 
			
		||||
          }
 | 
			
		||||
          if (params[prop] && params[prop].entityLabel) {
 | 
			
		||||
            result = this.utils.insertVariable(result, prop + ':entityLabel', params[prop].entityLabel);
 | 
			
		||||
            result = insertVariable(result, prop + ':entityLabel', params[prop].entityLabel);
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
@ -276,20 +275,13 @@ export class EntityStateControllerComponent extends StateControllerComponent imp
 | 
			
		||||
    if (this.stateObject.length === 1) {
 | 
			
		||||
      const state = this.stateObject[0];
 | 
			
		||||
      const rootStateId = this.dashboardUtils.getRootStateId(this.states);
 | 
			
		||||
      if (state.id === rootStateId && (!state.params || this.isEmpty(state.params))) {
 | 
			
		||||
      if (state.id === rootStateId && (!state.params || isEmpty(state.params))) {
 | 
			
		||||
        return true;
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    return false;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  private isEmpty(obj: any): boolean {
 | 
			
		||||
    for (const key of Object.keys(obj)) {
 | 
			
		||||
      return !Object.prototype.hasOwnProperty.call(obj, key);
 | 
			
		||||
    }
 | 
			
		||||
    return true;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  private resolveEntity(params: StateParams): Observable<void> {
 | 
			
		||||
    if (params && params.targetEntityParamName) {
 | 
			
		||||
      params = params[params.targetEntityParamName];
 | 
			
		||||
@ -313,10 +305,7 @@ export class EntityStateControllerComponent extends StateControllerComponent imp
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  private isEntityResolved(params: StateParams): boolean {
 | 
			
		||||
    if (!params.entityName || !params.entityName.length) {
 | 
			
		||||
      return false;
 | 
			
		||||
    }
 | 
			
		||||
    return true;
 | 
			
		||||
    return !(!params.entityName || !params.entityName.length);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  private getStateObjById(id: string): StateObject {
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user