Timewindow: quick-time interval - add allowed intervals support; refactoring

This commit is contained in:
Chantsova Ekaterina 2024-10-28 18:51:57 +02:00
parent 7e8ea0775e
commit ecf8a90492
4 changed files with 95 additions and 19 deletions

View File

@ -14,12 +14,14 @@
/// limitations under the License. /// limitations under the License.
/// ///
import { Component, forwardRef, Input, OnChanges, OnInit, SimpleChanges } from '@angular/core'; import { Component, forwardRef, Input, OnChanges, OnDestroy, OnInit, SimpleChanges } from '@angular/core';
import { ControlValueAccessor, FormBuilder, FormGroup, NG_VALUE_ACCESSOR } from '@angular/forms'; import { ControlValueAccessor, FormBuilder, FormGroup, NG_VALUE_ACCESSOR } from '@angular/forms';
import { TranslateService } from '@ngx-translate/core'; import { TranslateService } from '@ngx-translate/core';
import { coerceBoolean } from '@shared/decorators/coercion'; import { coerceBoolean } from '@shared/decorators/coercion';
import { MatFormFieldAppearance, SubscriptSizing } from '@angular/material/form-field'; import { MatFormFieldAppearance, SubscriptSizing } from '@angular/material/form-field';
import { aggregationTranslations, AggregationType } from '@shared/models/time/time.models'; import { aggregationTranslations, AggregationType } from '@shared/models/time/time.models';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
@Component({ @Component({
selector: 'tb-aggregation-type-select', selector: 'tb-aggregation-type-select',
@ -31,7 +33,7 @@ import { aggregationTranslations, AggregationType } from '@shared/models/time/ti
multi: true multi: true
}] }]
}) })
export class AggregationTypeSelectComponent implements ControlValueAccessor, OnInit, OnChanges { export class AggregationTypeSelectComponent implements ControlValueAccessor, OnInit, OnChanges, OnDestroy {
aggregationTypeFormGroup: FormGroup; aggregationTypeFormGroup: FormGroup;
@ -75,6 +77,8 @@ export class AggregationTypeSelectComponent implements ControlValueAccessor, OnI
private propagateChange = (v: any) => { }; private propagateChange = (v: any) => { };
private destroy$ = new Subject<void>();
constructor(private translate: TranslateService, constructor(private translate: TranslateService,
private fb: FormBuilder) { private fb: FormBuilder) {
this.aggregationTypeFormGroup = this.fb.group({ this.aggregationTypeFormGroup = this.fb.group({
@ -91,10 +95,12 @@ export class AggregationTypeSelectComponent implements ControlValueAccessor, OnI
ngOnInit() { ngOnInit() {
this.aggregationTypes = this.allowedAggregationTypes?.length ? this.allowedAggregationTypes : this.allAggregationTypes; this.aggregationTypes = this.allowedAggregationTypes?.length ? this.allowedAggregationTypes : this.allAggregationTypes;
this.aggregationTypeFormGroup.get('aggregationType').valueChanges.subscribe( this.aggregationTypeFormGroup.get('aggregationType').valueChanges.pipe(
takeUntil(this.destroy$)
).subscribe(
(value) => { (value) => {
let modelValue; let modelValue;
if (!value || value === '') { if (!value) {
modelValue = null; modelValue = null;
} else { } else {
modelValue = value; modelValue = value;
@ -152,4 +158,9 @@ export class AggregationTypeSelectComponent implements ControlValueAccessor, OnI
return ''; return '';
} }
} }
ngOnDestroy() {
this.destroy$.next();
this.destroy$.complete();
}
} }

View File

@ -15,10 +15,10 @@
limitations under the License. limitations under the License.
--> -->
<section class="interval-section flex flex-1 flex-row"> <section class="interval-section flex flex-1 flex-row" [formGroup]="quickIntervalFormGroup">
<mat-form-field class="flex-1" [subscriptSizing]="subscriptSizing" [appearance]="appearance"> <mat-form-field class="flex-1" [subscriptSizing]="subscriptSizing" [appearance]="appearance">
<mat-label *ngIf="displayLabel" translate>timewindow.interval</mat-label> <mat-label *ngIf="displayLabel" translate>timewindow.interval</mat-label>
<mat-select [disabled]="disabled" [(ngModel)]="modelValue" (ngModelChange)="onIntervalChange()"> <mat-select formControlName="interval">
<mat-option *ngFor="let interval of intervals" [value]="interval"> <mat-option *ngFor="let interval of intervals" [value]="interval">
{{ timeIntervalTranslationMap.get(interval) | translate}} {{ timeIntervalTranslationMap.get(interval) | translate}}
</mat-option> </mat-option>

View File

@ -14,11 +14,13 @@
/// limitations under the License. /// limitations under the License.
/// ///
import { Component, forwardRef, Input, OnInit } from '@angular/core'; import { Component, forwardRef, Input, OnChanges, OnDestroy, OnInit, SimpleChanges } from '@angular/core';
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'; import { ControlValueAccessor, FormBuilder, FormGroup, NG_VALUE_ACCESSOR } from '@angular/forms';
import { QuickTimeInterval, QuickTimeIntervalTranslationMap } from '@shared/models/time/time.models'; import { QuickTimeInterval, QuickTimeIntervalTranslationMap } from '@shared/models/time/time.models';
import { MatFormFieldAppearance, SubscriptSizing } from '@angular/material/form-field'; import { MatFormFieldAppearance, SubscriptSizing } from '@angular/material/form-field';
import { coerceBoolean } from '@shared/decorators/coercion'; import { coerceBoolean } from '@shared/decorators/coercion';
import { Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
@Component({ @Component({
selector: 'tb-quick-time-interval', selector: 'tb-quick-time-interval',
@ -32,7 +34,7 @@ import { coerceBoolean } from '@shared/decorators/coercion';
} }
] ]
}) })
export class QuickTimeIntervalComponent implements OnInit, ControlValueAccessor { export class QuickTimeIntervalComponent implements OnInit, ControlValueAccessor, OnChanges, OnDestroy {
private allIntervals = Object.values(QuickTimeInterval); private allIntervals = Object.values(QuickTimeInterval);
@ -49,25 +51,60 @@ export class QuickTimeIntervalComponent implements OnInit, ControlValueAccessor
@Input() onlyCurrentInterval = false; @Input() onlyCurrentInterval = false;
@Input()
allowedIntervals: Array<QuickTimeInterval>
@Input() @Input()
subscriptSizing: SubscriptSizing = 'fixed'; subscriptSizing: SubscriptSizing = 'fixed';
@Input() @Input()
appearance: MatFormFieldAppearance = 'fill'; appearance: MatFormFieldAppearance = 'fill';
intervals: Array<QuickTimeInterval>;
private allAvailableIntervals: Array<QuickTimeInterval>;
quickIntervalFormGroup: FormGroup;
private propagateChange = (_: any) => {}; private propagateChange = (_: any) => {};
constructor() { private destroy$ = new Subject<void>();
}
get intervals() { constructor(private fb: FormBuilder) {
if (this.onlyCurrentInterval) { this.quickIntervalFormGroup = this.fb.group({
return this.allIntervals.filter(interval => interval.startsWith('CURRENT_')); interval: [ null ]
});
this.quickIntervalFormGroup.get('interval').valueChanges.pipe(
takeUntil(this.destroy$)
).subscribe((value) => {
let modelValue;
if (!value) {
modelValue = null;
} else {
modelValue = value;
} }
return this.allIntervals; this.updateView(modelValue);
});
} }
ngOnInit(): void { ngOnInit(): void {
this.allAvailableIntervals = this.getAllAvailableIntervals();
this.intervals = this.allowedIntervals?.length ? this.allowedIntervals : this.allAvailableIntervals;
}
ngOnChanges(changes: SimpleChanges): void {
for (const propName of Object.keys(changes)) {
const change = changes[propName];
if (!change.firstChange && change.currentValue !== change.previousValue) {
if (propName === 'allowedIntervals') {
this.intervals = this.allowedIntervals?.length ? this.allowedIntervals : this.allAvailableIntervals;
const currentInterval: QuickTimeInterval = this.quickIntervalFormGroup.get('interval').value;
if (currentInterval && !this.intervals.includes(currentInterval)) {
this.quickIntervalFormGroup.get('interval').patchValue(this.intervals[0], {emitEvent: true});
}
}
}
}
} }
registerOnChange(fn: any): void { registerOnChange(fn: any): void {
@ -79,13 +116,39 @@ export class QuickTimeIntervalComponent implements OnInit, ControlValueAccessor
setDisabledState(isDisabled: boolean): void { setDisabledState(isDisabled: boolean): void {
this.disabled = isDisabled; this.disabled = isDisabled;
if (this.disabled) {
this.quickIntervalFormGroup.disable({emitEvent: false});
} else {
this.quickIntervalFormGroup.enable({emitEvent: false});
}
} }
writeValue(interval: QuickTimeInterval): void { writeValue(interval: QuickTimeInterval): void {
if (interval != null) {
this.modelValue = interval; this.modelValue = interval;
this.quickIntervalFormGroup.get('interval').patchValue(interval, {emitEvent: true});
} else {
this.modelValue = null;
this.quickIntervalFormGroup.get('interval').patchValue(null, {emitEvent: true});
}
} }
onIntervalChange() { updateView(value: QuickTimeInterval | null) {
if (this.modelValue !== value) {
this.modelValue = value;
this.propagateChange(this.modelValue); this.propagateChange(this.modelValue);
} }
}
ngOnDestroy() {
this.destroy$.next();
this.destroy$.complete();
}
private getAllAvailableIntervals() {
if (this.onlyCurrentInterval) {
return this.allIntervals.filter(interval => interval.startsWith('CURRENT_'));
}
return this.allIntervals;
}
} }

View File

@ -89,6 +89,7 @@
onlyCurrentInterval="true" onlyCurrentInterval="true"
subscriptSizing="dynamic" subscriptSizing="dynamic"
appearance="outline" appearance="outline"
[allowedIntervals]="timewindowForm.get('realtime.allowedQuickIntervals').value"
[required]="timewindowForm.get('selectedTab').value === timewindowTypes.REALTIME && [required]="timewindowForm.get('selectedTab').value === timewindowTypes.REALTIME &&
timewindowForm.get('realtime.realtimeType').value === realtimeTypes.INTERVAL"> timewindowForm.get('realtime.realtimeType').value === realtimeTypes.INTERVAL">
<button *ngIf="!(timewindowForm.get('realtime.hideQuickInterval').value || <button *ngIf="!(timewindowForm.get('realtime.hideQuickInterval').value ||
@ -169,6 +170,7 @@
formControlName="quickInterval" formControlName="quickInterval"
subscriptSizing="dynamic" subscriptSizing="dynamic"
appearance="outline" appearance="outline"
[allowedIntervals]="timewindowForm.get('history.allowedQuickIntervals').value"
[required]="timewindowForm.get('selectedTab').value === timewindowTypes.HISTORY && [required]="timewindowForm.get('selectedTab').value === timewindowTypes.HISTORY &&
timewindowForm.get('history.historyType').value === historyTypes.INTERVAL"> timewindowForm.get('history.historyType').value === historyTypes.INTERVAL">
<button *ngIf="!(timewindowForm.get('history.hideQuickInterval').value || <button *ngIf="!(timewindowForm.get('history.hideQuickInterval').value ||