-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepeat.js
504 lines (472 loc) · 12.7 KB
/
repeat.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
var _ = require('../util')
var isObject = _.isObject
var isPlainObject = _.isPlainObject
var textParser = require('../parsers/text')
var expParser = require('../parsers/expression')
var templateParser = require('../parsers/template')
var compile = require('../compiler/compile')
var transclude = require('../compiler/transclude')
var mergeOptions = require('../util/merge-option')
var uid = 0
module.exports = {
/**
* Setup.
*/
bind: function () {
// uid as a cache identifier
this.id = '__v_repeat_' + (++uid)
// we need to insert the objToArray converter
// as the first read filter, because it has to be invoked
// before any user filters. (can't do it in `update`)
if (!this.filters) {
this.filters = {}
}
// add the object -> array convert filter
var objectConverter = _.bind(objToArray, this)
if (!this.filters.read) {
this.filters.read = [objectConverter]
} else {
this.filters.read.unshift(objectConverter)
}
// setup ref node
this.ref = document.createComment('v-repeat')
_.replace(this.el, this.ref)
// check if this is a block repeat
this.template = this.el.tagName === 'TEMPLATE'
? templateParser.parse(this.el, true)
: this.el
// check other directives that need to be handled
// at v-repeat level
this.checkIf()
this.checkRef()
this.checkComponent()
// check for trackby param
this.idKey =
this._checkParam('track-by') ||
this._checkParam('trackby') // 0.11.0 compat
// cache for primitive value instances
this.cache = Object.create(null)
},
/**
* Warn against v-if usage.
*/
checkIf: function () {
if (_.attr(this.el, 'if') !== null) {
_.warn(
'Don\'t use v-if with v-repeat. ' +
'Use v-show or the "filterBy" filter instead.'
)
}
},
/**
* Check if v-ref/ v-el is also present.
*/
checkRef: function () {
var refID = _.attr(this.el, 'ref')
this.refID = refID
? this.vm.$interpolate(refID)
: null
var elId = _.attr(this.el, 'el')
this.elId = elId
? this.vm.$interpolate(elId)
: null
},
/**
* Check the component constructor to use for repeated
* instances. If static we resolve it now, otherwise it
* needs to be resolved at build time with actual data.
*/
checkComponent: function () {
var id = _.attr(this.el, 'component')
var options = this.vm.$options
if (!id) {
this.Ctor = _.Vue // default constructor
this.inherit = true // inline repeats should inherit
// important: transclude with no options, just
// to ensure block start and block end
this.template = transclude(this.template)
this._linkFn = compile(this.template, options)
} else {
this._asComponent = true
var tokens = textParser.parse(id)
if (!tokens) { // static component
var Ctor = this.Ctor = options.components[id]
_.assertAsset(Ctor, 'component', id)
// If there's no parent scope directives and no
// content to be transcluded, we can optimize the
// rendering by pre-transcluding + compiling here
// and provide a link function to every instance.
if (!this.el.hasChildNodes() &&
!this.el.hasAttributes()) {
// merge an empty object with owner vm as parent
// so child vms can access parent assets.
var merged = mergeOptions(Ctor.options, {}, {
$parent: this.vm
})
this.template = transclude(this.template, merged)
this._linkFn = compile(this.template, merged, false, true)
}
} else {
// to be resolved later
var ctorExp = textParser.tokensToExp(tokens)
this.ctorGetter = expParser.parse(ctorExp).get
}
}
},
/**
* Update.
* This is called whenever the Array mutates.
*
* @param {Array} data
*/
update: function (data) {
if (typeof data === 'number') {
data = range(data)
}
this.vms = this.diff(data || [], this.vms)
// update v-ref
if (this.refID) {
this.vm.$[this.refID] = this.vms
}
if (this.elId) {
this.vm.$$[this.elId] = this.vms.map(function (vm) {
return vm.$el
})
}
},
/**
* Diff, based on new data and old data, determine the
* minimum amount of DOM manipulations needed to make the
* DOM reflect the new data Array.
*
* The algorithm diffs the new data Array by storing a
* hidden reference to an owner vm instance on previously
* seen data. This allows us to achieve O(n) which is
* better than a levenshtein distance based algorithm,
* which is O(m * n).
*
* @param {Array} data
* @param {Array} oldVms
* @return {Array}
*/
diff: function (data, oldVms) {
var idKey = this.idKey
var converted = this.converted
var ref = this.ref
var alias = this.arg
var init = !oldVms
var vms = new Array(data.length)
var obj, raw, vm, i, l
// First pass, go through the new Array and fill up
// the new vms array. If a piece of data has a cached
// instance for it, we reuse it. Otherwise build a new
// instance.
for (i = 0, l = data.length; i < l; i++) {
obj = data[i]
raw = converted ? obj.value : obj
vm = !init && this.getVm(raw)
if (vm) { // reusable instance
vm._reused = true
vm.$index = i // update $index
if (converted) {
vm.$key = obj.key // update $key
}
if (idKey) { // swap track by id data
if (alias) {
vm[alias] = raw
} else {
vm._setData(raw)
}
}
} else { // new instance
vm = this.build(obj, i)
vm._new = true
}
vms[i] = vm
// insert if this is first run
if (init) {
vm.$before(ref)
}
}
// if this is the first run, we're done.
if (init) {
return vms
}
// Second pass, go through the old vm instances and
// destroy those who are not reused (and remove them
// from cache)
for (i = 0, l = oldVms.length; i < l; i++) {
vm = oldVms[i]
if (!vm._reused) {
this.uncacheVm(vm)
vm.$destroy(true)
}
}
// final pass, move/insert new instances into the
// right place. We're going in reverse here because
// insertBefore relies on the next sibling to be
// resolved.
var targetNext, currentNext
i = vms.length
while (i--) {
vm = vms[i]
// this is the vm that we should be in front of
targetNext = vms[i + 1]
if (!targetNext) {
// This is the last item. If it's reused then
// everything else will eventually be in the right
// place, so no need to touch it. Otherwise, insert
// it.
if (!vm._reused) {
vm.$before(ref)
}
} else {
if (vm._reused) {
// this is the vm we are actually in front of
currentNext = findNextVm(vm, ref)
// we only need to move if we are not in the right
// place already.
if (currentNext !== targetNext) {
vm.$before(targetNext.$el, null, false)
}
} else {
// new instance, insert to existing next
vm.$before(targetNext.$el)
}
}
vm._new = false
vm._reused = false
}
return vms
},
/**
* Build a new instance and cache it.
*
* @param {Object} data
* @param {Number} index
*/
build: function (data, index) {
var original = data
var meta = { $index: index }
if (this.converted) {
meta.$key = original.key
}
var raw = this.converted ? data.value : data
var alias = this.arg
var hasAlias = !isPlainObject(raw) || alias
// wrap the raw data with alias
data = hasAlias ? {} : raw
if (alias) {
data[alias] = raw
} else if (hasAlias) {
meta.$value = raw
}
// resolve constructor
var Ctor = this.Ctor || this.resolveCtor(data, meta)
var vm = this.vm.$addChild({
el: templateParser.clone(this.template),
_asComponent: this._asComponent,
_linkFn: this._linkFn,
_meta: meta,
data: data,
inherit: this.inherit
}, Ctor)
// cache instance
this.cacheVm(raw, vm)
return vm
},
/**
* Resolve a contructor to use for an instance.
* The tricky part here is that there could be dynamic
* components depending on instance data.
*
* @param {Object} data
* @param {Object} meta
* @return {Function}
*/
resolveCtor: function (data, meta) {
// create a temporary context object and copy data
// and meta properties onto it.
// use _.define to avoid accidentally overwriting scope
// properties.
var context = Object.create(this.vm)
var key
for (key in data) {
_.define(context, key, data[key])
}
for (key in meta) {
_.define(context, key, meta[key])
}
var id = this.ctorGetter.call(context, context)
var Ctor = this.vm.$options.components[id]
_.assertAsset(Ctor, 'component', id)
return Ctor
},
/**
* Unbind, teardown everything
*/
unbind: function () {
if (this.refID) {
this.vm.$[this.refID] = null
}
if (this.vms) {
var i = this.vms.length
var vm
while (i--) {
vm = this.vms[i]
this.uncacheVm(vm)
vm.$destroy()
}
}
},
/**
* Cache a vm instance based on its data.
*
* If the data is an object, we save the vm's reference on
* the data object as a hidden property. Otherwise we
* cache them in an object and for each primitive value
* there is an array in case there are duplicates.
*
* @param {Object} data
* @param {Vue} vm
*/
cacheVm: function (data, vm) {
var idKey = this.idKey
var cache = this.cache
var id
if (idKey) {
id = data[idKey]
if (!cache[id]) {
cache[id] = vm
} else {
_.warn('Duplicate ID in v-repeat: ' + id)
}
} else if (isObject(data)) {
id = this.id
if (data.hasOwnProperty(id)) {
if (data[id] === null) {
data[id] = vm
} else {
_.warn(
'Duplicate objects are not supported in v-repeat.'
)
}
} else {
_.define(data, this.id, vm)
}
} else {
if (!cache[data]) {
cache[data] = [vm]
} else {
cache[data].push(vm)
}
}
vm._raw = data
},
/**
* Try to get a cached instance from a piece of data.
*
* @param {Object} data
* @return {Vue|undefined}
*/
getVm: function (data) {
if (this.idKey) {
return this.cache[data[this.idKey]]
} else if (isObject(data)) {
return data[this.id]
} else {
var cached = this.cache[data]
if (cached) {
var i = 0
var vm = cached[i]
// since duplicated vm instances might be a reused
// one OR a newly created one, we need to return the
// first instance that is neither of these.
while (vm && (vm._reused || vm._new)) {
vm = cached[++i]
}
return vm
}
}
},
/**
* Delete a cached vm instance.
*
* @param {Vue} vm
*/
uncacheVm: function (vm) {
var data = vm._raw
if (this.idKey) {
this.cache[data[this.idKey]] = null
} else if (isObject(data)) {
data[this.id] = null
vm._raw = null
} else {
this.cache[data].pop()
}
}
}
/**
* Helper to find the next element that is an instance
* root node. This is necessary because a destroyed vm's
* element could still be lingering in the DOM before its
* leaving transition finishes, but its __vue__ reference
* should have been removed so we can skip them.
*
* @param {Vue} vm
* @param {CommentNode} ref
* @return {Vue}
*/
function findNextVm (vm, ref) {
var el = (vm._blockEnd || vm.$el).nextSibling
while (!el.__vue__ && el !== ref) {
el = el.nextSibling
}
return el.__vue__
}
/**
* Attempt to convert non-Array objects to array.
* This is the default filter installed to every v-repeat
* directive.
*
* It will be called with **the directive** as `this`
* context so that we can mark the repeat array as converted
* from an object.
*
* @param {*} obj
* @return {Array}
* @private
*/
function objToArray (obj) {
if (!isPlainObject(obj)) {
return obj
}
var keys = Object.keys(obj)
var i = keys.length
var res = new Array(i)
var key
while (i--) {
key = keys[i]
res[i] = {
key: key,
value: obj[key]
}
}
// `this` points to the repeat directive instance
this.converted = true
return res
}
/**
* Create a range array from given number.
*
* @param {Number} n
* @return {Array}
*/
function range (n) {
var i = -1
var ret = new Array(n)
while (++i < n) {
ret[i] = i
}
return ret
}