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