Implement filterSet.targetFiltered function
[mkws-moved-to-github.git] / src / mkws-filter.js
1 // Factory function for sets of filters.
2 function filterSet() {
3     var that = {};
4     var m_list = [];
5
6     that.list = function() {
7         return m_list;
8     };
9
10     that.add = function(filter) {
11         m_list.push(filter);
12     };
13
14     that.removeMatching = function(matchFn) {
15         var newList = [];
16         for (var i in m_list) {
17             var filter = m_list[i];
18             if (matchFn(filter)) {
19                 log("removeMatching() removing filter " + $.toJSON(filter));
20             } else {
21                 log("removeMatching() keeping filter " + $.toJSON(filter));
22                 newList.push(filter);
23             }
24         }
25         m_list = newList;
26     };
27
28     that.targetFiltered = function(id) {
29         for (var i = 0; i < m_list.length; i++) {
30             if (m_list[i].id === id ||
31                 m_list[i].id === 'pz:id=' + id) {
32                 return true;
33             }
34         }
35         return false;
36     }
37
38     return that;
39 }
40
41
42 // Factory function for filters. These can be of several types.
43 function filter(id, name, field, value) {
44     var res;
45
46     if (id) {
47         res = { id: id, name: name };
48     } else {
49         res = { field: field, value: value };
50     }
51
52     return res;
53 }