checkpoint
[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 classname
18      see plc_table_tbody_matching
19   */
20   var totalMatches = opts.totalRows;
21   var tbody=document.getElementById(tablename).getElementsByTagName("tbody")[0];
22   var cn=tbody.className;
23   if (cn.match (/matching-\d+/)) {
24     totalMatches=cn.match(/matching-\d+/)[0].replace("matching-","");
25   } 
26
27   var label;
28
29   var matches_text;
30   if (totalMatches != opts.totalRows) {
31     matches_text = totalMatches + "/" + opts.totalRows;
32   } else {
33     matches_text = opts.totalRows;
34   }
35   var first = ((opts.currentPage-1) * opts.rowsPerPage) +1;
36   var last = Math.min((opts.currentPage * opts.rowsPerPage),totalMatches);
37   var items_text = "Items [" + first + " - " + last + "] of " + matches_text;
38   var page_text = "Page " + opts.currentPage + " of " + Math.ceil(totalMatches / opts.rowsPerPage);
39   label = items_text + " -- " + page_text;
40
41   p.className = "paginationText";    
42   p.appendChild(document.createTextNode(label));
43
44   /*  t.insertBefore(p.cloneNode(true), t.firstChild); */
45   b.appendChild(p);
46 }
47
48
49 /* locates a table from its id and alters the classname to reflect new table size */
50 function plc_pagesize_set (table_id,size_id,def_size) {
51   var table=document.getElementById(table_id);
52   var size_area=document.getElementById(size_id);
53   if ( ! size_area.value ) {
54     size_area.value=def_size;
55   }
56   var size=size_area.value;
57   table.className=table.className.replace(/paginate-\d+/,"paginate-"+size); 
58   tablePaginater.init(table_id);
59 }
60
61 function plc_pagesize_reset(table_id, size_id, size) {
62   var table=document.getElementById(table_id);
63   var size_area=document.getElementById(size_id);
64   size_area.value=size;
65   table.className=table.className.replace(/paginate-\d+/,"paginate-"+size); 
66   tablePaginater.init(table_id);
67 }
68   
69 /* set or clear the ' invisibleRow' in the tr's classname, according to visible */
70 function plc_table_row_visible (row,visible) {
71   var cn=row.className;
72   /* clear */
73   cn=cn.replace(" invisibleRow","");
74   if (! visible) cn += " invisibleRow";
75   row.className=cn;
76 }
77
78 /* maintain the number of matching entries in the <tbody> element's classname */
79 function plc_table_tbody_matching (tbody, matching) {
80   var new_cn="matching-" + matching;
81   var cn=tbody.className;
82   if (cn.match("matching-")) {
83     cn=cn.replace(/matching-\d+/,new_cn);
84   } else {
85     cn=cn + " " + new_cn;
86   }
87   cn=cn.replace(/^ +/,"");
88   tbody.className=cn;
89 }
90
91 /* scan the table, and mark as visible 
92    the rows that match (either AND or OR the patterns) */
93 function plc_table_filter (table_id,pattern_id,and_id) {
94   var tbody = document.getElementById(table_id).getElementsByTagName("tbody")[0];
95   var rows=tbody.rows;
96   var pattern_area = document.getElementById(pattern_id);
97   var pattern_text = pattern_area.value;
98   var row_index, row, cells, cell_index, cell, visible;
99   var pattern,i;
100   var matching_entries=0;
101   var and_button=document.getElementById(and_id);
102   var and_if_true=and_button.checked;
103
104   // remove whitespaces at the beginning and end
105   pattern_text = pattern_text.replace(/[ \t]+$/,"");
106   pattern_text = pattern_text.replace(/^[ \t]+/,"");
107   
108   if (pattern_text.indexOf ("&") != -1) {
109     pattern_text = pattern_text.replace(/&$/,"").replace(/&/," ");
110     pattern_area.value=pattern_text;
111     and_button.checked=true;
112     return;
113   } else if (pattern_text.indexOf ("|") != -1 ) {
114     pattern_text = pattern_text.replace(/\|$/,"").replace(/\|/," ");
115     pattern_area.value=pattern_text;
116     and_button.checked=false;
117     return;
118   }
119     
120   var patterns = pattern_text.split(" ");
121
122   for (row_index = 0; row=rows[row_index]; row_index++) {
123       cells=row.cells;
124     
125     /*empty pattern */
126     if (patterns.length == 0) {
127       visible=true;
128     } else if (and_if_true) {
129       /* AND mode: all patterns must match */
130       visible=true;
131       for (i in patterns) {
132         var pattern_matched=false;
133         pattern=patterns[i];
134         for (cell_index = 0; cell=cells[cell_index]; cell_index++) {
135           if ( cell.innerHTML.match(pattern)) {
136             pattern_matched=true;
137             // alert ('AND matched! p='+pattern+' c='+cell.innerHTML);
138             break;        
139           }
140         }
141         if ( ! pattern_matched ) visible=false;
142       }
143     } else {
144       /* OR mode: any match is good enough */
145       visible=false;
146       for (cell_index = 0; cell=cells[cell_index]; cell_index++) {
147         for (i in patterns) {
148           pattern=patterns[i];
149           if (cell.innerHTML.match(pattern)) {
150             visible=true;
151             // alert ('OR matched! p='+pattern+' c='+cell.innerHTML);
152             break;
153           }
154         }
155       }
156     }
157     plc_table_row_visible(row,visible);
158     if (visible) matching_entries +=1;
159   }
160   plc_table_tbody_matching(tbody,matching_entries);
161   tablePaginater.init(table_id);
162 }
163
164 function plc_table_filter_reset (table_id, pattern_id,and_id) {
165   /* reset pattern */
166   document.getElementById(pattern_id).value="";
167   plc_table_filter (table_id, pattern_id,and_id);
168 }