Add so-far-meaningless wrapper to 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 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         return that.OLD_pp2filter();
88     };
89
90     that.pp2limit = function(initial) {
91         var res = initial || "";
92
93         for (var i in m_list) {
94             var filter = m_list[i];
95             if (!filter.id) {
96                 if (res) res += ",";
97                 res += filter.field + "=" + filter.value.replace(/[\\|,]/g, '\\$&');
98             }
99         }
100
101         return res;
102     };
103
104
105     return that;
106 }
107
108
109 // Factory function for filters. These can be of several types.
110 function filter(id, name, field, value) {
111     var res;
112
113     if (id) {
114         res = { id: id, name: name };
115     } else {
116         res = { field: field, value: value };
117     }
118
119     return res;
120 }