forked from alexjoverm/v-runtime-template
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
118 lines (109 loc) · 3.3 KB
/
index.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import {h} from 'vue';
const defineDescriptor = (src, dest, name) => {
// eslint-disable-next-line no-prototype-builtins
if (!dest.hasOwnProperty(name)) {
const descriptor = Object.getOwnPropertyDescriptor(src, name);
Object.defineProperty(dest, name, descriptor);
}
};
const merge = (objs) => {
const res = {};
objs.forEach((obj) => {
obj &&
Object.getOwnPropertyNames(obj).forEach((name) =>
defineDescriptor(obj, res, name),
);
});
return res;
};
const buildFromProps = (obj, props) => {
const res = {};
props.forEach((prop) => defineDescriptor(obj, res, prop));
return res;
};
export default {
props: {
template: String,
parent: Object,
templateProps: {
type: Object,
default: () => ({}),
},
},
render() {
if (this.template) {
const parent = this.parent || this.$parent;
const {
$data: parentData = {},
$props: parentProps = {},
$options: parentOptions = {},
} = parent;
const {
components: parentComponents = {},
computed: parentComputed = {},
methods: parentMethods = {},
} = parentOptions;
const {
$data = {},
$props = {},
$options: {methods = {}, computed = {}, components = {}} = {},
} = this;
const passthrough = {
$data: {},
$props: {},
$options: {},
components: {},
computed: {},
methods: {},
};
// build new objects by removing keys if already exists (e.g. created by mixins)
Object.keys(parentData).forEach((e) => {
if (typeof $data[e] === 'undefined') {
passthrough.$data[e] = parentData[e];
}
});
Object.keys(parentProps).forEach((e) => {
if (typeof $props[e] === 'undefined') {
passthrough.$props[e] = parentProps[e];
}
});
Object.keys(parentMethods).forEach((e) => {
if (typeof methods[e] === 'undefined') {
passthrough.methods[e] = parentMethods[e];
}
});
Object.keys(parentComputed).forEach((e) => {
if (typeof computed[e] === 'undefined') {
passthrough.computed[e] = parentComputed[e];
}
});
Object.keys(parentComponents).forEach((e) => {
if (typeof components[e] === 'undefined') {
passthrough.components[e] = parentComponents[e];
}
});
const methodKeys = Object.keys(passthrough.methods || {});
const dataKeys = Object.keys(passthrough.$data || {});
const propKeys = Object.keys(passthrough.$props || {});
const templatePropKeys = Object.keys(this.templateProps);
const allKeys = dataKeys.concat(propKeys).concat(methodKeys).concat(templatePropKeys);
const methodsFromProps = buildFromProps(parent, methodKeys);
const finalProps = merge([
passthrough.$data,
passthrough.$props,
methodsFromProps,
this.templateProps,
]);
const provide = this.$parent.$.provides ? this.$parent.$.provides : {}; // Avoids Vue warning
const dynamic = {
template: this.template || '<div></div>',
props: allKeys,
computed: passthrough.computed,
components: passthrough.components,
provide: provide,
};
// debugger;
return h(dynamic, {...finalProps});
}
},
};