234391a975f6e84d67cd7d1d780a0a958adb55fb
[feed/routing.git] / luci-app-bmx7 / files / www / luci-static / resources / bmx7 / js / polling.js
1 /*
2 Copyright © 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/bmx7-info", {'status':''}, "status_table", function(st){
28 var table = Array()
29 table.push(st.first,st.second)
30 return table
31 }
32
33 The parameters are:
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
39
40 The callback_function must return an array of arrays (matrix).
41 In the code st is the data obtained from the json call
42 */
43
44 function TablePooler (time, jsonurl, getparams, div_id, callback) {
45 this.div_id = div_id;
46 this.div = document.getElementById(div_id);
47 this.callback = callback;
48 this.jsonurl = jsonurl;
49 this.getparams = getparams;
50 this.time = time;
51
52 this.start = function(){
53 XHR.poll(this.time, this.jsonurl, this.getparams, function(x, st){
54 var data = this.callback(st);
55 var content;
56 for (var i = 0; i < data.length; i++){
57 rowId = "trDiv_" + this.div_id + i;
58 rowDiv = document.getElementById(rowId);
59 if (rowDiv === null) {
60 rowDiv = document.createElement("div");
61 rowDiv.id = rowId;
62 rowDiv.className = "tr";
63 this.div.appendChild(rowDiv);
64 }
65 for (var j = 0; j < data[i].length; j++){
66 cellId = "tdDiv_" + this.div_id + i + j;
67 cellDiv = document.getElementById(cellId);
68 if (cellDiv === null) {
69 cellDiv = document.createElement("div");
70 cellDiv.id = cellId;
71 cellDiv.className = "td";
72 rowDiv.appendChild(cellDiv);
73 }
74 if (typeof data[i][j] !== 'undefined' && data[i][j].length == 2) {
75 content = data[i][j][0] + "/" + data[i][j][1];
76 }
77 else content = data[i][j];
78 cellDiv.innerHTML = content;
79 }
80 }
81 }.bind(this));
82 }
83
84
85 this.start();
86 }