-
Notifications
You must be signed in to change notification settings - Fork 19
/
filter-groups.js
165 lines (153 loc) · 4.1 KB
/
filter-groups.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
/**
* FilterGroups class
*
* List of Group representation
* @params Nothing
* @return FilterGroups object
*/
var async = require( "async" );
var fs = require( "fs" );
const {app} = require( "electron" ).remote;
const path = require( "path" );
var config = {};
// Check if config.json exists in app data, otherwise create it from default
// config file.
console.log( "Loading config from " + app.getPath( "userData" ) + path.sep + "config.json" );
config = require( app.getPath( "userData" ) + path.sep + "config.json" );
class FilterGroups {
constructor( groupList ) {
this.groupList = groupList;
this.length = groupList.length;
this.sort();
}
/**
* Sort group list alphabetically
*
* @params Nothing
* @return Groups in place
*/
sort() {
this.groupList.sort( function( a, b ) {
var alc = a.name.toLowerCase();
var blc = b.name.toLowerCase();
if ( alc < blc ) {
return -1;
} else if ( alc > blc ) {
return 1;
} else {
return 0;
}
});
}
/**
* Activate/deactivate group
*
* @params Group id, callback
* @return Nothing
*/
toggle( id, callback ) {
var self = this;
async.each( this.groupList, function( group, cbFilter ) {
if ( group.id === id ) {
group.checked = group.checked === "checked" ? "" : "checked"
// console.log( "Toggled filter " + filter.item );
}
cbFilter();
}, function( err ) {
if ( err ) {
console.log( err );
}
callback();
});
}
/**
* Add a new group to the group list
*
* @params Group object to add
* @return Nothing
*/
add( group ) {
this.groupList.push( group );
this.length = this.groupList.length;
this.sort();
}
/**
* Remove a group from the group list
*
* @params The group id corresponding to the group to be removed, callback
* @return Nothing
*/
remove( groupId, callback ) {
var self = this;
var groups = [];
async.each( this.groupList, function( group, cbGroup ) {
if ( group.id !== groupId ) {
groups.push( group );
cbGroup();
} else {
cbGroup();
}
}, function( err ) {
if ( err ) {
console.log( err );
}
self.groupList = groups;
self.length = self.groupList.length;
self.sort();
callback();
});
}
/**
* Update group inside group list
*
* @params Group to find and replace
* @return Callback
*/
update( groupToFind, callback ) {
var self = this;
var groups = [];
async.each( this.groupList, function( group, cbGroup ) {
if ( group.id === groupToFind.id ) {
groups.push( groupToFind );
cbGroup();
} else {
groups.push( group );
cbGroup();
}
}, function( err ) {
if ( err ) {
console.log( err );
}
self.groupList = groups;
self.sort();
callback();
});
}
/**
* Return the group associated to input id
*
* @params Group ID
* @return Group object through callback
*/
find( groupId, callback ) {
var found = null;
async.each( this.groupList, function( group, cbGroup ) {
if ( group.id === groupId ) {
found = group;
}
cbGroup();
}, function() {
callback( found );
});
}
/**
* Write the group list to disk
*
* @params Nothing
* @return Nothing
*/
save() {
fs.writeFileSync( app.getPath( "userData" ) + path.sep + "filter-groups.json", JSON.stringify( this.groupList ));
}
}
module.exports = FilterGroups;