-
Notifications
You must be signed in to change notification settings - Fork 920
/
vNode.js
42 lines (33 loc) · 973 Bytes
/
vNode.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
import {isFunction, isString, toString} from '@form-create/utils';
function parseVData(data) {
if (isString(data))
data = {domProps: {innerHTML: data}};
else if (data && isFunction(data.get))
data = data.get();
return data;
}
function getVNode(VNode) {
return isFunction(VNode) ? VNode() : (VNode || []);
}
export default class VNode {
constructor(vm) {
if (vm)
this.setVm(vm);
}
setVm(vm) {
this.vm = vm;
this.$h = vm.$createElement;
}
make(nodeName, data, VNodeFn) {
let Node = this.$h(nodeName, parseVData(data), getVNode(VNodeFn));
Node.context = this.vm;
return Node;
}
static use(nodes) {
Object.keys(nodes).forEach((k) => {
VNode.prototype[toString(k).toLocaleLowerCase()] = VNode.prototype[k] = function (data, VNodeFn) {
return this.make(nodes[k], data, VNodeFn);
};
});
}
}