|
|
|
@ -16,14 +16,12 @@ |
|
|
|
|
|
|
|
import { AfterViewInit, Component, ElementRef, forwardRef, Input, OnDestroy, OnInit, ViewChild } from '@angular/core'; |
|
|
|
import { ControlValueAccessor, FormBuilder, FormGroup, NG_VALUE_ACCESSOR, Validators } from '@angular/forms'; |
|
|
|
import { Observable, Subscription, throwError } from 'rxjs'; |
|
|
|
import { map, mergeMap, publishReplay, refCount, share } from 'rxjs/operators'; |
|
|
|
import { Store } from '@ngrx/store'; |
|
|
|
import { AppState } from '@app/core/core.state'; |
|
|
|
import { Observable, ReplaySubject, Subscription, throwError } from 'rxjs'; |
|
|
|
import { debounceTime, map, mergeMap, share } from 'rxjs/operators'; |
|
|
|
import { TranslateService } from '@ngx-translate/core'; |
|
|
|
import { EntitySubtype, EntityType } from '@shared/models/entity-type.models'; |
|
|
|
import { MatAutocomplete, MatAutocompleteSelectedEvent } from '@angular/material/autocomplete'; |
|
|
|
import { MatChipInputEvent, MatChipGrid } from '@angular/material/chips'; |
|
|
|
import { MatChipGrid, MatChipInputEvent } from '@angular/material/chips'; |
|
|
|
import { AssetService } from '@core/http/asset.service'; |
|
|
|
import { DeviceService } from '@core/http/device.service'; |
|
|
|
import { EdgeService } from '@core/http/edge.service'; |
|
|
|
@ -31,9 +29,10 @@ 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'; |
|
|
|
import { MatFormFieldAppearance, SubscriptSizing } from '@angular/material/form-field'; |
|
|
|
import { FloatLabelType, MatFormFieldAppearance, SubscriptSizing } from '@angular/material/form-field'; |
|
|
|
import { coerceArray, coerceBoolean } from '@shared/decorators/coercion'; |
|
|
|
import { FloatLabelType } from '@angular/material/form-field'; |
|
|
|
import { PageLink } from '@shared/models/page/page-link'; |
|
|
|
import { PageData } from '@shared/models/page/page-data'; |
|
|
|
|
|
|
|
@Component({ |
|
|
|
selector: 'tb-entity-subtype-list', |
|
|
|
@ -102,7 +101,7 @@ export class EntitySubTypeListComponent implements ControlValueAccessor, OnInit, |
|
|
|
|
|
|
|
entitySubtypeList: Array<string> = []; |
|
|
|
filteredEntitySubtypeList: Observable<Array<string>>; |
|
|
|
entitySubtypes: Observable<Array<string>>; |
|
|
|
private entitySubtypes: Observable<Array<string>>; |
|
|
|
|
|
|
|
private broadcastSubscription: Subscription; |
|
|
|
|
|
|
|
@ -119,8 +118,11 @@ export class EntitySubTypeListComponent implements ControlValueAccessor, OnInit, |
|
|
|
|
|
|
|
private propagateChange = (v: any) => { }; |
|
|
|
|
|
|
|
constructor(private store: Store<AppState>, |
|
|
|
private broadcast: BroadcastService, |
|
|
|
private hasPageDataEntitySubTypes = new Set<EntityType>([ |
|
|
|
EntityType.ALARM |
|
|
|
]); |
|
|
|
|
|
|
|
constructor(private broadcast: BroadcastService, |
|
|
|
public translate: TranslateService, |
|
|
|
private assetService: AssetService, |
|
|
|
private deviceService: DeviceService, |
|
|
|
@ -195,9 +197,6 @@ export class EntitySubTypeListComponent implements ControlValueAccessor, OnInit, |
|
|
|
this.secondaryPlaceholder = '+' + this.translate.instant('alarm.alarm-type'); |
|
|
|
this.noSubtypesMathingText = 'alarm.no-alarm-types-matching'; |
|
|
|
this.subtypeListEmptyText = 'alarm.alarm-type-list-empty'; |
|
|
|
this.broadcastSubscription = this.broadcast.on('alarmSaved', () => { |
|
|
|
this.entitySubtypes = null; |
|
|
|
}); |
|
|
|
break; |
|
|
|
} |
|
|
|
|
|
|
|
@ -208,12 +207,12 @@ export class EntitySubTypeListComponent implements ControlValueAccessor, OnInit, |
|
|
|
this.secondaryPlaceholder = this.filledInputPlaceholder; |
|
|
|
} |
|
|
|
|
|
|
|
this.filteredEntitySubtypeList = this.entitySubtypeListFormGroup.get('entitySubtype').valueChanges |
|
|
|
.pipe( |
|
|
|
map(value => value ? value : ''), |
|
|
|
mergeMap(name => this.fetchEntitySubtypes(name) ), |
|
|
|
share() |
|
|
|
); |
|
|
|
this.filteredEntitySubtypeList = this.entitySubtypeListFormGroup.get('entitySubtype').valueChanges.pipe( |
|
|
|
debounceTime(150), |
|
|
|
map(value => value ? value : ''), |
|
|
|
mergeMap(name => this.fetchEntitySubtypes(name)), |
|
|
|
share() |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
ngAfterViewInit(): void { |
|
|
|
@ -289,11 +288,16 @@ export class EntitySubTypeListComponent implements ControlValueAccessor, OnInit, |
|
|
|
return entitySubtype ? entitySubtype : undefined; |
|
|
|
} |
|
|
|
|
|
|
|
fetchEntitySubtypes(searchText?: string): Observable<Array<string>> { |
|
|
|
private fetchEntitySubtypes(searchText?: string): Observable<Array<string>> { |
|
|
|
this.searchText = searchText; |
|
|
|
return this.getEntitySubtypes().pipe( |
|
|
|
return this.getEntitySubtypes(searchText).pipe( |
|
|
|
map(subTypes => { |
|
|
|
let result = subTypes.filter( subType => searchText ? subType.toUpperCase().startsWith(searchText.toUpperCase()) : true); |
|
|
|
let result; |
|
|
|
if (this.hasPageDataEntitySubTypes.has(this.entityType)) { |
|
|
|
result = subTypes; |
|
|
|
} else { |
|
|
|
result = subTypes.filter(subType => searchText ? subType.toUpperCase().startsWith(searchText.toUpperCase()) : true); |
|
|
|
} |
|
|
|
if (!result.length) { |
|
|
|
result = [searchText]; |
|
|
|
} |
|
|
|
@ -302,7 +306,23 @@ export class EntitySubTypeListComponent implements ControlValueAccessor, OnInit, |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
getEntitySubtypes(): Observable<Array<string>> { |
|
|
|
private getEntitySubtypes(searchText?: string): Observable<Array<string>> { |
|
|
|
if (this.hasPageDataEntitySubTypes.has(this.entityType)) { |
|
|
|
const pageLink = new PageLink(25, 0, searchText); |
|
|
|
let subTypesPagesObservable: Observable<PageData<EntitySubtype>>; |
|
|
|
switch (this.entityType) { |
|
|
|
case EntityType.ALARM: |
|
|
|
subTypesPagesObservable = this.alarmService.getAlarmTypes(pageLink, {ignoreLoading: true}); |
|
|
|
break; |
|
|
|
} |
|
|
|
if (subTypesPagesObservable) { |
|
|
|
this.entitySubtypes = subTypesPagesObservable.pipe( |
|
|
|
map(subTypesPage => subTypesPage.data.map(subType => subType.type)), |
|
|
|
); |
|
|
|
} else { |
|
|
|
return throwError(null); |
|
|
|
} |
|
|
|
} |
|
|
|
if (!this.entitySubtypes) { |
|
|
|
let subTypesObservable: Observable<Array<EntitySubtype>>; |
|
|
|
switch (this.entityType) { |
|
|
|
@ -318,15 +338,16 @@ export class EntitySubTypeListComponent implements ControlValueAccessor, OnInit, |
|
|
|
case EntityType.ENTITY_VIEW: |
|
|
|
subTypesObservable = this.entityViewService.getEntityViewTypes({ignoreLoading: true}); |
|
|
|
break; |
|
|
|
case EntityType.ALARM: |
|
|
|
subTypesObservable = this.alarmService.getAlarmTypes({ignoreLoading: true}); |
|
|
|
break; |
|
|
|
} |
|
|
|
if (subTypesObservable) { |
|
|
|
this.entitySubtypes = subTypesObservable.pipe( |
|
|
|
map(subTypes => subTypes.map(subType => subType.type)), |
|
|
|
publishReplay(1), |
|
|
|
refCount() |
|
|
|
share({ |
|
|
|
connector: () => new ReplaySubject(1), |
|
|
|
resetOnError: false, |
|
|
|
resetOnComplete: false, |
|
|
|
resetOnRefCountZero: true, |
|
|
|
}), |
|
|
|
); |
|
|
|
} else { |
|
|
|
return throwError(null); |
|
|
|
|