Merge branch 'bmx6'
[lede-routing/.git] / luci-app-bmx6 / files / www / luci-static / resources / bmx6 / js / polling.js
1 /*
2     Copyright (C) 2011 Pau Escrich <pau@dabax.net>
3     Contributors Lluis Esquerda <eskerda@gmail.com>
4
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.
9
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.
14
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.
18
19     The full GNU General Public License is included in this distribution in
20     the file called "COPYING".
21 */
22
23                 
24 /*
25         Table pooler is a function to easy call XHR poller. 
26
27                 new TablePooler(5,"/cgi-bin/bmx6-info", {'status':''}, "status_table", function(st){
28                         var table = Array()
29                         table.push(st.first,st.second)
30                         return table
31                 }
32         Parameters are: 
33                 polling_time: time between pollings
34                 json_url: the json url to fetch the data
35                 json_call: the json call
36                 output_table_id: the table where javascript will put the data
37                 callback_function: the function that will be executed each polling_time
38         
39         The callback_function must return an array of arrays (matrix).
40         In the code st is the data obtained from the json call
41 */
42
43         function TablePooler (time, jsonurl, getparams, table_id, callback) {
44                 this.table = document.getElementById(table_id);
45                 this.callback = callback;
46                 this.jsonurl = jsonurl;
47                 this.getparams = getparams;
48                 this.time = time;
49
50                 this.clear = function(){
51                         /* clear all rows */
52                         while( this.table.rows.length > 1 ) this.table.deleteRow(1);
53                 }
54                 this.start = function(){
55                         XHR.poll(this.time, this.jsonurl, this.getparams, function(x, st){
56                                 var data = this.callback(st);
57                                 var content, tr, td;
58                                 this.clear();
59                                 for (var i = 0; i < data.length; i++){
60                                         tr = this.table.insertRow(-1);
61                                         tr.className = 'cbi-section-table-row cbi-rowstyle-' + ((i % 2) + 1);
62                                                 
63                                         for (var j = 0; j < data[i].length; j++){
64                                                 td = tr.insertCell(-1);
65                                                 if (data[i][j].length == 2) {
66                                                         td.colSpan = data[i][j][1];
67                                                         content = data[i][j][0];
68                                                 }
69                                                 else content = data[i][j];
70                                                 td.innerHTML = content;
71                                         }
72                                 }
73                         }.bind(this));
74                 }
75
76
77                 this.start();
78         }
79
80
81