2023-07-07 17:27:59 +03:00
|
|
|
///
|
2024-01-09 10:46:16 +02:00
|
|
|
/// Copyright © 2016-2024 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 {
|
|
|
|
|
Component,
|
|
|
|
|
ElementRef,
|
|
|
|
|
forwardRef,
|
|
|
|
|
HostBinding,
|
|
|
|
|
Input,
|
|
|
|
|
OnInit,
|
|
|
|
|
ViewChild,
|
2023-10-20 15:19:10 +03:00
|
|
|
ViewEncapsulation
|
2023-07-27 17:18:39 +03:00
|
|
|
} from '@angular/core';
|
2023-09-12 15:07:33 +03:00
|
|
|
import {
|
|
|
|
|
ControlValueAccessor,
|
|
|
|
|
FormBuilder,
|
|
|
|
|
FormControl,
|
|
|
|
|
NG_VALUE_ACCESSOR,
|
|
|
|
|
Validators
|
|
|
|
|
} from '@angular/forms';
|
2023-07-17 12:30:52 +03:00
|
|
|
import { Observable, of, shareReplay, switchMap } from 'rxjs';
|
2023-09-12 15:07:33 +03:00
|
|
|
import { getUnits, searchUnits, Unit, unitBySymbol, UnitsType } from '@shared/models/unit.models';
|
2023-07-17 12:30:52 +03:00
|
|
|
import { map, mergeMap, tap } from 'rxjs/operators';
|
2023-07-07 17:27:59 +03:00
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
2023-07-14 19:30:18 +03:00
|
|
|
import { ResourcesService } from '@core/services/resources.service';
|
2023-10-23 12:56:19 +03:00
|
|
|
import { coerceBoolean } from '@shared/decorators/coercion';
|
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
|
|
|
|
|
})
|
2023-10-20 15:16:51 +03:00
|
|
|
export class UnitInputComponent implements ControlValueAccessor, OnInit {
|
2023-07-07 17:27:59 +03:00
|
|
|
|
2023-07-27 17:18:39 +03:00
|
|
|
@HostBinding('style.display') get hostDisplay() {return 'flex';};
|
|
|
|
|
|
2023-07-07 17:27:59 +03:00
|
|
|
unitsFormControl: FormControl;
|
|
|
|
|
|
|
|
|
|
modelValue: string | null;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
disabled: boolean;
|
|
|
|
|
|
2023-09-12 15:07:33 +03:00
|
|
|
@Input()
|
2023-10-24 15:08:16 +03:00
|
|
|
@coerceBoolean()
|
|
|
|
|
required = false;
|
2023-09-12 15:07:33 +03:00
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
tagFilter: UnitsType;
|
|
|
|
|
|
2023-07-07 17:27:59 +03:00
|
|
|
@ViewChild('unitInput', {static: true}) unitInput: ElementRef;
|
|
|
|
|
|
|
|
|
|
filteredUnits: Observable<Array<Unit | string>>;
|
|
|
|
|
|
|
|
|
|
searchText = '';
|
|
|
|
|
|
|
|
|
|
private dirty = false;
|
|
|
|
|
|
2023-07-14 19:30:18 +03:00
|
|
|
private fetchUnits$: Observable<Array<Unit>> = 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,
|
2023-07-14 19:30:18 +03:00
|
|
|
private resourcesService: ResourcesService,
|
2023-07-07 17:27:59 +03:00
|
|
|
private translate: TranslateService) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnInit() {
|
2023-10-20 15:16:51 +03:00
|
|
|
this.unitsFormControl = this.fb.control('', this.required ? [Validators.required] : []);
|
2023-07-07 17:27:59 +03:00
|
|
|
this.filteredUnits = this.unitsFormControl.valueChanges
|
|
|
|
|
.pipe(
|
|
|
|
|
tap(value => {
|
|
|
|
|
this.updateView(value);
|
|
|
|
|
}),
|
|
|
|
|
map(value => (value as Unit)?.symbol ? (value as Unit).symbol : (value ? value as string : '')),
|
2023-07-17 12:30:52 +03:00
|
|
|
mergeMap(symbol => this.fetchUnits(symbol))
|
2023-07-07 17:27:59 +03:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writeValue(symbol?: string): void {
|
|
|
|
|
this.searchText = '';
|
|
|
|
|
this.modelValue = symbol;
|
2023-07-17 12:30:52 +03:00
|
|
|
of(symbol).pipe(
|
|
|
|
|
switchMap(value => value
|
|
|
|
|
? this.unitsConstant().pipe(map(units => unitBySymbol(units, value) ?? value))
|
2023-07-14 19:30:18 +03:00
|
|
|
: of(null))
|
|
|
|
|
).subscribe(result => {
|
|
|
|
|
this.unitsFormControl.patchValue(result, {emitEvent: false});
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateView(value: Unit | string | null) {
|
|
|
|
|
const res: string = (value as Unit)?.symbol ? (value as Unit)?.symbol : (value as string);
|
|
|
|
|
if (this.modelValue !== res) {
|
|
|
|
|
this.modelValue = res;
|
|
|
|
|
this.propagateChange(this.modelValue);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
displayUnitFn(unit?: Unit | string): string | undefined {
|
|
|
|
|
if (unit) {
|
|
|
|
|
if ((unit as Unit).symbol) {
|
|
|
|
|
return (unit as Unit).symbol;
|
|
|
|
|
} else {
|
|
|
|
|
return unit as string;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fetchUnits(searchText?: string): Observable<Array<Unit | string>> {
|
|
|
|
|
this.searchText = searchText;
|
2023-07-14 19:30:18 +03:00
|
|
|
return this.unitsConstant().pipe(
|
|
|
|
|
map(unit => searchUnits(unit, searchText))
|
|
|
|
|
);
|
2023-07-07 17:27:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerOnChange(fn: any): void {
|
|
|
|
|
this.propagateChange = fn;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerOnTouched(fn: any): void {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setDisabledState(isDisabled: boolean): void {
|
|
|
|
|
this.disabled = isDisabled;
|
|
|
|
|
if (this.disabled) {
|
|
|
|
|
this.unitsFormControl.disable({emitEvent: false});
|
|
|
|
|
} else {
|
|
|
|
|
this.unitsFormControl.enable({emitEvent: false});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clear() {
|
|
|
|
|
this.unitsFormControl.patchValue(null, {emitEvent: true});
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
this.unitInput.nativeElement.blur();
|
|
|
|
|
this.unitInput.nativeElement.focus();
|
|
|
|
|
}, 0);
|
|
|
|
|
}
|
2023-07-14 19:30:18 +03:00
|
|
|
|
|
|
|
|
private unitsConstant(): Observable<Array<Unit>> {
|
|
|
|
|
if (this.fetchUnits$ === null) {
|
2023-07-27 17:18:39 +03:00
|
|
|
this.fetchUnits$ = getUnits(this.resourcesService).pipe(
|
2023-09-12 15:07:33 +03:00
|
|
|
map((units) => {
|
|
|
|
|
if (this.tagFilter) {
|
|
|
|
|
units = units.filter(u => u.tags.includes(this.tagFilter));
|
|
|
|
|
}
|
|
|
|
|
return units.map(u => ({
|
|
|
|
|
symbol: u.symbol,
|
|
|
|
|
name: this.translate.instant(u.name),
|
|
|
|
|
tags: u.tags
|
|
|
|
|
}));
|
|
|
|
|
}),
|
2023-07-17 12:30:52 +03:00
|
|
|
shareReplay(1)
|
2023-07-14 19:30:18 +03:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return this.fetchUnits$;
|
|
|
|
|
}
|
2023-07-07 17:27:59 +03:00
|
|
|
}
|