Merge branch 'Toastman-RT' into Toastman-VLAN
[tomato.git] / release / src / router / www / bwm-common.js
blob0133b4289b1abc6dc308b0dcf3d00bb396b4b597
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']];
31 function xpsb(byt)
33 /* REMOVE-BEGIN
34         kbit/s = 1000 bits/s
35         125 = 1000 / 8
36         ((B * 8) / 1000)
37 REMOVE-END */
38         return (byt / 125).toFixed(2) + ' <small>kbit/s</small><br>(' + (byt / 1024).toFixed(2) + ' <small>KB/s</small>)';
41 function showCTab()
43         showTab('speed-tab-' + ifname);
46 function showSelectedOption(prefix, prev, now)
48         var e;
50         elem.removeClass(prefix + prev, 'selected');    // safe if prev doesn't exist
51         if ((e = E(prefix + now)) != null) {
52                 elem.addClass(e, 'selected');
53                 e.blur();
54         }
57 function showDraw()
59         if (drawLast == drawMode) return;
60         showSelectedOption('draw', drawLast, drawMode);
61         drawLast = drawMode;
64 function switchDraw(n)
66         if ((!svgReady) || (updating)) return;
67         drawMode = n;
68         showDraw();
69         showCTab();
70         cookie.set(cprefix + 'draw', drawMode);
73 function showColor()
75         E('drawcolor').innerHTML = colors[drawColor][0] + ' &raquo;';
76         E('rx-name').style.borderBottom = '2px dashed ' + colors[drawColor][1 + colorX];
77         E('tx-name').style.borderBottom = '2px dashed ' + colors[drawColor][1 + (colorX ^ 1)];
80 function switchColor(rev)
82         if ((!svgReady) || (updating)) return;
83         if (rev) colorX ^= 1;
84                 else drawColor = (drawColor + 1) % colors.length;
85         showColor();
86         showCTab();
87         cookie.set(cprefix + 'color', drawColor + ',' + colorX);
90 function showScale()
92         if (scaleMode == scaleLast) return;
93         showSelectedOption('scale', scaleLast, scaleMode);
94         scaleLast = scaleMode;
97 function switchScale(n)
99         scaleMode = n;
100         showScale();
101         showTab('speed-tab-' + ifname);
102         cookie.set(cprefix + 'scale', scaleMode);
105 function showAvg()
107         if (avgMode == avgLast) return;
108         showSelectedOption('avg', avgLast, avgMode);
109         avgLast = avgMode;
112 function switchAvg(n)
114         if ((!svgReady) || (updating)) return;
115         avgMode = n;
116         showAvg();
117         showCTab();
118         cookie.set(cprefix + 'avg', avgMode);
121 function tabSelect(name)
123         if (!updating) showTab(name);
126 function showTab(name)
128         var h;
129         var max;
130         var i;
131         var rx, tx;
132         var e;
134         ifname = name.replace('speed-tab-', '');
135         cookie.set(cprefix + 'tab', ifname, 14);
136         tabHigh(name);
138         h = speed_history[ifname];
139         if (!h) return;
141         E('rx-current').innerHTML = xpsb(h.rx[h.rx.length - 1] / updateDiv);
142         E('rx-avg').innerHTML = xpsb(h.rx_avg);
143         E('rx-max').innerHTML = xpsb(h.rx_max);
145         E('tx-current').innerHTML = xpsb(h.tx[h.tx.length - 1] / updateDiv);
146         E('tx-avg').innerHTML = xpsb(h.tx_avg);
147         E('tx-max').innerHTML = xpsb(h.tx_max);
149         E('rx-total').innerHTML = scaleSize(h.rx_total);
150         E('tx-total').innerHTML = scaleSize(h.tx_total);
152         if (svgReady) {
153                 max = scaleMode ? MAX(h.rx_max, h.tx_max) : xx_max
154                 if (max > 12500) max = Math.round((max + 12499) / 12500) * 12500;
155                         else max += 100;
156                 updateSVG(h.rx, h.tx, max, drawMode,
157                         colors[drawColor][1 + colorX], colors[drawColor][1 + (colorX ^ 1)],
158                         updateInt, updateMaxL, updateDiv, avgMode, clock);
159         }
162 function loadData()
164         var old;
165         var t, e;
166         var name;
167         var i;
168         var changed;
170         xx_max = 0;
171         old = tabs;
172         tabs = [];
173         clock = new Date();
175         if (!speed_history) {
176                 speed_history = [];
177         }
178         else {
179                 for (var i in speed_history) {
180                         var h = speed_history[i];
181                         if ((typeof(h.rx) == 'undefined') || (typeof(h.tx) == 'undefined')) {
182                                 delete speed_history[i];
183                                 continue;
184                         }
185                         if ((h.rx_total == 0) && (h.tx_total == 0) && (h.up == 0)) {
186                                 delete speed_history[i];
187                                 continue;
188                         }
190                         if (updateReTotal) {
191                                 h.rx_total = h.rx_max = 0;
192                                 h.tx_total = h.tx_max = 0;
193                                 for (j = (h.rx.length - updateMaxL); j < h.rx.length; ++j) {
194                                         t = h.rx[j];
195                                         if (t > h.rx_max) h.rx_max = t;
196                                         h.rx_total += t;
197                                         t = h.tx[j];
198                                         if (t > h.tx_max) h.tx_max = t;
199                                         h.tx_total += t;
200                                 }
201                                 h.rx_avg = h.rx_total / updateMaxL;
202                                 h.tx_avg = h.tx_total / updateMaxL;
203                         }
205                         if (updateDiv > 1) {
206                                 h.rx_max /= updateDiv;
207                                 h.tx_max /= updateDiv;
208                                 h.rx_avg /= updateDiv;
209                                 h.tx_avg /= updateDiv;
210                         }
211                         if (h.rx_max > xx_max) xx_max = h.rx_max;
212                         if (h.tx_max > xx_max) xx_max = h.tx_max;
214                         t = i;
215                         
216                         if (wl_ifidx(i) >= 0) {
217                                 t = 'WL <small>(' + i + ')</small>';
218                         }
219                         
220                         if (i == 'imq1')        {
221                                 t = 'Lim. OUT <small>(' + i + ')</small>';
222                         }
223                         
224                         if (i == 'imq2')        {
225                                 t = 'Lim. IN <small>(' + i + ')</small>';
226                         }
227                         
228                         if (i == 'imq3')        {
229                                 t = 'Mon. OUT <small>(' + i + ')</small>';
230                         }
231                         
232                         if (i == 'imq4')        {
233                                 t = 'Mon. IN <small>(' + i + ')</small>';
234                         }
235                         
236                         else if ((nvram.wan_proto == 'pptp') || (nvram.wan_proto == 'pppoe') || (nvram.wan_proto == 'l2tp')) {
237                                 if (nvram.wan_iface == i) t = 'WAN <small>(' + i + ')</small>';
238                                 else if (nvram.wan_ifname == i && nvram.wan_proto != 'pppoe') 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         var c = nvram.rstats_colors.split(',');
288         while (c.length >= 3) {
289                 c[0] = escapeHTML(c[0]);
290                 colors.push(c.splice(0, 3));
291         }
293         c = (cookie.get(cprefix + 'color') || '').split(',');
294         if (c.length == 2) {
295                 drawColor = fixInt(c[0], 0, colors.length - 1, defDrawColor);
296                 colorX = fixInt(c[1], 0, 1, 0);
297         }
298         else {
299                 drawColor = defDrawColor;
300         }
301         showColor();
303         scaleMode = fixInt(cookie.get(cprefix + 'scale'), 0, 1, 0);
304         showScale();
306         avgMode = fixInt(cookie.get(cprefix + 'avg'), 1, 10, defAvg);
307         showAvg();
309         // if just switched
310         if ((nvram.wan_proto == 'disabled') || (nvram.wan_proto == 'wet')) {
311                 nvram.wan_ifname = '';
312         }
314         htmReady = 1;
315         initData();
316         E('refresh-spinner').style.visibility = 'hidden';