Repository navigation
Expand file tree
/
Copy pathListWidget.js
More file actions
60 lines (48 loc) · 1.47 KB
/
Copy pathListWidget.js
File metadata and controls
60 lines (48 loc) · 1.47 KB
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
/** jToxKit - chem-informatics multi-tool-kit.
* A generic widget for list management
*
* Author: Ivan (Jonan) Georgiev
* Copyright © 2016, IDEAConsult Ltd. All rights reserved.
*/
jT.ListWidget = function (settings) {
a$.extend(true, this, a$.common(settings, this));
this.target = $(settings.target);
this.length = 0;
this.clearItems();
};
jT.ListWidget.prototype = {
itemId: "id",
populate: function (docs, callback) {
this.items = docs;
this.length = docs.length;
this.target.empty().hide(); // Hiding for performnance improvements.
for (var i = 0, l = docs.length; i < l; i++)
this.target.append(this.renderItem(typeof callback === "function" ? callback(docs[i]) : docs[i]));
this.target.show();
},
addItem: function (doc) {
this.items.push(doc);
++this.length;
return this.renderItem(doc);
},
clearItems: function () {
this.target.empty();
this.items = [];
this.length = 0;
},
findItem: function (id) {
var self = this;
return a$.findIndex(this.items, typeof id !== "string" ? id : function (doc) { return doc[self.itemId] === id; });
},
eraseItem: function (id) {
var i = this.findItem(id),
r = (i >= 0) ? this.items.splice(i, 1)[0] : false;
this.length = this.items.length;
return r;
},
enumerateItems: function (callback) {
var els = this.target.children();
for (var i = 0, l = this.items.length; i < l; ++i)
callback.call(els[i], this.items[i]);
}
}