UI cleanup, phase 5, ARM (functional changes)
[tomato.git] / release / src-rt-6.x.4708 / router / www / bwm-common.js
blob8da477d84ddef0de9ebc45c08827396c9a79dd6a
1 /*
2         Tomato GUI
3         Copyright (C) 2006-2010 Jonathan Zarate
4         http://www.polarcloud.com/tomato/
6         For use with Tomato Firmware only.
7         No part of this file may be used without permission.
8 */
10 var tabs = [];
11 var rx_max, rx_avg;
12 var tx_max, tx_avg;
13 var xx_max = 0;
14 var ifname;
15 var htmReady = 0;
16 var svgReady = 0;
17 var updating = 0;
18 var scaleMode = 0;
19 var scaleLast = -1;
20 var drawMode = 0;
21 var drawLast = -1;
22 var drawColor = 0;
23 var avgMode = 0;
24 var avgLast = -1;
25 var colorX = 0;
26 var colors = [
27         ['Green & Blue', '#118811', '#6495ed'], ['Blue & Orange', '#003EBA', '#FF9000'],
28         ['Blue & Red', '#003EDD', '#CC4040'], ['Blue', '#22f', '#225'], ['Gray', '#000', '#999'],
29         ['Red & Black', '#d00', '#000']];
30 //var hostnamecache = [];
32 function xpsb(byt)
34 /* REMOVE-BEGIN
35         kbit/s = 1000 bits/s
36         125 = 1000 / 8
37         ((B * 8) / 1000)
38 REMOVE-END */
39         return (byt / 125).toFixed(2) + ' <small>kbit/s</small><br>(' + (byt / 1024).toFixed(2) + ' <small>KB/s</small>)';
42 function showCTab()
44         showTab('speed-tab-' + ifname);
47 function showSelectedOption(prefix, prev, now)
49         var e;
51         elem.removeClass(prefix + prev, 'selected');    // safe if prev doesn't exist
52         if ((e = E(prefix + now)) != null) {
53                 elem.addClass(e, 'selected');
54                 e.blur();
55         }
58 function showDraw()
60         if (drawLast == drawMode) return;
61         showSelectedOption('draw', drawLast, drawMode);
62         drawLast = drawMode;
65 function switchDraw(n)
67         if ((!svgReady) || (updating)) return;
68         drawMode = n;
69         showDraw();
70         showCTab();
71         cookie.set(cprefix + 'draw', drawMode);
74 function showColor()
76         E('drawcolor').innerHTML = colors[drawColor][0] + ' &raquo;';
77         E('rx-name').style.borderBottom = '2px dashed ' + colors[drawColor][1 + colorX];
78         E('tx-name').style.borderBottom = '2px dashed ' + colors[drawColor][1 + (colorX ^ 1)];
81 function switchColor(rev)
83         if ((!svgReady) || (updating)) return;
84         if (rev) colorX ^= 1;
85                 else drawColor = (drawColor + 1) % colors.length;
86         showColor();
87         showCTab();
88         cookie.set(cprefix + 'color', drawColor + ',' + colorX);
91 function showScale()
93         if (scaleMode == scaleLast) return;
94         showSelectedOption('scale', scaleLast, scaleMode);
95         scaleLast = scaleMode;
98 function switchScale(n)
100         scaleMode = n;
101         showScale();
102         showTab('speed-tab-' + ifname);
103         cookie.set(cprefix + 'scale', scaleMode);
106 function showAvg()
108         if (avgMode == avgLast) return;
109         showSelectedOption('avg', avgLast, avgMode);
110         avgLast = avgMode;
113 function switchAvg(n)
115         if ((!svgReady) || (updating)) return;
116         avgMode = n;
117         showAvg();
118         showCTab();
119         cookie.set(cprefix + 'avg', avgMode);
122 function tabSelect(name)
124         if (!updating) showTab(name);
127 function showTab(name)
129         var h;
130         var max;
131         var i;
132         var rx, tx;
133         var e;
135         ifname = name.replace('speed-tab-', '');
136         cookie.set(cprefix + 'tab', ifname, 14);
137         tabHigh(name);
139         h = speed_history[ifname];
140         if (!h) return;
142         E('rx-current').innerHTML = xpsb(h.rx[h.rx.length - 1] / updateDiv);
143         E('rx-avg').innerHTML = xpsb(h.rx_avg);
144         E('rx-max').innerHTML = xpsb(h.rx_max);
146         E('tx-current').innerHTML = xpsb(h.tx[h.tx.length - 1] / updateDiv);
147         E('tx-avg').innerHTML = xpsb(h.tx_avg);
148         E('tx-max').innerHTML = xpsb(h.tx_max);
150         E('rx-total').innerHTML = scaleSize(h.rx_total);
151         E('tx-total').innerHTML = scaleSize(h.tx_total);
153         if (svgReady) {
154                 max = scaleMode ? MAX(h.rx_max, h.tx_max) : xx_max;
155                 if (max > 12500) max = Math.round((max + 12499) / 12500) * 12500;
156                         else max += 100;
157                 updateSVG(h.rx, h.tx, max, drawMode,
158                         colors[drawColor][1 + colorX], colors[drawColor][1 + (colorX ^ 1)],
159                         updateInt, updateMaxL, updateDiv, avgMode, clock);
160         }
163 function loadData()
165         var old;
166         var t, e;
167         var name;
168         var i;
169         var changed;
171         xx_max = 0;
172         old = tabs;
173         tabs = [];
174         clock = new Date();
176         if (!speed_history) {
177                 speed_history = [];
178         }
179         else {
180                 for (var i in speed_history) {
181                         var h = speed_history[i];
182                         if ((typeof(h.rx) == 'undefined') || (typeof(h.tx) == 'undefined')) {
183                                 delete speed_history[i];
184                                 continue;
185                         }
186                         if ((h.rx_total == 0) && (h.tx_total == 0) && (h.up == 0)) {
187                                 delete speed_history[i];
188                                 continue;
189                         }
191                         if (typeof(h.hide) != 'undefined') {
192                                 if (h.hide == 1) continue;
193                         }
195                         if (updateReTotal) {
196                                 h.rx_total = h.rx_max = 0;
197                                 h.tx_total = h.tx_max = 0;
198                                 for (j = (h.rx.length - updateMaxL); j < h.rx.length; ++j) {
199                                         t = h.rx[j];
200                                         if (t > h.rx_max) h.rx_max = t;
201                                         h.rx_total += t;
202                                         t = h.tx[j];
203                                         if (t > h.tx_max) h.tx_max = t;
204                                         h.tx_total += t;
205                                 }
206                                 h.rx_avg = h.rx_total / (h.count ? h.count : updateMaxL);
207                                 h.tx_avg = h.tx_total / (h.count ? h.count : updateMaxL);
208                         }
210                         if (updateDiv > 1) {
211                                 h.rx_max /= updateDiv;
212                                 h.tx_max /= updateDiv;
213                                 h.rx_avg /= updateDiv;
214                                 h.tx_avg /= updateDiv;
215                         }
216                         if (h.rx_max > xx_max) xx_max = h.rx_max;
217                         if (h.tx_max > xx_max) xx_max = h.tx_max;
219                         t = i;                                                                                  // by default, show only the IP address (or IF name)
220                         if ((typeof(hostnamecache) != 'undefined') && (hostnamecache[i] != null)) {
221                                 if (nvram['cstats_labels'] != null ) {
222                                         if (nvram['cstats_labels'] == '1') {    // if known, show only the hostname
223                                                 t = hostnamecache[i];
224                                         }
225                                         if (nvram['cstats_labels'] == '0') {    // show hostname and IP
226                                                 t = hostnamecache[i] + ' <small>(' + i + ')</small>';
227                                         }
228                                 }
229                         }
230                         else if (wl_ifidx(i) >= 0) {
231 /* REMOVE-BEGIN
232 //                      else if (i == nvram.wl_ifname) {
233 REMOVE-END */
234                                 t = 'WL <small>(' + i + ')</small>';
235                         }
236                         else if ((nvram.wan_proto == 'pptp') || (nvram.wan_proto == 'pppoe') || (nvram.wan_proto == 'ppp3g')) {
237                                 if (nvram.wan_iface == i) t = 'WAN <small>(' + i + ')</small>';
238                                 else if (nvram.wan_ifname == i && ((nvram.wan_proto != 'pppoe') && (nvram.wan_proto != 'ppp3g'))) t = 'MAN <small>(' + i + ')</small>';
239                         }
240                         else if (nvram.wan_proto != 'disabled') {
241                                 if (nvram.wan_ifname == i) t = 'WAN <small>(' + i + ')</small>';
242                         }
243                         tabs.push(['speed-tab-' + i, t]);
244                 }
246                 tabs = tabs.sort(
247                         function(a, b) {
248                                 if (a[1] < b[1]) return -1;
249                                 if (a[1] > b[1]) return 1;
250                                 return 0;
251                         });
252         }
254         if (tabs.length == old.length) {
255                 for (i = tabs.length - 1; i >= 0; --i)
256                         if (tabs[i][0] != old[i][0]) break;
257                 changed = i > 0;
258         }
259         else changed = 1;
261         if (changed) {
262                 E('tab-area').innerHTML = _tabCreate.apply(this, tabs);
263         }
264         if (((name = cookie.get(cprefix + 'tab')) != null) && ((speed_history[name] != undefined))) {
265                 showTab('speed-tab-' + name);
266                 return;
267         }
268         if (tabs.length) showTab(tabs[0][0]);
271 function initData()
273         if (htmReady) {
274                 loadData();
275                 if (svgReady) {
276                         E('graph').style.visibility = 'visible';
277                         E('bwm-controls').style.visibility = 'visible';
278                 }
279         }
282 function initCommon(defAvg, defDrawMode, defDrawColor)
284         drawMode = fixInt(cookie.get(cprefix + 'draw'), 0, 1, defDrawMode);
285         showDraw();
287         if (nvram['rstats_colors'] != null)
288                 var c = nvram.rstats_colors.split(',');
289         else if (nvram['cstats_colors'] != null)
290                 var c = nvram.cstats_colors.split(',');
291         while (c.length >= 3) {
292                 c[0] = escapeHTML(c[0]);
293                 colors.push(c.splice(0, 3));
294         }
296         c = (cookie.get(cprefix + 'color') || '').split(',');
297         if (c.length == 2) {
298                 drawColor = fixInt(c[0], 0, colors.length - 1, defDrawColor);
299                 colorX = fixInt(c[1], 0, 1, 0);
300         }
301         else {
302                 drawColor = defDrawColor;
303         }
304         showColor();
306         scaleMode = fixInt(cookie.get(cprefix + 'scale'), 0, 1, 0);
307         showScale();
309         avgMode = fixInt(cookie.get(cprefix + 'avg'), 1, 10, defAvg);
310         showAvg();
312         // if just switched
313         if ((nvram.wan_proto == 'disabled') || (nvram.wan_proto == 'wet')) {
314                 nvram.wan_ifname = '';
315         }
317         htmReady = 1;
318         initData();
319         E('refresh-spinner').style.visibility = 'hidden';
322 function populateCache() {
323         var s;
325         if (nvram['dhcpd_static'] != null ) {
326                 s = nvram.dhcpd_static.split('>');
327                 for (var i = 0; i < s.length; ++i) {
328                         var t = s[i].split('<');
329                         if ((t.length == 3) || (t.length == 4)) {
330                                 if (t[2] != '')
331                                         hostnamecache[t[1]] = t[2].split(' ').splice(0,1);
332                         }
333                 }
334         }
336         if (typeof(dhcpd_lease) != 'undefined') {
337                 for (var j=0; j<dhcpd_lease.length; ++j) {
338                         if (dhcpd_lease[j][0] != '') {
339                                 hostnamecache[dhcpd_lease[j][1]] = dhcpd_lease[j][0].split(' ').splice(0,1);
340                         }
341                 }
342         }
344         for (var i = 0 ; i <= MAX_BRIDGE_ID ; i++) {
345                 var j = (i == 0) ? '' : i.toString();
346                 if (nvram['lan' + j + '_ipaddr'] != null)
347                         if (nvram['lan' + j + '_netmask'] != null)
348                                 if (nvram['lan' + j + '_ipaddr'] != '')
349                                         if (nvram['lan' + j + '_netmask'] != '') {
350                                                 hostnamecache[getNetworkAddress(nvram['lan' + j + '_ipaddr'], nvram['lan' + j + '_netmask'])] = 'LAN' + j;
351                                         }
352         }