-
Notifications
You must be signed in to change notification settings - Fork 19
/
filters.js
214 lines (201 loc) · 6.06 KB
/
filters.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
/* jshint node: true */
/* jshint jquery: true */
/* jshint esversion: 6 */
"use strict";
/**
* Filters class
*
* List of Filter representation
* @param Nothing
* @return Filters 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 Filters {
constructor( filterList ) {
this.filterList = filterList;
this.length = filterList.length;
this.sort();
}
/**
* Sort filter list alphabetically
*
* @param Nothing
* @return Filters in place
*/
sort() {
this.filterList.sort( function( a, b ) {
var alc = a.item.toLowerCase();
var blc = b.item.toLowerCase();
// If both filters are not in groups or both filters are in group
if ((( !alc.group || alc.group === "" ) && ( !blc.group || blc.group === "" )) ||
( alc.group !== "" && blc.group !== "" )) {
if ( alc < blc ) {
return -1;
} else if ( alc > blc ) {
return 1;
} else {
return 0;
}
// If only filter a has a group, order a first
} else if ( alc.group !== "" ) {
return 1;
// Otherwise if only b has a group, order b first
} else {
return -1;
}
});
}
/**
* Activate/deactivate filter
*
* @param Filter id, callback
* @return Nothing
*/
toggle( id, callback ) {
var self = this;
async.each( this.filterList, function( filter, cbFilter ) {
if ( filter.id === id ) {
filter.active = !filter.active;
// console.log( "Toggled filter " + filter.item );
}
cbFilter();
}, function( err ) {
if ( err ) {
console.log( err );
}
callback();
});
}
/**
* Find the sorted index of a specific filter in the list
*
* @param Filter to be found
* @return Index through callback
*/
findFilterIndex( filter, callback ) {
var self = this;
var index = 0;
var found = false;
// If filter is not in a group we have to take into account
// grouped filters which are ordered first
if ( filter.group === "" ) {
// Count all grouped filters
async.each( this.filterList, function( f, cbFilter ) {
if ( f.group !== "" ) {
index++;
}
cbFilter();
}, function() {
// console.log( "counted " + index + " filters in groups" );
async.eachLimit( self.filterList, 1, function( f, cbFilter ) {
if ( f.group === "" && !found && JSON.stringify( filter ) === JSON.stringify( f )) {
found = true;
}
if ( !found && f.group === "" ) {
console.log( f.item );
index++;
}
cbFilter();
}, function() {
callback({ found: found, index: index });
});
});
} else {
async.each( this.filterList, function( f, cbFilter ) {
if ( !found && JSON.stringify( filter ) === JSON.stringify( f )) {
found = true;
}
if ( !found ) {
index++;
}
cbFilter();
}, function( err ) {
if ( err ) {
console.log( err );
}
callback({ found: found, index: index });
});
}
}
/**
* Add a new filter to the filter list
*
* @param Filter object to add
* @return Nothing
*/
add( filter ) {
this.filterList.push( filter );
this.length = this.filterList.length;
this.sort();
}
/**
* Remove a filter from the filter list
*
* @param The filter id corresponding to the filter to be removed, callback
* @return Nothing
*/
remove( filterId, callback ) {
var self = this;
var filters = [];
async.each( this.filterList, function( filter, cbFilter ) {
if ( filter.id !== filterId ) {
filters.push( filter );
cbFilter();
} else {
cbFilter();
}
}, function( err ) {
if ( err ) {
console.log( err );
}
self.filterList = filters;
self.length = filterList.length;
self.sort();
callback();
});
}
/**
* Update filter inside filter list
*
* @param Filter to find and replace
* @return Callback
*/
update( filterToFind, callback ) {
var self = this;
var filters = [];
async.each( this.filterList, function( filter, cbFilter ) {
if ( filter.id === filterToFind.id ) {
filters.push( filterToFind );
cbFilter();
} else {
filters.push( filter );
cbFilter();
}
}, function( err ) {
if ( err ) {
console.log( err );
}
self.filterList = filters;
self.sort();
callback();
});
}
/**
* Write the filter list to disk
*
* @param Nothing
* @return Nothing
*/
save() {
fs.writeFileSync( app.getPath( "userData" ) + path.sep + "filters.json", JSON.stringify( this.filterList ));
}
}
module.exports = Filters;