|
@@ -1,25 +1,64 @@
|
|
|
var config = require('../config')
|
|
var config = require('../config')
|
|
|
|
|
|
|
|
-var proto = Array.prototype,
|
|
|
|
|
- slice = proto.slice,
|
|
|
|
|
- mutatorMethods = [
|
|
|
|
|
- 'pop',
|
|
|
|
|
- 'push',
|
|
|
|
|
- 'reverse',
|
|
|
|
|
- 'shift',
|
|
|
|
|
- 'unshift',
|
|
|
|
|
- 'splice',
|
|
|
|
|
- 'sort'
|
|
|
|
|
- ]
|
|
|
|
|
|
|
+var mutationHandlers = {
|
|
|
|
|
+ push: function (m) {
|
|
|
|
|
+ var self = this
|
|
|
|
|
+ m.args.forEach(function (data, i) {
|
|
|
|
|
+ var seed = self.buildItem(data, self.collection.length + i)
|
|
|
|
|
+ self.container.insertBefore(seed.el, self.marker)
|
|
|
|
|
+ })
|
|
|
|
|
+ },
|
|
|
|
|
+ pop: function (m) {
|
|
|
|
|
+ m.result.$destroy()
|
|
|
|
|
+ },
|
|
|
|
|
+ unshift: function (m) {
|
|
|
|
|
+ var self = this
|
|
|
|
|
+ m.args.forEach(function (data, i) {
|
|
|
|
|
+ var seed = self.buildItem(data, i)
|
|
|
|
|
+ self.container.insertBefore(seed.el, self.collection[m.args.length].$seed.el)
|
|
|
|
|
+ })
|
|
|
|
|
+ self.reorder()
|
|
|
|
|
+ },
|
|
|
|
|
+ shift: function (m) {
|
|
|
|
|
+ m.result.$destroy()
|
|
|
|
|
+ var self = this
|
|
|
|
|
+ self.reorder()
|
|
|
|
|
+ },
|
|
|
|
|
+ splice: function (m) {
|
|
|
|
|
+ var self = this
|
|
|
|
|
+ m.result.forEach(function (scope) {
|
|
|
|
|
+ scope.$destroy()
|
|
|
|
|
+ })
|
|
|
|
|
+ if (m.args.length > 2) {
|
|
|
|
|
+ m.args.slice(2).forEach(function (data, i) {
|
|
|
|
|
+ var seed = self.buildItem(data, i),
|
|
|
|
|
+ index = m.args[0] - m.args[1] + (m.args.length - 1),
|
|
|
|
|
+ ref = self.collection[index]
|
|
|
|
|
+ ? self.collection[index].$seed.el
|
|
|
|
|
+ : self.marker
|
|
|
|
|
+ self.container.insertBefore(seed.el, ref)
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+ self.reorder()
|
|
|
|
|
+ },
|
|
|
|
|
+ sort: function () {
|
|
|
|
|
+ var self = this
|
|
|
|
|
+ self.collection.forEach(function (scope, i) {
|
|
|
|
|
+ scope.$index = i
|
|
|
|
|
+ self.container.insertBefore(scope.$seed.el, self.marker)
|
|
|
|
|
+ })
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+mutationHandlers.reverse = mutationHandlers.sort
|
|
|
|
|
|
|
|
function watchArray (arr, callback) {
|
|
function watchArray (arr, callback) {
|
|
|
- mutatorMethods.forEach(function (method) {
|
|
|
|
|
|
|
+ Object.keys(mutationHandlers).forEach(function (method) {
|
|
|
arr[method] = function () {
|
|
arr[method] = function () {
|
|
|
- proto[method].apply(this, arguments)
|
|
|
|
|
|
|
+ var result = Array.prototype[method].apply(this, arguments)
|
|
|
callback({
|
|
callback({
|
|
|
- event: method,
|
|
|
|
|
- args: slice.call(arguments),
|
|
|
|
|
- array: arr
|
|
|
|
|
|
|
+ method: method,
|
|
|
|
|
+ args: Array.prototype.slice.call(arguments),
|
|
|
|
|
+ result: result
|
|
|
})
|
|
})
|
|
|
}
|
|
}
|
|
|
})
|
|
})
|
|
@@ -33,21 +72,23 @@ module.exports = {
|
|
|
this.marker = document.createComment('sd-each-' + this.arg)
|
|
this.marker = document.createComment('sd-each-' + this.arg)
|
|
|
ctn.insertBefore(this.marker, this.el)
|
|
ctn.insertBefore(this.marker, this.el)
|
|
|
ctn.removeChild(this.el)
|
|
ctn.removeChild(this.el)
|
|
|
- this.childSeeds = []
|
|
|
|
|
},
|
|
},
|
|
|
|
|
|
|
|
update: function (collection) {
|
|
update: function (collection) {
|
|
|
this.unbind(true)
|
|
this.unbind(true)
|
|
|
- this.childSeeds = []
|
|
|
|
|
if (!Array.isArray(collection)) return
|
|
if (!Array.isArray(collection)) return
|
|
|
- watchArray(collection, this.mutate.bind(this))
|
|
|
|
|
|
|
+ this.collection = collection
|
|
|
var self = this
|
|
var self = this
|
|
|
- collection.forEach(function (item, i) {
|
|
|
|
|
- self.childSeeds.push(self.buildItem(item, i, collection))
|
|
|
|
|
|
|
+ watchArray(collection, function (mutation) {
|
|
|
|
|
+ mutationHandlers[mutation.method].call(self, mutation)
|
|
|
|
|
+ })
|
|
|
|
|
+ collection.forEach(function (data, i) {
|
|
|
|
|
+ var seed = self.buildItem(data, i)
|
|
|
|
|
+ self.container.insertBefore(seed.el, self.marker)
|
|
|
})
|
|
})
|
|
|
},
|
|
},
|
|
|
|
|
|
|
|
- buildItem: function (data, index, collection) {
|
|
|
|
|
|
|
+ buildItem: function (data, index) {
|
|
|
var Seed = require('../seed'),
|
|
var Seed = require('../seed'),
|
|
|
node = this.el.cloneNode(true)
|
|
node = this.el.cloneNode(true)
|
|
|
var spore = new Seed(node, {
|
|
var spore = new Seed(node, {
|
|
@@ -57,21 +98,23 @@ module.exports = {
|
|
|
index: index,
|
|
index: index,
|
|
|
data: data
|
|
data: data
|
|
|
})
|
|
})
|
|
|
- this.container.insertBefore(node, this.marker)
|
|
|
|
|
- collection[index] = spore.scope
|
|
|
|
|
|
|
+ this.collection[index] = spore.scope
|
|
|
return spore
|
|
return spore
|
|
|
},
|
|
},
|
|
|
|
|
|
|
|
- mutate: function (mutation) {
|
|
|
|
|
- console.log(mutation)
|
|
|
|
|
|
|
+ reorder: function () {
|
|
|
|
|
+ this.collection.forEach(function (scope, i) {
|
|
|
|
|
+ scope.$index = i
|
|
|
|
|
+ })
|
|
|
},
|
|
},
|
|
|
|
|
|
|
|
unbind: function (rm) {
|
|
unbind: function (rm) {
|
|
|
- if (this.childSeeds.length) {
|
|
|
|
|
|
|
+ if (this.collection && this.collection.length) {
|
|
|
var fn = rm ? '_destroy' : '_unbind'
|
|
var fn = rm ? '_destroy' : '_unbind'
|
|
|
- this.childSeeds.forEach(function (child) {
|
|
|
|
|
- child[fn]()
|
|
|
|
|
|
|
+ this.collection.forEach(function (scope) {
|
|
|
|
|
+ scope.$seed[fn]()
|
|
|
})
|
|
})
|
|
|
|
|
+ this.collection = null
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|