Merge pull request #1735 from sumpfralle/olsr-jsoninfo-parser-handle-empty-result
[oweals/luci.git] / modules / luci-mod-system / htdocs / luci-static / resources / view / system / system.js
index bbe483c0ab6439e5ef46579066a6c3b7653eb8af..ac6586af4692001da6a058134954a3b3d9f52fd3 100644 (file)
@@ -3,98 +3,98 @@
 'require rpc';
 'require form';
 
-return L.view.extend({
-       callInitList: rpc.declare({
-               object: 'luci',
-               method: 'initList',
-               params: [ 'name' ],
-               expect: { result: {} },
-               filter: function(res) {
-                       for (var k in res)
-                               return +res[k].enabled;
-                       return null;
-               }
-       }),
-
-       callInitAction: rpc.declare({
-               object: 'luci',
-               method: 'initCall',
-               params: [ 'name', 'action' ],
-               expect: { result: false }
-       }),
-
-       callLocaltime: rpc.declare({
-               object: 'luci',
-               method: 'localtime',
-               expect: { localtime: 0 }
-       }),
-
-       callTimezone: rpc.declare({
-               object: 'luci',
-               method: 'timezone',
-               expect: { result: {} }
-       }),
-
-       CBILocalTime: form.DummyValue.extend({
-               renderWidget: function(section_id, option_id, cfgvalue) {
-                       return E([], [
-                               E('span', { 'id': 'localtime' },
-                                       new Date(cfgvalue * 1000).toLocaleString()),
-                               ' ',
-                               E('button', {
-                                       'class': 'cbi-button cbi-button-apply',
-                                       'click': L.bind(function(ev) {
-                                               ev.target.blur();
-                                               ev.target.classList.add('spinning');
-                                               ev.target.disabled = true;
-                                               this.callLocaltime(Math.floor(Date.now() / 1000)).then(function() {
-                                                       ev.target.classList.remove('spinning');
-                                                       ev.target.disabled = false;
-                                               });
-                                       }, this)
-                               }, _('Sync with browser')),
-                               ' ',
-                               this.ntpd_support ? E('button', {
-                                       'class': 'cbi-button cbi-button-apply',
-                                       'click': L.bind(function(ev) {
-                                               ev.target.blur();
-                                               ev.target.classList.add('spinning');
-                                               ev.target.disabled = true;
-                                               this.callLocaltime(Math.floor(Date.now() / 1000)).then(function() {
-                                                       ev.target.classList.remove('spinning');
-                                                       ev.target.disabled = false;
-                                               });
-                                       }, this)
-                               }, _('Sync with NTP-Server')) : ''
-                       ]);
-               },
-       }),
+var callInitList, callInitAction, callTimezone,
+    callGetLocaltime, callSetLocaltime, CBILocalTime;
+
+callInitList = rpc.declare({
+       object: 'luci',
+       method: 'getInitList',
+       params: [ 'name' ],
+       expect: { '': {} },
+       filter: function(res) {
+               for (var k in res)
+                       return +res[k].enabled;
+               return null;
+       }
+});
+
+callInitAction = rpc.declare({
+       object: 'luci',
+       method: 'setInitAction',
+       params: [ 'name', 'action' ],
+       expect: { result: false }
+});
+
+callGetLocaltime = rpc.declare({
+       object: 'luci',
+       method: 'getLocaltime',
+       expect: { result: 0 }
+});
 
+callSetLocaltime = rpc.declare({
+       object: 'luci',
+       method: 'setLocaltime',
+       params: [ 'localtime' ],
+       expect: { result: 0 }
+});
+
+callTimezone = rpc.declare({
+       object: 'luci',
+       method: 'getTimezones',
+       expect: { '': {} }
+});
+
+CBILocalTime = form.DummyValue.extend({
+       renderWidget: function(section_id, option_id, cfgvalue) {
+               return E([], [
+                       E('span', {}, [
+                               E('input', {
+                                       'id': 'localtime',
+                                       'type': 'text',
+                                       'readonly': true,
+                                       'value': new Date(cfgvalue * 1000).toLocaleString()
+                               })
+                       ]),
+                       ' ',
+                       E('button', {
+                               'class': 'cbi-button cbi-button-apply',
+                               'click': L.ui.createHandlerFn(this, function() {
+                                       return callSetLocaltime(Math.floor(Date.now() / 1000));
+                               })
+                       }, _('Sync with browser')),
+                       ' ',
+                       this.ntpd_support ? E('button', {
+                               'class': 'cbi-button cbi-button-apply',
+                               'click': L.ui.createHandlerFn(this, function() {
+                                       return callInitAction('sysntpd', 'restart');
+                               })
+                       }, _('Sync with NTP-Server')) : ''
+               ]);
+       },
+});
+
+return L.view.extend({
        load: function() {
-               rpc.batch();
-               this.callInitList('sysntpd');
-               this.callInitList('zram');
-               this.callTimezone();
-               this.callLocaltime();
-               uci.load('luci');
-               uci.load('system');
-               return rpc.flush();
+               return Promise.all([
+                       callInitList('sysntpd'),
+                       callTimezone(),
+                       callGetLocaltime(),
+                       uci.load('luci'),
+                       uci.load('system')
+               ]);
        },
 
        render: function(rpc_replies) {
-               var ntpd_support = rpc_replies[0],
-                   zram_support = rpc_replies[1],
-                   timezones = rpc_replies[2],
-                   localtime = rpc_replies[3],
-                   view = this,
-                   ntp_setup, ntp_enabled, m, s, o;
+               var ntpd_enabled = rpc_replies[0],
+                   timezones = rpc_replies[1],
+                   localtime = rpc_replies[2],
+                   m, s, o;
 
                m = new form.Map('system',
                        _('System'),
                        _('Here you can configure the basic aspects of your device like its hostname or the timezone.'));
 
                m.chain('luci');
-               m.tabbed = true;
 
                s = m.section(form.TypedSection, 'system', _('System Properties'));
                s.anonymous = true;
@@ -109,9 +109,9 @@ return L.view.extend({
                 * 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.ntpd_support = ntpd_support;
+               o.ntpd_support = ntpd_enabled;
 
                o = s.taboption('general', form.Value, 'hostname', _('Hostname'));
                o.datatype = 'hostname';
@@ -176,7 +176,7 @@ return L.view.extend({
                 * Zram Properties
                 */
 
-               if (zram_support != null) {
+               if (L.hasSystemFeature('zram')) {
                        s.tab('zram', _('ZRam Settings'));
 
                        o = s.taboption('zram', form.Value, 'zram_size_mb', _('ZRam Size'), _('Size of the ZRam device in megabytes'));
@@ -226,7 +226,7 @@ return L.view.extend({
                 * NTP
                 */
 
-               if (ntpd_support != null) {
+               if (L.hasSystemFeature('sysntpd')) {
                        var default_servers = [
                                '0.openwrt.pool.ntp.org', '1.openwrt.pool.ntp.org',
                                '2.openwrt.pool.ntp.org', '3.openwrt.pool.ntp.org'
@@ -237,22 +237,22 @@ return L.view.extend({
                        o.ucisection = 'ntp';
                        o.default = o.disabled;
                        o.write = function(section_id, value) {
-                               ntpd_support = +value;
+                               ntpd_enabled = +value;
 
-                               if (ntpd_support && !uci.get('system', 'ntp')) {
+                               if (ntpd_enabled && !uci.get('system', 'ntp')) {
                                        uci.add('system', 'timeserver', 'ntp');
                                        uci.set('system', 'ntp', 'server', default_servers);
                                }
 
-                               if (!ntpd_support)
+                               if (!ntpd_enabled)
                                        uci.set('system', 'ntp', 'enabled', 0);
                                else
                                        uci.unset('system', 'ntp', 'enabled');
 
-                               return view.callInitAction('sysntpd', 'enable');
+                               return callInitAction('sysntpd', 'enable');
                        };
                        o.load = function(section_id) {
-                               return (ntpd_support == 1 &&
+                               return (ntpd_enabled == 1 &&
                                        uci.get('system', 'ntp') != null &&
                                        uci.get('system', 'ntp', 'enabled') != 0) ? '1' : '0';
                        };
@@ -273,13 +273,14 @@ return L.view.extend({
                        };
                }
 
-               window.setInterval(L.bind(function() {
-                       this.callLocaltime().then(function(t) {
-                               var lt = document.getElementById('localtime');
-                               if (lt) lt.innerHTML = new Date(t * 1000).toLocaleString();
+               return m.render().then(function(mapEl) {
+                       L.Poll.add(function() {
+                               return callGetLocaltime().then(function(t) {
+                                       mapEl.querySelector('#localtime').value = new Date(t * 1000).toLocaleString();
+                               });
                        });
-               }, this), 5000);
 
-               return m.render();
+                       return mapEl;
+               });
        }
 });