|
|
|
@ -6,7 +6,7 @@ |
|
|
|
*/ |
|
|
|
|
|
|
|
import { UntypedFormControl, UntypedFormGroup, Validators } from '@angular/forms'; |
|
|
|
import { BehaviorSubject, Observable } from 'rxjs'; |
|
|
|
import { BehaviorSubject, combineLatest, Observable, Subject } from 'rxjs'; |
|
|
|
import { distinctUntilChanged, map } from 'rxjs/operators'; |
|
|
|
import { debounceTimeSafe, ExtendedFormGroup, Form, FormArrayTemplate, TemplatedFormArray, Types, value$ } from '@app/framework'; |
|
|
|
import { FormGroupTemplate, TemplatedFormGroup } from '@app/framework/angular/forms/templated-form-group'; |
|
|
|
@ -168,52 +168,59 @@ export class EditContentForm extends Form<ExtendedFormGroup, any> { |
|
|
|
|
|
|
|
protected enable() { |
|
|
|
this.form.enable({ onlySelf: true }); |
|
|
|
|
|
|
|
this.updateState(this.value); |
|
|
|
} |
|
|
|
|
|
|
|
public setContext(context?: any) { |
|
|
|
this.context = context; |
|
|
|
|
|
|
|
this.updateState(this.value); |
|
|
|
} |
|
|
|
|
|
|
|
public submitCompleted(options?: { newValue?: any; noReset?: boolean }) { |
|
|
|
super.submitCompleted(options); |
|
|
|
|
|
|
|
this.updateInitialData(); |
|
|
|
} |
|
|
|
|
|
|
|
private updateState(data: any) { |
|
|
|
const context = { ...this.context || {}, data }; |
|
|
|
|
|
|
|
for (const field of Object.values(this.fields)) { |
|
|
|
field.updateState(context, data, { isDisabled: this.form.disabled }); |
|
|
|
} |
|
|
|
|
|
|
|
for (const section of this.sections) { |
|
|
|
section.updateHidden(); |
|
|
|
private updateInitial(data?: any) { |
|
|
|
for (const [key, field] of Object.entries(this.fields)) { |
|
|
|
field.updateInitial(Types.isObject(data) ? data[key] : undefined); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private updateValue(value: any) { |
|
|
|
this.valueChange$.next(value); |
|
|
|
|
|
|
|
this.updateState(value); |
|
|
|
} |
|
|
|
|
|
|
|
private updateInitialData() { |
|
|
|
this.initialData = this.form.value; |
|
|
|
this.updateInitial(this.initialData); |
|
|
|
} |
|
|
|
|
|
|
|
private updateState(data: any) { |
|
|
|
const context = { ...this.context || {}, data }; |
|
|
|
|
|
|
|
for (const field of Object.values(this.fields)) { |
|
|
|
field.updateState(context, data, { isDisabled: this.form.disabled }); |
|
|
|
} |
|
|
|
|
|
|
|
for (const section of this.sections) { |
|
|
|
section.updateHidden(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
export class FieldForm extends AbstractContentForm<FieldDto, UntypedFormGroup> { |
|
|
|
private readonly partitions: { [partition: string]: FieldItemForm } = {}; |
|
|
|
private readonly initialValue$ = new Subject<any>(); |
|
|
|
private isRequired: boolean; |
|
|
|
|
|
|
|
public readonly translationStatus = |
|
|
|
value$(this.form).pipe(map(x => fieldTranslationStatus(x))); |
|
|
|
|
|
|
|
public readonly hasChanges = |
|
|
|
combineLatest([this.initialValue$, value$(this.form)]).pipe(map(([lhs, rhs]) => !Types.equals(lhs, rhs))); |
|
|
|
|
|
|
|
constructor(args: ControlArgs<FieldDto>) { |
|
|
|
super(args, FieldForm.buildForm()); |
|
|
|
|
|
|
|
@ -273,6 +280,10 @@ export class FieldForm extends AbstractContentForm<FieldDto, UntypedFormGroup> { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public updateInitial(data: any) { |
|
|
|
this.initialValue$.next(data); |
|
|
|
} |
|
|
|
|
|
|
|
private static buildForm() { |
|
|
|
return new ExtendedFormGroup({}); |
|
|
|
} |
|
|
|
|