forked from meteor-vue/blaze-integration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blaze-template.js
164 lines (139 loc) · 4.44 KB
/
blaze-template.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import {_} from 'meteor/underscore';
import {Blaze} from 'meteor/blaze';
import {EJSON} from 'meteor/ejson';
const templateTypes = [String, Object];
function normalizeVnode(vnode) {
vnode = _.pick(vnode, 'children', 'tag', 'key', 'isComment', 'isStatic', 'text', 'raw', 'ns', 'data', 'componentOptions');
vnode.children = _.map(vnode.children, normalizeVnode);
vnode.data = _.omit(vnode.data, 'hook', 'on', 'pendingInsert');
return vnode;
}
function normalizeVnodes(vnodes) {
return _.map(vnodes, normalizeVnode);
}
function vnodesEquals(vnodes1, vnodes2) {
return EJSON.equals(normalizeVnodes(vnodes1), normalizeVnodes(vnodes2));
}
export default {
name: 'blaze-template',
props: {
template: {
type: templateTypes,
required: true,
},
tag: {
type: String,
default: 'div'
},
data: Object,
},
data() {
return {
renderedSlot: null,
};
},
methods: {
getTemplate() {
let template = this.template;
if (_.isString(template)) {
template = Blaze._getTemplate(template, null);
}
if (!template) {
throw new Error(`Blaze template '${this.template}' not found.`);
}
return template;
},
updateSlot() {
if (this.$slots.default) {
// To prevent observer to be setup on vnodes.
this.$slots.default._isVue = true;
}
this.renderedSlot = this.$slots.default;
},
renderTemplate() {
if (this.blazeView) {
Blaze.remove(this.blazeView);
this.blazeView = null;
}
// To make it available before we start rendering the Blaze template.
this.updateSlot();
const vm = this;
this.blazeView = Blaze.renderWithData(this.getTemplate().constructView(function () {
// This function defines how "Template.contentBlock" gets rendered inside block helpers.
// It does not provide any Blaze content (returns null), but after Blaze renders it,
// it uses Vue vdom patching to render slot content.
const view = this;
if (!view.isRendered) {
view.onViewReady(function () {
view.autorun(function (computation) {
const newVnodes = vm.renderedSlot;
const prevVnodes = view._vnodes;
view._vnodes = newVnodes;
// To prevent unnecessary reruns of the autorun if patch registers any dependency.
// The only dependency we care about is on "renderedSlot" which has already been established.
Tracker.nonreactive(function () {
if (!prevVnodes) {
_.each(newVnodes, function (vnode, i, list) {
// We prepend rendered vnodes before "lastNode" in order.
// So rendered content is in fact between "firstNode" and "lastNode".
const el = vm.__patch__(null, vnode, false, false);
view._domrange.parentElement.insertBefore(el, view.lastNode());
});
}
else {
_.each(_.zip(prevVnodes, newVnodes), function (vnodes, i, list) {
vm.__patch__(vnodes[0], vnodes[1]);
});
}
})
});
});
view.onViewDestroyed(function () {
if (vm._vnodes) {
_.each(vm._vnodes, function (vnode, i, list) {
vm.__patch__(vnode, null);
});
vm._vnodes = null;
}
});
}
return null;
}), () => this.data, this.$el, null, this);
this.renderedToElement = this.$el;
},
},
watch: {
// Template has changed.
template(newValue, oldValue) {
this.renderTemplate();
},
},
created() {
// So that we can use this Vue component instance as a parent view to the Blaze template.
this._scopeBindings = {};
this.renderedToElement = null;
},
render(createElement) {
return createElement(this.tag);
},
updated() {
// We rerender the template when primary element changes (when tag changes).
if (this.renderedToElement !== this.$el) {
this.renderTemplate();
}
// Slot changed.
else if (!vnodesEquals(this.renderedSlot, this.$slots.default)) {
this.updateSlot();
}
},
// Initial rendering.
mounted() {
this.renderTemplate();
},
destroyed() {
if (this.blazeView) {
Blaze.remove(this.blazeView);
this.blazeView = null;
}
},
}