02ce4223f71ad244099ef94cc138bc2a3bb6e8b9
[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 OLD = that.OLD_pp2filter();
60         var NEW = that.NEW_pp2filter();
61         if (OLD !== NEW) {
62             alert("pp2filter(): OLD[" + OLD + "] !== NEW[" + NEW + "]");
63         }
64         return OLD;
65     };
66
67     that.OLD_pp2filter = function() {
68         var res = "";
69
70         for (var i in m_list) {
71             var filter = m_list[i];
72             if (filter.id) {
73                 if (res) res += ",";
74                 if (filter.id.match(/^[a-z:]+[=~]/)) {
75                     m_team.log("filter '" + filter.id + "' already begins with SETTING OP");
76                 } else {
77                     filter.id = 'pz:id=' + filter.id;
78                 }
79                 res += filter.id;
80             }
81         }
82
83         return res;
84     };
85
86     that.NEW_pp2filter = function() {
87         var res = "";
88
89         that.visitTargets(function(id, name) {
90             if (res) res += ",";
91             if (id.match(/^[a-z:]+[=~]/)) {
92                 m_team.log("filter '" + id + "' already begins with SETTING OP");
93             } else {
94                 id = 'pz:id=' + id;
95             }
96             res += id;
97         });
98
99         return res;
100     };
101
102     that.pp2limit = function(initial) {
103         var res = initial || "";
104
105         for (var i in m_list) {
106             var filter = m_list[i];
107             if (!filter.id) {
108                 if (res) res += ",";
109                 res += filter.field + "=" + filter.value.replace(/[\\|,]/g, '\\$&');
110             }
111         }
112
113         return res;
114     };
115
116
117     return that;
118 }
119
120
121 // Factory function for filters. These can be of several types.
122 function filter(id, name, field, value) {
123     var res;
124
125     if (id) {
126         res = { id: id, name: name };
127     } else {
128         res = { field: field, value: value };
129     }
130
131     return res;
132 }