import { BehaviorSubject, Observable } from 'rxjs'; import { FieldSetModel } from './set'; import { BasicModel } from './basic'; import { ValidateOption } from '../validate'; import UniqueId from '../unique-id'; enum FormStrategy { /** * 指定 model 模式 */ Model, /** * 视图驱动模式 */ View, } const FORM_ID = Symbol('form'); const uniqueId = new UniqueId('form'); class FormModel< Children extends Record> = Record> > extends FieldSetModel { /** * @internal */ [FORM_ID]!: boolean; /** @internal */ private readonly workingValidators = new Set>(); readonly isValidating$ = new BehaviorSubject(false); get owner(): BasicModel | null { return this; } set owner(owner: BasicModel | null) { // noop } get form(): FormModel | null | undefined { return this as FormModel; } constructor(public readonly children: Children) { super(children, uniqueId.get()); const keys = Object.keys(children); const keysLength = keys.length; for (let index = 0; index < keysLength; index++) { const name = keys[index]; const child = children[name]; this.registerChild(name, child); } } /** * 执行整个 `Form` 的校验,默认会递归触发所有表单元素的校验 * @param option 表单校验的参数 */ validate(option: ValidateOption = ValidateOption.Default | ValidateOption.IncludeChildrenRecursively) { return super.validate(option); } /** @internal */ addWorkingValidator(v: Observable) { this.workingValidators.add(v); this.updateIsValidating(); } /** @internal */ removeWorkingValidator(v: Observable) { this.workingValidators.delete(v); this.updateIsValidating(); } /** @internal */ private updateIsValidating() { const isValidating = this.workingValidators.size > 0; if (isValidating !== this.isValidating$.getValue()) { this.isValidating$.next(isValidating); } } } FormModel.prototype[FORM_ID] = true; function isFormModel> = Record>>( maybeModel: any, ): maybeModel is FormModel { return !!(maybeModel && maybeModel[FORM_ID]); } export { FormStrategy, FormModel, isFormModel };