1ed8f64d8f0d1226529cd6a8a75f0b136b915802
[project/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', { '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
107 s = m.section(form.TypedSection, 'system', _('System Properties'));
108 s.anonymous = true;
109 s.addremove = false;
110
111 s.tab('general', _('General Settings'));
112 s.tab('logging', _('Logging'));
113 s.tab('timesync', _('Time Synchronization'));
114 s.tab('language', _('Language and Style'));
115
116 /*
117 * System Properties
118 */
119
120 o = s.taboption('general', CBILocalTime, '_systime', _('Local Time'));
121 o.cfgvalue = function() { return localtime };
122 o.ntpd_support = ntpd_support;
123
124 o = s.taboption('general', form.Value, 'hostname', _('Hostname'));
125 o.datatype = 'hostname';
126
127 o = s.taboption('general', form.ListValue, 'zonename', _('Timezone'));
128 o.value('UTC');
129
130 var zones = Object.keys(timezones || {}).sort();
131 for (var i = 0; i < zones.length; i++)
132 o.value(zones[i]);
133
134 o.write = function(section_id, formvalue) {
135 var tz = timezones[formvalue] ? timezones[formvalue].tzstring : null;
136 uci.set('system', section_id, 'zonename', formvalue);
137 uci.set('system', section_id, 'timezone', tz);
138 };
139
140 /*
141 * Logging
142 */
143
144 o = s.taboption('logging', form.Value, 'log_size', _('System log buffer size'), "kiB")
145 o.optional = true
146 o.placeholder = 16
147 o.datatype = 'uinteger'
148
149 o = s.taboption('logging', form.Value, 'log_ip', _('External system log server'))
150 o.optional = true
151 o.placeholder = '0.0.0.0'
152 o.datatype = 'ip4addr'
153
154 o = s.taboption('logging', form.Value, 'log_port', _('External system log server port'))
155 o.optional = true
156 o.placeholder = 514
157 o.datatype = 'port'
158
159 o = s.taboption('logging', form.ListValue, 'log_proto', _('External system log server protocol'))
160 o.value('udp', 'UDP')
161 o.value('tcp', 'TCP')
162
163 o = s.taboption('logging', form.Value, 'log_file', _('Write system log to file'))
164 o.optional = true
165 o.placeholder = '/tmp/system.log'
166
167 o = s.taboption('logging', form.ListValue, 'conloglevel', _('Log output level'))
168 o.value(8, _('Debug'))
169 o.value(7, _('Info'))
170 o.value(6, _('Notice'))
171 o.value(5, _('Warning'))
172 o.value(4, _('Error'))
173 o.value(3, _('Critical'))
174 o.value(2, _('Alert'))
175 o.value(1, _('Emergency'))
176
177 o = s.taboption('logging', form.ListValue, 'cronloglevel', _('Cron Log Level'))
178 o.default = 8
179 o.value(5, _('Debug'))
180 o.value(8, _('Normal'))
181 o.value(9, _('Warning'))
182
183 /*
184 * Zram Properties
185 */
186
187 if (zram_support != null) {
188 s.tab('zram', _('ZRam Settings'));
189
190 o = s.taboption('zram', form.Value, 'zram_size_mb', _('ZRam Size'), _('Size of the ZRam device in megabytes'));
191 o.optional = true;
192 o.placeholder = 16;
193 o.datatype = 'uinteger';
194
195 o = s.taboption('zram', form.ListValue, 'zram_comp_algo', _('ZRam Compression Algorithm'));
196 o.optional = true;
197 o.placeholder = 'lzo';
198 o.value('lzo', 'lzo');
199 o.value('lz4', 'lz4');
200 o.value('deflate', 'deflate');
201
202 o = s.taboption('zram', form.Value, 'zram_comp_streams', _('ZRam Compression Streams'), _('Number of parallel threads used for compression'));
203 o.optional = true;
204 o.placeholder = 1;
205 o.datatype = 'uinteger';
206 }
207
208 /*
209 * Language & Style
210 */
211
212 o = s.taboption('language', form.ListValue, '_lang', _('Language'))
213 o.uciconfig = 'luci';
214 o.ucisection = 'main';
215 o.ucioption = 'lang';
216 o.value('auto');
217
218 var k = Object.keys(uci.get('luci', 'languages') || {}).sort();
219 for (var i = 0; i < k.length; i++)
220 if (k[i].charAt(0) != '.')
221 o.value(k[i], uci.get('luci', 'languages', k[i]));
222
223 o = s.taboption('language', form.ListValue, '_mediaurlbase', _('Design'))
224 o.uciconfig = 'luci';
225 o.ucisection = 'main';
226 o.ucioption = 'mediaurlbase';
227
228 var k = Object.keys(uci.get('luci', 'themes') || {}).sort();
229 for (var i = 0; i < k.length; i++)
230 if (k[i].charAt(0) != '.')
231 o.value(uci.get('luci', 'themes', k[i]), k[i]);
232
233 /*
234 * NTP
235 */
236
237 if (ntpd_support != null) {
238 var default_servers = [
239 '0.openwrt.pool.ntp.org', '1.openwrt.pool.ntp.org',
240 '2.openwrt.pool.ntp.org', '3.openwrt.pool.ntp.org'
241 ];
242
243 o = s.taboption('timesync', form.Flag, 'enabled', _('Enable NTP client'));
244 o.rmempty = false;
245 o.ucisection = 'ntp';
246 o.default = o.disabled;
247 o.write = function(section_id, value) {
248 ntpd_support = +value;
249
250 if (ntpd_support && !uci.get('system', 'ntp')) {
251 uci.add('system', 'timeserver', 'ntp');
252 uci.set('system', 'ntp', 'server', default_servers);
253 }
254
255 if (!ntpd_support)
256 uci.set('system', 'ntp', 'enabled', 0);
257 else
258 uci.unset('system', 'ntp', 'enabled');
259
260 return callInitAction('sysntpd', 'enable');
261 };
262 o.load = function(section_id) {
263 return (ntpd_support == 1 &&
264 uci.get('system', 'ntp') != null &&
265 uci.get('system', 'ntp', 'enabled') != 0) ? '1' : '0';
266 };
267
268 o = s.taboption('timesync', form.Flag, 'enable_server', _('Provide NTP server'));
269 o.ucisection = 'ntp';
270 o.depends('enabled', '1');
271
272 o = s.taboption('timesync', form.DynamicList, 'server', _('NTP server candidates'));
273 o.datatype = 'host(0)';
274 o.ucisection = 'ntp';
275 o.depends('enabled', '1');
276 o.remove = function() {}; // retain server list even if disabled
277 o.load = function(section_id) {
278 return uci.get('system', 'ntp')
279 ? uci.get('system', 'ntp', 'server')
280 : default_servers;
281 };
282 }
283
284 return m.render().then(function(mapEl) {
285 L.Poll.add(function() {
286 return callGetLocaltime().then(function(t) {
287 mapEl.querySelector('#localtime').innerHTML = new Date(t * 1000).toLocaleString();
288 });
289 });
290
291 return mapEl;
292 });
293 }
294 });