New implementation of pp2limit method based on visitFields.
[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 OLD = that.OLD_pp2limit(initial);
76         var NEW = that.NEW_pp2limit(initial);
77         if (OLD !== NEW) {
78             alert("pp2limit(): OLD[" + OLD + "] !== NEW[" + NEW + "]");
79         }
80         return OLD;
81     };
82
83     that.OLD_pp2limit = function(initial) {
84         var res = initial || "";
85
86         for (var i in m_list) {
87             var filter = m_list[i];
88             if (!filter.id) {
89                 if (res) res += ",";
90                 res += filter.field + "=" + filter.value.replace(/[\\|,]/g, '\\$&');
91             }
92         }
93
94         return res;
95     };
96
97     that.NEW_pp2limit = function(initial) {
98         var res = initial || "";
99
100         that.visitFields(function(field, value) {
101             if (res) res += ",";
102             res += field + "=" + value.replace(/[\\|,]/g, '\\$&');
103         });
104         return res;
105     }
106
107     return that;
108 }
109
110
111 // Factory function for filters. These can be of several types.
112 function filter(id, name, field, value) {
113     var res;
114
115     if (id) {
116         res = { id: id, name: name };
117     } else {
118         res = { field: field, value: value };
119     }
120
121     return res;
122 }