|
|
@ -14,31 +14,26 @@ |
|
|
/// limitations under the License.
|
|
|
/// limitations under the License.
|
|
|
///
|
|
|
///
|
|
|
|
|
|
|
|
|
import { Component, DestroyRef, Inject, OnInit, SkipSelf } from '@angular/core'; |
|
|
import { Component, DestroyRef, Inject, SkipSelf } from '@angular/core'; |
|
|
import { ErrorStateMatcher } from '@angular/material/core'; |
|
|
import { ErrorStateMatcher } from '@angular/material/core'; |
|
|
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog'; |
|
|
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog'; |
|
|
import { Store } from '@ngrx/store'; |
|
|
import { Store } from '@ngrx/store'; |
|
|
import { AppState } from '@core/core.state'; |
|
|
import { AppState } from '@core/core.state'; |
|
|
import { |
|
|
import { FormArray, FormBuilder, FormControl, FormGroup, FormGroupDirective, NgForm, Validators } from '@angular/forms'; |
|
|
AbstractControl, UntypedFormArray, |
|
|
|
|
|
UntypedFormBuilder, |
|
|
|
|
|
UntypedFormControl, |
|
|
|
|
|
UntypedFormGroup, |
|
|
|
|
|
FormGroupDirective, |
|
|
|
|
|
NgForm, |
|
|
|
|
|
Validators |
|
|
|
|
|
} from '@angular/forms'; |
|
|
|
|
|
import { Router } from '@angular/router'; |
|
|
import { Router } from '@angular/router'; |
|
|
import { DialogComponent } from '@app/shared/components/dialog.component'; |
|
|
import { DialogComponent } from '@app/shared/components/dialog.component'; |
|
|
import { TranslateService } from '@ngx-translate/core'; |
|
|
import { TranslateService } from '@ngx-translate/core'; |
|
|
import { |
|
|
import { |
|
|
EntityKeyValueType, |
|
|
EntityKeyValueType, |
|
|
Filter, FilterPredicateValue, |
|
|
Filter, |
|
|
|
|
|
FilterPredicateValue, |
|
|
filterToUserFilterInfoList, |
|
|
filterToUserFilterInfoList, |
|
|
UserFilterInputInfo |
|
|
UserFilterInputInfo |
|
|
} from '@shared/models/query/query.models'; |
|
|
} from '@shared/models/query/query.models'; |
|
|
import { isDefinedAndNotNull } from '@core/utils'; |
|
|
import { isDefinedAndNotNull } from '@core/utils'; |
|
|
import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; |
|
|
import { takeUntilDestroyed } from '@angular/core/rxjs-interop'; |
|
|
|
|
|
import { UnitService } from '@core/services/unit.service'; |
|
|
|
|
|
import { getSourceTbUnitSymbol, TbUnitConverter } from '@shared/models/unit.models'; |
|
|
|
|
|
|
|
|
export interface UserFilterDialogData { |
|
|
export interface UserFilterDialogData { |
|
|
filter: Filter; |
|
|
filter: Filter; |
|
|
@ -48,14 +43,14 @@ export interface UserFilterDialogData { |
|
|
selector: 'tb-user-filter-dialog', |
|
|
selector: 'tb-user-filter-dialog', |
|
|
templateUrl: './user-filter-dialog.component.html', |
|
|
templateUrl: './user-filter-dialog.component.html', |
|
|
providers: [{provide: ErrorStateMatcher, useExisting: UserFilterDialogComponent}], |
|
|
providers: [{provide: ErrorStateMatcher, useExisting: UserFilterDialogComponent}], |
|
|
styleUrls: [] |
|
|
styleUrls: ['./user-filter-dialog.component.scss'], |
|
|
}) |
|
|
}) |
|
|
export class UserFilterDialogComponent extends DialogComponent<UserFilterDialogComponent, Filter> |
|
|
export class UserFilterDialogComponent extends DialogComponent<UserFilterDialogComponent, Filter> |
|
|
implements OnInit, ErrorStateMatcher { |
|
|
implements ErrorStateMatcher { |
|
|
|
|
|
|
|
|
filter: Filter; |
|
|
filter: Filter; |
|
|
|
|
|
|
|
|
userFilterFormGroup: UntypedFormGroup; |
|
|
userFilterFormGroup: FormGroup; |
|
|
|
|
|
|
|
|
valueTypeEnum = EntityKeyValueType; |
|
|
valueTypeEnum = EntityKeyValueType; |
|
|
|
|
|
|
|
|
@ -66,14 +61,15 @@ export class UserFilterDialogComponent extends DialogComponent<UserFilterDialogC |
|
|
@Inject(MAT_DIALOG_DATA) public data: UserFilterDialogData, |
|
|
@Inject(MAT_DIALOG_DATA) public data: UserFilterDialogData, |
|
|
@SkipSelf() private errorStateMatcher: ErrorStateMatcher, |
|
|
@SkipSelf() private errorStateMatcher: ErrorStateMatcher, |
|
|
public dialogRef: MatDialogRef<UserFilterDialogComponent, Filter>, |
|
|
public dialogRef: MatDialogRef<UserFilterDialogComponent, Filter>, |
|
|
private fb: UntypedFormBuilder, |
|
|
private fb: FormBuilder, |
|
|
public translate: TranslateService, |
|
|
private translate: TranslateService, |
|
|
private destroyRef: DestroyRef) { |
|
|
private destroyRef: DestroyRef, |
|
|
|
|
|
private unitService: UnitService) { |
|
|
super(store, router, dialogRef); |
|
|
super(store, router, dialogRef); |
|
|
this.filter = data.filter; |
|
|
this.filter = data.filter; |
|
|
const userInputs = filterToUserFilterInfoList(this.filter, translate); |
|
|
const userInputs = filterToUserFilterInfoList(this.filter, this.translate); |
|
|
|
|
|
|
|
|
const userInputControls: Array<AbstractControl> = []; |
|
|
const userInputControls: Array<FormGroup> = []; |
|
|
for (const userInput of userInputs) { |
|
|
for (const userInput of userInputs) { |
|
|
userInputControls.push(this.createUserInputFormControl(userInput)); |
|
|
userInputControls.push(this.createUserInputFormControl(userInput)); |
|
|
} |
|
|
} |
|
|
@ -83,12 +79,21 @@ export class UserFilterDialogComponent extends DialogComponent<UserFilterDialogC |
|
|
}); |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private createUserInputFormControl(userInput: UserFilterInputInfo): AbstractControl { |
|
|
private createUserInputFormControl(userInput: UserFilterInputInfo): FormGroup { |
|
|
const predicateValue: FilterPredicateValue<string | number | boolean> = (userInput.info.keyFilterPredicate as any).value; |
|
|
const predicateValue: FilterPredicateValue<string | number | boolean> = (userInput.info.keyFilterPredicate as any).value; |
|
|
const value = isDefinedAndNotNull(predicateValue.userValue) ? predicateValue.userValue : predicateValue.defaultValue; |
|
|
let value = isDefinedAndNotNull(predicateValue.userValue) ? predicateValue.userValue : predicateValue.defaultValue; |
|
|
|
|
|
let unitSymbol = ''; |
|
|
|
|
|
let valueConvertor: TbUnitConverter; |
|
|
|
|
|
if (userInput.valueType === EntityKeyValueType.NUMERIC) { |
|
|
|
|
|
unitSymbol = this.unitService.getTargetUnitSymbol(userInput.unit); |
|
|
|
|
|
const sourceUnit = getSourceTbUnitSymbol(userInput.unit); |
|
|
|
|
|
value = this.unitService.convertUnitValue(value as number, userInput.unit); |
|
|
|
|
|
valueConvertor = this.unitService.geUnitConverter(unitSymbol, sourceUnit); |
|
|
|
|
|
} |
|
|
const userInputControl = this.fb.group({ |
|
|
const userInputControl = this.fb.group({ |
|
|
label: [userInput.label], |
|
|
label: [userInput.label], |
|
|
valueType: [userInput.valueType], |
|
|
valueType: [userInput.valueType], |
|
|
|
|
|
unitSymbol: [unitSymbol], |
|
|
value: [value, |
|
|
value: [value, |
|
|
userInput.valueType === EntityKeyValueType.NUMERIC || |
|
|
userInput.valueType === EntityKeyValueType.NUMERIC || |
|
|
userInput.valueType === EntityKeyValueType.DATE_TIME ? [Validators.required] : []] |
|
|
userInput.valueType === EntityKeyValueType.DATE_TIME ? [Validators.required] : []] |
|
|
@ -96,19 +101,20 @@ export class UserFilterDialogComponent extends DialogComponent<UserFilterDialogC |
|
|
userInputControl.get('value').valueChanges.pipe( |
|
|
userInputControl.get('value').valueChanges.pipe( |
|
|
takeUntilDestroyed(this.destroyRef) |
|
|
takeUntilDestroyed(this.destroyRef) |
|
|
).subscribe(userValue => { |
|
|
).subscribe(userValue => { |
|
|
(userInput.info.keyFilterPredicate as any).value.userValue = userValue; |
|
|
let value = userValue; |
|
|
|
|
|
if (valueConvertor) { |
|
|
|
|
|
value = valueConvertor(value as number); |
|
|
|
|
|
} |
|
|
|
|
|
(userInput.info.keyFilterPredicate as any).value.userValue = value; |
|
|
}); |
|
|
}); |
|
|
return userInputControl; |
|
|
return userInputControl; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
userInputsFormArray(): UntypedFormArray { |
|
|
userInputsFormArray(): FormArray { |
|
|
return this.userFilterFormGroup.get('userInputs') as UntypedFormArray; |
|
|
return this.userFilterFormGroup.get('userInputs') as FormArray; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
ngOnInit(): void { |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
isErrorState(control: UntypedFormControl | null, form: FormGroupDirective | NgForm | null): boolean { |
|
|
isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean { |
|
|
const originalErrorState = this.errorStateMatcher.isErrorState(control, form); |
|
|
const originalErrorState = this.errorStateMatcher.isErrorState(control, form); |
|
|
const customErrorState = !!(control && control.invalid && this.submitted); |
|
|
const customErrorState = !!(control && control.invalid && this.submitted); |
|
|
return originalErrorState || customErrorState; |
|
|
return originalErrorState || customErrorState; |
|
|
|