| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441 |
- var _ = require('../util')
- var config = require('../config')
- var FragmentFactory = require('../fragment/factory')
- var isObject = _.isObject
- var uid = 0
- // TODO: ref, el
- module.exports = {
- bind: function () {
- // support "item in items" syntax
- var inMatch = this.expression.match(/(.*) in (.*)/)
- if (inMatch) {
- this.arg = inMatch[1]
- this._watcherExp = inMatch[2]
- }
- if (!this.arg) {
- process.env.NODE_ENV !== 'production' && _.warn(
- 'Alias is required in v-for.'
- )
- return
- }
- // uid as a cache identifier
- this.id = '__v-for__' + (++uid)
- // setup anchor nodes
- this.start = _.createAnchor('v-repeat-start')
- this.end = _.createAnchor('v-repeat-end')
- _.replace(this.el, this.end)
- _.before(this.start, this.end)
- // check for trackby param
- this.idKey = this._checkParam('track-by')
- // check v-ref
- this.refId = this._checkParam(config.prefix + 'ref')
- // check for transition stagger
- var stagger = +this._checkParam('stagger')
- this.enterStagger = +this._checkParam('enter-stagger') || stagger
- this.leaveStagger = +this._checkParam('leave-stagger') || stagger
- // cache
- this.cache = Object.create(null)
- // fragment factory
- this.factory = new FragmentFactory(this.vm, this.el)
- },
- create: function (value, alias, index, key) {
- var host = this._host
- // create iteration scope
- var parentScope = this._scope || this.vm
- var scope = Object.create(parentScope)
- // make sure point $parent to parent scope
- scope.$parent = parentScope
- scope.$alias = alias
- // define scope properties
- _.defineReactive(scope, alias, value)
- _.defineReactive(scope, '$index', index)
- if (key) {
- _.defineReactive(scope, '$key', key)
- }
- var frag = this.factory.create(host, scope)
- frag.forId = this.id
- this.cacheFrag(value, frag, index, key)
- return frag
- },
- update: function (data) {
- this.diff(data)
- if (this.refId) {
- this.updateRef()
- }
- },
- diff: function (data) {
- var idKey = this.idKey
- var converted = this.converted
- var oldFrags = this.frags
- var frags = this.frags = new Array(data.length)
- var alias = this.arg
- var start = this.start
- var end = this.end
- var inDoc = _.inDoc(start)
- var init = !oldFrags
- var i, l, frag, item, key, value, primitive
- // First pass, go through the new Array and fill up
- // the new frags 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++) {
- item = data[i]
- key = converted ? item.$key : null
- value = converted ? item.$value : item
- primitive = !isObject(value)
- frag = !init && this.getCachedFrag(value, i, key)
- if (frag) { // reusable fragment
- frag.reused = true
- // update $index
- frag.scope.$index = i
- // update $key
- if (key) {
- frag.scope.$key = key
- }
- // update data for track-by, object repeat &
- // primitive values.
- if (idKey || converted || primitive) {
- frag.scope[alias] = value
- }
- } else { // new isntance
- frag = this.create(value, alias, i, key)
- }
- frags[i] = frag
- if (init) {
- frag.before(end)
- }
- }
- // we're done for the initial render.
- if (init) {
- return
- }
- // Second pass, go through the old fragments and
- // destroy those who are not reused (and remove them
- // from cache)
- var removalIndex = 0
- var totalRemoved = oldFrags.length - frags.length
- for (i = 0, l = oldFrags.length; i < l; i++) {
- frag = oldFrags[i]
- if (!frag.reused) {
- this.deleteCachedFrag(frag)
- frag.unlink()
- this.remove(frag, removalIndex++, totalRemoved, inDoc)
- }
- }
- // Final pass, move/insert new fragments into the
- // right place.
- var targetPrev, prevEl, currentPrev
- var insertionIndex = 0
- for (i = 0, l = frags.length; i < l; i++) {
- frag = frags[i]
- // this is the frag that we should be after
- targetPrev = frags[i - 1]
- prevEl = targetPrev
- ? targetPrev.staggerCb
- ? targetPrev.staggerAnchor
- : targetPrev.end || targetPrev.node
- : start
- if (frag.reused && !frag.staggerCb) {
- currentPrev = findPrevFrag(frag, start, this.id)
- if (currentPrev !== targetPrev) {
- this.move(frag, prevEl)
- }
- } else {
- // new instance, or still in stagger.
- // insert with updated stagger index.
- this.insert(frag, insertionIndex++, prevEl, inDoc)
- }
- frag.reused = false
- }
- },
- updateRef: function () {
- if (!this.converted) {
- this.vm.$[this.refId] = this.frags.map(findVmFromFrag)
- } else {
- var refs = this.vm.$[this.refId] = {}
- this.frags.forEach(function (frag) {
- refs[frag.scope.$key] = findVmFromFrag(frag)
- })
- }
- },
- insert: function (frag, index, prevEl, inDoc) {
- if (frag.staggerCb) {
- frag.staggerCb.cancel()
- frag.staggerCb = null
- }
- var staggerAmount = this.getStagger(frag, index, null, 'enter')
- if (inDoc && staggerAmount) {
- // create an anchor and insert it synchronously,
- // so that we can resolve the correct order without
- // worrying about some elements not inserted yet
- var anchor = frag.staggerAnchor
- if (!anchor) {
- anchor = frag.staggerAnchor = _.createAnchor('stagger-anchor')
- anchor.__vfrag__ = frag
- }
- _.after(anchor, prevEl)
- var op = frag.staggerCb = _.cancellable(function () {
- frag.staggerCb = null
- frag.before(anchor)
- _.remove(anchor)
- })
- setTimeout(op, staggerAmount)
- } else {
- frag.before(prevEl.nextSibling)
- }
- },
- remove: function (frag, index, total, inDoc) {
- if (frag.staggerCb) {
- frag.staggerCb.cancel()
- frag.staggerCb = null
- // it's not possible for the same frag to be removed
- // twice, so if we have a pending stagger callback,
- // it means this frag is queued for enter but removed
- // before its transition started. Since it is already
- // destroyed, we can just leave it in detached state.
- return
- }
- var staggerAmount = this.getStagger(frag, index, total, 'leave')
- if (inDoc && staggerAmount) {
- var op = frag.staggerCb = _.cancellable(function () {
- frag.staggerCb = null
- frag.remove()
- })
- setTimeout(op, staggerAmount)
- } else {
- frag.remove()
- }
- },
- move: function (frag, prevEl) {
- frag.before(prevEl.nextSibling, false)
- },
- cacheFrag: function (value, frag, index, key) {
- var idKey = this.idKey
- var cache = this.cache
- var primitive = !isObject(value)
- var id
- if (key || idKey || primitive) {
- id = idKey
- ? idKey === '$index'
- ? index
- : value[idKey]
- : (key || index)
- if (!cache[id]) {
- cache[id] = frag
- } else if (!primitive && idKey !== '$index') {
- process.env.NODE_ENV !== 'production' && _.warn(
- 'Duplicate track-by key in v-repeat: ' + id
- )
- }
- } else {
- id = this.id
- if (value.hasOwnProperty(id)) {
- if (value[id] === null) {
- value[id] = frag
- } else {
- process.env.NODE_ENV !== 'production' && _.warn(
- 'Duplicate objects are not supported in v-repeat ' +
- 'when using components or transitions.'
- )
- }
- } else {
- _.define(value, id, frag)
- }
- }
- frag.raw = value
- },
- getCachedFrag: function (value, index, key) {
- var idKey = this.idKey
- var primitive = !isObject(value)
- if (key || idKey || primitive) {
- var id = idKey
- ? idKey === '$index'
- ? index
- : value[idKey]
- : (key || index)
- return this.cache[id]
- } else {
- return value[this.id]
- }
- },
- deleteCachedFrag: function (frag) {
- var value = frag.raw
- var idKey = this.idKey
- var scope = frag.scope
- var index = scope.$index
- // fix #948: avoid accidentally fall through to
- // a parent repeater which happens to have $key.
- var key = scope.hasOwnProperty('$key') && scope.$key
- var primitive = !isObject(value)
- if (idKey || key || primitive) {
- var id = idKey
- ? idKey === '$index'
- ? index
- : value[idKey]
- : (key || index)
- this.cache[id] = null
- } else {
- value[this.id] = null
- frag.raw = null
- }
- },
- /**
- * Get the stagger amount for an insertion/removal.
- *
- * @param {Fragment} frag
- * @param {Number} index
- * @param {Number} total
- * @param {String} type
- */
- getStagger: function (frag, index, total, type) {
- type = type + 'Stagger'
- var transition = frag.node.__v_trans
- var hooks = transition && transition.hooks
- var hook = hooks && (hooks[type] || hooks.stagger)
- return hook
- ? hook.call(frag, index, total)
- : index * this[type]
- },
- /**
- * Pre-process the value before piping it through the
- * filters, and convert non-Array objects to arrays.
- *
- * This function will be bound to this directive instance
- * and passed into the watcher.
- *
- * @param {*} value
- * @return {Array}
- * @private
- */
- _preProcess: function (value) {
- // regardless of type, store the un-filtered raw value.
- this.rawValue = value
- var type = this.rawType = typeof value
- if (!_.isPlainObject(value)) {
- this.converted = false
- if (type === 'number') {
- value = range(value)
- } else if (type === 'string') {
- value = _.toArray(value)
- }
- return value || []
- } else {
- // convert plain object to array.
- var keys = Object.keys(value)
- var i = keys.length
- var res = new Array(i)
- var key
- while (i--) {
- key = keys[i]
- res[i] = {
- $key: key,
- $value: value[key]
- }
- }
- this.converted = true
- return res
- }
- },
- unbind: function () {
- if (this.refId) {
- this.vm.$[this.redId] = null
- }
- if (this.frags) {
- var i = this.frags.length
- var frag
- while (i--) {
- frag = this.frags[i]
- this.deleteCachedFrag(frag)
- frag.unlink()
- }
- }
- }
- }
- /**
- * Helper to find the previous element that is a fragment
- * anchor. This is necessary because a destroyed frag's
- * element could still be lingering in the DOM before its
- * leaving transition finishes, but its inserted flag
- * should have been set to false so we can skip them.
- *
- * If this is a block repeat, we want to make sure we only
- * return frag that is bound to this v-repeat. (see #929)
- *
- * @param {Fragment} frag
- * @param {Comment|Text} anchor
- * @param {String} id
- * @return {Fragment}
- */
- function findPrevFrag (frag, anchor, id) {
- var el = frag.node.previousSibling
- /* istanbul ignore if */
- if (!el) return
- var frag = el.__vfrag__
- while (
- (!frag || frag.forId !== id || !frag.inserted) &&
- el !== anchor
- ) {
- el = el.previousSibling
- frag = el.__vfrag__
- }
- return frag
- }
- /**
- * Find a vm from a fragment.
- *
- * @param {Fragment} frag
- * @return {Vue|undefined}
- */
- function findVmFromFrag (frag) {
- return frag.node.__vue__ || frag.node.nextSibling.__vue__
- }
- /**
- * 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
- }
|