2023-01-27 15:47:08 +02:00
|
|
|
///
|
|
|
|
|
/// Copyright © 2016-2022 The Thingsboard Authors
|
|
|
|
|
///
|
|
|
|
|
/// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
/// you may not use this file except in compliance with the License.
|
|
|
|
|
/// You may obtain a copy of the License at
|
|
|
|
|
///
|
|
|
|
|
/// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
///
|
|
|
|
|
/// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
/// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
/// See the License for the specific language governing permissions and
|
|
|
|
|
/// limitations under the License.
|
|
|
|
|
///
|
|
|
|
|
|
|
|
|
|
import { Component, forwardRef, Input, OnDestroy } from '@angular/core';
|
|
|
|
|
import {
|
|
|
|
|
AbstractControl,
|
|
|
|
|
ControlValueAccessor,
|
|
|
|
|
FormArray,
|
|
|
|
|
FormBuilder,
|
|
|
|
|
FormGroup,
|
|
|
|
|
NG_VALIDATORS,
|
|
|
|
|
NG_VALUE_ACCESSOR,
|
|
|
|
|
ValidationErrors,
|
|
|
|
|
Validator,
|
|
|
|
|
Validators
|
|
|
|
|
} from '@angular/forms';
|
|
|
|
|
import { Store } from '@ngrx/store';
|
|
|
|
|
import { AppState } from '@app/core/core.state';
|
|
|
|
|
import { coerceBooleanProperty } from '@angular/cdk/coercion';
|
2023-01-31 02:03:51 +02:00
|
|
|
import { Subject } from 'rxjs';
|
2023-01-27 15:47:08 +02:00
|
|
|
import { UtilsService } from '@core/services/utils.service';
|
|
|
|
|
import { NonConfirmedNotificationEscalation } from '@shared/models/notification.models';
|
2023-01-31 02:03:51 +02:00
|
|
|
import { takeUntil } from 'rxjs/operators';
|
2023-01-27 15:47:08 +02:00
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'tb-escalations-component',
|
|
|
|
|
templateUrl: './escalations.component.html',
|
|
|
|
|
styleUrls: [],
|
|
|
|
|
providers: [
|
|
|
|
|
{
|
|
|
|
|
provide: NG_VALUE_ACCESSOR,
|
|
|
|
|
useExisting: forwardRef(() => EscalationsComponent),
|
|
|
|
|
multi: true
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
provide: NG_VALIDATORS,
|
|
|
|
|
useExisting: forwardRef(() => EscalationsComponent),
|
|
|
|
|
multi: true,
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
})
|
|
|
|
|
export class EscalationsComponent implements ControlValueAccessor, Validator, OnDestroy {
|
|
|
|
|
|
|
|
|
|
escalationsFormGroup: FormGroup;
|
|
|
|
|
newEscalation = false;
|
|
|
|
|
|
|
|
|
|
private requiredValue: boolean;
|
|
|
|
|
get required(): boolean {
|
|
|
|
|
return this.requiredValue;
|
|
|
|
|
}
|
|
|
|
|
@Input()
|
|
|
|
|
set required(value: boolean) {
|
|
|
|
|
this.requiredValue = coerceBooleanProperty(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
disabled: boolean;
|
|
|
|
|
|
|
|
|
|
private mainEscalaion = {
|
2023-01-31 02:03:51 +02:00
|
|
|
delayInSec: 0,
|
|
|
|
|
targets: null
|
2023-01-27 15:47:08 +02:00
|
|
|
};
|
|
|
|
|
|
2023-01-31 02:03:51 +02:00
|
|
|
private destroy$ = new Subject();
|
2023-01-27 15:47:08 +02:00
|
|
|
|
|
|
|
|
private propagateChange = (v: any) => { };
|
|
|
|
|
|
|
|
|
|
constructor(private store: Store<AppState>,
|
|
|
|
|
private utils: UtilsService,
|
|
|
|
|
private fb: FormBuilder) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerOnChange(fn: any): void {
|
|
|
|
|
this.propagateChange = fn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
2023-01-31 02:03:51 +02:00
|
|
|
this.destroy$.next();
|
|
|
|
|
this.destroy$.complete();
|
2023-01-27 15:47:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerOnTouched(fn: any): void {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
|
this.escalationsFormGroup = this.fb.group({
|
|
|
|
|
escalations: this.fb.array([])
|
|
|
|
|
});
|
2023-01-31 02:03:51 +02:00
|
|
|
|
|
|
|
|
this.escalationsFormGroup.valueChanges.pipe(
|
|
|
|
|
takeUntil(this.destroy$)
|
|
|
|
|
).subscribe(() => this.updateModel());
|
2023-01-27 15:47:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get escalationsFormArray(): FormArray {
|
|
|
|
|
return this.escalationsFormGroup.get('escalations') as FormArray;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setDisabledState(isDisabled: boolean): void {
|
|
|
|
|
this.disabled = isDisabled;
|
|
|
|
|
if (this.disabled) {
|
|
|
|
|
this.escalationsFormGroup.disable({emitEvent: false});
|
|
|
|
|
} else {
|
|
|
|
|
this.escalationsFormGroup.enable({emitEvent: false});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writeValue(escalations: Array<NonConfirmedNotificationEscalation> | null): void {
|
2023-01-31 02:03:51 +02:00
|
|
|
if (escalations?.length === this.escalationsFormArray.length) {
|
|
|
|
|
this.escalationsFormArray.patchValue(escalations, {emitEvent: false});
|
2023-01-27 15:47:08 +02:00
|
|
|
} else {
|
2023-01-31 02:03:51 +02:00
|
|
|
const escalationsControls: Array<AbstractControl> = [];
|
|
|
|
|
if (escalations) {
|
|
|
|
|
escalations.forEach((escalation, index) => {
|
|
|
|
|
escalationsControls.push(this.fb.control(escalation, [Validators.required]));
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
escalationsControls.push(this.fb.control(this.mainEscalaion, [Validators.required]));
|
|
|
|
|
}
|
|
|
|
|
this.escalationsFormGroup.setControl('escalations', this.fb.array(escalationsControls), {emitEvent: false});
|
|
|
|
|
if (this.disabled) {
|
|
|
|
|
this.escalationsFormGroup.disable({emitEvent: false});
|
|
|
|
|
} else {
|
|
|
|
|
this.escalationsFormGroup.enable({emitEvent: false});
|
|
|
|
|
}
|
2023-01-27 15:47:08 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public removeEscalation(index: number) {
|
|
|
|
|
(this.escalationsFormGroup.get('escalations') as FormArray).removeAt(index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public addEscalation() {
|
2023-01-31 02:03:51 +02:00
|
|
|
const escalation = {
|
|
|
|
|
delayInSec: 0,
|
|
|
|
|
targets: null
|
2023-01-27 15:47:08 +02:00
|
|
|
};
|
|
|
|
|
this.newEscalation = true;
|
|
|
|
|
const escalationArray = this.escalationsFormGroup.get('escalations') as FormArray;
|
|
|
|
|
escalationArray.push(this.fb.control(escalation, []));
|
|
|
|
|
this.escalationsFormGroup.updateValueAndValidity();
|
|
|
|
|
if (!this.escalationsFormGroup.valid) {
|
|
|
|
|
this.updateModel();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public validate(c: AbstractControl): ValidationErrors | null {
|
|
|
|
|
return this.escalationsFormGroup.valid ? null : {
|
|
|
|
|
escalation: {
|
|
|
|
|
valid: false,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private updateModel() {
|
2023-01-31 02:03:51 +02:00
|
|
|
const escalations = this.escalationsFormGroup.get('escalations').value;
|
2023-01-27 15:47:08 +02:00
|
|
|
this.propagateChange(escalations);
|
|
|
|
|
}
|
|
|
|
|
}
|