Explorar o código

unify fragment implementation

Evan You %!s(int64=10) %!d(string=hai) anos
pai
achega
acc13e3965
Modificáronse 5 ficheiros con 104 adicións e 152 borrados
  1. 3 8
      src/fragment/factory.js
  2. 101 0
      src/fragment/fragment.js
  3. 0 65
      src/fragment/multi.js
  4. 0 46
      src/fragment/single.js
  5. 0 33
      src/transition/index.js

+ 3 - 8
src/fragment/factory.js

@@ -1,8 +1,7 @@
 var _ = require('../util')
 var _ = require('../util')
 var compiler = require('../compiler')
 var compiler = require('../compiler')
 var templateParser = require('../parsers/template')
 var templateParser = require('../parsers/template')
-var SingleFragment = require('./single')
-var MultiFragment = require('./multi')
+var Fragment = require('./fragment')
 var Cache = require('../cache')
 var Cache = require('../cache')
 var linkerCache = new Cache(5000)
 var linkerCache = new Cache(5000)
 
 
@@ -46,15 +45,11 @@ function FragmentFactory (vm, el) {
  *
  *
  * @param {Vue} host
  * @param {Vue} host
  * @param {Object} scope
  * @param {Object} scope
- * @param {String} id - v-for id
  */
  */
 
 
-FragmentFactory.prototype.create = function (host, scope, id) {
+FragmentFactory.prototype.create = function (host, scope) {
   var frag = templateParser.clone(this.template)
   var frag = templateParser.clone(this.template)
-  var unlink = this.linker(this.vm, frag, host, scope)
-  return frag.childNodes.length > 1
-    ? new MultiFragment(frag, unlink, scope, id)
-    : new SingleFragment(frag.childNodes[0], unlink, scope, id)
+  return new Fragment(this.linker, this.vm, frag, host, scope)
 }
 }
 
 
 module.exports = FragmentFactory
 module.exports = FragmentFactory

+ 101 - 0
src/fragment/fragment.js

@@ -0,0 +1,101 @@
+var _ = require('../util')
+var transition = require('../transition')
+
+/**
+ * Abstraction for a partially-compiled fragment.
+ * Can optionally compile content with a child scope.
+ *
+ * @param {Function} linker
+ * @param {Vue} vm
+ * @param {DocumentFragment} frag
+ * @param {Vue} [host]
+ * @param {Object} [scope]
+ */
+
+function Fragment (linker, vm, frag, host, scope) {
+  this.unlink = linker(vm, frag, host, scope, this)
+  this.scope = scope
+  this.inserted = false
+  this.children = []
+  var single = this.single = frag.childNodes.length === 1
+  if (single) {
+    this.node = frag.childNodes[0]
+    this.before = singleBefore
+    this.remove = singleRemove
+  } else {
+    this.node = _.createAnchor('fragment-start')
+    this.end = _.createAnchor('fragment-end')
+    this.nodes = _.toArray(frag.childNodes)
+    this.before = multiBefore
+    this.remove = multiRemove
+  }
+  this.node.__vfrag__ = this
+}
+
+/**
+ * Insert fragment before target, single node version
+ *
+ * @param {Node} target
+ * @param {Boolean} trans
+ */
+
+function singleBefore (target, trans) {
+  var method = trans !== false
+    ? transition.before
+    : _.before
+  method(this.node, target, this.scope)
+  this.inserted = true
+}
+
+/**
+ * Remove fragment, single node version
+ */
+
+function singleRemove () {
+  transition.remove(this.node, this.scope)
+  this.inserted = false
+}
+
+/**
+ * Insert fragment before target, multi-nodes version
+ *
+ * @param {Node} target
+ * @param {Boolean} trans
+ */
+
+function multiBefore (target, trans) {
+  _.before(this.node, target)
+  var nodes = this.nodes
+  var scope = this.scope
+  var method = trans !== false
+    ? transition.before
+    : _.before
+  for (var i = 0, l = nodes.length; i < l; i++) {
+    method(nodes[i], target, scope)
+  }
+  _.before(this.end, target)
+  this.inserted = true
+}
+
+/**
+ * Remove fragment, multi-nodes version
+ */
+
+function multiRemove () {
+  var parent = this.node.parentNode
+  var node = this.node.nextSibling
+  var nodes = this.nodes = []
+  var scope = this.scope
+  var next
+  while (node !== this.end) {
+    nodes.push(node)
+    next = node.nextSibling
+    transition.remove(node, scope)
+    node = next
+  }
+  parent.removeChild(this.node)
+  parent.removeChild(this.end)
+  this.inserted = false
+}
+
+module.exports = Fragment

+ 0 - 65
src/fragment/multi.js

@@ -1,65 +0,0 @@
-var _ = require('../util')
-var transition = require('../transition')
-
-/**
- * Multi-node fragment that has a start and an end node.
- *
- * @param {Node} node
- * @param {Function} unlink
- * @param {Object} [scope]
- */
-
-function MultiFragment (frag, unlink, scope) {
-  this.start = this.node = _.createAnchor('fragment-start')
-  this.end = _.createAnchor('fragment-end')
-  this.node.__vfrag__ = this
-  this.reused = false
-  this.inserted = false
-  this.nodes = _.toArray(frag.childNodes)
-  this.scope = scope
-  this.unlink = unlink
-}
-
-/**
- * Insert fragment before target.
- *
- * @param {Node} target
- * @param {Boolean} trans
- */
-
-MultiFragment.prototype.before = function (target, trans) {
-  _.before(this.start, target)
-  var nodes = this.nodes
-  var scope = this.scope
-  var method = trans !== false
-    ? transition.before
-    : _.before
-  for (var i = 0, l = nodes.length; i < l; i++) {
-    method(nodes[i], target, scope)
-  }
-  _.before(this.end, target)
-  this.inserted = true
-}
-
-/**
- * Remove fragment.
- */
-
-MultiFragment.prototype.remove = function () {
-  var parent = this.start.parentNode
-  var node = this.start.nextSibling
-  var nodes = this.nodes = []
-  var scope = this.scope
-  var next
-  while (node !== this.end) {
-    nodes.push(node)
-    next = node.nextSibling
-    transition.remove(node, scope)
-    node = next
-  }
-  parent.removeChild(this.start)
-  parent.removeChild(this.end)
-  this.inserted = false
-}
-
-module.exports = MultiFragment

+ 0 - 46
src/fragment/single.js

@@ -1,46 +0,0 @@
-var _ = require('../util')
-var transition = require('../transition')
-
-/**
- * Single-node fragment, optimize insertion/removal for
- * single-node repeats.
- *
- * @param {Node} node
- * @param {Function} unlink
- * @param {Object} [scope]
- */
-
-function SingleFragment (node, unlink, scope) {
-  this.reused = false
-  this.node = node
-  node.__vfrag__ = this
-  this.scope = scope
-  this.unlink = unlink
-  this.inserted = false
-}
-
-/**
- * Insert fragment before target.
- *
- * @param {Node} target
- * @param {Boolean} trans
- */
-
-SingleFragment.prototype.before = function (target, trans) {
-  var method = trans !== false
-    ? transition.before
-    : _.before
-  method(this.node, target, this.scope)
-  this.inserted = true
-}
-
-/**
- * Remove fragment.
- */
-
-SingleFragment.prototype.remove = function () {
-  transition.remove(this.node, this.scope)
-  this.inserted = false
-}
-
-module.exports = SingleFragment

+ 0 - 33
src/transition/index.js

@@ -60,39 +60,6 @@ exports.removeThenAppend = function (el, target, vm, cb) {
   }, vm, cb)
   }, vm, cb)
 }
 }
 
 
-/**
- * Append the childNodes of a fragment to target.
- *
- * @param {DocumentFragment} block
- * @param {Node} target
- * @param {Vue} vm
- */
-
-exports.blockAppend = function (block, target, vm) {
-  var nodes = _.toArray(block.childNodes)
-  for (var i = 0, l = nodes.length; i < l; i++) {
-    exports.before(nodes[i], target, vm)
-  }
-}
-
-/**
- * Remove a block of nodes between two edge nodes.
- *
- * @param {Node} start
- * @param {Node} end
- * @param {Vue} vm
- */
-
-exports.blockRemove = function (start, end, vm) {
-  var node = start.nextSibling
-  var next
-  while (node !== end) {
-    next = node.nextSibling
-    exports.remove(node, vm)
-    node = next
-  }
-}
-
 /**
 /**
  * Apply transitions with an operation callback.
  * Apply transitions with an operation callback.
  *
  *