thingsboard/ui-ngx/src/app/modules/home/components/widget/legend-config.component.ts

136 lines
4.0 KiB
TypeScript
Raw Normal View History

2019-10-24 19:52:19 +03:00
///
2021-01-11 13:42:16 +02:00
/// Copyright © 2016-2021 The Thingsboard Authors
2019-10-24 19:52:19 +03:00
///
/// 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,
OnInit,
ViewContainerRef
} from '@angular/core';
2021-10-27 13:33:51 +03:00
import { ControlValueAccessor, FormBuilder, FormGroup, NG_VALUE_ACCESSOR } from '@angular/forms';
import { isDefined } from '@core/utils';
2019-10-24 19:52:19 +03:00
import {
2021-10-27 13:33:51 +03:00
LegendConfig,
LegendDirection,
legendDirectionTranslationMap,
LegendPosition,
legendPositionTranslationMap
} from '@shared/models/widget.models';
import { Subscription } from 'rxjs';
2019-12-23 14:36:44 +02:00
// @dynamic
2019-10-24 19:52:19 +03:00
@Component({
selector: 'tb-legend-config',
templateUrl: './legend-config.component.html',
styleUrls: [],
providers: [
{
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => LegendConfigComponent),
multi: true
}
]
})
export class LegendConfigComponent implements OnInit, OnDestroy, ControlValueAccessor {
@Input() disabled: boolean;
2021-10-27 13:33:51 +03:00
legendSettings: LegendConfig;
legendConfigForm: FormGroup;
legendDirection = LegendDirection;
legendDirections = Object.keys(LegendDirection);
legendDirectionTranslations = legendDirectionTranslationMap;
legendPosition = LegendPosition;
legendPositions = Object.keys(LegendPosition);
legendPositionTranslations = legendPositionTranslationMap;
2019-10-24 19:52:19 +03:00
2021-10-27 13:33:51 +03:00
legendSettingsChangesSubscription: Subscription;
2019-10-24 19:52:19 +03:00
private propagateChange = (_: any) => {};
2021-10-27 13:33:51 +03:00
constructor(public fb: FormBuilder,
public viewContainerRef: ViewContainerRef) {
2019-10-24 19:52:19 +03:00
}
ngOnInit(): void {
2021-10-27 13:33:51 +03:00
this.legendConfigForm = this.fb.group({
direction: [null, []],
position: [null, []],
sortDataKeys: [null, []],
showMin: [null, []],
showMax: [null, []],
showAvg: [null, []],
showTotal: [null, []]
2019-10-24 19:52:19 +03:00
});
}
2021-10-27 13:33:51 +03:00
ngOnDestroy(): void {
this.removeChangeSubscriptions();
2019-10-24 19:52:19 +03:00
}
registerOnChange(fn: any): void {
this.propagateChange = fn;
}
registerOnTouched(fn: any): void {
}
setDisabledState(isDisabled: boolean): void {
this.disabled = isDisabled;
2021-10-27 13:33:51 +03:00
if (this.disabled) {
this.legendConfigForm.disable({emitEvent: false});
} else {
this.legendConfigForm.enable({emitEvent: false});
}
}
private removeChangeSubscriptions() {
if (this.legendSettingsChangesSubscription) {
this.legendSettingsChangesSubscription.unsubscribe();
this.legendSettingsChangesSubscription = null;
}
}
private createChangeSubscriptions() {
this.legendSettingsChangesSubscription = this.legendConfigForm.valueChanges.subscribe(
() => this.legendConfigUpdated()
);
2019-10-24 19:52:19 +03:00
}
writeValue(obj: LegendConfig): void {
2021-10-27 13:33:51 +03:00
this.legendSettings = obj;
this.removeChangeSubscriptions();
if (this.legendSettings) {
this.legendConfigForm.patchValue({
direction: this.legendSettings.direction,
position: this.legendSettings.position,
sortDataKeys: isDefined(this.legendSettings.sortDataKeys) ? this.legendSettings.sortDataKeys : false,
showMin: isDefined(this.legendSettings.showMin) ? this.legendSettings.showMin : false,
showMax: isDefined(this.legendSettings.showMax) ? this.legendSettings.showMax : false,
showAvg: isDefined(this.legendSettings.showAvg) ? this.legendSettings.showAvg : false,
showTotal: isDefined(this.legendSettings.showTotal) ? this.legendSettings.showTotal : false
});
}
this.createChangeSubscriptions();
2019-10-24 19:52:19 +03:00
}
2021-10-27 13:33:51 +03:00
private legendConfigUpdated() {
this.legendSettings = this.legendConfigForm.value;
this.propagateChange(this.legendSettings);
2019-10-24 19:52:19 +03:00
}
}