-
Notifications
You must be signed in to change notification settings - Fork 927
/
creator.js
80 lines (67 loc) · 1.98 KB
/
creator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import {$set, extend, isFunction, isPlainObject} from '@form-create/utils';
import VData from './vData';
function baseRule() {
return {
validate: [],
options: [],
col: {},
children: [],
emit: [],
template: undefined,
emitPrefix: undefined,
native: undefined,
info: undefined,
};
}
export function creatorFactory(name) {
return (title, field, value, props = {}) => new Creator(name, title, field, value, props);
}
export function creatorTypeFactory(name, type, typeName = 'type') {
return (title, field, value, props = {}) => {
const maker = new Creator(name, title, field, value, props);
if (isFunction(type)) type(maker);
else maker.props(typeName, type);
return maker;
};
}
export default class Creator extends VData {
constructor(type, title, field, value, props = {}) {
super();
extend(this._data, baseRule());
extend(this._data, {type, title, field, value});
if (isPlainObject(props)) this.props(props);
}
type(type) {
this.props('type', type);
return this;
}
getRule() {
return this._data;
}
event(...args) {
this.on(...args);
return this;
}
}
const keyAttrs = ['emitPrefix', 'className', 'value', 'name', 'title', 'native', 'info', 'hidden', 'visibility'];
keyAttrs.forEach(attr => {
Creator.prototype[attr] = function (value) {
$set(this._data, attr, value);
return this;
};
});
const objAttrs = ['col'];
objAttrs.forEach(attr => {
Creator.prototype[attr] = function (opt) {
$set(this._data, attr, extend(this._data[attr], opt));
return this;
};
});
const arrAttrs = ['validate', 'options', 'children', 'emit'];
arrAttrs.forEach(attr => {
Creator.prototype[attr] = function (opt) {
if (!Array.isArray(opt)) opt = [opt];
$set(this._data, attr, this._data[attr].concat(opt));
return this;
};
});