Merge pull request #4410 from cybrnook/master
[project/luci.git] / modules / luci-mod-dashboard / htdocs / luci-static / resources / view / dashboard / include / 10_router.js
1 'use strict';
2 'require baseclass';
3 'require fs';
4 'require rpc';
5 'require network';
6
7 var callSystemBoard = rpc.declare({
8 object: 'system',
9 method: 'board'
10 });
11
12 var callSystemInfo = rpc.declare({
13 object: 'system',
14 method: 'info'
15 });
16
17 return baseclass.extend({
18
19 params: [],
20
21 load: function() {
22 return Promise.all([
23 network.getWANNetworks(),
24 network.getWAN6Networks(),
25 L.resolveDefault(callSystemBoard(), {}),
26 L.resolveDefault(callSystemInfo(), {})
27 ]);
28 },
29
30 renderHtml: function(data, type) {
31
32 var icon = type;
33 var title = 'router' == type ? _('System') : _('Internet');
34 var container_wapper = E('div', { 'class': type + '-status-self dashboard-bg box-s1'});
35 var container_box = E('div', { 'class': type + '-status-info'});
36 var container_item = E('div', { 'class': 'settings-info'});
37
38 if ('internet' == type) {
39 icon = (data.v4.connected.value || data.v6.connected.value) ? type : 'not-internet';
40 }
41
42 container_box.appendChild(E('div', { 'class': 'title'}, [
43 E('img', {
44 'src': L.resource('view/dashboard/icons/' + icon + '.svg'),
45 'width': 'router' == type ? 64 : 54,
46 'title': title,
47 'class': 'middle'
48 }),
49 E('h3', title)
50 ]));
51
52 container_box.appendChild(E('hr'));
53
54 if ('internet' == type) {
55 var container_internet_v4 = E('div');
56 var container_internet_v6 = E('div');
57
58 for(var idx in data) {
59
60 for(var ver in data[idx]) {
61 var classname = ver,
62 suppelements = '',
63 visible = data[idx][ver].visible;
64
65 if('connected' === ver) {
66 classname = data[idx][ver].value ? 'label label-success' : 'label label-danger';
67 data[idx][ver].value = data[idx][ver].value ? _('yes') : _('no');
68 }
69
70 if ('v4' === idx) {
71
72 if ('title' === ver) {
73 container_internet_v4.appendChild(
74 E('p', { 'class': 'mt-2'}, [
75 E('span', {'class': ''}, [ data[idx].title ]),
76 ])
77 );
78 continue;
79 }
80
81 if ('addrsv4' === ver) {
82 var addrs = data[idx][ver].value;
83 if(Array.isArray(addrs) && addrs.length) {
84 for(var ip in addrs) {
85 data[idx][ver].value = addrs[ip].split('/')[0];
86 }
87 }
88 }
89
90 if (visible) {
91 container_internet_v4.appendChild(
92 E('p', { 'class': 'mt-2'}, [
93 E('span', {'class': ''}, [ data[idx][ver].title + ':' ]),
94 E('span', {'class': classname }, [ data[idx][ver].value ]),
95 suppelements
96 ])
97 );
98 }
99
100 } else {
101
102 if ('title' === ver) {
103 container_internet_v6.appendChild(
104 E('p', { 'class': 'mt-2'}, [
105 E('span', {'class': ''}, [ data[idx].title ]),
106 ])
107 );
108 continue;
109 }
110
111 if (visible) {
112 container_internet_v6.appendChild(
113 E('p', {'class': 'mt-2'}, [
114 E('span', {'class': ''}, [data[idx][ver].title + ':']),
115 E('span', {'class': classname}, [data[idx][ver].value]),
116 suppelements
117 ])
118 );
119 }
120 }
121 }
122 }
123
124 container_item.appendChild(container_internet_v4);
125 container_item.appendChild(container_internet_v6);
126 } else {
127 for(var idx in data) {
128 container_item.appendChild(
129 E('p', { 'class': 'mt-2'}, [
130 E('span', {'class': ''}, [ data[idx].title + ':' ]),
131 E('span', {'class': ''}, [ data[idx].value ])
132 ])
133 );
134 }
135 }
136
137 container_box.appendChild(container_item);
138 container_box.appendChild(E('hr'));
139 container_wapper.appendChild(container_box);
140 return container_wapper;
141 },
142
143 renderUpdateWanData: function(data, v6) {
144
145 for (var i = 0; i < data.length; i++) {
146 var ifc = data[i];
147
148 if (v6) {
149 this.params.internet.v6.ipprefixv6.value = ifc.getIP6Prefix() || '-';
150 this.params.internet.v6.gatewayv6.value = ifc.getGateway6Addr() || '-';
151 this.params.internet.v6.protocol.value= ifc.getI18n() || E('em', _('Not connected'));
152 this.params.internet.v6.addrsv6.value = ifc.getIP6Addrs() || [ '-' ];
153 this.params.internet.v6.dnsv6.value = ifc.getDNS6Addrs() || [ '-' ];
154 this.params.internet.v6.connected.value = ifc.isUp();
155 } else {
156 var uptime = ifc.getUptime();
157 this.params.internet.v4.uptime.value = (uptime > 0) ? '%t'.format(uptime) : '-';
158 this.params.internet.v4.protocol.value= ifc.getI18n() || E('em', _('Not connected'));
159 this.params.internet.v4.gatewayv4.value = ifc.getGatewayAddr() || '0.0.0.0';
160 this.params.internet.v4.connected.value = ifc.isUp();
161 this.params.internet.v4.addrsv4.value = ifc.getIPAddrs() || [ '-'];
162 this.params.internet.v4.dnsv4.value = ifc.getDNSAddrs() || [ '-' ];
163 }
164 }
165 },
166
167 renderInternetBox: function(data) {
168
169 this.params.internet = {
170
171 v4: {
172 title: _('IPv4 Internet'),
173
174 connected: {
175 title: _('Connected'),
176 visible: true,
177 value: false
178 },
179
180 uptime: {
181 title: _('Connected since'),
182 visible: true,
183 value: '-'
184 },
185
186 protocol: {
187 title: _('Protocol'),
188 visible: true,
189 value: '-'
190 },
191
192 addrsv4: {
193 title: _('IPv4'),
194 visible: true,
195 value: [ '-' ]
196 },
197
198 gatewayv4: {
199 title: _('GatewayV4'),
200 visible: true,
201 value: '-'
202 },
203
204 dnsv4: {
205 title: _('DNSv4'),
206 visible: true,
207 value: ['-']
208 }
209 },
210
211 v6: {
212 title: _('IPv6 Internet'),
213
214 connected: {
215 title: _('Connected'),
216 visible: true,
217 value: false
218 },
219
220 uptime: {
221 title: _('Connected since'),
222 visible: true,
223 value: '-'
224 },
225
226 protocol: {
227 title: _('Protocol'),
228 visible: true,
229 value: ' - '
230 },
231
232 ipprefixv6 : {
233 title: _('IPv6 prefix'),
234 visible: true,
235 value: ' - '
236 },
237
238 addrsv6: {
239 title: _('IPv6'),
240 visible: false,
241 value: [ '-' ]
242 },
243
244 gatewayv6: {
245 title: _('GatewayV6'),
246 visible: true,
247 value: '-'
248 },
249
250 dnsv6: {
251 title: _('DNSv6'),
252 visible: true,
253 value: [ '-' ]
254 }
255 }
256 };
257
258 this.renderUpdateWanData(data[0], false);
259 this.renderUpdateWanData(data[1], true);
260
261 return this.renderHtml(this.params.internet, 'internet');
262 },
263
264 renderRouterBox: function(data) {
265
266 var boardinfo = data[2],
267 systeminfo = data[3];
268
269 var datestr = null;
270
271 if (systeminfo.localtime) {
272 var date = new Date(systeminfo.localtime * 1000);
273
274 datestr = '%04d-%02d-%02d %02d:%02d:%02d'.format(
275 date.getUTCFullYear(),
276 date.getUTCMonth() + 1,
277 date.getUTCDate(),
278 date.getUTCHours(),
279 date.getUTCMinutes(),
280 date.getUTCSeconds()
281 );
282 }
283
284 this.params.router = {
285 uptime: {
286 title: _('Uptime'),
287 value: systeminfo.uptime ? '%t'.format(systeminfo.uptime) : null,
288 },
289
290 localtime: {
291 title: _('Local Time'),
292 value: datestr
293 },
294
295 kernel: {
296 title: _('Kernel Version'),
297 value: boardinfo.kernel
298 },
299
300 model: {
301 title: _('Model'),
302 value: boardinfo.model
303 },
304
305 system: {
306 title: _('Architecture'),
307 value: boardinfo.system
308 },
309
310 release: {
311 title: _('Firmware Version'),
312 value: boardinfo.release.description
313 }
314 };
315
316 return this.renderHtml(this.params.router, 'router');
317 },
318
319 render: function(data) {
320 return [this.renderInternetBox(data), this.renderRouterBox(data)];
321 }
322 });