-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathContainerRender.js
98 lines (91 loc) · 2.57 KB
/
ContainerRender.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
import Vue from 'vue';
import PropTypes from './vue-types';
export default {
props: {
autoMount: PropTypes.bool.def(true),
autoDestroy: PropTypes.bool.def(true),
visible: PropTypes.bool,
forceRender: PropTypes.bool.def(false),
parent: PropTypes.any,
getComponent: PropTypes.func.isRequired,
getContainer: PropTypes.func.isRequired,
children: PropTypes.func.isRequired
},
mounted: function mounted() {
if (this.autoMount) {
this.renderComponent();
}
},
updated: function updated() {
if (this.autoMount) {
this.renderComponent();
}
},
beforeDestroy: function beforeDestroy() {
if (this.autoDestroy) {
this.removeContainer();
}
},
methods: {
removeContainer: function removeContainer() {
if (this.container) {
this._component && this._component.$destroy();
this.container.parentNode.removeChild(this.container);
this.container = null;
}
},
renderComponent: function renderComponent() {
var props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var ready = arguments[1];
var visible = this.visible,
getComponent = this.getComponent,
forceRender = this.forceRender,
getContainer = this.getContainer,
parent = this.parent;
var self = this;
if (visible || parent.$refs._component || forceRender) {
var el = this.componentEl;
if (!this.container) {
this.container = getContainer();
el = document.createElement('div');
this.componentEl = el;
this.container.appendChild(el);
}
if (!this._component) {
this._component = new Vue({
data: {
comProps: props
},
parent: self.parent,
el: el,
mounted: function mounted() {
this.$nextTick(function () {
if (ready) {
ready.call(self);
}
});
},
updated: function updated() {
this.$nextTick(function () {
if (ready) {
ready.call(self);
}
});
},
render: function render() {
return getComponent(this.comProps);
}
});
} else {
this._component.comProps = props;
}
}
}
},
render: function render() {
return this.children({
renderComponent: this.renderComponent,
removeContainer: this.removeContainer
});
}
};