Refactoring

This commit is contained in:
Igor Kulikov 2020-05-05 12:19:21 +03:00
parent 98a15cd098
commit 9953853f71
3 changed files with 43 additions and 42 deletions

View File

@ -20,7 +20,15 @@
import { Inject, Injectable, NgZone } from '@angular/core'; import { Inject, Injectable, NgZone } from '@angular/core';
import { WINDOW } from '@core/services/window.service'; import { WINDOW } from '@core/services/window.service';
import { ExceptionData } from '@app/shared/models/error.models'; 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 { WindowMessage } from '@shared/models/window-message.model';
import { TranslateService } from '@ngx-translate/core'; import { TranslateService } from '@ngx-translate/core';
import { customTranslationsPrefix } from '@app/shared/models/constants'; 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 materialIconsCodepoints from '!raw-loader!material-design-icons/iconfont/codepoints';
import { Observable, of, ReplaySubject } from 'rxjs'; import { Observable, of, ReplaySubject } from 'rxjs';
const varsRegex = /\$\{([^}]*)\}/g;
const predefinedFunctions: { [func: string]: string } = { const predefinedFunctions: { [func: string]: string } = {
Sin: 'return Math.round(1000*Math.sin(time/5000));', Sin: 'return Math.round(1000*Math.sin(time/5000));',
Cos: 'return Math.round(1000*Math.cos(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 { public customTranslation(translationValue: string, defaultValue: string): string {
let result = ''; let result: string;
const translationId = customTranslationsPrefix + translationValue; const translationId = customTranslationsPrefix + translationValue;
const translation = this.translate.instant(translationId); const translation = this.translate.instant(translationId);
if (translation !== translationId) { if (translation !== translationId) {
@ -226,20 +232,6 @@ export class UtilsService {
return result; 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 { public guid(): string {
return guid(); return guid();
} }
@ -353,10 +345,6 @@ export class UtilsService {
return additionalDataKey; return additionalDataKey;
} }
public createLabelFromDatasource(datasource: Datasource, pattern: string) {
return createLabelFromDatasource(datasource, pattern);
}
public generateColors(datasources: Array<Datasource>) { public generateColors(datasources: Array<Datasource>) {
let index = 0; let index = 0;
datasources.forEach((datasource) => { datasources.forEach((datasource) => {

View File

@ -20,6 +20,8 @@ import { finalize, share } from 'rxjs/operators';
import base64js from 'base64-js'; import base64js from 'base64-js';
import { Datasource } from '@app/shared/models/widget.models'; import { Datasource } from '@app/shared/models/widget.models';
const varsRegex = /\${([^}]*)}/g;
export function onParentScrollOrWindowResize(el: Node): Observable<Event> { export function onParentScrollOrWindowResize(el: Node): Observable<Event> {
const scrollSubject = new Subject<Event>(); const scrollSubject = new Subject<Event>();
const scrollParentNodes = scrollParents(el); const scrollParentNodes = scrollParents(el);
@ -103,6 +105,15 @@ export function isString(value: any): boolean {
return typeof value === 'string'; 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 { export function formatValue(value: any, dec?: number, units?: string, showZeroDecimals?: boolean): string | undefined {
if (isDefined(value) && if (isDefined(value) &&
value !== null && isNumeric(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); 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) { export function createLabelFromDatasource(datasource: Datasource, pattern: string) {
const varsRegex = /\${([^}]*)}/g;
let label = pattern; let label = pattern;
if (!datasource) { if (!datasource) {
return label; return label;

View File

@ -23,7 +23,7 @@ import { StateControllerComponent } from './state-controller.component';
import { StatesControllerService } from '@home/pages/dashboard/states/states-controller.service'; import { StatesControllerService } from '@home/pages/dashboard/states/states-controller.service';
import { EntityId } from '@app/shared/models/id/entity-id'; import { EntityId } from '@app/shared/models/id/entity-id';
import { UtilsService } from '@core/services/utils.service'; 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 { DashboardUtilsService } from '@core/services/dashboard-utils.service';
import { EntityService } from '@core/http/entity.service'; import { EntityService } from '@core/http/entity.service';
import { EntityType } from '@shared/models/entity-type.models'; import { EntityType } from '@shared/models/entity-type.models';
@ -123,11 +123,10 @@ export class EntityStateControllerComponent extends StateControllerComponent imp
if (this.states && this.states[id]) { if (this.states && this.states[id]) {
this.resolveEntity(params).subscribe( this.resolveEntity(params).subscribe(
() => { () => {
const newState: StateObject = { this.stateObject[this.stateObject.length - 1] = {
id, id,
params params
}; };
this.stateObject[this.stateObject.length - 1] = newState;
this.gotoState(this.stateObject[this.stateObject.length - 1].id, true, openRightLayout); 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 targetParams = params && params.targetEntityParamName ? params[params.targetEntityParamName] : params;
const entityName = targetParams && targetParams.entityName ? targetParams.entityName : ''; const entityName = targetParams && targetParams.entityName ? targetParams.entityName : '';
const entityLabel = targetParams && targetParams.entityLabel ? targetParams.entityLabel : ''; const entityLabel = targetParams && targetParams.entityLabel ? targetParams.entityLabel : '';
result = this.utils.insertVariable(stateName, 'entityName', entityName); result = insertVariable(stateName, 'entityName', entityName);
result = this.utils.insertVariable(result, 'entityLabel', entityLabel); result = insertVariable(result, 'entityLabel', entityLabel);
for (const prop of Object.keys(params)) { for (const prop of Object.keys(params)) {
if (params[prop] && params[prop].entityName) { 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) { 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) { if (this.stateObject.length === 1) {
const state = this.stateObject[0]; const state = this.stateObject[0];
const rootStateId = this.dashboardUtils.getRootStateId(this.states); 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 true;
} }
} }
return false; 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> { private resolveEntity(params: StateParams): Observable<void> {
if (params && params.targetEntityParamName) { if (params && params.targetEntityParamName) {
params = params[params.targetEntityParamName]; params = params[params.targetEntityParamName];
@ -313,10 +305,7 @@ export class EntityStateControllerComponent extends StateControllerComponent imp
} }
private isEntityResolved(params: StateParams): boolean { private isEntityResolved(params: StateParams): boolean {
if (!params.entityName || !params.entityName.length) { return !(!params.entityName || !params.entityName.length);
return false;
}
return true;
} }
private getStateObjById(id: string): StateObject { private getStateObjById(id: string): StateObject {