Skip to content

Commit

Permalink
Fix #323 - Dynamic dropdown
Browse files Browse the repository at this point in the history
  • Loading branch information
yunusyerli1 committed Nov 7, 2023
1 parent 957b2eb commit 082297a
Show file tree
Hide file tree
Showing 11 changed files with 393 additions and 371 deletions.
101 changes: 84 additions & 17 deletions core/app/common/src/lib/record/field.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@
* the words "Supercharged by SuiteCRM".
*/

import {SearchCriteriaFieldFilter} from '../views/list/search-criteria.model';
import {cloneDeep, isArray, isEqual, isObject, isString, uniq} from 'lodash-es';
import {BehaviorSubject, Observable} from 'rxjs';
import {AsyncValidatorFn, FormArray, FormControl, ValidatorFn} from '@angular/forms';
import {SearchCriteriaFieldFilter} from '../views/list/search-criteria.model';
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 {deepClone} from '../utils/object-utils';

export type DisplayType = 'none' | 'show' | 'readonly' | 'inline' | 'disabled' | 'default';

Expand Down Expand Up @@ -97,7 +99,7 @@ export interface FieldDefinition {
default?: string;
modes?: ViewMode[];
relationship?: string;
relationshipMetadata?: RelationshipMetadata
relationshipMetadata?: RelationshipMetadata;

[key: string]: any;
}
Expand Down Expand Up @@ -157,6 +159,16 @@ export interface AttributeDependency {
types: string[];
}

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

export interface FieldValueMap {
[key: string]: FieldValue;
}

export interface Field {
type: string;
value?: string;
Expand Down Expand Up @@ -185,11 +197,14 @@ export interface Field {
asyncValidators?: AsyncValidatorFn[];
valueSubject?: BehaviorSubject<FieldValue>;
valueChanges$?: Observable<FieldValue>;
options?: Option[];
optionsState?: Option[];
optionsSubject?: BehaviorSubject<Option[]>;
optionsChanges$?: Observable<Option[]>;
fieldDependencies?: ObjectMap;
attributeDependencies?: AttributeDependency[];
logic?: FieldLogicMap;
displayLogic?: FieldLogicMap;
previousValue?: string;
}

export class BaseField implements Field {
Expand All @@ -212,6 +227,9 @@ export class BaseField implements Field {
attributes?: FieldAttributeMap;
valueSubject?: BehaviorSubject<FieldValue>;
valueChanges$?: Observable<FieldValue>;
optionsState?: Option[];
optionsSubject?: BehaviorSubject<Option[]>;
optionsChanges$?: Observable<Option[]>;
fieldDependencies: ObjectMap = {};
attributeDependencies: AttributeDependency[] = [];
logic?: FieldLogicMap;
Expand All @@ -225,30 +243,48 @@ export class BaseField implements Field {
constructor() {
this.valueSubject = new BehaviorSubject<FieldValue>({} as FieldValue);
this.valueChanges$ = this.valueSubject.asObservable();

this.optionsSubject = new BehaviorSubject<Option[]>(this.optionsState);
this.optionsChanges$ = this.optionsSubject.asObservable();
}

get value(): string {
return this.valueState;
}

set value(value: string) {
const changed = value !== this.valueState;
if (!isString(value)) {
this.setValue(value);
return;
}

this.valueState = value;
const valueClean: string = value.trim();

if (changed) {
this.emitValueChanges();
if (isEqual(this.valueState, valueClean)) {
return;
}

this.valueState = valueClean;
this.emitValueChanges();
}

get valueList(): string[] {
return this.valueListState;
}

set valueList(value: string[]) {
if (!isArray(value)) {
this.setValue(value);
return;
}

this.valueListState = value;
const valueListClean: string[] = uniq(deepClone(value));

if (isEqual(this.valueListState, valueListClean)) {
return;
}

this.valueListState = valueListClean;
this.emitValueChanges();
}

Expand All @@ -257,7 +293,16 @@ export class BaseField implements Field {
}

set valueObject(value: any) {
this.valueObjectState = value;
if (!isObject(value)) {
this.setValue(value);
return;
}

if (isEqual(this.valueObjectState, value)) {
return;
}

this.valueObjectState = deepClone(value);
this.emitValueChanges();
}

Expand All @@ -266,23 +311,45 @@ export class BaseField implements Field {
}

set valueObjectArray(value: ObjectMap[]) {
if (isEqual(this.valueObjectArrayState, value)) {
return;
}

this.valueObjectArrayState = value;
this.emitValueChanges();
}

protected emitValueChanges() {
public setValue(value: string | string[] | any): void {
if (isString(value)) {
this.value = value;
} else if (isArray(value)) {
this.valueList = value;
} else if (isObject(value)) {
this.valueObject = value;
} else {
this.value = value?.toString() ?? '';
}
}

protected emitValueChanges(): void {
this.valueSubject.next({
value: this.valueState,
valueList: this.valueListState,
valueObject: this.valueObjectState
})
});
}
}

export interface FieldValue {
value?: string;
valueList?: string[];
valueObject?: any;
}
get options(): Option[] {
return this.optionsState
}

set options(options: Option[]) {
this.optionsState = options;
this.emitOptionsChanges();
}

protected emitOptionsChanges() {
this.optionsSubject.next(cloneDeep(this.optionsState))
}

}
Loading

0 comments on commit 082297a

Please sign in to comment.