forked from javve/list.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
268 lines (241 loc) · 6.34 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
(function( window, undefined ) {
"use strict";
var document = window.document,
getByClass = require('./src/utils/get-by-class'),
extend = require('./src/utils/extend'),
indexOf = require('./src/utils/index-of'),
events = require('./src/utils/events'),
toString = require('./src/utils/to-string'),
naturalSort = require('./src/utils/natural-sort'),
classes = require('./src/utils/classes'),
getAttribute = require('./src/utils/get-attribute'),
toArray = require('./src/utils/to-array');
var List = function(id, options, values) {
var self = this,
init,
Item = require('./src/item')(self),
addAsync = require('./src/add-async')(self);
init = {
start: function() {
self.listClass = "list";
self.searchClass = "search";
self.sortClass = "sort";
self.page = 10000;
self.i = 1;
self.items = [];
self.visibleItems = [];
self.matchingItems = [];
self.searched = false;
self.filtered = false;
self.searchColumns = undefined;
self.handlers = { 'updated': [] };
self.plugins = {};
self.valueNames = [];
self.utils = {
getByClass: getByClass,
extend: extend,
indexOf: indexOf,
events: events,
toString: toString,
naturalSort: naturalSort,
classes: classes,
getAttribute: getAttribute,
toArray: toArray
};
self.utils.extend(self, options);
self.listContainer = (typeof(id) === 'string') ? document.getElementById(id) : id;
if (!self.listContainer) { return; }
self.list = getByClass(self.listContainer, self.listClass, true);
self.parse = require('./src/parse')(self);
self.templater = require('./src/templater')(self);
self.search = require('./src/search')(self);
self.filter = require('./src/filter')(self);
self.sort = require('./src/sort')(self);
this.handlers();
this.items();
self.update();
this.plugins();
},
handlers: function() {
for (var handler in self.handlers) {
if (self[handler]) {
self.on(handler, self[handler]);
}
}
},
items: function() {
self.parse(self.list);
if (values !== undefined) {
self.add(values);
}
},
plugins: function() {
for (var i = 0; i < self.plugins.length; i++) {
var plugin = self.plugins[i];
self[plugin.name] = plugin;
plugin.init(self, List);
}
}
};
/*
* Re-parse the List, use if html have changed
*/
this.reIndex = function() {
self.items = [];
self.visibleItems = [];
self.matchingItems = [];
self.searched = false;
self.filtered = false;
self.parse(self.list);
};
this.toJSON = function() {
var json = [];
for (var i = 0, il = self.items.length; i < il; i++) {
json.push(self.items[i].values());
}
return json;
};
/*
* Add object to list
*/
this.add = function(values, callback) {
if (values.length === 0) {
return;
}
if (callback) {
addAsync(values, callback);
return;
}
var added = [],
notCreate = false;
if (values[0] === undefined){
values = [values];
}
for (var i = 0, il = values.length; i < il; i++) {
var item = null;
notCreate = (self.items.length > self.page) ? true : false;
item = new Item(values[i], undefined, notCreate);
self.items.push(item);
added.push(item);
}
self.update();
return added;
};
this.show = function(i, page) {
this.i = i;
this.page = page;
self.update();
return self;
};
/* Removes object from list.
* Loops through the list and removes objects where
* property "valuename" === value
*/
this.remove = function(valueName, value, options) {
var found = 0;
for (var i = 0, il = self.items.length; i < il; i++) {
if (self.items[i].values()[valueName] == value) {
self.templater.remove(self.items[i], options);
self.items.splice(i,1);
il--;
i--;
found++;
}
}
self.update();
return found;
};
/* Gets the objects in the list which
* property "valueName" === value
*/
this.get = function(valueName, value) {
var matchedItems = [];
for (var i = 0, il = self.items.length; i < il; i++) {
var item = self.items[i];
if (item.values()[valueName] == value) {
matchedItems.push(item);
}
}
return matchedItems;
};
/*
* Get size of the list
*/
this.size = function() {
return self.items.length;
};
/*
* Removes all items from the list
*/
this.clear = function() {
self.templater.clear();
self.items = [];
return self;
};
this.on = function(event, callback) {
self.handlers[event].push(callback);
return self;
};
this.off = function(event, callback) {
var e = self.handlers[event];
var index = indexOf(e, callback);
if (index > -1) {
e.splice(index, 1);
}
return self;
};
this.trigger = function(event) {
var i = self.handlers[event].length;
while(i--) {
self.handlers[event][i](self);
}
return self;
};
this.reset = {
filter: function() {
var is = self.items,
il = is.length;
while (il--) {
is[il].filtered = false;
}
return self;
},
search: function() {
var is = self.items,
il = is.length;
while (il--) {
is[il].found = false;
}
return self;
}
};
this.update = function() {
var is = self.items,
il = is.length;
self.visibleItems = [];
self.matchingItems = [];
self.templater.clear();
for (var i = 0; i < il; i++) {
if (is[i].matching() && ((self.matchingItems.length+1) >= self.i && self.visibleItems.length < self.page)) {
is[i].show();
self.visibleItems.push(is[i]);
self.matchingItems.push(is[i]);
} else if (is[i].matching()) {
self.matchingItems.push(is[i]);
is[i].hide();
} else {
is[i].hide();
}
}
self.trigger('updated');
return self;
};
init.start();
};
// AMD support
if (typeof define === 'function' && define.amd) {
define(function () { return List; });
}
module.exports = List;
window.List = List;
})(window);