luci-mod-system: rework leds.js and system.js views

- Drop manual RPC batch usage
- Use LuCI.Poll for status updates

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
This commit is contained in:
Jo-Philipp Wich 2019-04-05 08:17:10 +02:00
parent b21a2813b4
commit 5264a943b9
2 changed files with 121 additions and 117 deletions

View file

@ -3,25 +3,26 @@
'require rpc'; 'require rpc';
'require form'; 'require form';
return L.view.extend({ var callInitAction, callLeds, callUSB, callNetdevs;
callInitAction: rpc.declare({
callInitAction = rpc.declare({
object: 'luci', object: 'luci',
method: 'initCall', method: 'initCall',
params: [ 'name', 'action' ], params: [ 'name', 'action' ],
expect: { result: false } expect: { result: false }
}), });
callLeds: rpc.declare({ callLeds = rpc.declare({
object: 'luci', object: 'luci',
method: 'leds' method: 'leds'
}), });
callUSB: rpc.declare({ callUSB = rpc.declare({
object: 'luci', object: 'luci',
method: 'usb' method: 'usb'
}), });
callNetdevs: rpc.declare({ callNetdevs = rpc.declare({
object: 'luci', object: 'luci',
method: 'ifaddrs', method: 'ifaddrs',
expect: { result: [] }, expect: { result: [] },
@ -31,14 +32,15 @@ return L.view.extend({
devs[res[i].name] = true; devs[res[i].name] = true;
return Object.keys(devs).sort(); return Object.keys(devs).sort();
} }
}), });
return L.view.extend({
load: function() { load: function() {
rpc.batch(); return Promise.all([
this.callLeds(); callLeds(),
this.callUSB(); callUSB(),
this.callNetdevs(); callNetdevs()
return rpc.flush(); ]);
}, },
render: function(results) { render: function(results) {

View file

@ -3,8 +3,9 @@
'require rpc'; 'require rpc';
'require form'; 'require form';
return L.view.extend({ var callInitList, callInitAction, callLocaltime, callTimezone, CBILocalTime;
callInitList: rpc.declare({
callInitList = rpc.declare({
object: 'luci', object: 'luci',
method: 'initList', method: 'initList',
params: [ 'name' ], params: [ 'name' ],
@ -14,28 +15,28 @@ return L.view.extend({
return +res[k].enabled; return +res[k].enabled;
return null; return null;
} }
}), });
callInitAction: rpc.declare({ callInitAction = rpc.declare({
object: 'luci', object: 'luci',
method: 'initCall', method: 'initCall',
params: [ 'name', 'action' ], params: [ 'name', 'action' ],
expect: { result: false } expect: { result: false }
}), });
callLocaltime: rpc.declare({ callLocaltime = rpc.declare({
object: 'luci', object: 'luci',
method: 'localtime', method: 'localtime',
expect: { localtime: 0 } expect: { localtime: 0 }
}), });
callTimezone: rpc.declare({ callTimezone = rpc.declare({
object: 'luci', object: 'luci',
method: 'timezone', method: 'timezone',
expect: { result: {} } expect: { result: {} }
}), });
CBILocalTime: form.DummyValue.extend({ CBILocalTime = form.DummyValue.extend({
renderWidget: function(section_id, option_id, cfgvalue) { renderWidget: function(section_id, option_id, cfgvalue) {
return E([], [ return E([], [
E('span', { 'id': 'localtime' }, E('span', { 'id': 'localtime' },
@ -43,42 +44,43 @@ return L.view.extend({
' ', ' ',
E('button', { E('button', {
'class': 'cbi-button cbi-button-apply', 'class': 'cbi-button cbi-button-apply',
'click': L.bind(function(ev) { 'click': function() {
ev.target.blur(); this.blur();
ev.target.classList.add('spinning'); this.classList.add('spinning');
ev.target.disabled = true; this.disabled = true;
this.callLocaltime(Math.floor(Date.now() / 1000)).then(function() { callLocaltime(Math.floor(Date.now() / 1000)).then(L.bind(function() {
ev.target.classList.remove('spinning'); this.classList.remove('spinning');
ev.target.disabled = false; this.disabled = false;
}); }, this));
}, this) }
}, _('Sync with browser')), }, _('Sync with browser')),
' ', ' ',
this.ntpd_support ? E('button', { this.ntpd_support ? E('button', {
'class': 'cbi-button cbi-button-apply', 'class': 'cbi-button cbi-button-apply',
'click': L.bind(function(ev) { 'click': function() {
ev.target.blur(); this.blur();
ev.target.classList.add('spinning'); this.classList.add('spinning');
ev.target.disabled = true; this.disabled = true;
this.callLocaltime(Math.floor(Date.now() / 1000)).then(function() { callInitAction('sysntpd', 'restart').then(L.bind(function() {
ev.target.classList.remove('spinning'); this.classList.remove('spinning');
ev.target.disabled = false; this.disabled = false;
}); }, this));
}, this) }
}, _('Sync with NTP-Server')) : '' }, _('Sync with NTP-Server')) : ''
]); ]);
}, },
}), });
return L.view.extend({
load: function() { load: function() {
rpc.batch(); return Promise.all([
this.callInitList('sysntpd'); callInitList('sysntpd'),
this.callInitList('zram'); callInitList('zram'),
this.callTimezone(); callTimezone(),
this.callLocaltime(); callLocaltime(),
uci.load('luci'); uci.load('luci'),
uci.load('system'); uci.load('system')
return rpc.flush(); ]);
}, },
render: function(rpc_replies) { render: function(rpc_replies) {
@ -86,7 +88,6 @@ return L.view.extend({
zram_support = rpc_replies[1], zram_support = rpc_replies[1],
timezones = rpc_replies[2], timezones = rpc_replies[2],
localtime = rpc_replies[3], localtime = rpc_replies[3],
view = this,
ntp_setup, ntp_enabled, m, s, o; ntp_setup, ntp_enabled, m, s, o;
m = new form.Map('system', m = new form.Map('system',
@ -109,7 +110,7 @@ return L.view.extend({
* System Properties * System Properties
*/ */
o = s.taboption('general', this.CBILocalTime, '_systime', _('Local Time')); o = s.taboption('general', CBILocalTime, '_systime', _('Local Time'));
o.cfgvalue = function() { return localtime }; o.cfgvalue = function() { return localtime };
o.ntpd_support = ntpd_support; o.ntpd_support = ntpd_support;
@ -249,7 +250,7 @@ return L.view.extend({
else else
uci.unset('system', 'ntp', 'enabled'); uci.unset('system', 'ntp', 'enabled');
return view.callInitAction('sysntpd', 'enable'); return callInitAction('sysntpd', 'enable');
}; };
o.load = function(section_id) { o.load = function(section_id) {
return (ntpd_support == 1 && return (ntpd_support == 1 &&
@ -273,13 +274,14 @@ return L.view.extend({
}; };
} }
window.setInterval(L.bind(function() { return m.render().then(function(mapEl) {
this.callLocaltime().then(function(t) { L.Poll.add(function() {
var lt = document.getElementById('localtime'); return callLocaltime().then(function(t) {
if (lt) lt.innerHTML = new Date(t * 1000).toLocaleString(); mapEl.querySelector('#localtime').innerHTML = new Date(t * 1000).toLocaleString();
});
}); });
}, this), 5000);
return m.render(); return mapEl;
});
} }
}); });