checkpoint
[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 the rows that have at least one cell that contains the pattern */
26 function plc_table_filter (table_id,pattern_id) {
27   var tbody = document.getElementById(table_id).getElementsByTagName("tbody")[0];
28   var rows=tbody.rows;
29   var pattern_text = document.getElementById(pattern_id).value;
30   var row_index, row, cells, cell_index, cell, visible;
31   var pattern,i;
32   var matching_entries=0;
33   
34   // remove whitespaces at the beginning and end
35   pattern_text = pattern_text.replace(/[ \t]+$/,"");
36   pattern_text = pattern_text.replace(/^[ \t]+/,"");
37   
38   var patterns = pattern_text.split(" ");
39
40   for (row_index = 0; row=rows[row_index]; row_index++) {
41     
42     /* xxx deal with empty patterns and whitespaces */
43     if (patterns.length == 0) {
44       visible=true;
45     } else {
46       visible=false;
47       cells=row.cells;
48       for (cell_index = 0; cell=cells[cell_index]; cell_index++) {
49         for (i in patterns) {
50           pattern=patterns[i];
51           if (cell.innerHTML.match(pattern)) visible=true;
52         }
53       }
54     }
55     plc_table_row_visible(row,visible);
56     if (visible) matching_entries +=1;
57   }
58   plc_table_tbody_matching(tbody,matching_entries);
59   tablePaginater.init(table_id);
60 }
61
62 function plc_table_filter_reset (table_id, pattern_id) {
63   /* reset pattern */
64   document.getElementById(pattern_id).value="";
65   plc_table_filter (table_id, pattern_id);
66 }