-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponent.js
298 lines (270 loc) · 7.15 KB
/
component.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
var _ = require('../util')
var templateParser = require('../parsers/template')
module.exports = {
isLiteral: true,
/**
* Setup. Two possible usages:
*
* - static:
* v-component="comp"
*
* - dynamic:
* v-component="{{currentView}}"
*/
bind: function () {
if (!this.el.__vue__) {
// create a ref anchor
this.anchor = _.createAnchor('v-component')
_.replace(this.el, this.anchor)
// check keep-alive options.
// If yes, instead of destroying the active vm when
// hiding (v-if) or switching (dynamic literal) it,
// we simply remove it from the DOM and save it in a
// cache object, with its constructor id as the key.
this.keepAlive = this._checkParam('keep-alive') != null
// wait for event before insertion
this.readyEvent = this._checkParam('wait-for')
// check ref
this.refID = _.attr(this.el, 'ref')
if (this.keepAlive) {
this.cache = {}
}
// check inline-template
if (this._checkParam('inline-template') !== null) {
// extract inline template as a DocumentFragment
this.template = _.extractContent(this.el, true)
}
// component resolution related state
this._pendingCb =
this.ctorId =
this.Ctor = null
// if static, build right now.
if (!this._isDynamicLiteral) {
this.resolveCtor(this.expression, _.bind(this.initStatic, this))
} else {
// check dynamic component params
this.transMode = this._checkParam('transition-mode')
}
} else {
_.warn(
'Do not create a component that only contains ' +
'a single other component - they will be mounted to ' +
'the same element and cause conflict. Wrap it with ' +
'an outer element.'
)
}
},
/**
* Initialize a static component.
*/
initStatic: function () {
var child = this.build()
var anchor = this.anchor
this.setCurrent(child)
if (!this.readyEvent) {
child.$before(anchor)
} else {
child.$once(this.readyEvent, function () {
child.$before(anchor)
})
}
},
/**
* Public update, called by the watcher in the dynamic
* literal scenario, e.g. v-component="{{view}}"
*/
update: function (value) {
this.setComponent(value)
},
/**
* Switch dynamic components. May resolve the component
* asynchronously, and perform transition based on
* specified transition mode. Accepts a few additional
* arguments specifically for vue-router.
*
* @param {String} value
* @param {Object} data
* @param {Function} afterBuild
* @param {Function} afterTransition
*/
setComponent: function (value, data, afterBuild, afterTransition) {
this.invalidatePending()
if (!value) {
// just remove current
this.unbuild()
this.remove(this.childVM, afterTransition)
this.unsetCurrent()
} else {
this.resolveCtor(value, _.bind(function () {
this.unbuild()
var newComponent = this.build(data)
/* istanbul ignore if */
if (afterBuild) afterBuild(newComponent)
var self = this
if (this.readyEvent) {
newComponent.$once(this.readyEvent, function () {
self.transition(newComponent, afterTransition)
})
} else {
this.transition(newComponent, afterTransition)
}
}, this))
}
},
/**
* Resolve the component constructor to use when creating
* the child vm.
*/
resolveCtor: function (id, cb) {
var self = this
this._pendingCb = _.cancellable(function (ctor) {
self.ctorId = id
self.Ctor = ctor
cb()
})
this.vm._resolveComponent(id, this._pendingCb)
},
/**
* When the component changes or unbinds before an async
* constructor is resolved, we need to invalidate its
* pending callback.
*/
invalidatePending: function () {
if (this._pendingCb) {
this._pendingCb.cancel()
this._pendingCb = null
}
},
/**
* Instantiate/insert a new child vm.
* If keep alive and has cached instance, insert that
* instance; otherwise build a new one and cache it.
*
* @param {Object} [data]
* @return {Vue} - the created instance
*/
build: function (data) {
if (this.keepAlive) {
var cached = this.cache[this.ctorId]
if (cached) {
return cached
}
}
if (this.Ctor) {
var parent = this._host || this.vm
var el = templateParser.clone(this.el)
var child = parent.$addChild({
el: el,
data: data,
template: this.template,
// if no inline-template, then the compiled
// linker can be cached for better performance.
_linkerCachable: !this.template,
_asComponent: true,
_isRouterView: this._isRouterView,
_context: this.vm
}, this.Ctor)
if (this.keepAlive) {
this.cache[this.ctorId] = child
}
return child
}
},
/**
* Teardown the current child, but defers cleanup so
* that we can separate the destroy and removal steps.
*/
unbuild: function () {
var child = this.childVM
if (!child || this.keepAlive) {
return
}
// the sole purpose of `deferCleanup` is so that we can
// "deactivate" the vm right now and perform DOM removal
// later.
child.$destroy(false, true)
},
/**
* Remove current destroyed child and manually do
* the cleanup after removal.
*
* @param {Function} cb
*/
remove: function (child, cb) {
var keepAlive = this.keepAlive
if (child) {
child.$remove(function () {
if (!keepAlive) child._cleanup()
if (cb) cb()
})
} else if (cb) {
cb()
}
},
/**
* Actually swap the components, depending on the
* transition mode. Defaults to simultaneous.
*
* @param {Vue} target
* @param {Function} [cb]
*/
transition: function (target, cb) {
var self = this
var current = this.childVM
this.unsetCurrent()
this.setCurrent(target)
switch (self.transMode) {
case 'in-out':
target.$before(self.anchor, function () {
self.remove(current, cb)
})
break
case 'out-in':
self.remove(current, function () {
if (!target._isDestroyed) {
target.$before(self.anchor, cb)
}
})
break
default:
self.remove(current)
target.$before(self.anchor, cb)
}
},
/**
* Set childVM and parent ref
*/
setCurrent: function (child) {
this.childVM = child
var refID = child._refID || this.refID
if (refID) {
this.vm.$[refID] = child
}
},
/**
* Unset childVM and parent ref
*/
unsetCurrent: function () {
var child = this.childVM
this.childVM = null
var refID = (child && child._refID) || this.refID
if (refID) {
this.vm.$[refID] = null
}
},
/**
* Unbind.
*/
unbind: function () {
this.invalidatePending()
this.unbuild()
this.unsetCurrent()
// destroy all keep-alive cached instances
if (this.cache) {
for (var key in this.cache) {
this.cache[key].$destroy()
}
this.cache = null
}
}
}