From b044c661d3d4d29ad8cc3d9ac1a084019a8908b8 Mon Sep 17 00:00:00 2001 From: rusikv Date: Wed, 3 Jan 2024 15:18:18 +0200 Subject: [PATCH 1/4] UI: added getAssetProfileNames and getDeviceProfileNames APIs --- .../app/core/http/asset-profile.service.ts | 10 +++++ .../app/core/http/device-profile.service.ts | 9 ++++ .../entity-subtype-autocomplete.component.ts | 26 ++++++++---- .../entity/entity-subtype-list.component.ts | 21 ++++++---- .../entity/entity-subtype-select.component.ts | 42 ++++++++++++------- 5 files changed, 75 insertions(+), 33 deletions(-) diff --git a/ui-ngx/src/app/core/http/asset-profile.service.ts b/ui-ngx/src/app/core/http/asset-profile.service.ts index 029e1afe5c..5ffc3f1e24 100644 --- a/ui-ngx/src/app/core/http/asset-profile.service.ts +++ b/ui-ngx/src/app/core/http/asset-profile.service.ts @@ -21,6 +21,8 @@ import { defaultHttpOptionsFromConfig, RequestConfig } from './http-utils'; import { Observable } from 'rxjs'; import { PageData } from '@shared/models/page/page-data'; import { AssetProfile, AssetProfileInfo } from '@shared/models/asset.models'; +import { EntityInfoData } from '@shared/models/entity.models'; +import { isDefinedAndNotNull } from '@core/utils'; @Injectable({ providedIn: 'root' @@ -68,4 +70,12 @@ export class AssetProfileService { return this.http.get>(`/api/assetProfileInfos${pageLink.toQuery()}`, defaultHttpOptionsFromConfig(config)); } + public getAssetProfileNames(activeOnly: boolean = false, config?: RequestConfig): Observable> { + let url = '/api/assetProfile/names'; + if (isDefinedAndNotNull(activeOnly)) { + url += `?activeOnly=${activeOnly}`; + } + return this.http.get>(url, defaultHttpOptionsFromConfig(config)); + } + } diff --git a/ui-ngx/src/app/core/http/device-profile.service.ts b/ui-ngx/src/app/core/http/device-profile.service.ts index fd7753a6ab..73f4e14845 100644 --- a/ui-ngx/src/app/core/http/device-profile.service.ts +++ b/ui-ngx/src/app/core/http/device-profile.service.ts @@ -31,6 +31,7 @@ import { SortOrder } from '@shared/models/page/sort-order'; import { OtaPackageService } from '@core/http/ota-package.service'; import { map, mergeMap, tap } from 'rxjs/operators'; import { Lwm2mSecurityType } from '@shared/models/lwm2m-security-config.models'; +import { EntityInfoData } from '@shared/models/entity.models'; @Injectable({ providedIn: 'root' @@ -169,4 +170,12 @@ export class DeviceProfileService { return this.http.get>(url, defaultHttpOptionsFromConfig(config)); } + public getDeviceProfileNames(activeOnly: boolean = false, config?: RequestConfig): Observable> { + let url = '/api/deviceProfile/names'; + if (isDefinedAndNotNull(activeOnly)) { + url += `?activeOnly=${activeOnly}`; + } + return this.http.get>(url, defaultHttpOptionsFromConfig(config)); + } + } diff --git a/ui-ngx/src/app/shared/components/entity/entity-subtype-autocomplete.component.ts b/ui-ngx/src/app/shared/components/entity/entity-subtype-autocomplete.component.ts index 3c28d9ceaa..d9c6bf0a6c 100644 --- a/ui-ngx/src/app/shared/components/entity/entity-subtype-autocomplete.component.ts +++ b/ui-ngx/src/app/shared/components/entity/entity-subtype-autocomplete.component.ts @@ -30,14 +30,15 @@ import { import { Store } from '@ngrx/store'; import { AppState } from '@app/core/core.state'; import { TranslateService } from '@ngx-translate/core'; -import { DeviceService } from '@core/http/device.service'; +import { DeviceProfileService } from '@core/http/device-profile.service'; import { EntitySubtype, EntityType } from '@app/shared/models/entity-type.models'; import { BroadcastService } from '@app/core/services/broadcast.service'; import { coerceBooleanProperty } from '@angular/cdk/coercion'; -import { AssetService } from '@core/http/asset.service'; +import { AssetProfileService } from '@core/http/asset-profile.service'; import { EntityViewService } from '@core/http/entity-view.service'; import { EdgeService } from '@core/http/edge.service'; import { MatFormFieldAppearance } from '@angular/material/form-field'; +import { EntityInfoData } from '@shared/models/entity.models'; @Component({ selector: 'tb-entity-subtype-autocomplete', @@ -100,8 +101,8 @@ export class EntitySubTypeAutocompleteComponent implements ControlValueAccessor, constructor(private store: Store, private broadcast: BroadcastService, public translate: TranslateService, - private deviceService: DeviceService, - private assetService: AssetService, + private deviceProfileService: DeviceProfileService, + private assetProfileService: AssetProfileService, private edgeService: EdgeService, private entityViewService: EntityViewService, private fb: UntypedFormBuilder) { @@ -229,13 +230,13 @@ export class EntitySubTypeAutocompleteComponent implements ControlValueAccessor, getSubTypes(): Observable> { if (!this.subTypes) { - let subTypesObservable: Observable>; + let subTypesObservable: Observable>; switch (this.entityType) { case EntityType.ASSET: - subTypesObservable = this.assetService.getAssetTypes({ignoreLoading: true}); + subTypesObservable = this.assetProfileService.getAssetProfileNames(false, {ignoreLoading: true}); break; case EntityType.DEVICE: - subTypesObservable = this.deviceService.getDeviceTypes({ignoreLoading: true}); + subTypesObservable = this.deviceProfileService.getDeviceProfileNames(false,{ignoreLoading: true}); break; case EntityType.EDGE: subTypesObservable = this.edgeService.getEdgeTypes({ignoreLoading: true}); @@ -247,10 +248,13 @@ export class EntitySubTypeAutocompleteComponent implements ControlValueAccessor, if (subTypesObservable) { const excludeSubTypesSet = new Set(this.excludeSubTypes); this.subTypes = subTypesObservable.pipe( - catchError(() => of([] as Array)), + catchError(() => of([] as Array)), map(subTypes => { const filteredSubTypes: Array = []; - subTypes.forEach(subType => !excludeSubTypesSet.has(subType.type) && filteredSubTypes.push(subType.type)); + subTypes.forEach(subType => { + const typeName = this.isEntitySubType(subType) ? subType.type : subType.name; + return !excludeSubTypesSet.has(typeName) && filteredSubTypes.push(typeName); + }); return filteredSubTypes; }), publishReplay(1), @@ -263,6 +267,10 @@ export class EntitySubTypeAutocompleteComponent implements ControlValueAccessor, return this.subTypes; } + private isEntitySubType(object: EntitySubtype | EntityInfoData): object is EntitySubtype { + return 'type' in object; + } + clear() { this.subTypeFormGroup.get('subType').patchValue(null, {emitEvent: true}); setTimeout(() => { diff --git a/ui-ngx/src/app/shared/components/entity/entity-subtype-list.component.ts b/ui-ngx/src/app/shared/components/entity/entity-subtype-list.component.ts index 89900fb065..fe843e77fc 100644 --- a/ui-ngx/src/app/shared/components/entity/entity-subtype-list.component.ts +++ b/ui-ngx/src/app/shared/components/entity/entity-subtype-list.component.ts @@ -22,8 +22,8 @@ import { TranslateService } from '@ngx-translate/core'; import { EntitySubtype, EntityType } from '@shared/models/entity-type.models'; import { MatAutocomplete, MatAutocompleteSelectedEvent } from '@angular/material/autocomplete'; import { MatChipGrid, MatChipInputEvent } from '@angular/material/chips'; -import { AssetService } from '@core/http/asset.service'; -import { DeviceService } from '@core/http/device.service'; +import { AssetProfileService } from '@core/http/asset-profile.service'; +import { DeviceProfileService } from '@core/http/device-profile.service'; import { EdgeService } from '@core/http/edge.service'; import { EntityViewService } from '@core/http/entity-view.service'; import { BroadcastService } from '@core/services/broadcast.service'; @@ -34,6 +34,7 @@ import { coerceArray, coerceBoolean } from '@shared/decorators/coercion'; import { PageLink } from '@shared/models/page/page-link'; import { PageData } from '@shared/models/page/page-data'; import { UtilsService } from '@core/services/utils.service'; +import { EntityInfoData } from '@shared/models/entity.models'; @Component({ selector: 'tb-entity-subtype-list', @@ -125,8 +126,8 @@ export class EntitySubTypeListComponent implements ControlValueAccessor, OnInit, constructor(private broadcast: BroadcastService, public translate: TranslateService, - private assetService: AssetService, - private deviceService: DeviceService, + private assetProfileService: AssetProfileService, + private deviceProfileService: DeviceProfileService, private edgeService: EdgeService, private entityViewService: EntityViewService, private alarmService: AlarmService, @@ -326,13 +327,13 @@ export class EntitySubTypeListComponent implements ControlValueAccessor, OnInit, } } if (!this.entitySubtypes) { - let subTypesObservable: Observable>; + let subTypesObservable: Observable>; switch (this.entityType) { case EntityType.ASSET: - subTypesObservable = this.assetService.getAssetTypes({ignoreLoading: true}); + subTypesObservable = this.assetProfileService.getAssetProfileNames(false, {ignoreLoading: true}); break; case EntityType.DEVICE: - subTypesObservable = this.deviceService.getDeviceTypes({ignoreLoading: true}); + subTypesObservable = this.deviceProfileService.getDeviceProfileNames(false,{ignoreLoading: true}); break; case EntityType.EDGE: subTypesObservable = this.edgeService.getEdgeTypes({ignoreLoading: true}); @@ -343,7 +344,7 @@ export class EntitySubTypeListComponent implements ControlValueAccessor, OnInit, } if (subTypesObservable) { this.entitySubtypes = subTypesObservable.pipe( - map(subTypes => subTypes.map(subType => subType.type)), + map(subTypes => subTypes.map(subType => this.isEntitySubType(subType) ? subType.type : subType.name)), share({ connector: () => new ReplaySubject(1), resetOnError: false, @@ -358,6 +359,10 @@ export class EntitySubTypeListComponent implements ControlValueAccessor, OnInit, return this.entitySubtypes; } + private isEntitySubType(object: EntitySubtype | EntityInfoData): object is EntitySubtype { + return 'type' in object; + } + onFocus() { if (this.dirty) { this.entitySubtypeListFormGroup.get('entitySubtype').updateValueAndValidity({onlySelf: true, emitEvent: true}); diff --git a/ui-ngx/src/app/shared/components/entity/entity-subtype-select.component.ts b/ui-ngx/src/app/shared/components/entity/entity-subtype-select.component.ts index e999f06762..eb2230860d 100644 --- a/ui-ngx/src/app/shared/components/entity/entity-subtype-select.component.ts +++ b/ui-ngx/src/app/shared/components/entity/entity-subtype-select.component.ts @@ -21,13 +21,14 @@ import { map, mergeMap, publishReplay, refCount, startWith, tap } from 'rxjs/ope import { Store } from '@ngrx/store'; import { AppState } from '@app/core/core.state'; import { TranslateService } from '@ngx-translate/core'; -import { DeviceService } from '@core/http/device.service'; +import { DeviceProfileService } from '@core/http/device-profile.service'; import { EntitySubtype, EntityType } from '@app/shared/models/entity-type.models'; import { BroadcastService } from '@app/core/services/broadcast.service'; -import { AssetService } from '@core/http/asset.service'; +import { AssetProfileService } from '@core/http/asset-profile.service'; import { EdgeService } from '@core/http/edge.service'; import { EntityViewService } from '@core/http/entity-view.service'; import { SubscriptSizing } from '@angular/material/form-field'; +import { EntityInfoData } from '@shared/models/entity.models'; @Component({ selector: 'tb-entity-subtype-select', @@ -66,11 +67,11 @@ export class EntitySubTypeSelectComponent implements ControlValueAccessor, OnIni entitySubtypeTitle: string; entitySubtypeRequiredText: string; - subTypesOptions: Observable>; + subTypesOptions: Observable>; private subTypesOptionsSubject: Subject = new Subject(); - subTypes: Observable>; + subTypes: Observable>; subTypesLoaded = false; @@ -81,8 +82,8 @@ export class EntitySubTypeSelectComponent implements ControlValueAccessor, OnIni constructor(private store: Store, private broadcast: BroadcastService, public translate: TranslateService, - private deviceService: DeviceService, - private assetService: AssetService, + private deviceProfileService: DeviceProfileService, + private assetProfileService: AssetProfileService, private edgeService: EdgeService, private entityViewService: EntityViewService, private fb: UntypedFormBuilder) { @@ -136,7 +137,7 @@ export class EntitySubTypeSelectComponent implements ControlValueAccessor, OnIni } this.subTypesOptions = this.subTypesOptionsSubject.asObservable().pipe( - startWith(''), + startWith(''), mergeMap(() => this.getSubTypes()) ); @@ -193,34 +194,39 @@ export class EntitySubTypeSelectComponent implements ControlValueAccessor, OnIni } } - displaySubTypeFn(subType?: EntitySubtype | string): string | undefined { + displaySubTypeFn(subType?: EntitySubtype | string | EntityInfoData): string | undefined { if (subType && typeof subType !== 'string') { + const typeName = this.isEntitySubType(subType) ? subType.type : subType.name; if (this.typeTranslatePrefix) { - return this.translate.instant(this.typeTranslatePrefix + '.' + subType.type); + return this.translate.instant(this.typeTranslatePrefix + '.' + typeName); } else { - return subType.type; + return typeName; } } else { return this.translate.instant('entity.all-subtypes'); } } - findSubTypes(searchText?: string): Observable> { + findSubTypes(searchText: string): Observable> { return this.getSubTypes().pipe( map(subTypes => subTypes.filter( subType => { - return searchText ? (typeof subType === 'string' ? false : subType.type === searchText) : false; + if (typeof subType === 'string') { + return false; + } else { + return this.isEntitySubType(subType) ? subType.type : subType.name === searchText; + } })) ); } - getSubTypes(): Observable> { + getSubTypes(): Observable> { if (!this.subTypes) { switch (this.entityType) { case EntityType.ASSET: - this.subTypes = this.assetService.getAssetTypes({ignoreLoading: true}); + this.subTypes = this.assetProfileService.getAssetProfileNames(false, {ignoreLoading: true}); break; case EntityType.DEVICE: - this.subTypes = this.deviceService.getDeviceTypes({ignoreLoading: true}); + this.subTypes = this.deviceProfileService.getDeviceProfileNames(false, {ignoreLoading: true}); break; case EntityType.EDGE: this.subTypes = this.edgeService.getEdgeTypes({ignoreLoading: true}); @@ -243,7 +249,7 @@ export class EntitySubTypeSelectComponent implements ControlValueAccessor, OnIni if (typeof subType === 'string') { return subType === type; } else { - return subType.type === strType; + return this.isEntitySubType(subType) ? subType.type : subType.name === strType; } }); if (found) { @@ -259,4 +265,8 @@ export class EntitySubTypeSelectComponent implements ControlValueAccessor, OnIni } return this.subTypes; } + + private isEntitySubType(object: EntitySubtype | EntityInfoData): object is EntitySubtype { + return 'type' in object; + } } From 7effa018ce6147809e84efc83fb4361464d23a12 Mon Sep 17 00:00:00 2001 From: rusikv Date: Fri, 5 Jan 2024 15:43:29 +0200 Subject: [PATCH 2/4] UI: added getEntitySubtypesObservable method to entity service, code cleanup --- ui-ngx/src/app/core/http/entity.service.ts | 27 ++++++ .../entity-subtype-autocomplete.component.ts | 49 ++++------- .../entity/entity-subtype-list.component.ts | 26 +----- .../entity/entity-subtype-select.component.ts | 83 ++++++------------- 4 files changed, 73 insertions(+), 112 deletions(-) diff --git a/ui-ngx/src/app/core/http/entity.service.ts b/ui-ngx/src/app/core/http/entity.service.ts index 968329d36a..677db94512 100644 --- a/ui-ngx/src/app/core/http/entity.service.ts +++ b/ui-ngx/src/app/core/http/entity.service.ts @@ -1519,4 +1519,31 @@ export class EntityService { } return entityObservable; } + + public getEntitySubtypesObservable(entityType: EntityType): Observable> { + let observable: Observable>; + switch (entityType) { + case EntityType.ASSET: + observable = this.assetProfileService.getAssetProfileNames(false, {ignoreLoading: true}).pipe( + map(subTypes => subTypes.map(subType => subType.name)) + ); + break; + case EntityType.DEVICE: + observable = this.deviceProfileService.getDeviceProfileNames(false,{ignoreLoading: true}).pipe( + map(subTypes => subTypes.map(subType => subType.name)) + ); + break; + case EntityType.EDGE: + observable = this.edgeService.getEdgeTypes({ignoreLoading: true}).pipe( + map(subTypes => subTypes.map(subType => subType.type)) + ); + break; + case EntityType.ENTITY_VIEW: + observable = this.entityViewService.getEntityViewTypes({ignoreLoading: true}).pipe( + map(subTypes => subTypes.map(subType => subType.type)) + ); + break; + } + return observable; + } } diff --git a/ui-ngx/src/app/shared/components/entity/entity-subtype-autocomplete.component.ts b/ui-ngx/src/app/shared/components/entity/entity-subtype-autocomplete.component.ts index d9c6bf0a6c..dddfe5fe4b 100644 --- a/ui-ngx/src/app/shared/components/entity/entity-subtype-autocomplete.component.ts +++ b/ui-ngx/src/app/shared/components/entity/entity-subtype-autocomplete.component.ts @@ -16,29 +16,28 @@ import { AfterViewInit, Component, ElementRef, forwardRef, Input, OnDestroy, OnInit, ViewChild } from '@angular/core'; import { ControlValueAccessor, UntypedFormBuilder, UntypedFormGroup, NG_VALUE_ACCESSOR, Validators } from '@angular/forms'; -import { Observable, of, Subscription, throwError } from 'rxjs'; +import { Observable, of, ReplaySubject, Subscription, throwError } from 'rxjs'; import { catchError, debounceTime, distinctUntilChanged, map, - publishReplay, - refCount, switchMap, - tap + tap, + share, } from 'rxjs/operators'; import { Store } from '@ngrx/store'; import { AppState } from '@app/core/core.state'; import { TranslateService } from '@ngx-translate/core'; import { DeviceProfileService } from '@core/http/device-profile.service'; -import { EntitySubtype, EntityType } from '@app/shared/models/entity-type.models'; +import { EntityType } from '@app/shared/models/entity-type.models'; import { BroadcastService } from '@app/core/services/broadcast.service'; import { coerceBooleanProperty } from '@angular/cdk/coercion'; import { AssetProfileService } from '@core/http/asset-profile.service'; import { EntityViewService } from '@core/http/entity-view.service'; import { EdgeService } from '@core/http/edge.service'; import { MatFormFieldAppearance } from '@angular/material/form-field'; -import { EntityInfoData } from '@shared/models/entity.models'; +import { EntityService } from '@core/http/entity.service'; @Component({ selector: 'tb-entity-subtype-autocomplete', @@ -105,7 +104,8 @@ export class EntitySubTypeAutocompleteComponent implements ControlValueAccessor, private assetProfileService: AssetProfileService, private edgeService: EdgeService, private entityViewService: EntityViewService, - private fb: UntypedFormBuilder) { + private fb: UntypedFormBuilder, + private entityService: EntityService) { this.subTypeFormGroup = this.fb.group({ subType: [null, Validators.maxLength(255)] }); @@ -230,35 +230,22 @@ export class EntitySubTypeAutocompleteComponent implements ControlValueAccessor, getSubTypes(): Observable> { if (!this.subTypes) { - let subTypesObservable: Observable>; - switch (this.entityType) { - case EntityType.ASSET: - subTypesObservable = this.assetProfileService.getAssetProfileNames(false, {ignoreLoading: true}); - break; - case EntityType.DEVICE: - subTypesObservable = this.deviceProfileService.getDeviceProfileNames(false,{ignoreLoading: true}); - break; - case EntityType.EDGE: - subTypesObservable = this.edgeService.getEdgeTypes({ignoreLoading: true}); - break; - case EntityType.ENTITY_VIEW: - subTypesObservable = this.entityViewService.getEntityViewTypes({ignoreLoading: true}); - break; - } + const subTypesObservable = this.entityService.getEntitySubtypesObservable(this.entityType); if (subTypesObservable) { const excludeSubTypesSet = new Set(this.excludeSubTypes); this.subTypes = subTypesObservable.pipe( - catchError(() => of([] as Array)), + catchError(() => of([] as Array)), map(subTypes => { const filteredSubTypes: Array = []; - subTypes.forEach(subType => { - const typeName = this.isEntitySubType(subType) ? subType.type : subType.name; - return !excludeSubTypesSet.has(typeName) && filteredSubTypes.push(typeName); - }); + subTypes.forEach(subType => !excludeSubTypesSet.has(subType) && filteredSubTypes.push(subType)); return filteredSubTypes; }), - publishReplay(1), - refCount() + share({ + connector: () => new ReplaySubject(1), + resetOnError: false, + resetOnComplete: false, + resetOnRefCountZero: false, + }) ); } else { return throwError(null); @@ -267,10 +254,6 @@ export class EntitySubTypeAutocompleteComponent implements ControlValueAccessor, return this.subTypes; } - private isEntitySubType(object: EntitySubtype | EntityInfoData): object is EntitySubtype { - return 'type' in object; - } - clear() { this.subTypeFormGroup.get('subType').patchValue(null, {emitEvent: true}); setTimeout(() => { diff --git a/ui-ngx/src/app/shared/components/entity/entity-subtype-list.component.ts b/ui-ngx/src/app/shared/components/entity/entity-subtype-list.component.ts index fe843e77fc..234f3c7e83 100644 --- a/ui-ngx/src/app/shared/components/entity/entity-subtype-list.component.ts +++ b/ui-ngx/src/app/shared/components/entity/entity-subtype-list.component.ts @@ -34,7 +34,7 @@ import { coerceArray, coerceBoolean } from '@shared/decorators/coercion'; import { PageLink } from '@shared/models/page/page-link'; import { PageData } from '@shared/models/page/page-data'; import { UtilsService } from '@core/services/utils.service'; -import { EntityInfoData } from '@shared/models/entity.models'; +import { EntityService } from '@core/http/entity.service'; @Component({ selector: 'tb-entity-subtype-list', @@ -132,7 +132,8 @@ export class EntitySubTypeListComponent implements ControlValueAccessor, OnInit, private entityViewService: EntityViewService, private alarmService: AlarmService, private utils: UtilsService, - private fb: FormBuilder) { + private fb: FormBuilder, + private entityService: EntityService) { this.entitySubtypeListFormGroup = this.fb.group({ entitySubtypeList: [this.entitySubtypeList, this.required ? [Validators.required] : []], entitySubtype: [null] @@ -327,24 +328,9 @@ export class EntitySubTypeListComponent implements ControlValueAccessor, OnInit, } } if (!this.entitySubtypes) { - let subTypesObservable: Observable>; - switch (this.entityType) { - case EntityType.ASSET: - subTypesObservable = this.assetProfileService.getAssetProfileNames(false, {ignoreLoading: true}); - break; - case EntityType.DEVICE: - subTypesObservable = this.deviceProfileService.getDeviceProfileNames(false,{ignoreLoading: true}); - break; - case EntityType.EDGE: - subTypesObservable = this.edgeService.getEdgeTypes({ignoreLoading: true}); - break; - case EntityType.ENTITY_VIEW: - subTypesObservable = this.entityViewService.getEntityViewTypes({ignoreLoading: true}); - break; - } + const subTypesObservable = this.entityService.getEntitySubtypesObservable(this.entityType); if (subTypesObservable) { this.entitySubtypes = subTypesObservable.pipe( - map(subTypes => subTypes.map(subType => this.isEntitySubType(subType) ? subType.type : subType.name)), share({ connector: () => new ReplaySubject(1), resetOnError: false, @@ -359,10 +345,6 @@ export class EntitySubTypeListComponent implements ControlValueAccessor, OnInit, return this.entitySubtypes; } - private isEntitySubType(object: EntitySubtype | EntityInfoData): object is EntitySubtype { - return 'type' in object; - } - onFocus() { if (this.dirty) { this.entitySubtypeListFormGroup.get('entitySubtype').updateValueAndValidity({onlySelf: true, emitEvent: true}); diff --git a/ui-ngx/src/app/shared/components/entity/entity-subtype-select.component.ts b/ui-ngx/src/app/shared/components/entity/entity-subtype-select.component.ts index eb2230860d..5da0c9ff4b 100644 --- a/ui-ngx/src/app/shared/components/entity/entity-subtype-select.component.ts +++ b/ui-ngx/src/app/shared/components/entity/entity-subtype-select.component.ts @@ -17,18 +17,19 @@ import { AfterViewInit, Component, forwardRef, Input, OnDestroy, OnInit } from '@angular/core'; import { ControlValueAccessor, UntypedFormBuilder, UntypedFormGroup, NG_VALUE_ACCESSOR } from '@angular/forms'; import { Observable, Subject, Subscription, throwError } from 'rxjs'; -import { map, mergeMap, publishReplay, refCount, startWith, tap } from 'rxjs/operators'; +import { map, mergeMap, startWith, tap, share } from 'rxjs/operators'; import { Store } from '@ngrx/store'; import { AppState } from '@app/core/core.state'; import { TranslateService } from '@ngx-translate/core'; import { DeviceProfileService } from '@core/http/device-profile.service'; -import { EntitySubtype, EntityType } from '@app/shared/models/entity-type.models'; +import { EntityType } from '@app/shared/models/entity-type.models'; import { BroadcastService } from '@app/core/services/broadcast.service'; import { AssetProfileService } from '@core/http/asset-profile.service'; import { EdgeService } from '@core/http/edge.service'; import { EntityViewService } from '@core/http/entity-view.service'; import { SubscriptSizing } from '@angular/material/form-field'; -import { EntityInfoData } from '@shared/models/entity.models'; +import { isNotEmptyStr } from '@core/utils'; +import { EntityService } from '@core/http/entity.service'; @Component({ selector: 'tb-entity-subtype-select', @@ -67,11 +68,11 @@ export class EntitySubTypeSelectComponent implements ControlValueAccessor, OnIni entitySubtypeTitle: string; entitySubtypeRequiredText: string; - subTypesOptions: Observable>; + subTypesOptions: Observable>; private subTypesOptionsSubject: Subject = new Subject(); - subTypes: Observable>; + private subTypes: Observable>; subTypesLoaded = false; @@ -86,7 +87,8 @@ export class EntitySubTypeSelectComponent implements ControlValueAccessor, OnIni private assetProfileService: AssetProfileService, private edgeService: EdgeService, private entityViewService: EntityViewService, - private fb: UntypedFormBuilder) { + private fb: UntypedFormBuilder, + private entityService: EntityService) { this.subTypeFormGroup = this.fb.group({ subType: [''] }); @@ -137,7 +139,7 @@ export class EntitySubTypeSelectComponent implements ControlValueAccessor, OnIni } this.subTypesOptions = this.subTypesOptionsSubject.asObservable().pipe( - startWith(''), + startWith(''), mergeMap(() => this.getSubTypes()) ); @@ -147,7 +149,7 @@ export class EntitySubTypeSelectComponent implements ControlValueAccessor, OnIni if (!value || value === '') { modelValue = ''; } else { - modelValue = value.type; + modelValue = value; } this.updateView(modelValue); } @@ -194,70 +196,41 @@ export class EntitySubTypeSelectComponent implements ControlValueAccessor, OnIni } } - displaySubTypeFn(subType?: EntitySubtype | string | EntityInfoData): string | undefined { - if (subType && typeof subType !== 'string') { - const typeName = this.isEntitySubType(subType) ? subType.type : subType.name; + displaySubTypeFn(subType?: string): string | undefined { + if (isNotEmptyStr(subType)) { if (this.typeTranslatePrefix) { - return this.translate.instant(this.typeTranslatePrefix + '.' + typeName); + return this.translate.instant(this.typeTranslatePrefix + '.' + subType); } else { - return typeName; + return subType; } } else { return this.translate.instant('entity.all-subtypes'); } } - findSubTypes(searchText: string): Observable> { + findSubTypes(searchText: string): Observable> { return this.getSubTypes().pipe( - map(subTypes => subTypes.filter( subType => { - if (typeof subType === 'string') { - return false; - } else { - return this.isEntitySubType(subType) ? subType.type : subType.name === searchText; - } - })) + map(subTypes => subTypes.filter( subType => subType === searchText)) ); } - getSubTypes(): Observable> { + getSubTypes(): Observable> { if (!this.subTypes) { - switch (this.entityType) { - case EntityType.ASSET: - this.subTypes = this.assetProfileService.getAssetProfileNames(false, {ignoreLoading: true}); - break; - case EntityType.DEVICE: - this.subTypes = this.deviceProfileService.getDeviceProfileNames(false, {ignoreLoading: true}); - break; - case EntityType.EDGE: - this.subTypes = this.edgeService.getEdgeTypes({ignoreLoading: true}); - break; - case EntityType.ENTITY_VIEW: - this.subTypes = this.entityViewService.getEntityViewTypes({ignoreLoading: true}); - break; - } - if (this.subTypes) { - this.subTypes = this.subTypes.pipe( - map((allSubtypes) => { - allSubtypes.unshift(''); - this.subTypesLoaded = true; - return allSubtypes; + const subTypesObservable = this.entityService.getEntitySubtypesObservable(this.entityType); + if (subTypesObservable) { + this.subTypes = subTypesObservable.pipe( + map((subTypes) => { + this.subTypesLoaded = true; + subTypes.unshift(''); + return subTypes; }), tap((subTypes) => { - const type: EntitySubtype | string = this.subTypeFormGroup.get('subType').value; - const strType = typeof type === 'string' ? type : type.type; - const found = subTypes.find((subType) => { - if (typeof subType === 'string') { - return subType === type; - } else { - return this.isEntitySubType(subType) ? subType.type : subType.name === strType; - } - }); + const found = subTypes.find(subType => subType === this.subTypeFormGroup.get('subType').value); if (found) { this.subTypeFormGroup.get('subType').patchValue(found); } }), - publishReplay(1), - refCount() + share() ); } else { return throwError(null); @@ -265,8 +238,4 @@ export class EntitySubTypeSelectComponent implements ControlValueAccessor, OnIni } return this.subTypes; } - - private isEntitySubType(object: EntitySubtype | EntityInfoData): object is EntitySubtype { - return 'type' in object; - } } From b1a2361b79405ada02ddb26c3b06d162a23a2fdf Mon Sep 17 00:00:00 2001 From: rusikv Date: Mon, 8 Jan 2024 11:17:50 +0200 Subject: [PATCH 3/4] UI: unused services cleanup --- .../entity/entity-subtype-autocomplete.component.ts | 4 ---- .../shared/components/entity/entity-subtype-list.component.ts | 4 ---- .../components/entity/entity-subtype-select.component.ts | 4 ---- 3 files changed, 12 deletions(-) diff --git a/ui-ngx/src/app/shared/components/entity/entity-subtype-autocomplete.component.ts b/ui-ngx/src/app/shared/components/entity/entity-subtype-autocomplete.component.ts index dddfe5fe4b..4152f48390 100644 --- a/ui-ngx/src/app/shared/components/entity/entity-subtype-autocomplete.component.ts +++ b/ui-ngx/src/app/shared/components/entity/entity-subtype-autocomplete.component.ts @@ -29,11 +29,9 @@ import { import { Store } from '@ngrx/store'; import { AppState } from '@app/core/core.state'; import { TranslateService } from '@ngx-translate/core'; -import { DeviceProfileService } from '@core/http/device-profile.service'; import { EntityType } from '@app/shared/models/entity-type.models'; import { BroadcastService } from '@app/core/services/broadcast.service'; import { coerceBooleanProperty } from '@angular/cdk/coercion'; -import { AssetProfileService } from '@core/http/asset-profile.service'; import { EntityViewService } from '@core/http/entity-view.service'; import { EdgeService } from '@core/http/edge.service'; import { MatFormFieldAppearance } from '@angular/material/form-field'; @@ -100,8 +98,6 @@ export class EntitySubTypeAutocompleteComponent implements ControlValueAccessor, constructor(private store: Store, private broadcast: BroadcastService, public translate: TranslateService, - private deviceProfileService: DeviceProfileService, - private assetProfileService: AssetProfileService, private edgeService: EdgeService, private entityViewService: EntityViewService, private fb: UntypedFormBuilder, diff --git a/ui-ngx/src/app/shared/components/entity/entity-subtype-list.component.ts b/ui-ngx/src/app/shared/components/entity/entity-subtype-list.component.ts index 234f3c7e83..126d284dac 100644 --- a/ui-ngx/src/app/shared/components/entity/entity-subtype-list.component.ts +++ b/ui-ngx/src/app/shared/components/entity/entity-subtype-list.component.ts @@ -22,8 +22,6 @@ import { TranslateService } from '@ngx-translate/core'; import { EntitySubtype, EntityType } from '@shared/models/entity-type.models'; import { MatAutocomplete, MatAutocompleteSelectedEvent } from '@angular/material/autocomplete'; import { MatChipGrid, MatChipInputEvent } from '@angular/material/chips'; -import { AssetProfileService } from '@core/http/asset-profile.service'; -import { DeviceProfileService } from '@core/http/device-profile.service'; import { EdgeService } from '@core/http/edge.service'; import { EntityViewService } from '@core/http/entity-view.service'; import { BroadcastService } from '@core/services/broadcast.service'; @@ -126,8 +124,6 @@ export class EntitySubTypeListComponent implements ControlValueAccessor, OnInit, constructor(private broadcast: BroadcastService, public translate: TranslateService, - private assetProfileService: AssetProfileService, - private deviceProfileService: DeviceProfileService, private edgeService: EdgeService, private entityViewService: EntityViewService, private alarmService: AlarmService, diff --git a/ui-ngx/src/app/shared/components/entity/entity-subtype-select.component.ts b/ui-ngx/src/app/shared/components/entity/entity-subtype-select.component.ts index 5da0c9ff4b..028f086b1a 100644 --- a/ui-ngx/src/app/shared/components/entity/entity-subtype-select.component.ts +++ b/ui-ngx/src/app/shared/components/entity/entity-subtype-select.component.ts @@ -21,10 +21,8 @@ import { map, mergeMap, startWith, tap, share } from 'rxjs/operators'; import { Store } from '@ngrx/store'; import { AppState } from '@app/core/core.state'; import { TranslateService } from '@ngx-translate/core'; -import { DeviceProfileService } from '@core/http/device-profile.service'; import { EntityType } from '@app/shared/models/entity-type.models'; import { BroadcastService } from '@app/core/services/broadcast.service'; -import { AssetProfileService } from '@core/http/asset-profile.service'; import { EdgeService } from '@core/http/edge.service'; import { EntityViewService } from '@core/http/entity-view.service'; import { SubscriptSizing } from '@angular/material/form-field'; @@ -83,8 +81,6 @@ export class EntitySubTypeSelectComponent implements ControlValueAccessor, OnIni constructor(private store: Store, private broadcast: BroadcastService, public translate: TranslateService, - private deviceProfileService: DeviceProfileService, - private assetProfileService: AssetProfileService, private edgeService: EdgeService, private entityViewService: EntityViewService, private fb: UntypedFormBuilder, From 9f6fd889425a5b0f33c2f4f9ab5a51cfacc4c7d4 Mon Sep 17 00:00:00 2001 From: rusikv Date: Mon, 8 Jan 2024 12:10:20 +0200 Subject: [PATCH 4/4] UI: unused servies cleanup --- .../entity/entity-subtype-autocomplete.component.ts | 4 ---- .../shared/components/entity/entity-subtype-list.component.ts | 4 ---- .../components/entity/entity-subtype-select.component.ts | 4 ---- 3 files changed, 12 deletions(-) diff --git a/ui-ngx/src/app/shared/components/entity/entity-subtype-autocomplete.component.ts b/ui-ngx/src/app/shared/components/entity/entity-subtype-autocomplete.component.ts index 4152f48390..f30cc92246 100644 --- a/ui-ngx/src/app/shared/components/entity/entity-subtype-autocomplete.component.ts +++ b/ui-ngx/src/app/shared/components/entity/entity-subtype-autocomplete.component.ts @@ -32,8 +32,6 @@ import { TranslateService } from '@ngx-translate/core'; import { EntityType } from '@app/shared/models/entity-type.models'; import { BroadcastService } from '@app/core/services/broadcast.service'; import { coerceBooleanProperty } from '@angular/cdk/coercion'; -import { EntityViewService } from '@core/http/entity-view.service'; -import { EdgeService } from '@core/http/edge.service'; import { MatFormFieldAppearance } from '@angular/material/form-field'; import { EntityService } from '@core/http/entity.service'; @@ -98,8 +96,6 @@ export class EntitySubTypeAutocompleteComponent implements ControlValueAccessor, constructor(private store: Store, private broadcast: BroadcastService, public translate: TranslateService, - private edgeService: EdgeService, - private entityViewService: EntityViewService, private fb: UntypedFormBuilder, private entityService: EntityService) { this.subTypeFormGroup = this.fb.group({ diff --git a/ui-ngx/src/app/shared/components/entity/entity-subtype-list.component.ts b/ui-ngx/src/app/shared/components/entity/entity-subtype-list.component.ts index 126d284dac..da76a27382 100644 --- a/ui-ngx/src/app/shared/components/entity/entity-subtype-list.component.ts +++ b/ui-ngx/src/app/shared/components/entity/entity-subtype-list.component.ts @@ -22,8 +22,6 @@ import { TranslateService } from '@ngx-translate/core'; import { EntitySubtype, EntityType } from '@shared/models/entity-type.models'; import { MatAutocomplete, MatAutocompleteSelectedEvent } from '@angular/material/autocomplete'; import { MatChipGrid, MatChipInputEvent } from '@angular/material/chips'; -import { EdgeService } from '@core/http/edge.service'; -import { EntityViewService } from '@core/http/entity-view.service'; import { BroadcastService } from '@core/services/broadcast.service'; import { COMMA, ENTER, SEMICOLON } from '@angular/cdk/keycodes'; import { AlarmService } from '@core/http/alarm.service'; @@ -124,8 +122,6 @@ export class EntitySubTypeListComponent implements ControlValueAccessor, OnInit, constructor(private broadcast: BroadcastService, public translate: TranslateService, - private edgeService: EdgeService, - private entityViewService: EntityViewService, private alarmService: AlarmService, private utils: UtilsService, private fb: FormBuilder, diff --git a/ui-ngx/src/app/shared/components/entity/entity-subtype-select.component.ts b/ui-ngx/src/app/shared/components/entity/entity-subtype-select.component.ts index 028f086b1a..dc3283cd9c 100644 --- a/ui-ngx/src/app/shared/components/entity/entity-subtype-select.component.ts +++ b/ui-ngx/src/app/shared/components/entity/entity-subtype-select.component.ts @@ -23,8 +23,6 @@ import { AppState } from '@app/core/core.state'; import { TranslateService } from '@ngx-translate/core'; import { EntityType } from '@app/shared/models/entity-type.models'; import { BroadcastService } from '@app/core/services/broadcast.service'; -import { EdgeService } from '@core/http/edge.service'; -import { EntityViewService } from '@core/http/entity-view.service'; import { SubscriptSizing } from '@angular/material/form-field'; import { isNotEmptyStr } from '@core/utils'; import { EntityService } from '@core/http/entity.service'; @@ -81,8 +79,6 @@ export class EntitySubTypeSelectComponent implements ControlValueAccessor, OnIni constructor(private store: Store, private broadcast: BroadcastService, public translate: TranslateService, - private edgeService: EdgeService, - private entityViewService: EntityViewService, private fb: UntypedFormBuilder, private entityService: EntityService) { this.subTypeFormGroup = this.fb.group({