multi.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. var _ = require('../util')
  2. var transition = require('../transition')
  3. /**
  4. * Multi-node fragment that has a start and an end node.
  5. *
  6. * @param {Node} node
  7. * @param {Function} unlink
  8. * @param {Object} [scope]
  9. */
  10. function MultiFragment (frag, unlink, scope) {
  11. this.start = this.node = _.createAnchor('fragment-start')
  12. this.end = _.createAnchor('fragment-end')
  13. this.node.__vfrag__ = this
  14. this.reused = false
  15. this.inserted = false
  16. this.nodes = _.toArray(frag.childNodes)
  17. this.scope = scope
  18. this.unlink = unlink
  19. }
  20. /**
  21. * Insert fragment before target.
  22. *
  23. * @param {Node} target
  24. * @param {Boolean} trans
  25. */
  26. MultiFragment.prototype.before = function (target, trans) {
  27. _.before(this.start, target)
  28. var nodes = this.nodes
  29. var scope = this.scope
  30. var method = trans !== false
  31. ? transition.before
  32. : _.before
  33. for (var i = 0, l = nodes.length; i < l; i++) {
  34. method(nodes[i], target, scope)
  35. }
  36. _.before(this.end, target)
  37. this.inserted = true
  38. }
  39. /**
  40. * Remove fragment.
  41. */
  42. MultiFragment.prototype.remove = function () {
  43. var parent = this.start.parentNode
  44. var node = this.start.nextSibling
  45. var nodes = this.nodes = []
  46. var scope = this.scope
  47. var next
  48. while (node !== this.end) {
  49. nodes.push(node)
  50. next = node.nextSibling
  51. transition.remove(node, scope)
  52. node = next
  53. }
  54. parent.removeChild(this.start)
  55. parent.removeChild(this.end)
  56. this.inserted = false
  57. }
  58. /**
  59. * Destroy fragment.
  60. */
  61. MultiFragment.prototype.destroy = function () {
  62. this.remove()
  63. this.unlink()
  64. }
  65. module.exports = MultiFragment