X-Git-Url: http://git.openwrt.org/?p=feed%2Frouting.git;a=blobdiff_plain;f=luci-app-bmx6%2Ffiles%2Fwww%2Fluci-static%2Fresources%2Fbmx6%2Fjs%2Fpolling.js;fp=luci-app-bmx6%2Ffiles%2Fwww%2Fluci-static%2Fresources%2Fbmx6%2Fjs%2Fpolling.js;h=8f814437428e80429cde3ebc3171da9e29ec96da;hp=0000000000000000000000000000000000000000;hb=da0fe5735d9dbdc205b3ab0596c6768c9af73ae2;hpb=be8bdc2fb9999ef418cecb442bbe5404197f2153 diff --git a/luci-app-bmx6/files/www/luci-static/resources/bmx6/js/polling.js b/luci-app-bmx6/files/www/luci-static/resources/bmx6/js/polling.js new file mode 100644 index 0000000..8f81443 --- /dev/null +++ b/luci-app-bmx6/files/www/luci-static/resources/bmx6/js/polling.js @@ -0,0 +1,81 @@ +/* + Copyright (C) 2011 Pau Escrich + Contributors Lluis Esquerda + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + The full GNU General Public License is included in this distribution in + the file called "COPYING". +*/ + + +/* + Table pooler is a function to easy call XHR poller. + + new TablePooler(5,"/cgi-bin/bmx6-info", {'status':''}, "status_table", function(st){ + var table = Array() + table.push(st.first,st.second) + return table + } + Parameters are: + polling_time: time between pollings + json_url: the json url to fetch the data + json_call: the json call + output_table_id: the table where javascript will put the data + callback_function: the function that will be executed each polling_time + + The callback_function must return an array of arrays (matrix). + In the code st is the data obtained from the json call +*/ + + function TablePooler (time, jsonurl, getparams, table_id, callback) { + this.table = document.getElementById(table_id); + this.callback = callback; + this.jsonurl = jsonurl; + this.getparams = getparams; + this.time = time; + + this.clear = function(){ + /* clear all rows */ + while( this.table.rows.length > 1 ) this.table.deleteRow(1); + } + this.start = function(){ + XHR.poll(this.time, this.jsonurl, this.getparams, function(x, st){ + var data = this.callback(st); + var content, tr, td; + this.clear(); + for (var i = 0; i < data.length; i++){ + tr = this.table.insertRow(-1); + tr.className = 'cbi-section-table-row cbi-rowstyle-' + ((i % 2) + 1); + + for (var j = 0; j < data[i].length; j++){ + td = tr.insertCell(-1); + if (data[i][j].length == 2) { + td.colSpan = data[i][j][1]; + content = data[i][j][0]; + } + else content = data[i][j]; + td.innerHTML = content; + } + } + }.bind(this)); + } + + + this.start(); + } + + +