* nodes page looks good, going generic
[plewww.git] / planetlab / js / plc_filter.js
1 /* $Id$ */
2
3 /* set or clear the ' invisibleRow' in the tr's classname, according to visible */
4 function plc_table_row_visible (row,visible) {
5   var cn=row.className;
6   /* clear */
7   cn=cn.replace(" invisibleRow","");
8   if (! visible) cn += " invisibleRow";
9   row.className=cn;
10 }
11
12 /* maintain the number of matching entries in the <tbody> element's classname */
13 function plc_table_tbody_matching (tbody, matching) {
14   var new_cn="matching-" + matching;
15   var cn=tbody.className;
16   if (cn.match("matching-")) {
17     cn=cn.replace(/matching-\d+/,new_cn);
18   } else {
19     cn=cn + " " + new_cn;
20   }
21   cn=cn.replace(/^ +/,"");
22   tbody.className=cn;
23 }
24
25 /* scan the table, and mark as visible 
26    the rows that match (either AND or OR the patterns) */
27 function plc_table_filter (table_id,pattern_id,and_id) {
28   var tbody = document.getElementById(table_id).getElementsByTagName("tbody")[0];
29   var rows=tbody.rows;
30   var pattern_area = document.getElementById(pattern_id);
31   var pattern_text = pattern_area.value;
32   var row_index, row, cells, cell_index, cell, visible;
33   var pattern,i;
34   var matching_entries=0;
35   var and_button=document.getElementById(and_id);
36   var and_if_true=and_button.checked;
37
38   
39   // remove whitespaces at the beginning and end
40   pattern_text = pattern_text.replace(/[ \t]+$/,"");
41   pattern_text = pattern_text.replace(/^[ \t]+/,"");
42   
43   if (pattern_text.indexOf ("&") != -1) {
44     pattern_text = pattern_text.replace(/&$/,"").replace(/&/," ");
45     pattern_area.value=pattern_text;
46     and_button.checked=true;
47     return;
48   } else if (pattern_text.indexOf ("|") != -1 ) {
49     pattern_text = pattern_text.replace(/\|$/,"").replace(/\|/," ");
50     pattern_area.value=pattern_text;
51     and_button.checked=false;
52     return;
53   }
54     
55   var patterns = pattern_text.split(" ");
56
57   for (row_index = 0; row=rows[row_index]; row_index++) {
58       cells=row.cells;
59     
60     /*empty pattern */
61     if (patterns.length == 0) {
62       visible=true;
63     } else if (and_if_true) {
64       /* AND mode: all patterns must match */
65       visible=true;
66       for (i in patterns) {
67         var pattern_matched=false;
68         pattern=patterns[i];
69         for (cell_index = 0; cell=cells[cell_index]; cell_index++) {
70           if ( cell.innerHTML.match(pattern)) pattern_matched=true;
71         }
72         if ( ! pattern_matched ) visible=false;
73       }
74     } else {
75       /* OR mode: any match is good enough */
76       visible=false;
77       for (cell_index = 0; cell=cells[cell_index]; cell_index++) {
78         for (i in patterns) {
79           pattern=patterns[i];
80           if (cell.innerHTML.match(pattern)) visible=true;
81         }
82       }
83     }
84     plc_table_row_visible(row,visible);
85     if (visible) matching_entries +=1;
86   }
87   plc_table_tbody_matching(tbody,matching_entries);
88   tablePaginater.init(table_id);
89 }
90
91 function plc_table_filter_reset (table_id, pattern_id) {
92   /* reset pattern */
93   document.getElementById(pattern_id).value="";
94   plc_table_filter (table_id, pattern_id);
95 }