luci-mod-system: use client side cbi forms for system and led config
[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 return L.view.extend({
7         callInitList: rpc.declare({
8                 object: 'luci',
9                 method: 'initList',
10                 params: [ 'name' ],
11                 expect: { result: {} },
12                 filter: function(res) {
13                         for (var k in res)
14                                 return +res[k].enabled;
15                         return null;
16                 }
17         }),
18
19         callInitAction: rpc.declare({
20                 object: 'luci',
21                 method: 'initCall',
22                 params: [ 'name', 'action' ],
23                 expect: { result: false }
24         }),
25
26         callLocaltime: rpc.declare({
27                 object: 'luci',
28                 method: 'localtime',
29                 expect: { localtime: 0 }
30         }),
31
32         callTimezone: rpc.declare({
33                 object: 'luci',
34                 method: 'timezone',
35                 expect: { result: {} }
36         }),
37
38         CBILocalTime: form.DummyValue.extend({
39                 renderWidget: function(section_id, option_id, cfgvalue) {
40                         return E([], [
41                                 E('span', { 'id': 'localtime' },
42                                         new Date(cfgvalue * 1000).toLocaleString()),
43                                 ' ',
44                                 E('button', {
45                                         'class': 'cbi-button cbi-button-apply',
46                                         'click': L.bind(function(ev) {
47                                                 ev.target.blur();
48                                                 ev.target.classList.add('spinning');
49                                                 ev.target.disabled = true;
50                                                 this.callLocaltime(Math.floor(Date.now() / 1000)).then(function() {
51                                                         ev.target.classList.remove('spinning');
52                                                         ev.target.disabled = false;
53                                                 });
54                                         }, this)
55                                 }, _('Sync with browser')),
56                                 ' ',
57                                 this.ntpd_support ? E('button', {
58                                         'class': 'cbi-button cbi-button-apply',
59                                         'click': L.bind(function(ev) {
60                                                 ev.target.blur();
61                                                 ev.target.classList.add('spinning');
62                                                 ev.target.disabled = true;
63                                                 this.callLocaltime(Math.floor(Date.now() / 1000)).then(function() {
64                                                         ev.target.classList.remove('spinning');
65                                                         ev.target.disabled = false;
66                                                 });
67                                         }, this)
68                                 }, _('Sync with NTP-Server')) : ''
69                         ]);
70                 },
71         }),
72
73         load: function() {
74                 rpc.batch();
75                 this.callInitList('sysntpd');
76                 this.callInitList('zram');
77                 this.callTimezone();
78                 this.callLocaltime();
79                 uci.load('luci');
80                 uci.load('system');
81                 return rpc.flush();
82         },
83
84         render: function(rpc_replies) {
85                 var ntpd_support = rpc_replies[0],
86                     zram_support = rpc_replies[1],
87                     timezones = rpc_replies[2],
88                     localtime = rpc_replies[3],
89                     view = this,
90                     ntp_setup, ntp_enabled, m, s, o;
91
92                 m = new form.Map('system',
93                         _('System'),
94                         _('Here you can configure the basic aspects of your device like its hostname or the timezone.'));
95
96                 m.chain('luci');
97                 m.tabbed = true;
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', this.CBILocalTime, '_systime', _('Local Time'));
113                 o.cfgvalue = function() { return localtime };
114                 o.ntpd_support = ntpd_support;
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 (zram_support != null) {
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 (ntpd_support != null) {
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_support = +value;
241
242                                 if (ntpd_support && !uci.get('system', 'ntp')) {
243                                         uci.add('system', 'timeserver', 'ntp');
244                                         uci.set('system', 'ntp', 'server', default_servers);
245                                 }
246
247                                 if (!ntpd_support)
248                                         uci.set('system', 'ntp', 'enabled', 0);
249                                 else
250                                         uci.unset('system', 'ntp', 'enabled');
251
252                                 return view.callInitAction('sysntpd', 'enable');
253                         };
254                         o.load = function(section_id) {
255                                 return (ntpd_support == 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                 window.setInterval(L.bind(function() {
277                         this.callLocaltime().then(function(t) {
278                                 var lt = document.getElementById('localtime');
279                                 if (lt) lt.innerHTML = new Date(t * 1000).toLocaleString();
280                         });
281                 }, this), 5000);
282
283                 return m.render();
284         }
285 });