oops
[plewww.git] / planetlab / js / plc_paginate.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_table_setsize (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_table_size_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