summaryrefslogtreecommitdiffstats
path: root/applications/luci-app-ledtrig-usbport/htdocs/luci-static/resources/view/system/led-trigger/usbport.js
blob: 58a71b4ae5876120c0ba351494a6e122a8a74a4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
'use strict';
'require baseclass';
'require rpc';
'require uci';
'require form';

const callUSB = rpc.declare({
	object: 'luci',
	method: 'getUSBDevices',
	expect: { 'ports': [] }
});
 
return baseclass.extend({
	trigger: _('USB (kernel: usbport)'),
	description: _('This LED trigger can be used for signalling to the user a presence of USB device in a given port.'),
	kernel: true,
	addFormOptions(s){
		let o;

		o = s.option(form.MultiValue, 'port', _('USB Ports'));
		o.depends('trigger', 'usbport');
		o.rmempty = true;
		o.modalonly = true;
		o.load = function(s) {
			return Promise.all([
				callUSB()
			]).then(L.bind(function(usbport){
				for (let i = 0; i < usbport[0].length; i++)
					o.value(usbport[0][i].port, _('Port %s').format(usbport[0][i].port));
			},this));
		};
		o.cfgvalue = function(section_id) {
			const ports = [];
			let value = uci.get('system', section_id, 'port');

			if (!Array.isArray(value))
				value = String(value || '').split(/\s+/);

			for (let i = 0; i < value.length; i++)
				if (value[i].match(/^(\d+)-(\d+)$/))
					ports.push('usb%d-port%d'.format(RegExp.$1, RegExp.$2));
				else
					ports.push(value[i]);

			return ports;
		};
	}
});