2023-07-07 17:27:59 +03:00
|
|
|
///
|
2025-02-25 09:39:16 +02:00
|
|
|
/// Copyright © 2016-2025 The Thingsboard Authors
|
2023-07-07 17:27:59 +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.
|
|
|
|
|
///
|
|
|
|
|
|
2023-07-27 17:18:39 +03:00
|
|
|
import {
|
2025-04-25 17:56:36 +03:00
|
|
|
booleanAttribute,
|
2023-07-27 17:18:39 +03:00
|
|
|
Component,
|
|
|
|
|
ElementRef,
|
|
|
|
|
forwardRef,
|
|
|
|
|
HostBinding,
|
|
|
|
|
Input,
|
2025-04-25 17:56:36 +03:00
|
|
|
OnChanges,
|
2023-07-27 17:18:39 +03:00
|
|
|
OnInit,
|
2025-04-25 17:56:36 +03:00
|
|
|
Renderer2,
|
|
|
|
|
SimpleChanges,
|
2023-07-27 17:18:39 +03:00
|
|
|
ViewChild,
|
2025-04-25 17:56:36 +03:00
|
|
|
ViewContainerRef,
|
2023-10-20 15:19:10 +03:00
|
|
|
ViewEncapsulation
|
2023-07-27 17:18:39 +03:00
|
|
|
} from '@angular/core';
|
2025-04-25 17:56:36 +03:00
|
|
|
import { ControlValueAccessor, FormBuilder, FormControl, NG_VALUE_ACCESSOR, Validators } from '@angular/forms';
|
|
|
|
|
import { Observable, of, shareReplay } from 'rxjs';
|
2025-05-09 10:19:37 +03:00
|
|
|
import {
|
|
|
|
|
AllMeasures,
|
|
|
|
|
getSourceTbUnitSymbol,
|
2025-05-09 10:36:40 +03:00
|
|
|
isTbUnitMapping,
|
2025-05-09 10:19:37 +03:00
|
|
|
TbUnit,
|
|
|
|
|
UnitInfo,
|
|
|
|
|
UnitsType,
|
|
|
|
|
UnitSystem
|
|
|
|
|
} from '@shared/models/unit.models';
|
2025-04-25 17:56:36 +03:00
|
|
|
import { map, mergeMap } from 'rxjs/operators';
|
2025-05-01 16:05:35 +03:00
|
|
|
import { UnitService } from '@core/services/unit.service';
|
2025-04-25 17:56:36 +03:00
|
|
|
import { TbPopoverService } from '@shared/components/popover.service';
|
|
|
|
|
import { ConvertUnitSettingsPanelComponent } from '@shared/components/convert-unit-settings-panel.component';
|
2025-05-09 10:36:40 +03:00
|
|
|
import { isDefinedAndNotNull, isEqual, isNotEmptyStr } from '@core/utils';
|
2023-07-14 19:30:18 +03:00
|
|
|
|
2023-07-07 17:27:59 +03:00
|
|
|
@Component({
|
|
|
|
|
selector: 'tb-unit-input',
|
|
|
|
|
templateUrl: './unit-input.component.html',
|
|
|
|
|
styleUrls: ['./unit-input.component.scss'],
|
|
|
|
|
providers: [
|
|
|
|
|
{
|
|
|
|
|
provide: NG_VALUE_ACCESSOR,
|
|
|
|
|
useExisting: forwardRef(() => UnitInputComponent),
|
|
|
|
|
multi: true
|
|
|
|
|
}
|
|
|
|
|
],
|
|
|
|
|
encapsulation: ViewEncapsulation.None
|
|
|
|
|
})
|
2025-04-25 17:56:36 +03:00
|
|
|
export class UnitInputComponent implements ControlValueAccessor, OnInit, OnChanges {
|
2023-07-27 17:18:39 +03:00
|
|
|
|
2025-04-25 17:56:36 +03:00
|
|
|
@HostBinding('style.display') readonly hostDisplay = 'flex';
|
|
|
|
|
@ViewChild('unitInput', {static: true}) unitInput: ElementRef;
|
2023-07-07 17:27:59 +03:00
|
|
|
|
2025-05-01 16:05:35 +03:00
|
|
|
unitsFormControl: FormControl<TbUnit | UnitInfo>;
|
2023-07-07 17:27:59 +03:00
|
|
|
|
2025-04-29 15:35:18 +03:00
|
|
|
@Input({transform: booleanAttribute})
|
2023-07-07 17:27:59 +03:00
|
|
|
disabled: boolean;
|
|
|
|
|
|
2025-04-25 17:56:36 +03:00
|
|
|
@Input({transform: booleanAttribute})
|
2023-10-24 15:08:16 +03:00
|
|
|
required = false;
|
2023-09-12 15:07:33 +03:00
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
tagFilter: UnitsType;
|
|
|
|
|
|
2025-04-25 17:56:36 +03:00
|
|
|
@Input()
|
|
|
|
|
measure: AllMeasures;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
unitSystem: UnitSystem;
|
|
|
|
|
|
|
|
|
|
@Input({transform: booleanAttribute})
|
2025-05-09 10:19:37 +03:00
|
|
|
supportsUnitConversion = false;
|
2023-07-07 17:27:59 +03:00
|
|
|
|
2025-05-09 10:36:40 +03:00
|
|
|
@Input({transform: booleanAttribute})
|
|
|
|
|
onlySystemUnits = false;
|
|
|
|
|
|
2025-05-09 10:19:37 +03:00
|
|
|
filteredUnits$: Observable<Array<[AllMeasures, Array<UnitInfo>]>>;
|
2023-07-07 17:27:59 +03:00
|
|
|
|
|
|
|
|
searchText = '';
|
|
|
|
|
|
2025-04-29 15:35:18 +03:00
|
|
|
isUnitMapping = false;
|
2025-04-28 18:27:35 +03:00
|
|
|
|
2023-07-07 17:27:59 +03:00
|
|
|
private dirty = false;
|
|
|
|
|
|
2025-04-25 17:56:36 +03:00
|
|
|
private modelValue: TbUnit | null;
|
|
|
|
|
|
2025-05-01 16:05:35 +03:00
|
|
|
private fetchUnits$: Observable<Array<[AllMeasures, Array<UnitInfo>]>> = null;
|
2023-07-07 17:27:59 +03:00
|
|
|
|
|
|
|
|
private propagateChange = (_val: any) => {};
|
|
|
|
|
|
2023-10-20 15:19:10 +03:00
|
|
|
constructor(private fb: FormBuilder,
|
2025-04-25 17:56:36 +03:00
|
|
|
private unitService: UnitService,
|
|
|
|
|
private popoverService: TbPopoverService,
|
|
|
|
|
private renderer: Renderer2,
|
|
|
|
|
private viewContainerRef: ViewContainerRef,
|
|
|
|
|
private elementRef: ElementRef) {
|
2023-07-07 17:27:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnInit() {
|
2025-05-01 16:05:35 +03:00
|
|
|
this.unitsFormControl = this.fb.control<TbUnit | UnitInfo>('', this.required ? [Validators.required] : []);
|
2025-05-09 10:19:37 +03:00
|
|
|
this.filteredUnits$ = this.unitsFormControl.valueChanges.pipe(
|
|
|
|
|
map(value => {
|
|
|
|
|
this.updateModel(value);
|
|
|
|
|
return getSourceTbUnitSymbol(value);
|
|
|
|
|
}),
|
|
|
|
|
mergeMap(symbol => this.fetchUnits(symbol))
|
|
|
|
|
);
|
2023-07-07 17:27:59 +03:00
|
|
|
}
|
|
|
|
|
|
2025-04-25 17:56:36 +03:00
|
|
|
ngOnChanges(changes: SimpleChanges) {
|
|
|
|
|
for (const propName of Object.keys(changes)) {
|
|
|
|
|
const change = changes[propName];
|
|
|
|
|
if (!change.firstChange && change.currentValue !== change.previousValue) {
|
|
|
|
|
if (propName === 'measure' || propName === 'unitSystem') {
|
|
|
|
|
this.fetchUnits$ = null;
|
|
|
|
|
this.dirty = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writeValue(symbol?: TbUnit): void {
|
2023-07-07 17:27:59 +03:00
|
|
|
this.searchText = '';
|
|
|
|
|
this.modelValue = symbol;
|
2025-04-25 17:56:36 +03:00
|
|
|
if (typeof symbol === 'string') {
|
2025-05-01 16:05:35 +03:00
|
|
|
this.unitsFormControl.patchValue(this.unitService.getUnitInfo(symbol) ?? symbol, {emitEvent: false});
|
2025-04-29 15:35:18 +03:00
|
|
|
this.isUnitMapping = false;
|
2025-04-25 17:56:36 +03:00
|
|
|
} else {
|
|
|
|
|
this.unitsFormControl.patchValue(symbol, {emitEvent: false});
|
2025-05-09 10:36:40 +03:00
|
|
|
this.isUnitMapping = isDefinedAndNotNull(symbol);
|
2025-04-25 17:56:36 +03:00
|
|
|
}
|
|
|
|
|
this.dirty = true;
|
2023-07-07 17:27:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onFocus() {
|
|
|
|
|
if (this.dirty) {
|
|
|
|
|
this.unitsFormControl.updateValueAndValidity({onlySelf: true, emitEvent: true});
|
|
|
|
|
this.dirty = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-01 16:05:35 +03:00
|
|
|
displayUnitFn(unit?: TbUnit | UnitInfo): string | undefined {
|
2023-07-07 17:27:59 +03:00
|
|
|
if (unit) {
|
2025-05-09 10:19:37 +03:00
|
|
|
return getSourceTbUnitSymbol(unit);
|
2023-07-07 17:27:59 +03:00
|
|
|
}
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerOnChange(fn: any): void {
|
|
|
|
|
this.propagateChange = fn;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-25 17:56:36 +03:00
|
|
|
registerOnTouched(_fn: any): void {
|
2023-07-07 17:27:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setDisabledState(isDisabled: boolean): void {
|
|
|
|
|
this.disabled = isDisabled;
|
|
|
|
|
if (this.disabled) {
|
|
|
|
|
this.unitsFormControl.disable({emitEvent: false});
|
|
|
|
|
} else {
|
|
|
|
|
this.unitsFormControl.enable({emitEvent: false});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-29 15:35:18 +03:00
|
|
|
clear($event: Event) {
|
|
|
|
|
$event.stopPropagation();
|
2023-07-07 17:27:59 +03:00
|
|
|
this.unitsFormControl.patchValue(null, {emitEvent: true});
|
2025-05-09 10:36:40 +03:00
|
|
|
setTimeout(() => {
|
|
|
|
|
this.unitInput.nativeElement.blur();
|
|
|
|
|
this.unitInput.nativeElement.focus();
|
|
|
|
|
}, 0);
|
2023-07-07 17:27:59 +03:00
|
|
|
}
|
2023-07-14 19:30:18 +03:00
|
|
|
|
2025-04-25 17:56:36 +03:00
|
|
|
openConvertSettingsPopup($event: Event) {
|
2025-05-09 10:19:37 +03:00
|
|
|
if (!this.supportsUnitConversion) {
|
2025-04-29 15:35:18 +03:00
|
|
|
return;
|
|
|
|
|
}
|
2025-05-09 10:19:37 +03:00
|
|
|
$event.stopPropagation();
|
2025-04-29 15:35:18 +03:00
|
|
|
this.unitInput.nativeElement.blur();
|
2025-04-25 17:56:36 +03:00
|
|
|
const trigger = this.elementRef.nativeElement;
|
|
|
|
|
if (this.popoverService.hasPopover(trigger)) {
|
|
|
|
|
this.popoverService.hidePopover(trigger);
|
|
|
|
|
} else {
|
2025-05-09 10:19:37 +03:00
|
|
|
const popover = this.popoverService.displayPopover({
|
2025-04-25 17:56:36 +03:00
|
|
|
trigger,
|
|
|
|
|
renderer: this.renderer,
|
|
|
|
|
componentType: ConvertUnitSettingsPanelComponent,
|
|
|
|
|
hostView: this.viewContainerRef,
|
|
|
|
|
preferredPlacement: ['left', 'bottom', 'top'],
|
|
|
|
|
context: {
|
2025-05-09 10:19:37 +03:00
|
|
|
unit: this.extractTbUnit(this.unitsFormControl.value),
|
2025-04-29 15:35:18 +03:00
|
|
|
required: this.required,
|
|
|
|
|
disabled: this.disabled,
|
2025-05-09 10:36:40 +03:00
|
|
|
tagFilter: this.tagFilter,
|
|
|
|
|
measure: this.measure
|
2025-04-25 17:56:36 +03:00
|
|
|
},
|
|
|
|
|
isModal: true
|
|
|
|
|
});
|
2025-05-09 10:19:37 +03:00
|
|
|
popover.tbComponentRef.instance.unitSettingsApplied.subscribe((unitSetting) => {
|
|
|
|
|
popover.hide();
|
2025-04-25 17:56:36 +03:00
|
|
|
this.unitsFormControl.patchValue(unitSetting, {emitEvent: false});
|
2025-05-09 10:19:37 +03:00
|
|
|
this.updateModel(unitSetting);
|
2025-04-25 17:56:36 +03:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-09 10:19:37 +03:00
|
|
|
private updateModel(value: UnitInfo | TbUnit ) {
|
2025-05-09 10:36:40 +03:00
|
|
|
let res = this.extractTbUnit(value);
|
|
|
|
|
if (this.onlySystemUnits && !isTbUnitMapping(res)) {
|
|
|
|
|
const unitInfo = this.unitService.getUnitInfo(res as string);
|
|
|
|
|
if (unitInfo) {
|
|
|
|
|
if (this.measure && unitInfo.measure !== this.measure) {
|
|
|
|
|
res = null;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
res = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!isEqual(this.modelValue, res)) {
|
2025-04-25 17:56:36 +03:00
|
|
|
this.modelValue = res;
|
2025-05-09 10:36:40 +03:00
|
|
|
this.isUnitMapping = isTbUnitMapping(res);
|
2025-04-25 17:56:36 +03:00
|
|
|
this.propagateChange(this.modelValue);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-01 16:05:35 +03:00
|
|
|
private fetchUnits(searchText?: string): Observable<Array<[AllMeasures, Array<UnitInfo>]>> {
|
2025-04-25 17:56:36 +03:00
|
|
|
this.searchText = searchText;
|
2025-05-09 10:19:37 +03:00
|
|
|
return this.getGroupedUnits().pipe(
|
2025-04-28 18:27:35 +03:00
|
|
|
map(unit => this.searchUnit(unit, searchText))
|
2025-04-25 17:56:36 +03:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-09 10:19:37 +03:00
|
|
|
private getGroupedUnits(): Observable<Array<[AllMeasures, Array<UnitInfo>]>> {
|
2023-07-14 19:30:18 +03:00
|
|
|
if (this.fetchUnits$ === null) {
|
2025-05-01 16:05:35 +03:00
|
|
|
this.fetchUnits$ = of(this.unitService.getUnitsGroupedByMeasure(this.measure, this.unitSystem)).pipe(
|
2025-04-28 18:27:35 +03:00
|
|
|
map(data => {
|
2025-05-01 16:05:35 +03:00
|
|
|
let objectData = Object.entries(data) as Array<[AllMeasures, UnitInfo[]]>;
|
2025-04-28 18:27:35 +03:00
|
|
|
|
2023-09-12 15:07:33 +03:00
|
|
|
if (this.tagFilter) {
|
2025-04-28 18:27:35 +03:00
|
|
|
objectData = objectData
|
2025-05-01 16:05:35 +03:00
|
|
|
.map((measure) => [measure[0], measure[1].filter(u => u.tags.includes(this.tagFilter))] as [AllMeasures, UnitInfo[]])
|
2025-04-28 18:27:35 +03:00
|
|
|
.filter((measure) => measure[1].length > 0);
|
2023-09-12 15:07:33 +03:00
|
|
|
}
|
2025-04-28 18:27:35 +03:00
|
|
|
return objectData;
|
2023-09-12 15:07:33 +03:00
|
|
|
}),
|
2023-07-17 12:30:52 +03:00
|
|
|
shareReplay(1)
|
2023-07-14 19:30:18 +03:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return this.fetchUnits$;
|
|
|
|
|
}
|
2025-04-25 17:56:36 +03:00
|
|
|
|
2025-05-01 16:05:35 +03:00
|
|
|
private searchUnit(units: Array<[AllMeasures, Array<UnitInfo>]>, searchText?: string): Array<[AllMeasures, Array<UnitInfo>]> {
|
2025-04-28 18:27:35 +03:00
|
|
|
if (isNotEmptyStr(searchText)) {
|
|
|
|
|
const filterValue = searchText.trim().toUpperCase()
|
2025-05-06 18:42:38 +03:00
|
|
|
return units.reduce((result: Array<[AllMeasures, Array<UnitInfo>]>, [measure, unitInfos]) => {
|
|
|
|
|
const filteredUnits = unitInfos.filter(unit => unit.searchText.toUpperCase().includes(filterValue));
|
|
|
|
|
if (filteredUnits.length > 0) {
|
|
|
|
|
result.push([measure, filteredUnits]);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}, []);
|
2025-04-28 18:27:35 +03:00
|
|
|
}
|
|
|
|
|
return units;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-09 10:19:37 +03:00
|
|
|
private extractTbUnit(value: TbUnit | UnitInfo | null): TbUnit {
|
2025-04-25 17:56:36 +03:00
|
|
|
if (value === null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2025-05-09 10:19:37 +03:00
|
|
|
if (value === undefined) {
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
2025-04-25 17:56:36 +03:00
|
|
|
if (typeof value === 'string') {
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
if ('abbr' in value) {
|
|
|
|
|
return value.abbr;
|
|
|
|
|
}
|
|
|
|
|
return value;
|
|
|
|
|
}
|
2023-07-07 17:27:59 +03:00
|
|
|
}
|