Remove no-longer needed OLD-vs-NEW scaffolding around pp2filter.
[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.id) {
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.id) {
29                 callback(filter.field, filter.value);
30             }
31         }
32     };
33
34     that.removeMatching = function(matchFn) {
35         var newList = [];
36         for (var i in m_list) {
37             var filter = m_list[i];
38             if (matchFn(filter)) {
39                 m_team.log("removeMatching() removing filter " + $.toJSON(filter));
40             } else {
41                 m_team.log("removeMatching() keeping filter " + $.toJSON(filter));
42                 newList.push(filter);
43             }
44         }
45         m_list = newList;
46     };
47
48     that.targetFiltered = function(id) {
49         for (var i = 0; i < m_list.length; i++) {
50             if (m_list[i].id === id ||
51                 m_list[i].id === 'pz:id=' + id) {
52                 return true;
53             }
54         }
55         return false;
56     };
57
58     that.pp2filter = function() {
59         var res = "";
60
61         that.visitTargets(function(id, name) {
62             if (res) res += ",";
63             if (id.match(/^[a-z:]+[=~]/)) {
64                 m_team.log("filter '" + id + "' already begins with SETTING OP");
65             } else {
66                 id = 'pz:id=' + id;
67             }
68             res += id;
69         });
70
71         return res;
72     };
73
74     that.pp2limit = function(initial) {
75         var res = initial || "";
76
77         for (var i in m_list) {
78             var filter = m_list[i];
79             if (!filter.id) {
80                 if (res) res += ",";
81                 res += filter.field + "=" + filter.value.replace(/[\\|,]/g, '\\$&');
82             }
83         }
84
85         return res;
86     };
87
88
89     return that;
90 }
91
92
93 // Factory function for filters. These can be of several types.
94 function filter(id, name, field, value) {
95     var res;
96
97     if (id) {
98         res = { id: id, name: name };
99     } else {
100         res = { field: field, value: value };
101     }
102
103     return res;
104 }