@lxlib/form
Version:
This library was generated with [Angular CLI](https://github.com/angular/angular-cli) version 9.1.1.
1,686 lines (1,678 loc) • 282 kB
JavaScript
import { Injectable, ɵɵdefineInjectable, Inject, ComponentFactoryResolver, EventEmitter, Component, ChangeDetectionStrategy, ViewEncapsulation, ChangeDetectorRef, Optional, Input, Output, ViewChild, ViewContainerRef, Directive, ElementRef, Renderer2, TemplateRef, Injector, HostBinding, NgModule } from '@angular/core';
import { __spread, __values, __rest, __assign, __extends, __decorate, __metadata } from 'tslib';
import { DomSanitizer } from '@angular/platform-browser';
import { ACLService } from '@lxlib/acl';
import { LxlibLocaleService, LXLIB_I18N_TOKEN, LxlibLocaleModule } from '@lxlib/theme';
import { toBoolean, deepCopy, InputBoolean, InputNumber, deepGet, LxlibUtilModule } from '@lxlib/util';
import { of, BehaviorSubject, Observable, combineLatest, Subject, merge } from 'rxjs';
import { map, distinctUntilChanged, takeUntil, filter, debounceTime, startWith, flatMap, tap } from 'rxjs/operators';
import { CommonModule } from '@angular/common';
import { NgModel, FormsModule } from '@angular/forms';
import { NzAutocompleteModule } from 'ng-zorro-antd/auto-complete';
import { NzButtonModule } from 'ng-zorro-antd/button';
import { NzCardModule } from 'ng-zorro-antd/card';
import { NzCascaderModule } from 'ng-zorro-antd/cascader';
import { NzCheckboxModule } from 'ng-zorro-antd/checkbox';
import { NzDatePickerModule } from 'ng-zorro-antd/date-picker';
import { NzFormModule } from 'ng-zorro-antd/form';
import { NzGridModule } from 'ng-zorro-antd/grid';
import { NzIconModule } from 'ng-zorro-antd/icon';
import { NzInputModule } from 'ng-zorro-antd/input';
import { NzInputNumberModule } from 'ng-zorro-antd/input-number';
import { NzMentionModule } from 'ng-zorro-antd/mention';
import { NzModalService, NzModalModule } from 'ng-zorro-antd/modal';
import { NzRadioModule } from 'ng-zorro-antd/radio';
import { NzRateModule } from 'ng-zorro-antd/rate';
import { NzSelectModule } from 'ng-zorro-antd/select';
import { NzSliderModule } from 'ng-zorro-antd/slider';
import { NzSwitchModule } from 'ng-zorro-antd/switch';
import { NzTagModule } from 'ng-zorro-antd/tag';
import { NzTimePickerModule } from 'ng-zorro-antd/time-picker';
import { NzToolTipModule } from 'ng-zorro-antd/tooltip';
import { NzTransferModule } from 'ng-zorro-antd/transfer';
import { NzTreeSelectModule } from 'ng-zorro-antd/tree-select';
import { NzUploadModule } from 'ng-zorro-antd/upload';
import { helpMotion } from 'ng-zorro-antd/core/animation';
import format from 'date-fns/format';
import parse from 'date-fns/parse';
/**
* @fileoverview added by tsickle
* Generated from: src/config.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
var LxlibFormConfig = /** @class */ (function () {
function LxlibFormConfig() {
/**
* 是否忽略某些数据类型校验 `ERRORSDEFAULT`,默认:`[ 'type', 'enum' ]`
*
* - `type` 限定 Schema 中 `type` 类型
* - `enum` 限定应当是预设定的枚举值之一
*/
this.ingoreKeywords = ['type', 'enum'];
/**
* 是否实时校验,默认:`true`
* - `true` 每一次都校验
* - `false` 提交时校验
*/
this.liveValidate = true;
/**
* 指定表单 `autocomplete` 值,默认:`on`
*/
this.autocomplete = null;
/**
* 是否立即呈现错误视觉,默认:`false`
*/
this.firstVisual = false;
/**
* 是否只展示错误视觉不显示错误文本,默认:`false`
*/
this.onlyVisual = false;
/**
* 自定义通用错误信息
*/
this.errors = {};
/**
* 按钮风格
*/
this.button = {
submit_type: 'primary',
reset_type: 'default',
};
/**
* date小部件:`type="string"` 且不指定 `schema.format` 和 `ui.format` 时日期格式,默认:`yyyy-MM-dd HH:mm:ss`
*/
this.uiDateStringFormat = 'yyyy-MM-dd HH:mm:ss';
/**
* date小部件:`type="number"` 且不指定 `schema.format` 和 `ui.format` 时日期格式,默认:`x` 13位Unix Timestamp
*/
this.uiDateNumberFormat = 'x';
/**
* time小部件:`type="string"` 且不指定 `schema.format` 和 `ui.format` 时日期格式,默认:`HH:mm:ss`
*/
this.uiTimeStringFormat = 'HH:mm:ss';
/**
* time小部件:`type="number"` 且不指定 `schema.format` 和 `ui.format` 时日期格式,默认:`x` 13位Unix Timestamp,日期统一使用 `1970-01-01`
*/
this.uiTimeNumberFormat = 'x';
/**
* 指定 `format: 'email'` 的默认Email后缀
*/
this.uiEmailSuffixes = ['qq.com', '163.com', 'gmail.com', '126.com', 'aliyun.com'];
}
LxlibFormConfig.decorators = [
{ type: Injectable, args: [{ providedIn: 'root' },] }
];
/** @nocollapse */ LxlibFormConfig.ɵprov = ɵɵdefineInjectable({ factory: function LxlibFormConfig_Factory() { return new LxlibFormConfig(); }, token: LxlibFormConfig, providedIn: "root" });
return LxlibFormConfig;
}());
if (false) {
/**
* 是否忽略某些数据类型校验 `ERRORSDEFAULT`,默认:`[ 'type', 'enum' ]`
*
* - `type` 限定 Schema 中 `type` 类型
* - `enum` 限定应当是预设定的枚举值之一
* @type {?}
*/
LxlibFormConfig.prototype.ingoreKeywords;
/**
* [ajv](http://epoberezkin.github.io/ajv/#options) 参数
* @type {?}
*/
LxlibFormConfig.prototype.ajv;
/**
* 是否实时校验,默认:`true`
* - `true` 每一次都校验
* - `false` 提交时校验
* @type {?}
*/
LxlibFormConfig.prototype.liveValidate;
/**
* 指定表单 `autocomplete` 值,默认:`on`
* @type {?}
*/
LxlibFormConfig.prototype.autocomplete;
/**
* 是否立即呈现错误视觉,默认:`false`
* @type {?}
*/
LxlibFormConfig.prototype.firstVisual;
/**
* 是否只展示错误视觉不显示错误文本,默认:`false`
* @type {?}
*/
LxlibFormConfig.prototype.onlyVisual;
/**
* 自定义通用错误信息
* @type {?}
*/
LxlibFormConfig.prototype.errors;
/**
* 默认全局布局
* @type {?}
*/
LxlibFormConfig.prototype.ui;
/**
* 元素组件大小,用于 `nzSize` 值
* @type {?}
*/
LxlibFormConfig.prototype.size;
/**
* 按钮风格
* @type {?}
*/
LxlibFormConfig.prototype.button;
/**
* date小部件:`type="string"` 且不指定 `schema.format` 和 `ui.format` 时日期格式,默认:`yyyy-MM-dd HH:mm:ss`
* @type {?}
*/
LxlibFormConfig.prototype.uiDateStringFormat;
/**
* date小部件:`type="number"` 且不指定 `schema.format` 和 `ui.format` 时日期格式,默认:`x` 13位Unix Timestamp
* @type {?}
*/
LxlibFormConfig.prototype.uiDateNumberFormat;
/**
* time小部件:`type="string"` 且不指定 `schema.format` 和 `ui.format` 时日期格式,默认:`HH:mm:ss`
* @type {?}
*/
LxlibFormConfig.prototype.uiTimeStringFormat;
/**
* time小部件:`type="number"` 且不指定 `schema.format` 和 `ui.format` 时日期格式,默认:`x` 13位Unix Timestamp,日期统一使用 `1970-01-01`
* @type {?}
*/
LxlibFormConfig.prototype.uiTimeNumberFormat;
/**
* 指定 `format: 'email'` 的默认Email后缀
* @type {?}
*/
LxlibFormConfig.prototype.uiEmailSuffixes;
}
/**
* @fileoverview added by tsickle
* Generated from: src/const.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/** @type {?} */
var SF_SEQ = '/';
/**
* @fileoverview added by tsickle
* Generated from: src/utils.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/** @type {?} */
var FORMATMAPS = {
'date-time': {
widget: 'date',
showTime: true,
format: 'yyyy-MM-ddTHH:mm:ssZ',
},
date: { widget: 'date', format: 'yyyy-MM-dd' },
'full-date': { widget: 'date', format: 'yyyy-MM-dd' },
time: { widget: 'time' },
'full-time': { widget: 'time' },
week: { widget: 'date', mode: 'week', format: 'yyyy-WW' },
month: { widget: 'date', mode: 'month', format: 'yyyy-MM' },
uri: { widget: 'upload' },
email: { widget: 'autocomplete', type: 'email' },
color: { widget: 'string', type: 'color' },
'': { widget: 'string' },
};
/**
* @param {?} o
* @return {?}
*/
function isBlank(o) {
return o == null;
}
/**
* @param {?} value
* @param {?} defaultValue
* @return {?}
*/
function toBool(value, defaultValue) {
value = toBoolean(value, true);
return value == null ? defaultValue : value;
}
/**
* @param {?} ui
* @param {...?} args
* @return {?}
*/
function di(ui) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
if (ui.debug) {
// tslint:disable-next-line:no-console
console.warn.apply(console, __spread(args));
}
}
/**
* 根据 `$ref` 查找 `definitions`
* @param {?} $ref
* @param {?} definitions
* @return {?}
*/
function findSchemaDefinition($ref, definitions) {
var e_1, _a;
/** @type {?} */
var match = /^#\/definitions\/(.*)$/.exec($ref);
if (match && match[1]) {
// parser JSON Pointer
/** @type {?} */
var parts = match[1].split(SF_SEQ);
/** @type {?} */
var current = definitions;
try {
for (var parts_1 = __values(parts), parts_1_1 = parts_1.next(); !parts_1_1.done; parts_1_1 = parts_1.next()) {
var part = parts_1_1.value;
part = part.replace(/~1/g, SF_SEQ).replace(/~0/g, '~');
if (current.hasOwnProperty(part)) {
current = current[part];
}
else {
throw new Error("Could not find a definition for " + $ref + ".");
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (parts_1_1 && !parts_1_1.done && (_a = parts_1.return)) _a.call(parts_1);
}
finally { if (e_1) throw e_1.error; }
}
return current;
}
throw new Error("Could not find a definition for " + $ref + ".");
}
/**
* 取回Schema,并处理 `$ref` 的关系
* @param {?} schema
* @param {?=} definitions
* @return {?}
*/
function retrieveSchema(schema, definitions) {
if (definitions === void 0) { definitions = {}; }
if (schema.hasOwnProperty('$ref')) {
/** @type {?} */
var $refSchema = findSchemaDefinition((/** @type {?} */ (schema.$ref)), definitions);
// remove $ref property
var $ref = schema.$ref, localSchema = __rest(schema, ["$ref"]);
return retrieveSchema(__assign(__assign({}, $refSchema), localSchema), definitions);
}
return schema;
}
/**
* @param {?} schema
* @param {?} ui
* @return {?}
*/
function resolveIf(schema, ui) {
if (!(schema.hasOwnProperty('if') && schema.hasOwnProperty('then')))
return null;
if (!(/** @type {?} */ (schema.if)).properties)
throw new Error("if: does not contain 'properties'");
/** @type {?} */
var allKeys = Object.keys((/** @type {?} */ (schema.properties)));
/** @type {?} */
var ifKeys = Object.keys((/** @type {?} */ ((/** @type {?} */ (schema.if)).properties)));
detectKey(allKeys, ifKeys);
detectKey(allKeys, (/** @type {?} */ ((/** @type {?} */ (schema.then)).required)));
schema.required = (/** @type {?} */ (schema.required)).concat((/** @type {?} */ ((/** @type {?} */ (schema.then)).required)));
/** @type {?} */
var hasElse = schema.hasOwnProperty('else');
if (hasElse) {
detectKey(allKeys, (/** @type {?} */ ((/** @type {?} */ (schema.else)).required)));
schema.required = schema.required.concat((/** @type {?} */ ((/** @type {?} */ (schema.else)).required)));
}
/** @type {?} */
var visibleIf = {};
/** @type {?} */
var visibleElse = {};
ifKeys.forEach((/**
* @param {?} key
* @return {?}
*/
function (key) {
/** @type {?} */
var cond = (/** @type {?} */ ((/** @type {?} */ (schema.if)).properties))[key].enum;
visibleIf[key] = cond;
if (hasElse)
visibleElse[key] = (/**
* @param {?} value
* @return {?}
*/
function (value) { return !(/** @type {?} */ (cond)).includes(value); });
}));
(/** @type {?} */ ((/** @type {?} */ (schema.then)).required)).forEach((/**
* @param {?} key
* @return {?}
*/
function (key) { return (ui["$" + key].visibleIf = visibleIf); }));
if (hasElse)
(/** @type {?} */ ((/** @type {?} */ (schema.else)).required)).forEach((/**
* @param {?} key
* @return {?}
*/
function (key) { return (ui["$" + key].visibleIf = visibleElse); }));
return schema;
}
/**
* @param {?} keys
* @param {?} detectKeys
* @return {?}
*/
function detectKey(keys, detectKeys) {
detectKeys.forEach((/**
* @param {?} key
* @return {?}
*/
function (key) {
if (!keys.includes(key)) {
throw new Error("if: properties does not contain '" + key + "'");
}
}));
}
/**
* @param {?} properties
* @param {?} order
* @return {?}
*/
function orderProperties(properties, order) {
if (!Array.isArray(order))
return properties;
/** @type {?} */
var arrayToHash = (/**
* @param {?} arr
* @return {?}
*/
function (arr) {
return arr.reduce((/**
* @param {?} prev
* @param {?} curr
* @return {?}
*/
function (prev, curr) {
prev[curr] = true;
return prev;
}), {});
});
/** @type {?} */
var errorPropList = (/**
* @param {?} arr
* @return {?}
*/
function (arr) { return "property [" + arr.join("', '") + "]"; });
/** @type {?} */
var propertyHash = arrayToHash(properties);
/** @type {?} */
var orderHash = arrayToHash(order);
/** @type {?} */
var extraneous = order.filter((/**
* @param {?} prop
* @return {?}
*/
function (prop) { return prop !== '*' && !propertyHash[prop]; }));
if (extraneous.length) {
throw new Error("ui schema order list contains extraneous " + errorPropList(extraneous));
}
/** @type {?} */
var rest = properties.filter((/**
* @param {?} prop
* @return {?}
*/
function (prop) { return !orderHash[prop]; }));
/** @type {?} */
var restIndex = order.indexOf('*');
if (restIndex === -1) {
if (rest.length) {
throw new Error("ui schema order list does not contain " + errorPropList(rest));
}
return order;
}
if (restIndex !== order.lastIndexOf('*')) {
throw new Error('ui schema order list contains more than one wildcard item');
}
/** @type {?} */
var complete = __spread(order);
complete.splice.apply(complete, __spread([restIndex, 1], rest));
return complete;
}
/**
* @param {?} list
* @param {?} formData
* @param {?} readOnly
* @return {?}
*/
function getEnum(list, formData, readOnly) {
if (isBlank(list) || !Array.isArray(list) || list.length === 0)
return [];
if (typeof list[0] !== 'object') {
list = list.map((/**
* @param {?} item
* @return {?}
*/
function (item) {
return (/** @type {?} */ ({ label: item, value: item }));
}));
}
if (formData) {
if (!Array.isArray(formData))
formData = [formData];
list.forEach((/**
* @param {?} item
* @return {?}
*/
function (item) {
if (~formData.indexOf(item.value))
item.checked = true;
}));
}
// fix disabled status
if (readOnly) {
list.forEach((/**
* @param {?} item
* @return {?}
*/
function (item) { return (item.disabled = true); }));
}
return list;
}
/**
* @param {?} list
* @param {?} formData
* @param {?} readOnly
* @return {?}
*/
function getCopyEnum(list, formData, readOnly) {
return getEnum(deepCopy(list || []), formData, readOnly);
}
/**
* @param {?} schema
* @param {?} ui
* @param {?} formData
* @param {?=} asyncArgs
* @return {?}
*/
function getData(schema, ui, formData, asyncArgs) {
if (typeof ui.asyncData === 'function') {
return ui.asyncData(asyncArgs).pipe(map((/**
* @param {?} list
* @return {?}
*/
function (list) { return getEnum(list, formData, (/** @type {?} */ (schema.readOnly))); })));
}
return of(getCopyEnum((/** @type {?} */ (schema.enum)), formData, (/** @type {?} */ (schema.readOnly))));
}
/**
* Whether to using date-fns to format a date
* @param {?} srv
* @return {?}
*/
function isDateFns(srv) {
if (!srv)
return false;
/** @type {?} */
var data = srv.getDateLocale();
// Compatible date-fns v1.x & v2.x
return data != null && !!data.formatDistance; // (!!data.distanceInWords || !!data.formatDistance);
}
/**
* @fileoverview added by tsickle
* Generated from: src/model/form.property.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @abstract
*/
var /**
* @abstract
*/
FormProperty = /** @class */ (function () {
function FormProperty(schemaValidatorFactory, schema, ui, formData, parent, path, _options) {
this._options = _options;
this._errors = null;
this._valueChanges = new BehaviorSubject(null);
this._errorsChanges = new BehaviorSubject(null);
this._visible = true;
this._visibilityChanges = new BehaviorSubject(true);
this._objErrors = {};
this._value = null;
this.schema = schema;
this.ui = ui;
this.schemaValidator = schemaValidatorFactory.createValidatorFn(schema, {
ingoreKeywords: (/** @type {?} */ (this.ui.ingoreKeywords)),
debug: (/** @type {?} */ ((/** @type {?} */ (((/** @type {?} */ (ui))))).debug)),
});
this.formData = formData || schema.default;
this._parent = parent;
if (parent) {
this._root = parent.root;
}
else {
this._root = (/** @type {?} */ (this));
}
this.path = path;
}
Object.defineProperty(FormProperty.prototype, "valueChanges", {
get: /**
* @return {?}
*/
function () {
return this._valueChanges;
},
enumerable: true,
configurable: true
});
Object.defineProperty(FormProperty.prototype, "errorsChanges", {
get: /**
* @return {?}
*/
function () {
return this._errorsChanges;
},
enumerable: true,
configurable: true
});
Object.defineProperty(FormProperty.prototype, "type", {
get: /**
* @return {?}
*/
function () {
return (/** @type {?} */ (this.schema.type));
},
enumerable: true,
configurable: true
});
Object.defineProperty(FormProperty.prototype, "parent", {
get: /**
* @return {?}
*/
function () {
return this._parent;
},
enumerable: true,
configurable: true
});
Object.defineProperty(FormProperty.prototype, "root", {
get: /**
* @return {?}
*/
function () {
return this._root;
},
enumerable: true,
configurable: true
});
Object.defineProperty(FormProperty.prototype, "value", {
get: /**
* @return {?}
*/
function () {
return this._value;
},
enumerable: true,
configurable: true
});
Object.defineProperty(FormProperty.prototype, "errors", {
get: /**
* @return {?}
*/
function () {
return this._errors;
},
enumerable: true,
configurable: true
});
Object.defineProperty(FormProperty.prototype, "visible", {
get: /**
* @return {?}
*/
function () {
return this._visible;
},
enumerable: true,
configurable: true
});
Object.defineProperty(FormProperty.prototype, "valid", {
get: /**
* @return {?}
*/
function () {
return this._errors === null || this._errors.length === 0;
},
enumerable: true,
configurable: true
});
Object.defineProperty(FormProperty.prototype, "options", {
get: /**
* @return {?}
*/
function () {
return this._options;
},
enumerable: true,
configurable: true
});
/**
* 更新值且校验数据
*
* @param [onlySelf=false] 是否包含上级字段
* @param [emitValueEvent=true] 是否触发值变更通知
*/
/**
* 更新值且校验数据
*
* @param {?=} onlySelf
* @param {?=} emitValueEvent
* @param {?=} emitValidator
* @return {?}
*/
FormProperty.prototype.updateValueAndValidity = /**
* 更新值且校验数据
*
* @param {?=} onlySelf
* @param {?=} emitValueEvent
* @param {?=} emitValidator
* @return {?}
*/
function (onlySelf, emitValueEvent, emitValidator) {
if (onlySelf === void 0) { onlySelf = false; }
if (emitValueEvent === void 0) { emitValueEvent = true; }
if (emitValidator === void 0) { emitValidator = true; }
this._updateValue();
if (emitValueEvent) {
this.valueChanges.next(this.value);
}
// `emitValidator` 每一次数据变更已经包含完整错误链路,后续父节点数据变更无须再触发校验
if (emitValidator && this.ui.liveValidate === true) {
this._runValidation();
}
if (this.parent && !onlySelf) {
this.parent.updateValueAndValidity(onlySelf, emitValueEvent, false);
}
};
/** 根据路径搜索表单属性 */
/**
* 根据路径搜索表单属性
* @param {?} path
* @return {?}
*/
FormProperty.prototype.searchProperty = /**
* 根据路径搜索表单属性
* @param {?} path
* @return {?}
*/
function (path) {
/** @type {?} */
var prop = this;
/** @type {?} */
var base = null;
/** @type {?} */
var result = null;
if (path[0] === SF_SEQ) {
base = this.findRoot();
result = base.getProperty(path.substr(1));
}
else {
while (result === null && prop.parent !== null) {
prop = base = prop.parent;
result = base.getProperty(path);
}
}
return (/** @type {?} */ (result));
};
/** 查找根表单属性 */
/**
* 查找根表单属性
* @return {?}
*/
FormProperty.prototype.findRoot = /**
* 查找根表单属性
* @return {?}
*/
function () {
/** @type {?} */
var property = this;
while (property.parent !== null) {
property = property.parent;
}
return (/** @type {?} */ (property));
};
// #region process errors
// #region process errors
/**
* @private
* @param {?} value
* @return {?}
*/
FormProperty.prototype.isEmptyData =
// #region process errors
/**
* @private
* @param {?} value
* @return {?}
*/
function (value) {
if (isBlank(value))
return true;
switch (this.type) {
case 'string':
return ('' + value).length === 0;
}
return false;
};
/**
* @internal
*/
/**
* \@internal
* @return {?}
*/
FormProperty.prototype._runValidation = /**
* \@internal
* @return {?}
*/
function () {
var _this = this;
/** @type {?} */
var errors;
// The definition of some rules:
// 1. Should not ajv validator when is empty data and required fields
// 2. Should not ajv validator when is empty data
/** @type {?} */
var isEmpty = this.isEmptyData(this._value);
if (isEmpty && this.ui._required) {
errors = [{ keyword: 'required' }];
}
else if (isEmpty) {
errors = [];
}
else {
errors = this.schemaValidator(this._value) || [];
}
/** @type {?} */
var customValidator = ((/** @type {?} */ (this.ui))).validator;
if (typeof customValidator === 'function') {
/** @type {?} */
var customErrors = customValidator(this.value, this, this.findRoot());
if (customErrors instanceof Observable) {
customErrors.subscribe((/**
* @param {?} res
* @return {?}
*/
function (res) {
_this.setCustomErrors(errors, res);
_this.widget.detectChanges();
}));
return;
}
this.setCustomErrors(errors, customErrors);
return;
}
this._errors = errors;
this.setErrors(this._errors);
};
/**
* @private
* @param {?} errors
* @param {?} list
* @return {?}
*/
FormProperty.prototype.setCustomErrors = /**
* @private
* @param {?} errors
* @param {?} list
* @return {?}
*/
function (errors, list) {
// fix error format
/** @type {?} */
var hasCustomError = list != null && list.length > 0;
if (hasCustomError) {
list.forEach((/**
* @param {?} err
* @return {?}
*/
function (err) {
if (!err.message) {
throw new Error("The custom validator must contain a 'message' attribute to viewed error text");
}
err._custom = true;
}));
}
this._errors = this.mergeErrors(errors, list);
this.setErrors(this._errors);
};
/**
* @private
* @param {?} errors
* @param {?} newErrors
* @return {?}
*/
FormProperty.prototype.mergeErrors = /**
* @private
* @param {?} errors
* @param {?} newErrors
* @return {?}
*/
function (errors, newErrors) {
if (newErrors) {
if (Array.isArray(newErrors)) {
errors = errors.concat.apply(errors, __spread(newErrors));
}
else {
errors.push(newErrors);
}
}
return errors;
};
/**
* @protected
* @param {?} errors
* @param {?=} emitFormat
* @return {?}
*/
FormProperty.prototype.setErrors = /**
* @protected
* @param {?} errors
* @param {?=} emitFormat
* @return {?}
*/
function (errors, emitFormat) {
var _this = this;
if (emitFormat === void 0) { emitFormat = true; }
if (emitFormat && errors && !this.ui.onlyVisual) {
/** @type {?} */
var l_1 = (this.widget && this.widget.l.error) || {};
errors = errors.map((/**
* @param {?} err
* @return {?}
*/
function (err) {
/** @type {?} */
var message = err._custom === true && err.message
? err.message
: (_this.ui.errors || {})[err.keyword] || (/** @type {?} */ (_this._options.errors))[err.keyword] || l_1[err.keyword] || "";
if (message && typeof message === 'function') {
message = (/** @type {?} */ (message(err)));
}
if (message) {
if (~((/** @type {?} */ (message))).indexOf('{')) {
message = ((/** @type {?} */ (message))).replace(/{([\.a-z0-9]+)}/g, (/**
* @param {?} _v
* @param {?} key
* @return {?}
*/
function (_v, key) { return (/** @type {?} */ (err.params))[key] || ''; }));
}
err.message = (/** @type {?} */ (message));
}
return err;
}));
}
this._errors = errors;
this._errorsChanges.next(errors);
// Should send errors to parent field
if (this._parent) {
this._parent.setParentAndPlatErrors(errors, this.path);
}
};
/**
* @param {?} errors
* @param {?} path
* @return {?}
*/
FormProperty.prototype.setParentAndPlatErrors = /**
* @param {?} errors
* @param {?} path
* @return {?}
*/
function (errors, path) {
var _this = this;
this._objErrors[path] = errors;
/** @type {?} */
var platErrors = [];
Object.keys(this._objErrors).forEach((/**
* @param {?} p
* @return {?}
*/
function (p) {
/** @type {?} */
var property = _this.searchProperty(p);
if (property && !property.visible)
return;
platErrors.push.apply(platErrors, __spread(_this._objErrors[p]));
}));
this.setErrors(platErrors, false);
};
// #endregion
// #region condition
// #endregion
// #region condition
/**
* @private
* @param {?} visible
* @return {?}
*/
FormProperty.prototype.setVisible =
// #endregion
// #region condition
/**
* @private
* @param {?} visible
* @return {?}
*/
function (visible) {
this._visible = visible;
this._visibilityChanges.next(visible);
// 部分数据源来自 reset
this.resetValue(this.value, true);
};
// A field is visible if AT LEAST ONE of the properties it depends on is visible AND has a value in the list
// A field is visible if AT LEAST ONE of the properties it depends on is visible AND has a value in the list
/**
* @return {?}
*/
FormProperty.prototype._bindVisibility =
// A field is visible if AT LEAST ONE of the properties it depends on is visible AND has a value in the list
/**
* @return {?}
*/
function () {
var _this = this;
/** @type {?} */
var visibleIf = ((/** @type {?} */ (this.ui))).visibleIf;
if (typeof visibleIf === 'object' && Object.keys(visibleIf).length === 0) {
this.setVisible(false);
}
else if (visibleIf !== undefined) {
/** @type {?} */
var propertiesBinding = [];
var _loop_1 = function (dependencyPath) {
if (visibleIf.hasOwnProperty(dependencyPath)) {
/** @type {?} */
var property = this_1.searchProperty(dependencyPath);
if (property) {
/** @type {?} */
var valueCheck = property.valueChanges.pipe(map((/**
* @param {?} value
* @return {?}
*/
function (value) {
/** @type {?} */
var vi = visibleIf[dependencyPath];
if (typeof vi === 'function')
return vi(value);
if (vi.indexOf('$ANY$') !== -1) {
return value.length > 0;
}
else {
return vi.indexOf(value) !== -1;
}
})));
/** @type {?} */
var visibilityCheck = property._visibilityChanges;
/** @type {?} */
var and = combineLatest([valueCheck, visibilityCheck]).pipe(map((/**
* @param {?} results
* @return {?}
*/
function (results) { return results[0] && results[1]; })));
propertiesBinding.push(and);
}
else {
console.warn("Can't find property " + dependencyPath + " for visibility check of " + this_1.path);
}
}
};
var this_1 = this;
for (var dependencyPath in visibleIf) {
_loop_1(dependencyPath);
}
combineLatest(propertiesBinding)
.pipe(map((/**
* @param {?} values
* @return {?}
*/
function (values) { return values.indexOf(true) !== -1; })), distinctUntilChanged())
.subscribe((/**
* @param {?} visible
* @return {?}
*/
function (visible) { return _this.setVisible(visible); }));
}
};
return FormProperty;
}());
if (false) {
/**
* @type {?}
* @private
*/
FormProperty.prototype._errors;
/**
* @type {?}
* @private
*/
FormProperty.prototype._valueChanges;
/**
* @type {?}
* @private
*/
FormProperty.prototype._errorsChanges;
/**
* @type {?}
* @private
*/
FormProperty.prototype._visible;
/**
* @type {?}
* @private
*/
FormProperty.prototype._visibilityChanges;
/**
* @type {?}
* @private
*/
FormProperty.prototype._root;
/**
* @type {?}
* @private
*/
FormProperty.prototype._parent;
/** @type {?} */
FormProperty.prototype._objErrors;
/** @type {?} */
FormProperty.prototype.schemaValidator;
/** @type {?} */
FormProperty.prototype.schema;
/** @type {?} */
FormProperty.prototype.ui;
/** @type {?} */
FormProperty.prototype.formData;
/** @type {?} */
FormProperty.prototype._value;
/** @type {?} */
FormProperty.prototype.widget;
/** @type {?} */
FormProperty.prototype.path;
/**
* @type {?}
* @private
*/
FormProperty.prototype._options;
/**
* 设置值
*
* @abstract
* @param {?} value
* @param {?} onlySelf `true` 只对当前字段更新值和校验;`false` 包含上级字段
* @return {?}
*/
FormProperty.prototype.setValue = function (value, onlySelf) { };
/**
* 重置值,默认值为 `schema.default`
*
* @abstract
* @param {?} value
* @param {?} onlySelf `true` 只对当前字段更新值和校验;`false` 包含上级字段
* @return {?}
*/
FormProperty.prototype.resetValue = function (value, onlySelf) { };
/**
* \@internal
* @abstract
* @return {?}
*/
FormProperty.prototype._hasValue = function () { };
/**
* \@internal
* @abstract
* @return {?}
*/
FormProperty.prototype._updateValue = function () { };
}
/**
* @abstract
*/
var /**
* @abstract
*/
PropertyGroup = /** @class */ (function (_super) {
__extends(PropertyGroup, _super);
function PropertyGroup() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.properties = null;
return _this;
}
/**
* @param {?} path
* @return {?}
*/
PropertyGroup.prototype.getProperty = /**
* @param {?} path
* @return {?}
*/
function (path) {
/** @type {?} */
var subPathIdx = path.indexOf(SF_SEQ);
/** @type {?} */
var propertyId = subPathIdx !== -1 ? path.substr(0, subPathIdx) : path;
/** @type {?} */
var property = ((/** @type {?} */ (this.properties)))[propertyId];
if (property !== null && subPathIdx !== -1 && property instanceof PropertyGroup) {
/** @type {?} */
var subPath = path.substr(subPathIdx + 1);
property = (/** @type {?} */ (((/** @type {?} */ (property))).getProperty(subPath)));
}
return property;
};
/**
* @param {?} fn
* @return {?}
*/
PropertyGroup.prototype.forEachChild = /**
* @param {?} fn
* @return {?}
*/
function (fn) {
for (var propertyId in this.properties) {
if (this.properties.hasOwnProperty(propertyId)) {
/** @type {?} */
var property = ((/** @type {?} */ (this.properties)))[propertyId];
fn(property, propertyId);
}
}
};
/**
* @param {?} fn
* @return {?}
*/
PropertyGroup.prototype.forEachChildRecursive = /**
* @param {?} fn
* @return {?}
*/
function (fn) {
this.forEachChild((/**
* @param {?} child
* @return {?}
*/
function (child) {
fn(child);
if (child instanceof PropertyGroup) {
((/** @type {?} */ (child))).forEachChildRecursive(fn);
}
}));
};
/**
* @return {?}
*/
PropertyGroup.prototype._bindVisibility = /**
* @return {?}
*/
function () {
_super.prototype._bindVisibility.call(this);
this._bindVisibilityRecursive();
};
/**
* @private
* @return {?}
*/
PropertyGroup.prototype._bindVisibilityRecursive = /**
* @private
* @return {?}
*/
function () {
this.forEachChildRecursive((/**
* @param {?} property
* @return {?}
*/
function (property) {
property._bindVisibility();
}));
};
/**
* @return {?}
*/
PropertyGroup.prototype.isRoot = /**
* @return {?}
*/
function () {
return this === this.root;
};
return PropertyGroup;
}(FormProperty));
if (false) {
/** @type {?} */
PropertyGroup.prototype.properties;
}
/**
* @fileoverview added by tsickle
* Generated from: src/model/object.property.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
var ObjectProperty = /** @class */ (function (_super) {
__extends(ObjectProperty, _super);
function ObjectProperty(formPropertyFactory, schemaValidatorFactory, schema, ui, formData, parent, path, options) {
var _this = _super.call(this, schemaValidatorFactory, schema, ui, formData, parent, path, options) || this;
_this.formPropertyFactory = formPropertyFactory;
_this._propertiesId = [];
_this.createProperties();
return _this;
}
Object.defineProperty(ObjectProperty.prototype, "propertiesId", {
get: /**
* @return {?}
*/
function () {
return this._propertiesId;
},
enumerable: true,
configurable: true
});
/**
* @private
* @return {?}
*/
ObjectProperty.prototype.createProperties = /**
* @private
* @return {?}
*/
function () {
var _this = this;
this.properties = {};
this._propertiesId = [];
/** @type {?} */
var orderedProperties;
try {
orderedProperties = orderProperties(Object.keys((/** @type {?} */ (this.schema.properties))), (/** @type {?} */ (this.ui.order)));
}
catch (e) {
console.error("Invalid " + (this.schema.title || 'root') + " object field configuration:", e);
}
(/** @type {?} */ (orderedProperties)).forEach((/**
* @param {?} propertyId
* @return {?}
*/
function (propertyId) {
((/** @type {?} */ (_this.properties)))[propertyId] = _this.formPropertyFactory.createProperty((/** @type {?} */ (_this.schema.properties))[propertyId], _this.ui['$' + propertyId], ((/** @type {?} */ ((_this.formData || {}))))[propertyId], _this, propertyId);
_this._propertiesId.push(propertyId);
}));
};
/**
* @param {?} value
* @param {?} onlySelf
* @return {?}
*/
ObjectProperty.prototype.setValue = /**
* @param {?} value
* @param {?} onlySelf
* @return {?}
*/
function (value, onlySelf) {
/** @type {?} */
var properties = (/** @type {?} */ (this.properties));
for (var propertyId in value) {
if (value.hasOwnProperty(propertyId) && properties[propertyId]) {
((/** @type {?} */ (properties[propertyId]))).setValue(value[propertyId], true);
}
}
this.updateValueAndValidity(onlySelf, true);
};
/**
* @param {?} value
* @param {?} onlySelf
* @return {?}
*/
ObjectProperty.prototype.resetValue = /**
* @param {?} value
* @param {?} onlySelf
* @return {?}
*/
function (value, onlySelf) {
value = value || this.schema.default || {};
/** @type {?} */
var properties = (/** @type {?} */ (this.properties));
// tslint:disable-next-line: forin
for (var propertyId in this.schema.properties) {
properties[propertyId].resetValue(value[propertyId], true);
}
this.updateValueAndValidity(onlySelf, true);
};
/**
* @return {?}
*/
ObjectProperty.prototype._hasValue = /**
* @return {?}
*/
function () {
return this.value != null && !!Object.keys(this.value).length;
};
/**
* @return {?}
*/
ObjectProperty.prototype._updateValue = /**
* @return {?}
*/
function () {
/** @type {?} */
var value = {};
this.forEachChild((/**
* @param {?} property
* @param {?} propertyId
* @return {?}
*/
function (property, propertyId) {
if (property.visible && property._hasValue()) {
value[propertyId] = property.value;
}
}));
this._value = value;
};
return ObjectProperty;
}(PropertyGroup));
if (false) {
/**
* @type {?}
* @private
*/
ObjectProperty.prototype._propertiesId;
/**
* @type {?}
* @private
*/
ObjectProperty.prototype.formPropertyFactory;
}
/**
* @fileoverview added by tsickle
* Generated from: src/model/array.property.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
var ArrayProperty = /** @class */ (function (_super) {
__extends(ArrayProperty, _super);
function ArrayProperty(formPropertyFactory, schemaValidatorFactory, schema, ui, formData, parent, path, options) {
var _this = _super.call(this, schemaValidatorFactory, schema, ui, formData, parent, path, options) || this;
_this.formPropertyFactory = formPropertyFactory;
_this.properties = [];
return _this;
}
/**
* @param {?} path
* @return {?}
*/
ArrayProperty.prototype.getProperty = /**
* @param {?} path
* @return {?}
*/
function (path) {
/** @type {?} */
var subPathIdx = path.indexOf(SF_SEQ);
/** @type {?} */
var pos = +(subPathIdx !== -1 ? path.substr(0, subPathIdx) : path);
/** @type {?} */
var list = (/** @type {?} */ (this.properties));
if (isNaN(pos) || pos >= list.length) {
return undefined;
}
/** @type {?} */
var subPath = path.substr(subPathIdx + 1);
return list[pos].getProperty(subPath);
};
/**
* @param {?} value
* @param {?} onlySelf
* @return {?}
*/
ArrayProperty.prototype.setValue = /**
* @param {?} value
* @param {?} onlySelf
* @return {?}
*/
function (value, onlySelf) {
this.properties = [];
this.clearErrors();
this.resetProperties(value);
this.updateValueAndValidity(onlySelf, true);
};
/**
* @param {?} value
* @param {?} onlySelf
* @return {?}
*/
ArrayProperty.prototype.resetValue = /**
* @param {?} value
* @param {?} onlySelf
* @return {?}
*/
function (value, onlySelf) {
this._value = value || this.schema.default || [];
this.setValue(this._value, onlySelf);
};
/**
* @return {?}
*/
ArrayProperty.prototype._hasValue = /**
* @return {?}
*/
function () {
return true;
};
/**
* @return {?}
*/
ArrayProperty.prototype._updateValue = /**
* @return {?}
*/
function () {
/** @type {?} */
var value = [];
this.forEachChild((/**
* @param {?} property
* @return {?}
*/
function (property) {
if (property.visible && property._hasValue()) {
value.push(__assign(__assign({}, property.formData), property.value));
}
}));
this._value = value;
};
/**
* @private
* @param {?} formData
* @return {?}
*/
ArrayProperty.prototype.addProperty = /**
* @private
* @param {?} formData
* @return {?}
*/
function (formData) {
/** @type {?} */
var newProperty = (/** @type {?} */ (this.formPropertyFactory.createProperty((/** @type {?} */ (this.schema.items)), this.ui.$items, formData, (/** @type {?} */ (this)))));
((/** @type {?} */ (this.properties))).push(newProperty);
return newProperty;
};
/**
* @private
* @param {?} formDatas
* @return {?}
*/
ArrayProperty.prototype.resetProperties = /**
* @private
* @param {?} formDatas
* @return {?}
*/
function (formDatas) {
var e_1, _a;
try {
for (var formDatas_1 = __values(formDatas), formDatas_1_1 = formDatas_1.next(); !formDatas_1_1.done; formDatas_1_1 = formDatas_1.next()) {
var item = formDatas_1_1.value;
/** @type {?} */
var property = this.addProperty(item);
property.resetValue(item, true);
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (formDatas_1_1 && !formDatas_1_1.done && (_a = formDatas_1.return)) _a.call(formDatas_1);
}
finally { if (e_1) throw e_1.error; }
}
};
/**
* @private
* @param {?=} property
* @return {?}
*/
ArrayProperty.prototype.clearErrors = /**
* @private
* @param {?=} property
* @return {?}
*/
function (property) {
(property || this)._objErrors = {};
};
// #region actions
// #region actions
/**
* @param {?} formData
* @return {?}
*/
ArrayProperty.prototype.add =
// #region actions
/**
* @param {?} formData
* @return {?}
*/
function (formData) {
/** @type {?} */
var newProperty = this.addProperty(formData);
newProperty.resetValue(formData, false);
return newProperty;
};
/**
* @param {?} index
* @return {?}
*/
ArrayProperty.prototype.remove = /**
* @param {?} index
* @return {?}
*/
function (index) {
var _this = this;
/** @type {?} */
var list = (/** @type {?} */ (this.properties));
this.clearErrors();
list.splice(index, 1);
list.forEach((/**
* @param {?} property
* @param {?} idx
* @return {?}
*/
function (property, idx) {
property.path = [(/** @type {?} */ (property.parent)).path, idx].join(SF_SEQ);
_this.clearErrors(property);
// TODO: 受限于 sf 的设计思路,对于移除数组项需要重新对每个子项进行校验,防止错误被父级合并后引起始终是错误的现象
if (property instanceof ObjectProperty) {
property.forEachChild((/**
* @param {?} p
* @return {?}
*/
function (p) {
p.updateValueAndValidity();
}));
}
}));
};
return ArrayProperty;
}(PropertyGroup));
if (false) {
/**
* @type {?}
* @private
*/
ArrayProperty.prototype.formPropertyFactory;
}
/**
* @fileoverview added by tsickle
* Generated from: src/model/atomic.property.ts
* @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
*/
/**
* @abstract
*/
var /**
* @abstract
*/
AtomicProperty = /** @class */ (function (_super) {
__extends(AtomicProperty, _super);
function AtomicProperty() {
return _super !== null && _super.apply(this, arguments) || this;
}
/**
* @param {?} value
* @param {?} onlySelf
* @return {?}
*/
AtomicProperty.prototype.setValue = /**
* @param {?} value
* @param {?} onlySelf
* @return {?}
*/
function (value, onlySelf) {
this._value = value;
this.updateValueAndValidity(onlySelf, true);
};
/**
* @param {?} value
* @param {?} onlySelf
* @return {?}
*/
AtomicProperty.prototype.resetValue = /**
* @param {?} value
* @param {?} onlySelf
* @return {?}
*/
function (value, onlySelf) {
if (value == null) {
value = this.schema.default !== undefined ? this.schema.default : this.fallbackValue();
}
this._value = value;
this.updateValueAndValidity(onlySelf, true);
if (this.widget)
this.widget.reset(value);
};
/**
* @return {?}
*/
AtomicProperty.prototype._hasValue = /**
* @return {?}
*/
function () {
return this.fallbackValue() !== this.value;
};
/**
* @return {?}
*/
AtomicProperty.prototype._updateValue = /**
* @return {?}
*/
function () { };
return AtomicProperty;
}(FormProperty));
if (false) {
/**
* @abstract
* @return {?}
*/
AtomicProperty.prototype.fallbackValue = function () { };
}
/**
* @fileoverview added by tsickle
* Generated from: src/model/boolean.property.ts
* @suppress {checkTypes,constantProperty,extraRequire,missing