| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615 |
- var _ = require('../util')
- var config = require('../config')
- 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
- this.hasTransition =
- this.el.hasAttribute(config.prefix + 'transition')
- 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
- // check inline-template
- if (this._checkParam('inline-template') !== null) {
- // extract inline template as a DocumentFragment
- this.inlineTempalte = _.extractContent(this.el, 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
- })
- merged.template = this.inlineTempalte || merged.template
- 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|Number|String} data
- */
- update: function (data) {
- data = data || []
- var type = typeof data
- if (type === 'number') {
- data = range(data)
- } else if (type === 'string') {
- data = _.toArray(data)
- }
- // There are two situations where we have to use the
- // more complex but more accurate diff algorithm:
- // 1. We are using components with v-repeat - the
- // components could have additional state outside
- // of v-repeat data.
- // 2. We have transitions on the list, which requires
- // precise DOM re-positioning.
- this.vms = this.asComponent || this.hasTransition
- ? this.diff(data, this.vms)
- : this.inplaceUpdate(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
- })
- }
- },
- /**
- * Inplace update that maximally reuses existing vm
- * instances and DOM nodes by simply swapping data into
- * existing vms.
- *
- * @param {Array} data
- * @param {Array} oldVms
- * @return {Array}
- */
- inplaceUpdate: function (data, oldVms) {
- oldVms = oldVms || []
- var vms
- var dir = this
- var alias = dir.arg
- var converted = dir.converted
- if (data.length < oldVms.length) {
- oldVms.slice(data.length).forEach(function (vm) {
- vm.$destroy(true)
- })
- vms = oldVms.slice(0, data.length)
- overwrite(data, vms, alias, converted)
- } else if (data.length > oldVms.length) {
- var newVms = data.slice(oldVms.length).map(function (data, i) {
- var vm = dir.build(data, i + oldVms.length)
- vm.$before(dir.ref)
- return vm
- })
- overwrite(data.slice(0, oldVms.length), oldVms, alias, converted)
- vms = oldVms.concat(newVms)
- } else {
- overwrite(data, oldVms, alias, converted)
- vms = oldVms
- }
- return vms
- },
- /**
- * 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, true)
- vm._new = true
- vm._reused = false
- }
- 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
- * @param {Boolean} needCache
- */
- build: function (data, index, needCache) {
- 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 = !isObject(raw) || !isPlainObject(data) || 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,
- template: this.inlineTempalte
- }, Ctor)
- // flag this instance as a repeat instance
- // so that we can skip it in vm._digest
- vm._repeat = true
- // cache instance
- if (needCache) {
- this.cacheVm(raw, vm)
- }
- // sync back changes for $value, particularly for
- // two-way bindings of primitive values
- var self = this
- vm.$watch('$value', function (val) {
- if (self.converted) {
- self.rawValue[vm.$key] = val
- } else {
- self.rawValue.$set(vm.$index, val)
- }
- })
- 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
- }
- var needUncache = this.asComponent || this.hasTransition
- if (this.vms) {
- var i = this.vms.length
- var vm
- while (i--) {
- vm = this.vms[i]
- if (needUncache) {
- 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 track-by key 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 ' +
- 'when using components or transitions.'
- )
- }
- } 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) {
- // regardless of type, store the un-filtered raw value.
- this.rawValue = 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
- }
- /**
- * Helper function to overwrite new data Array on to
- * existing vms. Used in `inplaceUpdate`.
- *
- * @param {Array} arr
- * @param {Array} vms
- * @param {String|undefined} alias
- * @param {Boolean} converted
- */
- function overwrite (arr, vms, alias, converted) {
- var vm, data, raw
- for (var i = 0, l = arr.length; i < l; i++) {
- vm = vms[i]
- data = raw = arr[i]
- if (converted) {
- vm.$key = data.$key
- raw = data.$value
- }
- if (alias) {
- vm[alias] = raw
- } else if (!isObject(raw)) {
- vm.$value = raw
- } else {
- vm._setData(raw)
- }
- }
- }
|