@caxa-form/core
Version:
vue动态表单,助你轻松搞定表单|form-create is a form generation component that can generate dynamic rendering, data collection, verification and submission functions through JSON. Supports 3 UI frameworks, and supports the generation of any Vue components. Built-in 20
1,865 lines (1,651 loc) • 90.1 kB
JavaScript
/*!
* @caxa-form/core v2.5.15
* (c) 2018-2021 wangjing11260
* Github https://github.com/wangjing0630/caxa-form
* Released under the MIT License.
*/
import Vue from 'vue';
function ownKeys(object, enumerableOnly) {
var keys = Object.keys(object);
if (Object.getOwnPropertySymbols) {
var symbols = Object.getOwnPropertySymbols(object);
if (enumerableOnly) {
symbols = symbols.filter(function (sym) {
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
});
}
keys.push.apply(keys, symbols);
}
return keys;
}
function _objectSpread2(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i] != null ? arguments[i] : {};
if (i % 2) {
ownKeys(Object(source), true).forEach(function (key) {
_defineProperty(target, key, source[key]);
});
} else if (Object.getOwnPropertyDescriptors) {
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
} else {
ownKeys(Object(source)).forEach(function (key) {
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
});
}
}
return target;
}
function _typeof(obj) {
"@babel/helpers - typeof";
if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
_typeof = function (obj) {
return typeof obj;
};
} else {
_typeof = function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
}
return _typeof(obj);
}
function _classCallCheck(instance, Constructor) {
if (!(instance instanceof Constructor)) {
throw new TypeError("Cannot call a class as a function");
}
}
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function _inherits(subClass, superClass) {
if (typeof superClass !== "function" && superClass !== null) {
throw new TypeError("Super expression must either be null or a function");
}
subClass.prototype = Object.create(superClass && superClass.prototype, {
constructor: {
value: subClass,
writable: true,
configurable: true
}
});
if (superClass) _setPrototypeOf(subClass, superClass);
}
function _getPrototypeOf(o) {
_getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
return o.__proto__ || Object.getPrototypeOf(o);
};
return _getPrototypeOf(o);
}
function _setPrototypeOf(o, p) {
_setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
o.__proto__ = p;
return o;
};
return _setPrototypeOf(o, p);
}
function _isNativeReflectConstruct() {
if (typeof Reflect === "undefined" || !Reflect.construct) return false;
if (Reflect.construct.sham) return false;
if (typeof Proxy === "function") return true;
try {
Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {}));
return true;
} catch (e) {
return false;
}
}
function _assertThisInitialized(self) {
if (self === void 0) {
throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
}
return self;
}
function _possibleConstructorReturn(self, call) {
if (call && (typeof call === "object" || typeof call === "function")) {
return call;
}
return _assertThisInitialized(self);
}
function _createSuper(Derived) {
var hasNativeReflectConstruct = _isNativeReflectConstruct();
return function _createSuperInternal() {
var Super = _getPrototypeOf(Derived),
result;
if (hasNativeReflectConstruct) {
var NewTarget = _getPrototypeOf(this).constructor;
result = Reflect.construct(Super, arguments, NewTarget);
} else {
result = Super.apply(this, arguments);
}
return _possibleConstructorReturn(this, result);
};
}
function _toConsumableArray(arr) {
return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
}
function _arrayWithoutHoles(arr) {
if (Array.isArray(arr)) return _arrayLikeToArray(arr);
}
function _iterableToArray(iter) {
if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter);
}
function _unsupportedIterableToArray(o, minLen) {
if (!o) return;
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
var n = Object.prototype.toString.call(o).slice(8, -1);
if (n === "Object" && o.constructor) n = o.constructor.name;
if (n === "Map" || n === "Set") return Array.from(o);
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
}
function _arrayLikeToArray(arr, len) {
if (len == null || len > arr.length) len = arr.length;
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
return arr2;
}
function _nonIterableSpread() {
throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
function $set(target, field, value) {
Vue.set(target, field, value);
}
function $del(target, field) {
Vue["delete"](target, field);
}
var _extends = Object.assign || function (a) {
for (var b, c = 1; c < arguments.length; c++) {
for (var d in b = arguments[c], b) {
Object.prototype.hasOwnProperty.call(b, d) && $set(a, d, b[d]);
}
}
return a;
};
function extend() {
return _extends.apply(this, arguments);
}
var NAME = 'FormCreate';
function $FormCreate(FormCreate) {
return {
name: NAME,
componentName: NAME,
model: {
prop: 'api'
},
provide: function provide() {
return {
$pfc: this
};
},
inject: {
$pfc: {
"default": null
}
},
props: {
rule: {
type: Array,
required: true
},
option: {
type: Object,
"default": function _default() {
return {};
}
},
extendOption: Boolean,
value: Object,
api: Object
},
data: function data() {
return {
formData: undefined,
destroyed: false,
validate: {},
$f: undefined,
isShow: true,
unique: 1,
renderRule: _toConsumableArray(this.rule || []),
updateValue: ''
};
},
render: function render() {
return this.formCreate.render();
},
methods: {
_refresh: function _refresh() {
++this.unique;
},
_renderRule: function _renderRule() {
this.renderRule = _toConsumableArray(this.rule || []);
},
_updateValue: function _updateValue(value) {
if (this.destroyed) return;
this.updateValue = JSON.stringify(value);
this.$emit('update:value', value);
}
},
watch: {
value: {
handler: function handler(n) {
if (JSON.stringify(n) === this.updateValue) return;
this.$f.setValue(n);
},
deep: true
},
option: {
handler: function handler(n) {
this.formCreate.initOptions(n);
this.$f.refresh();
},
deep: true
},
rule: function rule(n) {
var _this = this;
if (n.length === this.renderRule.length && n.every(function (v) {
return _this.renderRule.indexOf(v) > -1;
})) return;
this.formCreate.$handle.reloadRule(n);
this._renderRule();
}
},
beforeCreate: function beforeCreate() {
var _this2 = this;
var _this$$options$propsD = this.$options.propsData,
rule = _this$$options$propsD.rule,
option = _this$$options$propsD.option,
value = _this$$options$propsD.value;
this.formCreate = new FormCreate(this, rule, option);
extend(this.formCreate.options.formData, value || {});
Object.keys(this.formCreate.prop).forEach(function (k) {
extend(_this2.$options[k], _this2.formCreate.prop[k]);
});
}
};
}
var is = {
type: function type(arg, _type) {
return Object.prototype.toString.call(arg) === '[object ' + _type + ']';
},
Undef: function Undef(v) {
return v === undefined || v === null;
},
Element: function Element(arg) {
return _typeof(arg) === 'object' && arg !== null && arg.nodeType === 1 && !is.Object(arg);
},
trueArray: function trueArray(data) {
return Array.isArray(data) && data.length > 0;
}
};
['Date', 'Object', 'Function', 'String', 'Boolean', ' Array', 'Number'].forEach(function (t) {
is[t] = function (arg) {
return is.type(arg, t);
};
});
function hasProperty(rule, k) {
return {}.hasOwnProperty.call(rule, k);
}
var normalMerge = ['attrs', 'props', 'domProps', 'scopedSlots'];
var toArrayMerge = ['class', 'style', 'directives'];
var functionalMerge = ['on', 'nativeOn'];
var mergeProps = function mergeProps(objects) {
var initial = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var opt = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
var _normalMerge = [].concat(normalMerge, _toConsumableArray(opt['normal'] || []));
var _toArrayMerge = [].concat(toArrayMerge, _toConsumableArray(opt['array'] || []));
var _functionalMerge = [].concat(functionalMerge, _toConsumableArray(opt['functional'] || []));
var propsMerge = opt['props'] || [];
return objects.reduce(function (a, b) {
for (var key in b) {
if (a[key]) {
if (propsMerge.indexOf(key) > -1) {
a[key] = mergeProps([b[key]], a[key]);
} else if (_normalMerge.indexOf(key) > -1) {
a[key] = _objectSpread2(_objectSpread2({}, a[key]), b[key]);
} else if (_toArrayMerge.indexOf(key) > -1) {
var arrA = a[key] instanceof Array ? a[key] : [a[key]];
var arrB = b[key] instanceof Array ? b[key] : [b[key]];
a[key] = [].concat(_toConsumableArray(arrA), _toConsumableArray(arrB));
} else if (_functionalMerge.indexOf(key) > -1) {
for (var event in b[key]) {
if (a[key][event]) {
var _arrA = a[key][event] instanceof Array ? a[key][event] : [a[key][event]];
var _arrB = b[key][event] instanceof Array ? b[key][event] : [b[key][event]];
a[key][event] = [].concat(_toConsumableArray(_arrA), _toConsumableArray(_arrB));
} else {
a[key][event] = b[key][event];
}
}
} else if (key === 'hook') {
for (var hook in b[key]) {
if (a[key][hook]) {
a[key][hook] = mergeFn(a[key][hook], b[key][hook]);
} else {
a[key][hook] = b[key][hook];
}
}
} else {
a[key] = b[key];
}
} else {
if (_normalMerge.indexOf(key) > -1 || _functionalMerge.indexOf(key) > -1 || propsMerge.indexOf(key) > -1) {
a[key] = _objectSpread2({}, b[key]);
} else if (_toArrayMerge.indexOf(key) > -1) {
a[key] = b[key] instanceof Array ? _toConsumableArray(b[key]) : _typeof(b[key]) === 'object' ? _objectSpread2({}, b[key]) : b[key];
} else a[key] = b[key];
}
}
return a;
}, initial);
};
var mergeFn = function mergeFn(fn1, fn2) {
return function () {
fn1 && fn1.apply(this, arguments);
fn2 && fn2.apply(this, arguments);
};
};
var keyAttrs = ['type', 'slot', 'emitPrefix', 'value', 'name', 'native', 'hidden', 'display', 'inject', 'options', 'emit', 'nativeEmit', 'link', 'prefix', 'suffix', 'update', 'sync', 'optionsTo'];
var arrayAttrs = ['validate', 'children', 'control'];
var normalAttrs = ['effect'];
function attrs() {
return [].concat(keyAttrs, _toConsumableArray(normalMerge), _toConsumableArray(toArrayMerge), _toConsumableArray(functionalMerge), arrayAttrs, normalAttrs);
}
function deepExtend(origin) {
var target = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var mode = arguments.length > 2 ? arguments[2] : undefined;
var isArr = false;
for (var key in target) {
if (Object.prototype.hasOwnProperty.call(target, key)) {
var clone = target[key];
if ((isArr = Array.isArray(clone)) || is.Object(clone)) {
var nst = origin[key] === undefined;
if (isArr) {
isArr = false;
nst && $set(origin, key, []);
} else if (clone._clone && mode !== undefined) {
if (mode) {
clone = clone.getRule();
nst && $set(origin, key, {});
} else {
$set(origin, key, clone._clone());
continue;
}
} else {
nst && $set(origin, key, {});
}
origin[key] = deepExtend(origin[key], clone, mode);
} else {
$set(origin, key, clone);
}
}
}
return mode !== undefined && Array.isArray(origin) ? origin.filter(function (v) {
return !v || !v.__ctrl;
}) : origin;
}
function deepCopy(value) {
return deepExtend({}, {
value: value
}).value;
}
function format(type, msg, rule) {
return "[form-create ".concat(type, "]: ").concat(msg) + (rule ? '\n\nrule: ' + JSON.stringify(rule.getRule ? rule.getRule() : rule) : '');
}
function tip(msg, rule) {
console.warn(format('tip', msg, rule));
}
function err(msg, rule) {
console.error(format('err', msg, rule));
}
function logError(e) {
err(e.toString());
console.error(e);
}
var PREFIX = '[[FORM-CREATE-PREFIX-';
var SUFFIX = '-FORM-CREATE-SUFFIX]]';
var $T = '$FN:';
var FUNCTION = 'function';
function toJson(obj, space) {
return JSON.stringify(deepExtend([], obj, true), function (key, val) {
if (val && val._isVue === true) return undefined;
if (_typeof(val) !== FUNCTION) {
return val;
}
if (val.__inject) val = val.__origin;
if (val.__emit) return undefined;
return PREFIX + val + SUFFIX;
}, space);
}
function makeFn(fn) {
return eval('(' + FUNCTION + '(){return ' + fn + ' })()');
}
function parseFn(fn, mode) {
if (fn && is.String(fn)) {
var v = fn.trim();
var flag = false;
if (v.indexOf(SUFFIX) > 0 && v.indexOf(PREFIX) === 0) {
v = v.replace(SUFFIX, '').replace(PREFIX, '');
flag = true;
} else if (v.indexOf($T) === 0) {
v = v.replace($T, '');
flag = true;
} else if (!mode && v.indexOf(FUNCTION) === 0 && v !== FUNCTION) {
flag = true;
}
if (!flag) return fn;
try {
return makeFn(v.indexOf(FUNCTION) === -1 && v.indexOf('(') !== 0 ? FUNCTION + ' ' + v : v);
} catch (e) {
err("\u89E3\u6790\u5931\u8D25:".concat(v));
return undefined;
}
}
return fn;
}
function parseJson(json, mode) {
return JSON.parse(json, function (k, v) {
if (is.Undef(v) || !v.indexOf) return v;
return parseFn(v, mode);
});
}
function enumerable(value, writable) {
return {
value: value,
enumerable: false,
configurable: false,
writable: !!writable
};
} //todo 优化位置
function copyRule(rule, mode) {
return copyRules([rule], mode || false)[0];
}
function copyRules(rules, mode) {
return deepExtend([], _toConsumableArray(rules), mode || false);
}
function mergeRule(rule, merge) {
mergeProps(Array.isArray(merge) ? merge : [merge], rule, {
array: arrayAttrs,
normal: normalAttrs
});
return rule;
}
function getRule(rule) {
return is.Function(rule.getRule) ? rule.getRule() : rule;
}
function mergeGlobal(target, merge) {
if (!target) return merge;
Object.keys(merge || {}).forEach(function (k) {
if (merge[k]) {
target[k] = mergeRule(target[k] || {}, merge[k]);
}
});
return target;
}
function funcProxy(that, proxy) {
Object.defineProperties(that, Object.keys(proxy).reduce(function (initial, k) {
initial[k] = {
get: function get() {
return proxy[k]();
}
};
return initial;
}, {}));
}
function byCtx(rule) {
return rule.__fc__ || (rule.__origin__ ? rule.__origin__.__fc__ : null);
}
function invoke(fn, def) {
try {
def = fn();
} catch (e) {
logError(e);
}
return def;
}
function toArray(value) {
return Array.isArray(value) ? value : [null, undefined, ''].indexOf(value) > -1 ? [] : [value];
}
function toString(val) {
return val == null ? '' : _typeof(val) === 'object' ? JSON.stringify(val, null, 2) : String(val);
}
function toLine(name) {
var line = name.replace(/([A-Z])/g, '-$1').toLocaleLowerCase();
if (line.indexOf('-') === 0) line = line.substr(1);
return line;
}
var id = 0;
function uniqueId() {
return Math.random().toString(36).substr(3, 3) + Number("".concat(Date.now()).concat(++id)).toString(36);
}
function deepSet(data, idx, val) {
var _data = data,
to;
(idx || '').split('.').forEach(function (v) {
if (to) {
_data = _data[to] || {};
}
to = v;
});
_data[to] = val;
return _data;
}
function baseRule() {
return {
props: {},
on: {},
options: [],
children: [],
hidden: false,
display: true,
value: undefined
};
}
function creatorFactory(name, init) {
return function (title, field, value) {
var props = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
var maker = new Creator(name, title, field, value, props);
if (init) {
if (is.Function(init)) init(maker);else maker.props(init);
}
return maker;
};
}
function Creator(type, title, field, value, props) {
this._data = extend(baseRule(), {
type: type,
title: title,
field: field,
value: value,
props: props || {}
});
this.event = this.on;
}
extend(Creator.prototype, {
getRule: function getRule() {
return this._data;
},
setProp: function setProp(key, value) {
$set(this._data, key, value);
return this;
},
_clone: function _clone() {
var clone = new this.constructor();
clone._data = copyRule(this._data);
return clone;
}
});
function appendProto(attrs) {
attrs.forEach(function (name) {
Creator.prototype[name] = function (key) {
mergeRule(this._data, _defineProperty({}, name, arguments.length < 2 ? key : _defineProperty({}, key, arguments[1])));
return this;
};
});
}
appendProto(attrs());
var commonMaker = creatorFactory('');
function create(type, field, title) {
var make = commonMaker('', field);
make._data.type = type;
make._data.title = title;
return make;
}
function createTmp(template, vm, field, title) {
var make = commonMaker('', field);
make._data.type = 'template';
make._data.template = template;
make._data.title = title;
make._data.vm = vm;
return make;
}
function makerFactory() {
return {
create: create,
createTmp: createTmp,
template: createTmp,
factory: creatorFactory
};
}
function copy(value) {
return deepCopy(value);
}
function byRules(ctxs, origin) {
return Object.keys(ctxs).reduce(function (initial, key) {
initial[key] = origin ? ctxs[key].origin : ctxs[key].rule;
return initial;
}, {});
}
function Api(h) {
function tidyFields(fields) {
if (is.Undef(fields)) fields = h.fields();else if (!Array.isArray(fields)) fields = [fields];
return fields;
}
function props(fields, key, val) {
tidyFields(fields).forEach(function (field) {
var ctx = h.getCtx(field);
if (!ctx) return;
$set(ctx.rule, key, val);
h.$render.clearCache(ctx);
});
}
function allSubForm() {
var subs = h.subForm;
return Object.keys(subs).reduce(function (initial, k) {
var sub = subs[k];
if (!sub) return initial;
if (Array.isArray(sub)) initial.push.apply(initial, _toConsumableArray(sub));else initial.push(sub);
return initial;
}, []);
}
var api = {
helper: {
tidyFields: tidyFields,
props: props
},
get config() {
return h.options;
},
get options() {
return h.options;
},
get form() {
return h.form;
},
get rule() {
return h.rules;
},
get parent() {
return h.vm.$pfc && h.vm.$pfc.$f;
},
get children() {
return allSubForm();
},
formData: function formData(fields) {
return tidyFields(fields).reduce(function (initial, id) {
var ctx = h.fieldCtx[id];
if (!ctx) return initial;
initial[ctx.field] = copy(ctx.rule.value);
return initial;
}, {});
},
getValue: function getValue(field) {
var ctx = h.fieldCtx[field];
if (!ctx) return;
return copy(ctx.rule.value);
},
coverValue: function coverValue(formData) {
h.deferSyncValue(function () {
Object.keys(h.fieldCtx).forEach(function (key) {
var ctx = h.fieldCtx[key];
if (!ctx) return h.appendData[key] = formData[key];
ctx.rule.value = hasProperty(formData, key) ? formData[key] : undefined;
});
});
},
setValue: function setValue(field) {
var formData = field;
if (arguments.length >= 2) formData = _defineProperty({}, field, arguments[1]);
h.deferSyncValue(function () {
Object.keys(formData).forEach(function (key) {
var ctx = h.fieldCtx[key];
if (!ctx) return h.appendData[key] = formData[key];
ctx.rule.value = formData[key];
});
});
},
removeField: function removeField(field) {
var ctx = h.getCtx(field);
if (!ctx) return;
ctx.rm();
return ctx.origin;
},
removeRule: function removeRule(rule) {
var ctx = rule && byCtx(rule);
if (!ctx) return;
ctx.rm();
return ctx.origin;
},
destroy: function destroy() {
h.vm.$el.parentNode && h.vm.$el.parentNode.removeChild(h.vm.$el);
h.vm.$destroy();
},
fields: function fields() {
return h.fields();
},
append: function append(rule, after, child) {
var fields = Object.keys(h.fieldCtx),
index = h.sort.length - 1,
rules;
if (rule.field && fields.indexOf(rule.field) > -1) return err("".concat(rule.field, " \u5B57\u6BB5\u5DF2\u5B58\u5728"), rule);
var ctx = h.getCtx(after);
if (ctx) {
if (child) {
rules = ctx.rule.children;
index = ctx.rule.children.length - 1;
} else {
index = ctx.root.indexOf(ctx.origin);
rules = ctx.root;
}
} else rules = h.rules;
rules.splice(index + 1, 0, rule);
},
prepend: function prepend(rule, after, child) {
var fields = Object.keys(h.fieldCtx),
index = 0,
rules;
if (rule.field && fields.indexOf(rule.field) > -1) return err("".concat(rule.field, " \u5B57\u6BB5\u5DF2\u5B58\u5728"), rule);
var ctx = h.getCtx(after);
if (ctx) {
if (child) {
rules = ctx.rule.children;
} else {
index = ctx.root.indexOf(ctx.origin);
rules = ctx.root;
}
} else rules = h.rules;
rules.splice(index, 0, rule);
},
hidden: function hidden(state, fields) {
props(fields, 'hidden', !!state);
h.refresh();
},
hiddenStatus: function hiddenStatus(id) {
var ctx = h.getCtx(id);
if (!ctx) return;
return !!ctx.rule.hidden;
},
display: function display(state, fields) {
props(fields, 'display', !!state);
h.refresh();
},
displayStatus: function displayStatus(id) {
var ctx = h.getCtx(id);
if (!ctx) return;
return !!ctx.rule.display;
},
disabled: function disabled(_disabled, fields) {
tidyFields(fields).forEach(function (field) {
var ctx = h.fieldCtx[field];
if (!ctx) return;
$set(ctx.rule.props, 'disabled', !!_disabled);
});
h.refresh();
},
model: function model(origin) {
return byRules(h.fieldCtx, origin);
},
component: function component(origin) {
return byRules(h.nameCtx, origin);
},
bind: function bind() {
return api.form;
},
reload: function reload(rules) {
h.reloadRule(rules);
},
updateOptions: function updateOptions(options) {
h.fc.updateOptions(options);
api.refresh();
},
onSubmit: function onSubmit(fn) {
api.updateOptions({
onSubmit: fn
});
},
sync: function sync(field) {
var ctx = is.Object(field) ? byCtx(field) : h.getCtx(field);
if (ctx && !ctx.deleted) {
var subForm = h.subForm[field];
if (subForm) {
if (Array.isArray(subForm)) {
subForm.forEach(function (form) {
form.refresh();
});
} else if (subForm) {
subForm.refresh();
}
} //ctx.updateKey(true);
h.$render.clearCache(ctx);
h.refresh();
}
},
refresh: function refresh() {
allSubForm().forEach(function (sub) {
sub.refresh();
});
h.$render.clearCacheAll();
h.refresh();
},
refreshOptions: function refreshOptions() {
h.$manager.updateOptions(h.options);
api.refresh();
},
hideForm: function hideForm(hide) {
$set(h.vm, 'isShow', !hide);
},
changeStatus: function changeStatus() {
return h.changeStatus;
},
clearChangeStatus: function clearChangeStatus() {
h.changeStatus = false;
},
updateRule: function updateRule(id, rule) {
var r = api.getRule(id);
r && extend(r, rule);
},
updateRules: function updateRules(rules) {
Object.keys(rules).forEach(function (id) {
api.updateRule(id, rules[id]);
});
},
mergeRule: function mergeRule$1(id, rule) {
var ctx = h.getCtx(id);
ctx && mergeRule(ctx.rule, rule);
},
mergeRules: function mergeRules(rules) {
Object.keys(rules).forEach(function (id) {
api.mergeRule(id, rules[id]);
});
},
getRule: function getRule(id, origin) {
var ctx = h.getCtx(id);
if (ctx) {
return origin ? ctx.origin : ctx.rule;
}
},
updateValidate: function updateValidate(id, validate, merge) {
if (merge) {
api.mergeRule(id, {
validate: validate
});
} else {
props(id, 'validate', validate);
}
},
updateValidates: function updateValidates(validates, merge) {
Object.keys(validates).forEach(function (id) {
api.updateValidate(id, validates[id], merge);
});
},
refreshValidate: function refreshValidate() {
h.vm.validate = {};
api.refresh();
},
resetFields: function resetFields(fields) {
var ctxs = h.fieldCtx;
tidyFields(fields).forEach(function (field) {
var ctx = ctxs[field];
if (!ctx) return;
h.$render.clearCache(ctx);
ctx.rule.value = copy(ctx.defaultValue);
h.refreshControl(ctx);
});
},
method: function method(id, name) {
var el = api.el(id);
if (!el || !el[name]) throw new Error(format('err', "".concat(name, "\u65B9\u6CD5\u4E0D\u5B58\u5728")));
return function () {
return el[name].apply(el, arguments);
};
},
exec: function exec(id, name) {
for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
args[_key - 2] = arguments[_key];
}
return invoke(function () {
return api.method(id, name).apply(void 0, args);
});
},
toJson: function toJson$1(space) {
return toJson(api.rule, space);
},
trigger: function trigger(id, event) {
var el = api.el(id);
for (var _len2 = arguments.length, args = new Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) {
args[_key2 - 2] = arguments[_key2];
}
el && el.$emit.apply(el, [event].concat(args));
},
el: function el(id) {
var ctx = h.getCtx(id);
if (ctx) return ctx.el || h.vm.$refs[ctx.ref];
},
closeModal: function closeModal(id) {
var el = api.el(id);
el && el.$emit && el.$emit('close-modal');
},
getSubForm: function getSubForm(field) {
return h.subForm[field];
},
nextTick: function nextTick(fn) {
h.bus.$once('next-tick', fn);
h.refresh();
},
nextRefresh: function nextRefresh(fn) {
h.nextRefresh();
fn && invoke(fn);
}
};
['on', 'once', 'off', 'set'].forEach(function (n) {
api[n] = function () {
var _h$vm;
(_h$vm = h.vm)["$".concat(n)].apply(_h$vm, arguments);
};
});
api.changeValue = api.changeField = api.setValue;
return api;
}
function useCache(Render) {
extend(Render.prototype, {
initCache: function initCache() {
this.clearCacheAll();
},
clearCache: function clearCache(ctx) {
if (!this.cache[ctx.id]) {
ctx.parent && this.clearCache(ctx.parent);
return;
}
if (this.cache[ctx.id].use === true || this.cache[ctx.id].parent) {
this.$handle.refresh();
}
var parent = this.cache[ctx.id].parent;
this.cache[ctx.id] = null;
parent && this.clearCache(parent);
},
clearCacheAll: function clearCacheAll() {
this.cache = {};
},
setCache: function setCache(ctx, vnode, parent) {
this.cache[ctx.id] = {
vnode: vnode,
use: false,
parent: parent
};
},
getCache: function getCache(ctx) {
var cache = this.cache[ctx.id];
cache.use = true;
return cache.vnode;
}
});
}
function toCase(str) {
var to = str.replace(/(-[a-z])/g, function (v) {
return v.replace('-', '').toLocaleUpperCase();
});
return lower(to);
}
function lower(str) {
return str.replace(str[0], str[0].toLowerCase());
}
function setTempProps(vm, ctx, api) {
if (!vm.$props) return;
var prop = ctx.prop;
var keys = Object.keys(vm.$props);
var inject = injectProp(ctx, api);
var injectKeys = Object.keys(inject);
keys.forEach(function (key) {
if (hasProperty(prop.props, key)) vm.$props[key] = prop.props[key];else if (injectKeys.indexOf(key) > -1) vm.$props[key] = inject[key];
});
var key = vm.$options.model && vm.$options.model.prop || 'value';
if (keys.indexOf(key) > -1) {
vm.$props[key] = prop.value;
}
}
function injectProp(ctx, api) {
return {
formCreate: api,
formCreateField: ctx.field,
formCreateOptions: ctx.prop.options,
formCreateRule: function () {
var temp = _objectSpread2({}, ctx.prop);
return temp.on = temp.on ? _objectSpread2({}, temp.on) : {}, temp;
}()
};
}
function useRender(Render) {
extend(Render.prototype, {
initRender: function initRender() {
this.renderList = {};
this.clearOrgChildren();
},
initOrgChildren: function initOrgChildren() {
var ctxs = this.$handle.ctxs;
this.orgChildren = Object.keys(ctxs).reduce(function (initial, id) {
var children = ctxs[id].rule.children;
initial[id] = is.trueArray(children) ? _toConsumableArray(children) : [];
return initial;
}, {});
},
clearOrgChildren: function clearOrgChildren() {
this.orgChildren = {};
},
render: function render() {
var _this = this;
if (!this.vm.isShow) {
return;
}
this.$h = this.vm.$createElement;
this.$manager.beforeRender();
var vn = this.sort.map(function (id) {
return _this.renderCtx(_this.$handle.ctxs[id]);
}).filter(function (val) {
return val !== undefined;
});
return this.$manager.render(vn);
},
makeVm: function makeVm(rule) {
var _this2 = this;
var vm = rule.vm;
if (!vm) return new _vue();else if (is.Function(vm)) return invoke(function () {
return vm(_this2.$handle.getInjectData(rule));
});else if (!vm._isVue) return new _vue(vm);
return vm;
},
mergeGlobal: function mergeGlobal(ctx) {
var g = this.$handle.options.global;
if (!g) return; //todo 缓存配置,更新 option 更新
if (!ctx.cacheConfig) ctx.cacheConfig = g[ctx.originType] || g[ctx.type] || g[ctx.trueType] || {};
ctx.prop = mergeRule({}, [g['*'], ctx.cacheConfig, ctx.prop]);
},
setOptions: function setOptions(ctx) {
if (ctx.prop.optionsTo && ctx.prop.options) {
deepSet(ctx.prop, ctx.prop.optionsTo, ctx.prop.options);
}
},
renderTemp: function renderTemp(ctx) {
var _this3 = this;
if (!_vue.compile) {
tip('当前使用的Vue构建版本不支持compile,无法使用template功能');
return [];
}
var rule = ctx.prop;
var id = ctx.id,
key = ctx.key;
if (!this.renderList[id]) {
if (!ctx.el) {
ctx.el = this.makeVm(rule);
this.vm.$nextTick(function () {
return ctx.parser.mounted(ctx);
});
}
var _vm = ctx.el;
if (ctx.input) _vm.$on(_vm.$options.model && _vm.$options.model.event || 'input', function (value) {
_this3.onInput(ctx, value);
});
this.renderList[id] = {
vm: _vm,
template: _vue.compile(rule.template)
};
}
var _this$renderList$id = this.renderList[id],
vm = _this$renderList$id.vm,
template = _this$renderList$id.template;
setTempProps(vm, ctx, this.$handle.api);
var vn = template.render.call(vm);
if (is.Undef(vn.data)) vn.data = {};
vn.key = key;
vn.data.ref = ctx.ref;
vn.data.key = key;
return vn;
},
renderSides: function renderSides(vn, ctx, temp) {
var prop = ctx[temp ? 'rule' : 'prop'];
return [this.renderRule(prop.prefix), vn, this.renderRule(prop.suffix)];
},
renderCtx: function renderCtx(ctx, parent) {
if (ctx.type === 'hidden') return;
if (!this.cache[ctx.id]) {
var vn;
var cacheFlag = true;
var _type = ctx.trueType;
var none = !(is.Undef(ctx.rule.display) || !!ctx.rule.display);
if (_type === 'template' && !ctx.rule.template) {
vn = this.renderSides(this.renderChildren(ctx), ctx, true);
if (none) {
this.display(vn);
}
vn = this.item(ctx, vn);
} else if (_type === 'fcFragment') {
vn = this.renderChildren(ctx);
} else {
ctx.initProp();
this.mergeGlobal(ctx);
this.$manager.tidyRule(ctx);
this.setOptions(ctx);
this.ctxProp(ctx);
var prop = ctx.prop;
if (prop.hidden) {
this.setCache(ctx, undefined, parent);
return;
}
if (_type === 'template' && prop.template) {
vn = this.renderTemp(ctx);
cacheFlag = false;
} else {
vn = ctx.parser.render(this.renderChildren(ctx), ctx);
}
vn = this.renderSides(vn, ctx);
if (!(!ctx.input && is.Undef(prop["native"])) && prop["native"] !== true) {
vn = this.$manager.makeWrap(ctx, vn);
}
if (none) {
vn = this.display(vn);
}
vn = this.item(ctx, vn);
}
if (cacheFlag) {
this.setCache(ctx, vn, parent);
}
return vn;
}
return this.getCache(ctx);
},
display: function display(vn) {
var _this4 = this;
if (Array.isArray(vn)) {
var data = [];
vn.forEach(function (v) {
if (Array.isArray(v)) return _this4.display(v);
if (_this4.none(v)) data.push(v);
});
return data;
} else {
return this.none(vn);
}
},
none: function none(vn) {
if (vn && vn.data) {
if (Array.isArray(vn.data.style)) {
vn.data.style.push({
display: 'none'
});
} else {
vn.data.style = [vn.data.style, {
display: 'none'
}];
}
return vn;
}
},
item: function item(ctx, vn) {
return this.$h('fcFragment', {
slot: ctx.rule.slot,
key: ctx.key
}, [vn]);
},
ctxProp: function ctxProp(ctx, custom) {
var _this5 = this;
var ref = ctx.ref,
key = ctx.key;
this.$manager.mergeProp(ctx, custom);
ctx.parser.mergeProp(ctx, custom);
var props = [{
props: injectProp(ctx, this.$handle.api),
ref: ref,
key: "".concat(key, "fc"),
slot: undefined
}];
if (!custom) {
props.push({
on: {
'hook:mounted': function hookMounted() {
_this5.onMounted(ctx);
},
'fc.sub-form': function fcSubForm(subForm) {
_this5.$handle.addSubForm(ctx, subForm);
}
},
model: ctx.input ? {
value: this.$handle.getFormData(ctx),
callback: function callback(value) {
_this5.onInput(ctx, value);
},
expression: "formData.".concat(ctx.field)
} : undefined
});
}
mergeProps(props, ctx.prop);
return ctx.prop;
},
onMounted: function onMounted(ctx) {
ctx.el = this.vm.$refs[ctx.ref];
ctx.parser.mounted(ctx);
this.$handle.effect(ctx, 'mounted');
},
onInput: function onInput(ctx, value) {
this.$handle.onInput(ctx, value);
},
renderChildren: function renderChildren(ctx) {
var _this6 = this;
var children = ctx.rule.children,
orgChildren = this.orgChildren[ctx.id];
var isRm = function isRm(child) {
return !is.String(child) && child.__fc__ && !_this6.$handle.ctxs[child.__fc__.id];
};
if (!is.trueArray(children) && orgChildren) {
this.$handle.deferSyncValue(function () {
orgChildren.forEach(function (child) {
if (!child) return;
if (isRm(child)) {
_this6.$handle.rmCtx(child.__fc__);
}
});
});
this.orgChildren[ctx.id] = [];
return [];
}
orgChildren && this.$handle.deferSyncValue(function () {
orgChildren.forEach(function (child) {
if (!child) return;
if (children.indexOf(child) === -1 && isRm(child)) {
_this6.$handle.rmCtx(child.__fc__);
}
});
});
return children.map(function (child) {
if (!child) return;
if (is.String(child)) return child;
if (child.__fc__) {
return _this6.renderCtx(child.__fc__, ctx);
}
if (!_this6.$handle.isRepeatRule(child.__origin__ || child) && child.type) {
_this6.vm.$nextTick(function () {
_this6.$handle.loadChildren(children, ctx);
_this6.$handle.refresh();
});
}
});
},
defaultRender: function defaultRender(ctx, children) {
var prop = ctx.prop;
if (this.vNode[ctx.type]) return this.vNode[ctx.type](prop, children);
if (this.vNode[ctx.originType]) return this.vNode[ctx.originType](prop, children);
return this.vNode.make(lower(ctx.originType), prop, children);
},
renderRule: function renderRule(rule, children, origin) {
var _this7 = this;
if (!rule) return undefined;
if (is.String(rule)) return rule;
var type;
if (origin) {
type = rule.type;
} else {
type = rule.is;
if (rule.type) {
type = toCase(rule.type);
var alias = this.vNode.aliasMap[type];
if (alias) type = toCase(alias);
}
}
if (!type) return undefined;
var data = [[children]];
if (is.trueArray(rule.children)) {
data.push(rule.children.map(function (v) {
return _this7.renderRule(v);
}));
}
return this.$h(type, _objectSpread2({}, rule), data);
}
});
}
function Render(handle) {
extend(this, {
$handle: handle,
fc: handle.fc,
vm: handle.vm,
$manager: handle.$manager,
vNode: new handle.fc.CreateNode(handle.vm)
});
funcProxy(this, {
options: function options() {
return handle.options;
},
sort: function sort() {
return handle.sort;
}
});
this.initCache();
this.initRender();
}
useCache(Render);
useRender(Render);
function useInject(Handler) {
extend(Handler.prototype, {
parseInjectEvent: function parseInjectEvent(rule, on) {
var _this = this;
if (rule.inject === false) return;
var inject = rule.inject || this.options.injectEvent;
if (is.Undef(inject)) return;
Object.keys(on).forEach(function (k) {
if (is.Function(on[k])) on[k] = _this.inject(rule, on[k], inject);
});
return on;
},
parseEmit: function parseEmit(ctx, on) {
var _this2 = this;
var event = {},
rule = ctx.rule,
emitPrefix = rule.emitPrefix,
field = rule.field,
name = rule.name,
inject = rule.inject;
var emit = rule[on ? 'emit' : 'nativeEmit'] || [];
if (is.trueArray(emit)) {
var emitKey = emitPrefix || field || name;
if (emitKey) {
if (!on) emitKey = "native-".concat(emitKey);
emit.forEach(function (eventName) {
if (!eventName) return;
var eventInject;
if (is.Object(eventName)) {
eventInject = eventName.inject;
eventName = eventName.name;
}
var fieldKey = toLine("".concat(emitKey, "-").concat(eventName));
var fn = function fn() {
var _this2$vm, _this2$vm2;
for (var _len = arguments.length, arg = new Array(_len), _key = 0; _key < _len; _key++) {
arg[_key] = arguments[_key];
}
(_this2$vm = _this2.vm).$emit.apply(_this2$vm, [fieldKey].concat(arg));
(_this2$vm2 = _this2.vm).$emit.apply(_this2$vm2, ['emit-event', fieldKey].concat(arg));
};
fn.__emit = true;
if (!eventInject && inject === false) {
event[eventName] = fn;
} else {
var _inject = eventInject || inject || _this2.options.injectEvent;
event[eventName] = is.Undef(_inject) ? fn : _this2.inject(rule, fn, _inject);
}
});
}
}
ctx.computed[on ? 'on' : 'nativeOn'] = event;
return event;
},
getInjectData: function getInjectData(self, inject) {
var _this$vm$$options$pro = this.vm.$options.propsData,
option = _this$vm$$options$pro.option,
rule = _this$vm$$options$pro.rule;
return {
$f: this.api,
rule: rule,
self: self.__origin__,
option: option,
inject: inject
};
},
inject: function inject(self, _fn, _inject2) {
if (_fn.__inject) {
if (this.watching && !this.loading) return _fn;
_fn = _fn.__origin;
}
var h = this;
var fn = function fn() {
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
args.unshift(h.getInjectData(self, _inject2));
return _fn.apply(this, args);
};
fn.__inject = true;
fn.__origin = _fn;
return fn;
}
});
}
var EVENT = ['hook:updated', 'hook:mounted'];
function usePage(Handler) {
extend(Handler.prototype, {
usePage: function usePage() {
var _this = this;
var page = this.options.page;
if (!page) return;
var first = 25;
var limit = getLimit(this.rules);
if (is.Object(page)) {
if (page.first) first = parseInt(page.first, 10) || first;
if (page.limit) limit = parseInt(page.limit, 10) || limit;
}
extend(this, {
first: first,
limit: limit,
pageEnd: this.rules.length <= first
});
this.bus.$on('page-end', function () {
return _this.vm.$emit('page-end', _this.api);
});
this.pageLoad();
},
pageLoad: function pageLoad() {
var _this2 = this;
var pageFn = function pageFn() {
if (_this2.pageEnd) {
_this2.vm.$off(EVENT, pageFn);
_this2.bus.$emit('page-end');
} else {
_this2.first += _this2.limit;
_this2.pageEnd = _this2.rules.length <= _this2.first;
_this2.loadRule();
_this2.refresh();
}
};
this.vm.$on(EVENT, pageFn);
}
});
}
function getLimit(rules) {
return rules.length < 31 ? 31 : Math.ceil(rules.length / 3);
}
function useRender$1(Handler) {
extend(Handler.prototype, {
clearNextTick: function clearNextTick() {
this.nextTick && clearTimeout(this.nextTick);
this.nextTick = null;
},
bindNextTick: function bindNextTick(fn) {
var _this = this;
this.clearNextTick();
this.nextTick = setTimeout(function () {
fn();
_this.nextTick = null;
}, 10);
},
render: function render() {
// console.warn('%c render', 'color:green');
++this.loadedId;
if (this.vm.unique > 0) return this.$render.render();else {
this.vm.unique = 1;
return [];
}
}
});
}
function bind(ctx) {
Object.defineProperties(ctx.origin, {
__fc__: enumerable(ctx, true)
});
}
function RuleContext(handle, rule) {
var id = uniqueId();
extend(this, {
id: id,
ref: id,
wrapRef: id + 'fi',
rule: rule,
origin: rule.__origin__ || rule,
name: rule.name,
watch: [],
linkOn: [],
root: [],
ctrlRule: [],
parent: null,
cacheConfig: null,
prop: _objectSpread2({}, rule),
computed: {},
payload: {},
input: !!rule.field,
el: undefined,
defaultValue: rule.field ? deepCopy(rule.value) : undefined,
field: rule.field || undefined
});
this.updateType();
this.updateKey();
bind(this);
this.update(handle, true);
}
extend(RuleContext.prototype, {
effectData: function effectData(name) {
if (!this.payload[name]) {
this.payload[name] = {};
}
return this.payload[name];
},
clearEffectData: function clearEffectData(name) {
delete this.payload[name];
},
updateKey: function updateKey(flag) {
this.key = uniqueId();
flag && this.parent && this.parent.updateKey(flag);
},
updateType: function updateType() {
this.originType = this.rule.type;
this.type = toCase(this.rule.type);
},
setParser: function setParser(parser) {
this.parser = parser;
parser.init(this);
},
initProp: function initProp() {
var _this = this;
this.prop = mergeProps([this.rule].concat(_toConsumableArray(Object.keys(this.payload).map(function (k) {
return _this.payload[k];
})), [this.computed]));
},
check: function check(handle) {
return this.vm === handle.vm;
},
unwatch: function unwatch() {
this.watch.forEach(function (un) {
return un();
});
this.watch = [];
},
unlink: function unlink() {
this.linkOn.forEach(function (un) {
return un();
});
this.linkOn = [];
},
link: function link() {
this.unlink();
this.$handle.appendLink(this);
},
watchTo: function watchTo() {
this.$handle.watchCtx(this);
},
"delete": function _delete() {
var undef = void 0;
this.unwatch();
this.unlink();
this.rmCtrl();
extend(this, {
deleted: true,
prop: _objectSpread2({}, this.rule),
computed: {},
el: undef,
$handle: undef,
$render: undef,
$api: undef,
vm: undef,
vNode: undef,
parent: null,
cacheConfig: null
});
},
rmCtrl: function rmCtrl() {
this.ctrlRule.forEach(function (ctrl) {
return ctrl.__fc__.rm();
});
this.ctrlRule = [];
},
rm: function rm() {
var _this2 = this;
var _rm = function _rm() {
var index = _this2.root.indexOf(_this2.origin);
if (index > -1) {
_this2.root.splice(index, 1);
_this2.$handle.refresh();
}
};
if (this.deleted) {
_rm();
return;
}
this.$handle.noWatch(function () {
_this2.$handle.deferSyncValue(function () {
_this2.rmCtrl();
_rm();
_this2.$handle.rmCtx(_this2);
extend(_this2, {
root: []
});
});
});
},
update: function update(handle, init) {
extend(this, {
deleted: false,
$handle: handle,
$render: handle.$render,
$api: handle.api,
vm: handle.vm,
trueType: handle.getType(this.originType),
vNode: handle.$render.vNode,
updated: false
});
!init && this.unwatch();
this.watchTo();
this.link();
}
});
function useLoader(Handler) {
extend(Handler.prototype, {
nextRefresh: function nextRefresh(fn) {
var _this = this;
var id = this.loadedId;
this.vm.$nextTick(function () {
id === _this.loadedId && (fn ? fn() : _this.refresh());
});
},
parseRule: function parseRule(_rule) {
var _this2 = this;
var rule = getRule(_rule);
Object.defineProperties(rule, {
__origin__: enumerable(_rule, true)
});
fullRule(rule);
if (rule.field && hasProperty(this.options.formData || {}, rule.field)) rule.value = this.options.formData[rule.field];
rule.options = Array.isArray(rule.options) ? rule.options : [];
['on', 'props', 'nativeOn'].forEach(function (k) {
var v = rule[k];
if (v) {
Object.keys(v).forEach(function (n) {
v[n] = parseFn(v[n]);
});
_this2.parseInjectEvent(rule, v);
}
});
return rule;
},
syncProp: function syncProp(ctx) {
var _this3 = this;
var rule = ctx.