970016c42bd9079d633d6de6e6ddc7cfaf28770a
[oweals/luci.git] /
1 'use strict';
2 'require rpc';
3 'require uci';
4 'require form';
5
6 var callUSB = rpc.declare({
7         object: 'luci',
8         method: 'getUSBDevices',
9         expect: { 'ports': [] }
10 });
11
12 return L.Class.extend({
13         trigger: _('usbport (kernel)'),
14         kernel: true,
15         addFormOptions(s){
16                 var o;
17
18                 o = s.option(form.Value, 'port', _('USB Ports'));
19                 o.depends('trigger', 'usbport');
20                 o.rmempty = true;
21                 o.modalonly = true;
22                 o.load = function(s) {
23                         return Promise.all([
24                                 callUSB()
25                         ]).then(L.bind(function(usbport){
26                                 for (var i = 0; i < usbport[0].length; i++)
27                                         o.value(usbport[0][i].port, _('Port %s').format(usbport[0][i].port));
28                         },this));
29                 };
30                 o.cfgvalue = function(section_id) {
31                         var ports = [],
32                                 value = uci.get('system', section_id, 'port');
33
34                         if (!Array.isArray(value))
35                                 value = String(value || '').split(/\s+/);
36
37                         for (var i = 0; i < value.length; i++)
38                                 if (value[i].match(/^(\d+)-(\d+)$/))
39                                         ports.push('usb%d-port%d'.format(Regexp.$1, Regexp.$2));
40                                 else
41                                         ports.push(value[i]);
42
43                         return ports;
44                 };
45         }
46 });