4a382eb2f3a87e54221f523951d9f704b16c89cf
2 Copyright © 2011 Pau Escrich <pau@dabax.net>
3 Contributors Lluis Esquerda <eskerda@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 The full GNU General Public License is included in this distribution in
20 the file called "COPYING".
25 Table pooler is a function to easy call XHR poller.
27 new TablePooler(5,"/cgi-bin/bmx7-info", {'status':''}, "status_table", function(st){
29 table.push(st.first,st.second)
34 polling_time: time between pollings
35 json_url: the json url to fetch the data
36 json_call: the json call
37 output_table_id: the table where javascript will put the data
38 callback_function: the function that will be executed each polling_time
40 The callback_function must return an array of arrays (matrix).
41 In the code st is the data obtained from the json call
44 function TablePooler (time
, jsonurl
, getparams
, table_id
, callback
) {
45 this.table
= document
.getElementById(table_id
);
46 this.callback
= callback
;
47 this.jsonurl
= jsonurl
;
48 this.getparams
= getparams
;
52 this.clear = function(){
53 while( this.table
.rows
.length
> 1 ) this.table
.deleteRow(1);
56 this.start = function(){
57 XHR
.poll(this.time
, this.jsonurl
, this.getparams
, function(x
, st
){
58 var data
= this.callback(st
);
61 for (var i
= 0; i
< data
.length
; i
++){
62 tr
= this.table
.insertRow(-1);
63 tr
.className
= 'cbi-section-table-row cbi-rowstyle-' + ((i
% 2) + 1);
65 for (var j
= 0; j
< data
[i
].length
; j
++){
66 td
= tr
.insertCell(-1);
67 if (data
[i
][j
].length
== 2) {
68 td
.colSpan
= data
[i
][j
][1];
69 content
= data
[i
][j
][0];
71 else content
= data
[i
][j
];
72 td
.innerHTML
= content
;