2019-10-24 19:52:19 +03:00
|
|
|
///
|
2022-01-17 14:07:46 +02:00
|
|
|
/// Copyright © 2016-2022 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.
|
|
|
|
|
///
|
|
|
|
|
|
2021-10-28 13:41:37 +03:00
|
|
|
import { Component, forwardRef, Input, OnDestroy, OnInit } 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';
|
2021-10-28 13:41:37 +03:00
|
|
|
|
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
|
|
|
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-28 13:41:37 +03:00
|
|
|
private legendSettingsFormChanges$: Subscription;
|
|
|
|
|
private legendSettingsFormDirectionChanges$: Subscription;
|
2019-10-24 19:52:19 +03:00
|
|
|
private propagateChange = (_: any) => {};
|
|
|
|
|
|
2021-10-28 13:41:37 +03:00
|
|
|
constructor(private fb: FormBuilder) {
|
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-28 13:41:37 +03:00
|
|
|
this.legendSettingsFormDirectionChanges$ = this.legendConfigForm.get('direction').valueChanges
|
|
|
|
|
.subscribe((direction: LegendDirection) => {
|
|
|
|
|
this.onDirectionChanged(direction);
|
|
|
|
|
});
|
|
|
|
|
this.legendSettingsFormChanges$ = this.legendConfigForm.valueChanges.subscribe(
|
|
|
|
|
() => this.legendConfigUpdated()
|
|
|
|
|
);
|
2021-10-28 09:57:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private onDirectionChanged(direction: LegendDirection) {
|
|
|
|
|
if (direction === LegendDirection.row) {
|
|
|
|
|
let position: LegendPosition = this.legendConfigForm.get('position').value;
|
|
|
|
|
if (position !== LegendPosition.bottom && position !== LegendPosition.top) {
|
|
|
|
|
position = LegendPosition.bottom;
|
|
|
|
|
}
|
|
|
|
|
this.legendConfigForm.patchValue({position}, {emitEvent: false}
|
|
|
|
|
);
|
|
|
|
|
}
|
2019-10-24 19:52:19 +03:00
|
|
|
}
|
|
|
|
|
|
2021-10-27 13:33:51 +03:00
|
|
|
ngOnDestroy(): void {
|
2021-10-28 13:41:37 +03:00
|
|
|
if (this.legendSettingsFormDirectionChanges$) {
|
|
|
|
|
this.legendSettingsFormDirectionChanges$.unsubscribe();
|
|
|
|
|
this.legendSettingsFormDirectionChanges$ = null;
|
|
|
|
|
}
|
|
|
|
|
if (this.legendSettingsFormChanges$) {
|
|
|
|
|
this.legendSettingsFormChanges$.unsubscribe();
|
|
|
|
|
this.legendSettingsFormChanges$ = null;
|
|
|
|
|
}
|
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});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-28 13:41:37 +03:00
|
|
|
writeValue(legendConfig: LegendConfig): void {
|
|
|
|
|
if (legendConfig) {
|
2021-10-27 13:33:51 +03:00
|
|
|
this.legendConfigForm.patchValue({
|
2021-10-28 13:41:37 +03:00
|
|
|
direction: legendConfig.direction,
|
|
|
|
|
position: legendConfig.position,
|
|
|
|
|
sortDataKeys: isDefined(legendConfig.sortDataKeys) ? legendConfig.sortDataKeys : false,
|
|
|
|
|
showMin: isDefined(legendConfig.showMin) ? legendConfig.showMin : false,
|
|
|
|
|
showMax: isDefined(legendConfig.showMax) ? legendConfig.showMax : false,
|
|
|
|
|
showAvg: isDefined(legendConfig.showAvg) ? legendConfig.showAvg : false,
|
|
|
|
|
showTotal: isDefined(legendConfig.showTotal) ? legendConfig.showTotal : false
|
|
|
|
|
}, {emitEvent: false});
|
2021-10-27 13:33:51 +03:00
|
|
|
}
|
2021-10-28 13:41:37 +03:00
|
|
|
this.onDirectionChanged(legendConfig.direction);
|
2019-10-24 19:52:19 +03:00
|
|
|
}
|
|
|
|
|
|
2021-10-27 13:33:51 +03:00
|
|
|
private legendConfigUpdated() {
|
2021-10-28 13:41:37 +03:00
|
|
|
this.propagateChange(this.legendConfigForm.value);
|
2019-10-24 19:52:19 +03:00
|
|
|
}
|
|
|
|
|
}
|