thingsboard/ui-ngx/src/app/modules/home/components/profile/alarm/device-profile-alarm.component.ts

133 lines
3.3 KiB
TypeScript
Raw Normal View History

2020-09-09 18:27:28 +03:00
///
/// Copyright © 2016-2020 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, EventEmitter, forwardRef, Input, OnInit, Output } from '@angular/core';
import {
ControlValueAccessor,
FormBuilder,
FormControl,
2020-09-10 19:37:14 +03:00
FormGroup,
NG_VALIDATORS,
NG_VALUE_ACCESSOR,
Validator,
2020-09-09 18:27:28 +03:00
Validators
} from '@angular/forms';
2020-09-10 19:37:14 +03:00
import { AlarmRule, DeviceProfileAlarm } from '@shared/models/device.models';
2020-09-09 18:27:28 +03:00
import { MatDialog } from '@angular/material/dialog';
@Component({
selector: 'tb-device-profile-alarm',
templateUrl: './device-profile-alarm.component.html',
styleUrls: ['./device-profile-alarm.component.scss'],
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => DeviceProfileAlarmComponent),
multi: true
},
{
provide: NG_VALIDATORS,
useExisting: forwardRef(() => DeviceProfileAlarmComponent),
multi: true,
}
]
})
export class DeviceProfileAlarmComponent implements ControlValueAccessor, OnInit, Validator {
@Input()
disabled: boolean;
@Output()
removeAlarm = new EventEmitter();
2020-09-10 19:37:14 +03:00
expanded = false;
2020-09-09 18:27:28 +03:00
private modelValue: DeviceProfileAlarm;
alarmFormGroup: FormGroup;
private propagateChange = (v: any) => { };
constructor(private dialog: MatDialog,
private fb: FormBuilder) {
}
registerOnChange(fn: any): void {
this.propagateChange = fn;
}
registerOnTouched(fn: any): void {
}
ngOnInit() {
this.alarmFormGroup = this.fb.group({
id: [null, Validators.required],
alarmType: [null, Validators.required],
createRules: [null],
clearRule: [null],
propagate: [null],
propagateRelationTypes: [null]
});
this.alarmFormGroup.valueChanges.subscribe(() => {
this.updateModel();
});
}
setDisabledState(isDisabled: boolean): void {
this.disabled = isDisabled;
if (this.disabled) {
this.alarmFormGroup.disable({emitEvent: false});
} else {
this.alarmFormGroup.enable({emitEvent: false});
}
}
writeValue(value: DeviceProfileAlarm): void {
this.modelValue = value;
2020-09-10 19:37:14 +03:00
if (!this.modelValue.alarmType) {
this.expanded = true;
}
this.alarmFormGroup.reset(this.modelValue || undefined, {emitEvent: false});
2020-09-09 18:27:28 +03:00
}
2020-09-10 19:37:14 +03:00
public addClearAlarmRule() {
const clearAlarmRule: AlarmRule = {
condition: {
condition: []
2020-09-09 18:27:28 +03:00
}
2020-09-10 19:37:14 +03:00
};
this.alarmFormGroup.patchValue({clearRule: clearAlarmRule});
}
public removeClearAlarmRule() {
this.alarmFormGroup.patchValue({clearRule: null});
}
2020-09-09 18:27:28 +03:00
public validate(c: FormControl) {
return (this.alarmFormGroup.valid) ? null : {
alarm: {
valid: false,
},
};
}
private updateModel() {
2020-09-10 19:37:14 +03:00
const value = this.alarmFormGroup.value;
this.modelValue = {...this.modelValue, ...value};
this.propagateChange(this.modelValue);
2020-09-09 18:27:28 +03:00
}
}