UI: Improve load units models
This commit is contained in:
parent
523ac331e6
commit
2d1ef5db8c
@ -15,11 +15,18 @@
|
|||||||
///
|
///
|
||||||
|
|
||||||
import { Component, ElementRef, forwardRef, Input, OnInit, ViewChild, ViewEncapsulation } from '@angular/core';
|
import { Component, ElementRef, forwardRef, Input, OnInit, ViewChild, ViewEncapsulation } from '@angular/core';
|
||||||
import { ControlValueAccessor, FormControl, NG_VALUE_ACCESSOR, UntypedFormBuilder } from '@angular/forms';
|
import { ControlValueAccessor, FormBuilder, FormControl, NG_VALUE_ACCESSOR } from '@angular/forms';
|
||||||
import { Observable, of } from 'rxjs';
|
import { EMPTY, Observable, of, ReplaySubject, switchMap } from 'rxjs';
|
||||||
import { searchUnits, Unit, unitBySymbol, units } from '@shared/models/unit.models';
|
import { searchUnits, Unit, unitBySymbol } from '@shared/models/unit.models';
|
||||||
import { map, mergeMap, startWith, tap } from 'rxjs/operators';
|
import { map, mergeMap, share, startWith, tap } from 'rxjs/operators';
|
||||||
import { TranslateService } from '@ngx-translate/core';
|
import { TranslateService } from '@ngx-translate/core';
|
||||||
|
import { ResourcesService } from '@core/services/resources.service';
|
||||||
|
|
||||||
|
const unitsModels = '/assets/model/units.json';
|
||||||
|
|
||||||
|
interface UnitsJson {
|
||||||
|
units: Array<Unit>;
|
||||||
|
}
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'tb-unit-input',
|
selector: 'tb-unit-input',
|
||||||
@ -51,13 +58,12 @@ export class UnitInputComponent implements ControlValueAccessor, OnInit {
|
|||||||
|
|
||||||
private dirty = false;
|
private dirty = false;
|
||||||
|
|
||||||
private translatedUnits: Array<Unit> = units.map(u => ({symbol: u.symbol,
|
private fetchUnits$: Observable<Array<Unit>> = null;
|
||||||
name: this.translate.instant(u.name),
|
|
||||||
tags: u.tags}));
|
|
||||||
|
|
||||||
private propagateChange = (_val: any) => {};
|
private propagateChange = (_val: any) => {};
|
||||||
|
|
||||||
constructor(private fb: UntypedFormBuilder,
|
constructor(private fb: FormBuilder,
|
||||||
|
private resourcesService: ResourcesService,
|
||||||
private translate: TranslateService) {
|
private translate: TranslateService) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -68,7 +74,6 @@ export class UnitInputComponent implements ControlValueAccessor, OnInit {
|
|||||||
tap(value => {
|
tap(value => {
|
||||||
this.updateView(value);
|
this.updateView(value);
|
||||||
}),
|
}),
|
||||||
startWith<string | Unit>(''),
|
|
||||||
map(value => (value as Unit)?.symbol ? (value as Unit).symbol : (value ? value as string : '')),
|
map(value => (value as Unit)?.symbol ? (value as Unit).symbol : (value ? value as string : '')),
|
||||||
mergeMap(symbol => this.fetchUnits(symbol) )
|
mergeMap(symbol => this.fetchUnits(symbol) )
|
||||||
);
|
);
|
||||||
@ -77,13 +82,15 @@ export class UnitInputComponent implements ControlValueAccessor, OnInit {
|
|||||||
writeValue(symbol?: string): void {
|
writeValue(symbol?: string): void {
|
||||||
this.searchText = '';
|
this.searchText = '';
|
||||||
this.modelValue = symbol;
|
this.modelValue = symbol;
|
||||||
let res: Unit | string = null;
|
EMPTY.pipe(
|
||||||
if (symbol) {
|
startWith(''),
|
||||||
const unit = unitBySymbol(symbol);
|
switchMap(() => symbol
|
||||||
res = unit ? unit : symbol;
|
? this.unitsConstant().pipe(map(units => unitBySymbol(units, symbol) ?? symbol))
|
||||||
}
|
: of(null))
|
||||||
this.unitsFormControl.patchValue(res, {emitEvent: false});
|
).subscribe(result => {
|
||||||
this.dirty = true;
|
this.unitsFormControl.patchValue(result, {emitEvent: false});
|
||||||
|
this.dirty = true;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
onFocus() {
|
onFocus() {
|
||||||
@ -114,12 +121,9 @@ export class UnitInputComponent implements ControlValueAccessor, OnInit {
|
|||||||
|
|
||||||
fetchUnits(searchText?: string): Observable<Array<Unit | string>> {
|
fetchUnits(searchText?: string): Observable<Array<Unit | string>> {
|
||||||
this.searchText = searchText;
|
this.searchText = searchText;
|
||||||
const result = searchUnits(this.translatedUnits, searchText);
|
return this.unitsConstant().pipe(
|
||||||
if (result.length) {
|
map(unit => searchUnits(unit, searchText))
|
||||||
return of(result);
|
);
|
||||||
} else {
|
|
||||||
return of([]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
registerOnChange(fn: any): void {
|
registerOnChange(fn: any): void {
|
||||||
@ -145,4 +149,23 @@ export class UnitInputComponent implements ControlValueAccessor, OnInit {
|
|||||||
this.unitInput.nativeElement.focus();
|
this.unitInput.nativeElement.focus();
|
||||||
}, 0);
|
}, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private unitsConstant(): Observable<Array<Unit>> {
|
||||||
|
if (this.fetchUnits$ === null) {
|
||||||
|
this.fetchUnits$ = this.resourcesService.loadJsonResource<UnitsJson>(unitsModels).pipe(
|
||||||
|
map(units => units.units.map(u => ({
|
||||||
|
symbol: u.symbol,
|
||||||
|
name: this.translate.instant(u.name),
|
||||||
|
tags: u.tags
|
||||||
|
}))),
|
||||||
|
share({
|
||||||
|
connector: () => new ReplaySubject(1),
|
||||||
|
resetOnError: false,
|
||||||
|
resetOnComplete: false,
|
||||||
|
resetOnRefCountZero: false
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return this.fetchUnits$;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
2022
ui-ngx/src/assets/model/units.json
Normal file
2022
ui-ngx/src/assets/model/units.json
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user