luci-app-olsr: handle empty result for non-status tables
[oweals/luci.git] / modules / luci-mod-system / htdocs / luci-static / resources / view / system / system.js
1 'use strict';
2 'require uci';
3 'require rpc';
4 'require form';
5
6 var callInitList, callInitAction, callTimezone,
7     callGetLocaltime, callSetLocaltime, CBILocalTime;
8
9 callInitList = rpc.declare({
10         object: 'luci',
11         method: 'initList',
12         params: [ 'name' ],
13         expect: { result: {} },
14         filter: function(res) {
15                 for (var k in res)
16                         return +res[k].enabled;
17                 return null;
18         }
19 });
20
21 callInitAction = rpc.declare({
22         object: 'luci',
23         method: 'initCall',
24         params: [ 'name', 'action' ],
25         expect: { result: false }
26 });
27
28 callGetLocaltime = rpc.declare({
29         object: 'luci',
30         method: 'getLocaltime',
31         expect: { localtime: 0 }
32 });
33
34 callSetLocaltime = rpc.declare({
35         object: 'luci',
36         method: 'setLocaltime',
37         params: [ 'localtime' ],
38         expect: { localtime: 0 }
39 });
40
41 callTimezone = rpc.declare({
42         object: 'luci',
43         method: 'timezone',
44         expect: { result: {} }
45 });
46
47 CBILocalTime = form.DummyValue.extend({
48         renderWidget: function(section_id, option_id, cfgvalue) {
49                 return E([], [
50                         E('span', { 'id': 'localtime' },
51                                 new Date(cfgvalue * 1000).toLocaleString()),
52                         ' ',
53                         E('button', {
54                                 'class': 'cbi-button cbi-button-apply',
55                                 'click': function() {
56                                         this.blur();
57                                         this.classList.add('spinning');
58                                         this.disabled = true;
59                                         callSetLocaltime(Math.floor(Date.now() / 1000)).then(L.bind(function() {
60                                                 this.classList.remove('spinning');
61                                                 this.disabled = false;
62                                         }, this));
63                                 }
64                         }, _('Sync with browser')),
65                         ' ',
66                         this.ntpd_support ? E('button', {
67                                 'class': 'cbi-button cbi-button-apply',
68                                 'click': function() {
69                                         this.blur();
70                                         this.classList.add('spinning');
71                                         this.disabled = true;
72                                         callInitAction('sysntpd', 'restart').then(L.bind(function() {
73                                                 this.classList.remove('spinning');
74                                                 this.disabled = false;
75                                         }, this));
76                                 }
77                         }, _('Sync with NTP-Server')) : ''
78                 ]);
79         },
80 });
81
82 return L.view.extend({
83         load: function() {
84                 return Promise.all([
85                         callInitList('sysntpd'),
86                         callInitList('zram'),
87                         callTimezone(),
88                         callGetLocaltime(),
89                         uci.load('luci'),
90                         uci.load('system')
91                 ]);
92         },
93
94         render: function(rpc_replies) {
95                 var ntpd_support = rpc_replies[0],
96                     zram_support = rpc_replies[1],
97                     timezones = rpc_replies[2],
98                     localtime = rpc_replies[3],
99                     ntp_setup, ntp_enabled, m, s, o;
100
101                 m = new form.Map('system',
102                         _('System'),
103                         _('Here you can configure the basic aspects of your device like its hostname or the timezone.'));
104
105                 m.chain('luci');
106                 m.tabbed = true;
107
108                 s = m.section(form.TypedSection, 'system', _('System Properties'));
109                 s.anonymous = true;
110                 s.addremove = false;
111
112                 s.tab('general', _('General Settings'));
113                 s.tab('logging', _('Logging'));
114                 s.tab('timesync', _('Time Synchronization'));
115                 s.tab('language', _('Language and Style'));
116
117                 /*
118                  * System Properties
119                  */
120
121                 o = s.taboption('general', CBILocalTime, '_systime', _('Local Time'));
122                 o.cfgvalue = function() { return localtime };
123                 o.ntpd_support = ntpd_support;
124
125                 o = s.taboption('general', form.Value, 'hostname', _('Hostname'));
126                 o.datatype = 'hostname';
127
128                 o = s.taboption('general', form.ListValue, 'zonename', _('Timezone'));
129                 o.value('UTC');
130
131                 var zones = Object.keys(timezones || {}).sort();
132                 for (var i = 0; i < zones.length; i++)
133                         o.value(zones[i]);
134
135                 o.write = function(section_id, formvalue) {
136                         var tz = timezones[formvalue] ? timezones[formvalue].tzstring : null;
137                         uci.set('system', section_id, 'zonename', formvalue);
138                         uci.set('system', section_id, 'timezone', tz);
139                 };
140
141                 /*
142                  * Logging
143                  */
144
145                 o = s.taboption('logging', form.Value, 'log_size', _('System log buffer size'), "kiB")
146                 o.optional    = true
147                 o.placeholder = 16
148                 o.datatype    = 'uinteger'
149
150                 o = s.taboption('logging', form.Value, 'log_ip', _('External system log server'))
151                 o.optional    = true
152                 o.placeholder = '0.0.0.0'
153                 o.datatype    = 'ip4addr'
154
155                 o = s.taboption('logging', form.Value, 'log_port', _('External system log server port'))
156                 o.optional    = true
157                 o.placeholder = 514
158                 o.datatype    = 'port'
159
160                 o = s.taboption('logging', form.ListValue, 'log_proto', _('External system log server protocol'))
161                 o.value('udp', 'UDP')
162                 o.value('tcp', 'TCP')
163
164                 o = s.taboption('logging', form.Value, 'log_file', _('Write system log to file'))
165                 o.optional    = true
166                 o.placeholder = '/tmp/system.log'
167
168                 o = s.taboption('logging', form.ListValue, 'conloglevel', _('Log output level'))
169                 o.value(8, _('Debug'))
170                 o.value(7, _('Info'))
171                 o.value(6, _('Notice'))
172                 o.value(5, _('Warning'))
173                 o.value(4, _('Error'))
174                 o.value(3, _('Critical'))
175                 o.value(2, _('Alert'))
176                 o.value(1, _('Emergency'))
177
178                 o = s.taboption('logging', form.ListValue, 'cronloglevel', _('Cron Log Level'))
179                 o.default = 8
180                 o.value(5, _('Debug'))
181                 o.value(8, _('Normal'))
182                 o.value(9, _('Warning'))
183
184                 /*
185                  * Zram Properties
186                  */
187
188                 if (zram_support != null) {
189                         s.tab('zram', _('ZRam Settings'));
190
191                         o = s.taboption('zram', form.Value, 'zram_size_mb', _('ZRam Size'), _('Size of the ZRam device in megabytes'));
192                         o.optional    = true;
193                         o.placeholder = 16;
194                         o.datatype    = 'uinteger';
195
196                         o = s.taboption('zram', form.ListValue, 'zram_comp_algo', _('ZRam Compression Algorithm'));
197                         o.optional    = true;
198                         o.placeholder = 'lzo';
199                         o.value('lzo', 'lzo');
200                         o.value('lz4', 'lz4');
201                         o.value('deflate', 'deflate');
202
203                         o = s.taboption('zram', form.Value, 'zram_comp_streams', _('ZRam Compression Streams'), _('Number of parallel threads used for compression'));
204                         o.optional    = true;
205                         o.placeholder = 1;
206                         o.datatype    = 'uinteger';
207                 }
208
209                 /*
210                  * Language & Style
211                  */
212
213                 o = s.taboption('language', form.ListValue, '_lang', _('Language'))
214                 o.uciconfig = 'luci';
215                 o.ucisection = 'main';
216                 o.ucioption = 'lang';
217                 o.value('auto');
218
219                 var k = Object.keys(uci.get('luci', 'languages') || {}).sort();
220                 for (var i = 0; i < k.length; i++)
221                         if (k[i].charAt(0) != '.')
222                                 o.value(k[i], uci.get('luci', 'languages', k[i]));
223
224                 o = s.taboption('language', form.ListValue, '_mediaurlbase', _('Design'))
225                 o.uciconfig = 'luci';
226                 o.ucisection = 'main';
227                 o.ucioption = 'mediaurlbase';
228
229                 var k = Object.keys(uci.get('luci', 'themes') || {}).sort();
230                 for (var i = 0; i < k.length; i++)
231                         if (k[i].charAt(0) != '.')
232                                 o.value(uci.get('luci', 'themes', k[i]), k[i]);
233
234                 /*
235                  * NTP
236                  */
237
238                 if (ntpd_support != null) {
239                         var default_servers = [
240                                 '0.openwrt.pool.ntp.org', '1.openwrt.pool.ntp.org',
241                                 '2.openwrt.pool.ntp.org', '3.openwrt.pool.ntp.org'
242                         ];
243
244                         o = s.taboption('timesync', form.Flag, 'enabled', _('Enable NTP client'));
245                         o.rmempty = false;
246                         o.ucisection = 'ntp';
247                         o.default = o.disabled;
248                         o.write = function(section_id, value) {
249                                 ntpd_support = +value;
250
251                                 if (ntpd_support && !uci.get('system', 'ntp')) {
252                                         uci.add('system', 'timeserver', 'ntp');
253                                         uci.set('system', 'ntp', 'server', default_servers);
254                                 }
255
256                                 if (!ntpd_support)
257                                         uci.set('system', 'ntp', 'enabled', 0);
258                                 else
259                                         uci.unset('system', 'ntp', 'enabled');
260
261                                 return callInitAction('sysntpd', 'enable');
262                         };
263                         o.load = function(section_id) {
264                                 return (ntpd_support == 1 &&
265                                         uci.get('system', 'ntp') != null &&
266                                         uci.get('system', 'ntp', 'enabled') != 0) ? '1' : '0';
267                         };
268
269                         o = s.taboption('timesync', form.Flag, 'enable_server', _('Provide NTP server'));
270                         o.ucisection = 'ntp';
271                         o.depends('enabled', '1');
272
273                         o = s.taboption('timesync', form.DynamicList, 'server', _('NTP server candidates'));
274                         o.datatype = 'host(0)';
275                         o.ucisection = 'ntp';
276                         o.depends('enabled', '1');
277                         o.remove = function() {}; // retain server list even if disabled
278                         o.load = function(section_id) {
279                                 return uci.get('system', 'ntp')
280                                         ? uci.get('system', 'ntp', 'server')
281                                         : default_servers;
282                         };
283                 }
284
285                 return m.render().then(function(mapEl) {
286                         L.Poll.add(function() {
287                                 return callGetLocaltime().then(function(t) {
288                                         mapEl.querySelector('#localtime').innerHTML = new Date(t * 1000).toLocaleString();
289                                 });
290                         });
291
292                         return mapEl;
293                 });
294         }
295 });