1 // Factory function for sets of filters.
2 function filterSet(team) {
8 that.toJSON = function() {
9 return mkws.$.toJSON(m_list);
12 that.add = function(filter) {
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);
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);
34 that.visitCategories = function(callback) {
35 for (var i in m_list) {
36 var filter = m_list[i];
37 if (filter.type === 'category') {
43 that.removeMatching = function(matchFn) {
45 for (var i in m_list) {
46 var filter = m_list[i];
47 if (matchFn(filter)) {
48 m_team.info("removeMatching: removing filter " + mkws.$.toJSON(filter));
50 m_team.info("removeMatching: keeping filter " + mkws.$.toJSON(filter));
57 // ### Surely the || in this function should be &&
58 that.targetFiltered = function(id) {
59 for (var i = 0; i < m_list.length; i++) {
60 if (m_list[i].type === 'target' ||
61 m_list[i].id === 'pz:id=' + id) {
68 that.pp2filter = function() {
71 that.visitTargets(function(id, name) {
73 if (id.match(/^[a-z:]+[=~]/)) {
74 m_team.info("filter '" + id + "' already begins with SETTING OP");
84 that.pp2limit = function(initial) {
85 var res = initial || "";
87 that.visitFields(function(field, value) {
89 res += field + "=" + value.replace(/[\\|,]/g, '\\$&');
94 that.pp2catLimit = function() {
97 that.visitCategories(function(id) {
99 res += "category~" + id.replace(/[\\,]/g, '\\$&');
104 // Returns a hash of key=value pairs representing the filter-set
105 // These will become part of the URL-fragment representing the state
106 that.fragmentItems = function() {
109 for (var i in m_list) {
110 var filter = m_list[i];
111 var type = filter.type;
112 if (type === 'target') {
113 hash['xt-' + filter.id] = filter.name;
114 } else if (type === 'field') {
115 // Ugly names, but we need to include the value because fields can be repeated
116 hash['xf-' + filter.field + '-' + filter.value] = 1;
117 } else if (type === 'category') {
118 hash['xc-' + filter.id] = 1;
120 alert("unsupported filter-type '" + type + "'");
131 // Factory functions for filters. These can be of several types.
132 function targetFilter(id, name) {
140 function fieldFilter(field, value) {
148 function categoryFilter(id) {