togglable sections everywhere
[plewww.git] / planetlab / js / plc_tables.js
1 /* $Id$ */
2
3 /* when a table gets paginated, displays context info */
4 function plc_table_paginator (opts,tablename) {
5
6   if(!("currentPage" in opts)) { return; }
7     
8   var p = document.createElement('p');
9   var t = document.getElementById(tablename+'-fdtablePaginaterWrapTop');
10   var b = document.getElementById(tablename+'-fdtablePaginaterWrapBottom');
11
12   /* when there's no visible entry, the pagination code removes the wrappers */
13   if ( (!t) || (!b) ) return;
14
15   /* get how many entries are matching:
16      opts.visibleRows only holds the contents of the current page
17      so we store the number of matching entries in the tbody's 'matching' attribute
18   */
19   var totalMatches = opts.totalRows;
20   var tbody=document.getElementById(tablename).getElementsByTagName("tbody")[0];
21   var matching=tbody['matching'];
22   if (matching) totalMatches = matching;
23
24   var label;
25
26   var matches_text;
27   if (totalMatches != opts.totalRows) {
28     matches_text = totalMatches + "/" + opts.totalRows;
29   } else {
30     matches_text = opts.totalRows;
31   }
32   var first = ((opts.currentPage-1) * opts.rowsPerPage) +1;
33   var last = Math.min((opts.currentPage * opts.rowsPerPage),totalMatches);
34   var items_text = "Items [" + first + " - " + last + "] of " + matches_text;
35   var page_text = "Page " + opts.currentPage + " of " + Math.ceil(totalMatches / opts.rowsPerPage);
36   label = items_text + " -- " + page_text;
37
38   p.className = "paginationText";    
39   p.appendChild(document.createTextNode(label));
40
41   /*  t.insertBefore(p.cloneNode(true), t.firstChild); */
42   b.appendChild(p);
43 }
44
45
46 /* locates a table from its id and alters the classname to reflect new table size */
47 function plc_pagesize_set (table_id,size_id,def_size) {
48   var table=document.getElementById(table_id);
49   var size_area=document.getElementById(size_id);
50   if ( ! size_area.value ) {
51     size_area.value=def_size;
52   }
53   var size=size_area.value;
54   table.className=table.className.replace(/paginate-\d+/,"paginate-"+size); 
55   tablePaginater.init(table_id);
56 }
57
58 function plc_pagesize_reset(table_id, size_id, size) {
59   var table=document.getElementById(table_id);
60   var size_area=document.getElementById(size_id);
61   size_area.value=size;
62   table.className=table.className.replace(/paginate-\d+/,"paginate-"+size); 
63   tablePaginater.init(table_id);
64 }
65   
66 /* set or clear the ' invisibleRow' in the tr's classname, according to visible */
67 function plc_table_row_visible (row,visible) {
68   var cn=row.className;
69   /* clear */
70   cn=cn.replace(" invisibleRow","");
71   if (! visible) cn += " invisibleRow";
72   row.className=cn;
73 }
74
75 // from a cell, extract visible text by removing <> and cache in 'plc_text' attribute
76 var re_brackets = new RegExp ('<[^>]*>','g');
77
78 function plc_table_cell_text (cell) {
79   if (cell['plc_text']) return cell['plc_text'];
80   var text = cell.innerHTML;
81   // remove what's between <>
82   text = text.replace(re_brackets,'');
83   cell['plc_text'] = text;
84   return text;
85 }
86
87 /* scan the table, and mark as visible 
88    the rows that match (either AND or OR the patterns) */
89 function plc_table_filter (table_id,pattern_id,and_id) {
90   var tbody = document.getElementById(table_id).getElementsByTagName("tbody")[0];
91   var rows=tbody.rows;
92   var pattern_area = document.getElementById(pattern_id);
93   var pattern_text = pattern_area.value;
94   var row_index, row, cells, cell_index, cell, visible;
95   var matching_entries=0;
96   var and_button=document.getElementById(and_id);
97   var and_if_true=and_button.checked;
98
99   // remove whitespaces at the beginning and end
100   pattern_text = pattern_text.replace(/[ \t]+$/,"");
101   pattern_text = pattern_text.replace(/^[ \t]+/,"");
102   
103   if (pattern_text.indexOf ("&") != -1) {
104     pattern_text = pattern_text.replace(/&/," ");
105     pattern_area.value=pattern_text;
106     and_button.checked=true;
107     return;
108   } else if (pattern_text.indexOf ("|") != -1 ) {
109     pattern_text = pattern_text.replace(/\|/," ");
110     pattern_area.value=pattern_text;
111     and_button.checked=false;
112     return;
113   }
114     
115   var match_attempts=0;
116   var start=(new Date).getTime();
117
118   // re compile all patterns - ignore case
119   var pattern_texts = pattern_text.split(" ");
120   var patterns=new Array();
121   var i;
122   for (i in pattern_texts) 
123     patterns[i]=new RegExp(pattern_texts[i],"i");
124
125   // scan rows
126   for (row_index = 0; row=rows[row_index]; row_index++) {
127       cells=row.cells;
128     
129     /*empty pattern */
130     if (patterns.length == 0) {
131       visible=true;
132     } else if (and_if_true) {
133       /* AND mode: all patterns must match */
134       visible=true;
135       for (i in patterns) {
136         var matched=false;
137         var pattern=patterns[i];
138         for (cell_index = 0; cell=cells[cell_index]; cell_index++) {
139           var against=plc_table_cell_text (cell);
140           match_attempts++;
141           if ( against.match(pattern)) {
142             matched=true;
143             break;        
144           }
145         }
146         if ( ! matched ) visible=false;
147       }
148     } else {
149       /* OR mode: any match is good enough */
150       visible=false;
151       for (cell_index = 0; cell=cells[cell_index]; cell_index++) {
152         var against = cell.plc_table_cell_text(cell);
153         for (i in patterns) {
154           pattern=patterns[i];
155           match_attempts++;
156           if (against.match(pattern)) {
157             visible=true;
158             // alert ('OR matched! p='+pattern+' c='+cell.innerHTML);
159             break;
160           }
161         }
162       }
163     }
164     plc_table_row_visible(row,visible);
165     if (visible) matching_entries +=1;
166   }
167   var end=(new Date).getTime();
168   var ms=end-start;
169   window.console.log ("plc_table_filter: " + 
170                       match_attempts + " matches - " +
171                       matching_entries + " lines - " + ms + " ms");
172   tbody['matching']=matching_entries;
173   tbody['match_attempts']=match_attempts;
174   tablePaginater.init(table_id);
175 }
176
177 function plc_table_filter_reset (table_id, pattern_id,and_id) {
178   /* reset pattern */
179   document.getElementById(pattern_id).value="";
180   plc_table_filter (table_id, pattern_id,and_id);
181 }