-
-
Notifications
You must be signed in to change notification settings - Fork 415
/
filter.js
136 lines (121 loc) · 3.32 KB
/
filter.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
// Minimum number of entries to allow filtering
var minFilterEntries = 3;
// Filter popup
enyo.kind({
name: "Abcd.FilterPopup",
kind: "onyx.Popup",
classes: "filter-popup",
centered: true,
modal: true,
floating: true,
published: {
filter: null,
},
events: {
onFilterChanged: ""
},
components: [
{name: "box", classes: "filterBox", components: [
]},
{name: "trash", kind: "Image", src: "images/trashcan.png", classes: "standardButton trashButton", ontap: "trashTaped"},
],
// Constructor
create: function() {
this.inherited(arguments);
this.filterChanged();
},
rendered: function() {
this.displayThemes();
},
filterChanged: function() {
this.render();
},
// Display themes and letters
displayThemes: function() {
// Clean all
this.cleanBox();
// Display themes
var length = Abcd.themes.length;
var theme = -1;
if (this.filter != null && this.filter.kind == "Abcd.Collection")
theme = Abcd.collections[this.filter.index].theme;
this.$.box.createComponent({ classes: "linebreak" }).render();
for (var i = 0 ; i < length ; i++) {
this.$.box.createComponent(
{ kind: "Abcd.Theme", index: i, selected: (i == theme), ontap: "displayCollections" },
{ owner: this }
).render();
}
// Display letters
this.$.box.createComponent({ classes: "linebreak" }).render();
this.$.box.createComponent({ classes: "linebreak" }).render();
for (var i = 0 ; i < 26 ; i++) {
var letter = String.fromCharCode(65+i).toLowerCase();
this.$.box.createComponent(
{ kind: "Abcd.Letter", letter: letter,
selected: (this.filter != null&&this.filter.letter == letter), ontap: "filterOnLetter" },
{ owner: this }
).render();
}
},
// Display filter, recompute each time
display: function(filter) {
this.filter = filter;
this.render();
this.show();
},
// Display all collection
displayCollections: function(inSender, inEvent) {
var theme = inSender.index;
var length = Abcd.collections.length;
this.cleanBox();
for (var i = 0 ; i < length ; i++) {
if (Abcd.collections[i].theme != theme) continue;
this.$.box.createComponent({ kind: "Abcd.Collection", index: i,
selected: (this.filter != null&&this.filter.index == i),
ontap: "filterOnCollection"}, {owner: this}).render();
}
},
// Trash taped, remove filter
trashTaped: function() {
this.hide();
this.filter = null;
this.doFilterChanged();
},
// Tap on a letter, filter on this letter
filterOnLetter: function(l, e) {
if (!Abcd.letters.hasOwnProperty(l.letter) || Abcd.letters[l.letter].length < minFilterEntries)
return;
this.hide();
this.filter = l;
this.doFilterChanged();
},
// Tap on a collection, filter on this collection
filterOnCollection: function(c, e) {
var index = c.index;
var collection = Abcd.collections[index];
var length = collection.entries.length;
var count = 0;
for (var i = 0 ; i < length ; i++) {
var entry = collection.entries[i];
if (Abcd.entries[entry][Abcd.context.lang] == 1)
count++;
}
if (count < minFilterEntries)
return;
this.hide();
this.filter = c;
this.doFilterChanged();
},
cleanBox: function() {
// Delete all
var items = [];
enyo.forEach(this.$.box.getControls(), function(item) {
items.push(item);
});
for (var i = 0 ; i < items.length ; i++) {
items[i].destroy();
}
}
});