Add visitCategories function
[mkws-moved-to-github.git] / src / mkws-filter.js
1 // Factory function for sets of filters.
2 function filterSet(team) {
3     var m_team = team;
4     var m_list = [];
5
6     var that = {};
7
8     that.toJSON = function() {
9         return $.toJSON(m_list);
10     };
11
12     that.add = function(filter) {
13         m_list.push(filter);
14     };
15
16     that.visitTargets = function(callback) {
17         for (var i in m_list) {
18             var filter = m_list[i];
19             if (filter.type === 'target') {
20                 callback(filter.id, filter.name);
21             }
22         }
23     };
24
25     that.visitFields = function(callback) {
26         for (var i in m_list) {
27             var filter = m_list[i];
28             if (filter.type === 'field') {
29                 callback(filter.field, filter.value);
30             }
31         }
32     };
33
34     that.visitCategories = function(callback) {
35         for (var i in m_list) {
36             var filter = m_list[i];
37             if (filter.type === 'category') {
38                 callback(filter.id);
39             }
40         }
41     };
42
43     that.removeMatching = function(matchFn) {
44         var newList = [];
45         for (var i in m_list) {
46             var filter = m_list[i];
47             if (matchFn(filter)) {
48                 m_team.log("removeMatching() removing filter " + $.toJSON(filter));
49             } else {
50                 m_team.log("removeMatching() keeping filter " + $.toJSON(filter));
51                 newList.push(filter);
52             }
53         }
54         m_list = newList;
55     };
56
57     that.targetFiltered = function(id) {
58         for (var i = 0; i < m_list.length; i++) {
59             if (m_list[i].type === 'target' ||
60                 m_list[i].id === 'pz:id=' + id) {
61                 return true;
62             }
63         }
64         return false;
65     };
66
67     that.pp2filter = function() {
68         var res = "";
69
70         that.visitTargets(function(id, name) {
71             if (res) res += ",";
72             if (id.match(/^[a-z:]+[=~]/)) {
73                 m_team.log("filter '" + id + "' already begins with SETTING OP");
74             } else {
75                 id = 'pz:id=' + id;
76             }
77             res += id;
78         });
79
80         return res;
81     };
82
83     that.pp2limit = function(initial) {
84         var res = initial || "";
85
86         that.visitFields(function(field, value) {
87             if (res) res += ",";
88             res += field + "=" + value.replace(/[\\|,]/g, '\\$&');
89         });
90         return res;
91     }
92
93     that.pp2catLimit = function() {
94         var res = "";
95
96         that.visitCategories(function(id) {
97             if (res) res += ",";
98             res += "category~" + id.replace(/[\\|,]/g, '\\$&');
99         });
100         return res;
101     }
102
103     return that;
104 }
105
106
107 // Factory functions for filters. These can be of several types.
108 function targetFilter(id, name) {
109     return {
110         type: 'target',
111         id: id,
112         name: name
113     };
114 }
115
116 function fieldFilter(field, value) {
117     return {
118         type: 'field',
119         field: field,
120         value: value
121     };
122 }
123
124 function categoryFilter(id) {
125     return {
126         type: 'category',
127         id: id,
128     };
129 }