-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
108 lines (96 loc) · 2.42 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
var observer = require('array-observer');
var each = require('each');
/**
* Export the plugin. It takes a view property key and a
* View for child elements. When the view is created a list
* will be made for the array at that key.
*
* @param {String} key
* @param {View} Item
*
* @return {Function}
*/
module.exports = exports = function() {
return function(View) {
View.on('created', function(view){
each(View.attrs, function(name, options) {
if (!options.map) return;
var List = exports.list(options.view);
var list = new List(view[options.map]);
view.on('destroying', list.destroy.bind(list));
view[name] = list;
});
});
};
};
/**
* Create a new List contructor that
* can be used to create new lists using
* a certain type of view.
*
* @param {View} View
*
* @return {Function}
*/
exports.list = function(View) {
function List(items) {
this.el = document.createDocumentFragment();
this.observer = observer(items);
this.observer.on('add', this.add.bind(this));
this.observer.on('remove', this.remove.bind(this));
this.observer.on('sort', this.render.bind(this));
this.items = [];
this.source = items;
items.forEach(this.add.bind(this));
}
List.prototype.at = function(index) {
return this.items[index];
};
List.prototype.add = function(obj, index) {
var item;
if (Object(obj) !== obj) {
item = new View({ value: obj });
}
else {
item = new View(obj);
}
if (index < this.el.children.length) {
this.items.splice(index, 0, item);
item.before(this.el.children[index]);
}
else {
this.items.push(item);
item.appendTo(this.el);
}
return this;
};
List.prototype.remove = function(index) {
var items = this.items.splice(index, 1);
items[0].remove();
return items[0];
};
List.prototype.render = function() {
var el = this.el;
var items = this.items;
this.source.forEach(function(obj){
items.some(function(item){
if (item.attrs === obj) {
item.appendTo(el);
return true;
}
});
});
};
List.prototype.length = function() {
return this.items.length;
};
List.prototype.destroy = function() {
this.el.parentNode.removeChild(this.el);
this.items.forEach(function(item){
item.destroy();
});
this.observer.dispose();
this.items = null;
};
return List;
};