/
TagFilter.js
58 lines (47 loc) · 1.15 KB
/
TagFilter.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
define(['underscore', 'ko'], function (_, ko) {
var TagState = {
off: -1,
'default': 0,
on: 1
};
function TagFilter(label) {
this.label = label;
this.state = ko.observable(TagState['default']);
}
TagFilter.prototype.getSymbol = function () {
switch (this.state()) {
case TagState.on:
return '✓';
case TagState.off:
return '✗';
default:
return ' ';
}
};
TagFilter.prototype.isActive = function () {
return this.state() !== TagState['default'];
};
TagFilter.prototype.toggle = function () {
var next = this.state() === TagState.on ? TagState.off : this.state()+1;
this.state(next);
};
TagFilter.getOff = function (tagFilters) {
return withState(tagFilters, TagState.off);
};
TagFilter.getOn = function (tagFilters) {
return withState(tagFilters, TagState.on);
};
TagFilter.resetAll = function (tagFilters) {
_.each(tagFilters, function (tagFilter) {
tagFilter.state(TagState['default']);
});
};
function withState(tagFilters, state) {
return _.compact(_.map(tagFilters, function (tagFilter) {
if (tagFilter.state() === state) {
return tagFilter.label;
}
}));
}
return TagFilter;
});