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