Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #323 - Make dropdownenum dynamic #324

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 28 additions & 22 deletions core/app/common/src/lib/record/field.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@
*/

import {SearchCriteriaFieldFilter} from '../views/list/search-criteria.model';
import {BehaviorSubject, Observable} from 'rxjs';
import {Observable, Subject} from 'rxjs';
import {AsyncValidatorFn, FormArray, FormControl, ValidatorFn} from '@angular/forms';
import {Record} from './record.model';
import {FieldLogicMap} from '../actions/field-logic-action.model';
import {ObjectMap} from '../types/object-map';
import {ViewMode} from '../views/view.model';
import {isEqual} from 'lodash-es';
import {deepClone} from '../utils/object-utils';


export interface Option {
value: string;
Expand Down Expand Up @@ -151,6 +154,12 @@ export interface AttributeDependency {
attribute: string;
}

export interface FieldValue {
value?: string;
valueList?: string[];
valueObject?: any;
}

export interface Field {
type: string;
value?: string;
Expand All @@ -176,7 +185,7 @@ export interface Field {
itemFormArray?: FormArray;
validators?: ValidatorFn[];
asyncValidators?: AsyncValidatorFn[];
valueSubject?: BehaviorSubject<FieldValue>;
valueSubject?: Subject<FieldValue>;
valueChanges$?: Observable<FieldValue>;
fieldDependencies?: string[];
attributeDependencies?: AttributeDependency[];
Expand All @@ -201,7 +210,7 @@ export class BaseField implements Field {
validators?: ValidatorFn[];
asyncValidators?: AsyncValidatorFn[];
attributes?: FieldAttributeMap;
valueSubject?: BehaviorSubject<FieldValue>;
valueSubject?: Subject<FieldValue>;
valueChanges$?: Observable<FieldValue>;
fieldDependencies: string[] = [];
attributeDependencies: AttributeDependency[] = [];
Expand All @@ -213,7 +222,7 @@ export class BaseField implements Field {
protected valueObjectArrayState?: ObjectMap[];

constructor() {
this.valueSubject = new BehaviorSubject<FieldValue>({} as FieldValue);
this.valueSubject = new Subject<FieldValue>();
this.valueChanges$ = this.valueSubject.asObservable();
}

Expand All @@ -223,9 +232,7 @@ export class BaseField implements Field {

set value(value: string) {
const changed = value !== this.valueState;

this.valueState = value;

if (changed) {
this.emitValueChanges();
}
Expand All @@ -236,28 +243,35 @@ export class BaseField implements Field {
}

set valueList(value: string[]) {

this.valueListState = value;

this.emitValueChanges();
const changed = !isEqual(value, this.valueListState)
this.valueListState = deepClone(value);
if (changed) {
this.emitValueChanges();
}
}

get valueObject(): any {
return this.valueObjectState;
}

set valueObject(value: any) {
this.valueObjectState = value;
this.emitValueChanges();
const changed = !isEqual(value, this.valueObjectState)
this.valueObjectState = deepClone(value);
if (changed) {
this.emitValueChanges();
}
}

get valueObjectArray(): any {
return this.valueObjectArrayState;
}

set valueObjectArray(value: ObjectMap[]) {
this.valueObjectArrayState = value;
this.emitValueChanges();
const changed = !isEqual(value, this.valueObjectArrayState)
this.valueObjectArrayState = deepClone(value);
if (changed) {
this.emitValueChanges();
}
}

protected emitValueChanges() {
Expand All @@ -268,11 +282,3 @@ export class BaseField implements Field {
})
}
}

export interface FieldValue {
value?: string;
valueList?: string[];
valueObject?: any;
}


35 changes: 20 additions & 15 deletions core/app/core/src/lib/fields/base/base-enum.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,23 +300,28 @@ export class BaseEnumComponent extends BaseFieldComponent implements OnInit, OnD
}

protected subscribeToParentValueChanges(parentEnum: Field): void {
if (parentEnum.formControl) {
this.subs.push(parentEnum.formControl.valueChanges.subscribe(values => {

if (typeof values === 'string') {
values = [values];
}

// Reset selected values on Form Control
this.field.value = '';
this.field.formControl.setValue('');
this.subs.push(parentEnum.valueChanges$.subscribe(fieldValue => {
let valueList = [];
if (fieldValue.valueList && fieldValue.valueList.length) {
valueList = fieldValue.valueList;
} else if(parentEnum.value) {
const value = fieldValue.value ?? ''
valueList = [value]
} else {
valueList = [];
}

// Rebuild available enum options
this.options = this.filterMatchingOptions(values);
// Rebuild available enum options
this.options = this.filterMatchingOptions(valueList);
this.setDependentValue();
this.initValue();
}));
}

this.initValue();
}));
}
protected setDependentValue(): void {
// Reset selected values on Form Control
this.field.value = '';
this.field.formControl.setValue('');
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<div class="dropdownenum">
<select [formControl]="field.formControl" class="custom-select custom-select-sm">
<ng-container *ngIf="this.options && this.options.length">
<option class="d-none" disabled value=""></option>
<option *ngFor="let item of this.options;"
class="{{getId(item)}}"
[ngValue]="item.value">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export class DropdownEnumEditFieldComponent extends BaseEnumComponent {
}

ngOnInit(): void {
this.checkAndInitAsDynamicEnum();
super.ngOnInit();

this.subscribeValueChanges();
Expand Down