Skip to content

Commit 8c2b575

Browse files
committed
luci-mod-status: ES6 treatment
Signed-off-by: Paul Donald <newtwen+github@gmail.com>
1 parent cbd1b7a commit 8c2b575

File tree

2 files changed

+73
-74
lines changed

2 files changed

+73
-74
lines changed

modules/luci-mod-status/htdocs/luci-static/resources/view/status/bandwidth.js

Lines changed: 72 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
'require rpc';
88
'require network';
99

10-
var callLuciRealtimeStats = rpc.declare({
10+
const callLuciRealtimeStats = rpc.declare({
1111
object: 'luci',
1212
method: 'getRealtimeStats',
1313
params: [ 'mode', 'device' ],
1414
expect: { result: [] }
1515
});
1616

17-
var graphPolls = [],
18-
pollInterval = 3;
17+
const graphPolls = [];
18+
const pollInterval = 3;
1919

2020
Math.log2 = Math.log2 || function(x) { return Math.log(x) * Math.LOG2E; };
2121

@@ -25,56 +25,56 @@ function rate(n, br) {
2525
}
2626

2727
return view.extend({
28-
load: function() {
28+
load() {
2929
return Promise.all([
3030
this.loadSVG(L.resource('svg/bandwidth.svg')),
3131
network.getDevices()
3232
]);
3333
},
3434

35-
updateGraph: function(ifname, svg, lines, cb) {
36-
var G = svg.firstElementChild;
35+
updateGraph(ifname, svg, lines, cb) {
36+
const G = svg.firstElementChild;
3737

38-
var view = document.querySelector('#view');
38+
const view = document.querySelector('#view');
3939

40-
var width = view.offsetWidth - 2;
41-
var height = 300 - 2;
42-
var step = 5;
40+
const width = view.offsetWidth - 2;
41+
const height = 300 - 2;
42+
const step = 5;
4343

44-
var data_wanted = Math.floor(width / step);
44+
const data_wanted = Math.floor(width / step);
4545

46-
var data_values = [],
47-
line_elements = [];
46+
const data_values = [];
47+
const line_elements = [];
4848

49-
for (var i = 0; i < lines.length; i++)
50-
if (lines[i] != null)
49+
for (const line of lines)
50+
if (line)
5151
data_values.push([]);
5252

53-
var info = {
53+
const info = {
5454
line_current: [],
5555
line_average: [],
5656
line_peak: []
5757
};
5858

5959
/* prefill datasets */
60-
for (var i = 0; i < data_values.length; i++)
61-
for (var j = 0; j < data_wanted; j++)
60+
for (let i = 0; i < data_values.length; i++)
61+
for (let j = 0; j < data_wanted; j++)
6262
data_values[i][j] = 0;
6363

6464
/* plot horizontal time interval lines */
65-
for (var i = width % (step * 60); i < width; i += step * 60) {
66-
var line = document.createElementNS('http://www.w3.org/2000/svg', 'line');
67-
line.setAttribute('x1', i);
68-
line.setAttribute('y1', 0);
69-
line.setAttribute('x2', i);
70-
line.setAttribute('y2', '100%');
71-
line.setAttribute('style', 'stroke:black;stroke-width:0.1');
72-
73-
var text = document.createElementNS('http://www.w3.org/2000/svg', 'text');
74-
text.setAttribute('x', i + 5);
75-
text.setAttribute('y', 15);
76-
text.setAttribute('style', 'fill:#eee; font-size:9pt; font-family:sans-serif; text-shadow:1px 1px 1px #000');
77-
text.appendChild(document.createTextNode(Math.round((width - i) / step / 60) + 'm'));
65+
for (let i = width % (step * 60); i < width; i += step * 60) {
66+
const line = document.createElementNS('http://www.w3.org/2000/svg', 'line');
67+
line.setAttribute('x1', i);
68+
line.setAttribute('y1', 0);
69+
line.setAttribute('x2', i);
70+
line.setAttribute('y2', '100%');
71+
line.setAttribute('style', 'stroke:black;stroke-width:0.1');
72+
73+
const text = document.createElementNS('http://www.w3.org/2000/svg', 'text');
74+
text.setAttribute('x', i + 5);
75+
text.setAttribute('y', 15);
76+
text.setAttribute('style', 'fill:#eee; font-size:9pt; font-family:sans-serif; text-shadow:1px 1px 1px #000');
77+
text.appendChild(document.createTextNode(Math.round((width - i) / step / 60) + 'm'));
7878

7979
G.appendChild(line);
8080
G.appendChild(text);
@@ -98,35 +98,34 @@ return view.extend({
9898
});
9999
},
100100

101-
pollData: function() {
101+
pollData() {
102102
poll.add(L.bind(function() {
103-
var tasks = [];
103+
const tasks = [];
104104

105-
for (var i = 0; i < graphPolls.length; i++) {
106-
var ctx = graphPolls[i];
105+
for (const ctx of graphPolls) {
107106
tasks.push(L.resolveDefault(callLuciRealtimeStats('interface', ctx.ifname), []));
108107
}
109108

110109
return Promise.all(tasks).then(L.bind(function(datasets) {
111-
for (var gi = 0; gi < graphPolls.length; gi++) {
112-
var ctx = graphPolls[gi],
113-
data = datasets[gi],
114-
values = ctx.values,
115-
lines = ctx.lines,
116-
info = ctx.info;
117-
118-
var data_scale = 0;
119-
var data_wanted = Math.floor(ctx.width / ctx.step);
120-
var last_timestamp = NaN;
121-
122-
for (var i = 0, di = 0; di < lines.length; di++) {
110+
for (let gi = 0; gi < graphPolls.length; gi++) {
111+
let ctx = graphPolls[gi];
112+
const data = datasets[gi];
113+
const values = ctx.values;
114+
const lines = ctx.lines;
115+
const info = ctx.info;
116+
117+
let data_scale = 0;
118+
const data_wanted = Math.floor(ctx.width / ctx.step);
119+
let last_timestamp = NaN;
120+
121+
for (let i = 0, di = 0; di < lines.length; di++) {
123122
if (lines[di] == null)
124123
continue;
125124

126-
var multiply = (lines[di].multiply != null) ? lines[di].multiply : 1,
127-
offset = (lines[di].offset != null) ? lines[di].offset : 0;
125+
const multiply = (lines[di].multiply != null) ? lines[di].multiply : 1;
126+
const offset = (lines[di].offset != null) ? lines[di].offset : 0;
128127

129-
for (var j = ctx.timestamp ? 0 : 1; j < data.length; j++) {
128+
for (let j = ctx.timestamp ? 0 : 1; j < data.length; j++) {
130129
/* skip overlapping entries */
131130
if (data[j][0] <= ctx.timestamp)
132131
continue;
@@ -160,15 +159,15 @@ return view.extend({
160159
/* cut off outdated entries */
161160
ctx.fill = Math.min(ctx.fill, data_wanted);
162161

163-
for (var i = 0; i < values.length; i++) {
164-
var len = values[i].length;
162+
for (let i = 0; i < values.length; i++) {
163+
const len = values[i].length;
165164
values[i] = values[i].slice(len - data_wanted, len);
166165

167166
/* find peaks, averages */
168167
info.line_peak[i] = NaN;
169168
info.line_average[i] = 0;
170169

171-
for (var j = 0; j < values[i].length; j++) {
170+
for (let j = 0; j < values[i].length; j++) {
172171
info.line_peak[i] = isNaN(info.line_peak[i]) ? values[i][j] : Math.max(info.line_peak[i], values[i][j]);
173172
info.line_average[i] += values[i][j];
174173
}
@@ -182,39 +181,39 @@ return view.extend({
182181
if (!isNaN(last_timestamp))
183182
ctx.timestamp = last_timestamp;
184183

185-
var size = Math.floor(Math.log2(info.peak)),
186-
div = Math.pow(2, size - (size % 10)),
187-
mult = info.peak / div,
188-
mult = (mult < 5) ? 2 : ((mult < 50) ? 10 : ((mult < 500) ? 100 : 1000));
184+
const size = Math.floor(Math.log2(info.peak));
185+
const div = Math.pow(2, size - (size % 10));
186+
const p_o_d = info.peak / div;
187+
const mult = (p_o_d < 5) ? 2 : ((p_o_d < 50) ? 10 : ((p_o_d < 500) ? 100 : 1000));
189188

190189
info.peak = info.peak + (mult * div) - (info.peak % (mult * div));
191190

192191
data_scale = ctx.height / info.peak;
193192

194193
/* plot data */
195-
for (var i = 0, di = 0; di < lines.length; di++) {
194+
for (let i = 0, di = 0; di < lines.length; di++) {
196195
if (lines[di] == null)
197196
continue;
198197

199-
var el = ctx.svg.firstElementChild.getElementById(lines[di].line),
200-
pt = '0,' + ctx.height,
201-
y = 0;
198+
const el = ctx.svg.firstElementChild.getElementById(lines[di].line);
199+
let pt = `0,${ctx.height}`;
200+
let y = 0;
202201

203202
if (!el)
204203
continue;
205204

206-
for (var j = 0; j < values[i].length; j++) {
207-
var x = j * ctx.step;
205+
for (let j = 0; j < values[i].length; j++) {
206+
const x = j * ctx.step;
208207

209208
y = ctx.height - Math.floor(values[i][j] * data_scale);
210209
//y -= Math.floor(y % (1 / data_scale));
211210

212211
y = isNaN(y) ? ctx.height : y;
213212

214-
pt += ' ' + x + ',' + y;
213+
pt += ` ${x},${y}`;
215214
}
216215

217-
pt += ' ' + ctx.width + ',' + y + ' ' + ctx.width + ',' + ctx.height;
216+
pt += ` ${ctx.width},${y} ${ctx.width},${ctx.height}`;
218217

219218
el.setAttribute('points', pt);
220219

@@ -232,7 +231,7 @@ return view.extend({
232231
}, this), pollInterval);
233232
},
234233

235-
loadSVG: function(src) {
234+
loadSVG(src) {
236235
return request.get(src).then(function(response) {
237236
if (!response.ok)
238237
throw new Error(response.statusText);
@@ -243,18 +242,18 @@ return view.extend({
243242
});
244243
},
245244

246-
render: function([svg, devs]) {
245+
render([svg, devs]) {
247246

248247
var v = E('div', { 'class': 'cbi-map', 'id': 'map' }, E('div'));
249248

250-
for (var i = 0; i < devs.length; i++) {
251-
var ifname = devs[i].getName();
252-
const ssid = devs[i].wif?.getSSID?.() || null;
249+
for (const dev of devs) {
250+
var ifname = dev.getName();
251+
const ssid = dev.wif?.getSSID?.() || null;
253252

254-
if (!ifname || !devs[i].isUp() || devs[i].wif?.isDisabled())
253+
if (!ifname || !dev.isUp() || dev.wif?.isDisabled())
255254
continue;
256255

257-
var csvg = svg.cloneNode(true);
256+
const csvg = svg.cloneNode(true);
258257

259258
v.firstElementChild.appendChild(E('div', { 'class': 'cbi-section', 'data-tab': ifname, 'data-tab-title': ssid ? `${ifname} ${ssid}` : ifname }, [
260259
csvg,
@@ -287,7 +286,7 @@ return view.extend({
287286
]));
288287

289288
this.updateGraph(ifname, csvg, [ { line: 'rx', counter: true }, null, { line: 'tx', counter: true } ], function(svg, info) {
290-
var G = svg.firstElementChild, tab = svg.parentNode;
289+
const G = svg.firstElementChild, tab = svg.parentNode;
291290

292291
G.getElementById('label_25').firstChild.data = rate(info.label_25).join('');
293292
G.getElementById('label_50').firstChild.data = rate(info.label_50).join('');

modules/luci-mod-status/htdocs/luci-static/resources/view/status/wireless.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ return view.extend({
118118

119119
return Promise.all(tasks).then(L.bind(function(datasets) {
120120
for (let gi = 0; gi < graphPolls.length; gi++) {
121-
const ctx = graphPolls[gi];
121+
let ctx = graphPolls[gi];
122122
const data = datasets[gi];
123123
const values = ctx.values;
124124
const lines = ctx.lines;

0 commit comments

Comments
 (0)