summaryrefslogtreecommitdiffhomepage
path: root/modules/luci-mod-system/htdocs/luci-static/resources/view/system/leds.js
blob: adcf4a4f8b09c3bcf76e7e00bf831114bdd23838 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
'use strict';
'require uci';
'require rpc';
'require form';

var callInitAction, callLeds, callUSB, callNetdevs;

callInitAction = rpc.declare({
	object: 'luci',
	method: 'initCall',
	params: [ 'name', 'action' ],
	expect: { result: false }
});

callLeds = rpc.declare({
	object: 'luci',
	method: 'leds'
});

callUSB = rpc.declare({
	object: 'luci',
	method: 'usb'
});

callNetdevs = rpc.declare({
	object: 'luci',
	method: 'ifaddrs',
	expect: { result: [] },
	filter: function(res) {
		var devs = {};
		for (var i = 0; i < res.length; i++)
			devs[res[i].name] = true;
		return Object.keys(devs).sort();
	}
});

return L.view.extend({
	load: function() {
		return Promise.all([
			callLeds(),
			callUSB(),
			callNetdevs()
		]);
	},

	render: function(results) {
		var leds = results[0],
		    usb = results[1],
		    netdevs = results[2],
		    triggers = {},
		    trigger, m, s, o;

		for (var k in leds)
			for (var i = 0; i < leds[k].triggers.length; i++)
				triggers[leds[k].triggers[i]] = true;

		m = new form.Map('system',
			_('<abbr title="Light Emitting Diode">LED</abbr> Configuration'),
			_('Customizes the behaviour of the device <abbr title="Light Emitting Diode">LED</abbr>s if possible.'));

		s = m.section(form.TypedSection, 'led', '');
		s.anonymous = true;
		s.addremove = true;

		s.option(form.Value, 'name', _('Name'));

		o = s.option(form.ListValue, 'sysfs', _('<abbr title="Light Emitting Diode">LED</abbr> Name'));
		Object.keys(leds).sort().forEach(function(name) { o.value(name) });

		o = s.option(form.Flag, 'default', _('Default state'));
		o.rmempty = false;

		trigger = s.option(form.ListValue, 'trigger', _('Trigger'));
		Object.keys(triggers).sort().forEach(function(t) { trigger.value(t, t.replace(/-/g, '')) });
		trigger.value('usbdev');
		trigger.value('usbport');

		o = s.option(form.Value, 'delayon', _('On-State Delay'));
		o.depends('trigger', 'timer');

		o = s.option(form.Value, 'delayoff', _('Off-State Delay'));
		o.depends('trigger', 'timer');

		o = s.option(form.ListValue, '_net_dev', _('Device'));
		o.rmempty = true;
		o.ucioption = 'dev';
		o.depends('trigger', 'netdev');
		o.remove = function(section_id) {
			var t = trigger.formvalue(section_id);
			if (t != 'netdev' && t != 'usbdev')
				uci.unset('system', section_id, 'dev');
		};
		o.value('');
		netdevs.sort().forEach(function(dev) { o.value(dev) });

		o = s.option(form.MultiValue, 'mode', _('Trigger Mode'));
		o.rmempty = true;
		o.depends('trigger', 'netdev');
		o.value('link', _('Link On'));
		o.value('tx', _('Transmit'));
		o.value('rx', _('Receive'));

		o = s.option(form.ListValue, '_usb_dev', _('USB Device'));
		o.depends('trigger', 'usbdev');
		o.rmempty = true;
		o.ucioption = 'dev';
		o.remove = function(section_id) {
			var t = trigger.formvalue(section_id);
			if (t != 'netdev' && t != 'usbdev')
				uci.unset('system', section_id, 'dev');
		}
		o.value('');
		(usb.devices || []).forEach(function(usbdev) {
			o.value(usbdev.id, '%s (%s - %s)'.format(usbdev.id, usbdev.vendor || '?', usbdev.product || '?'));
		});

		o = s.option(form.MultiValue, 'port', _('USB Ports'));
		o.depends('trigger', 'usbport');
		o.rmempty = true;
		o.cfgvalue = function(section_id) {
			var ports = [],
			    value = uci.get('system', section_id, 'port');

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

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

			return ports;
		};
		(usb.ports || []).forEach(function(usbport) {
			o.value('usb%d-port%d'.format(usbport.hub, usbport.port),
			        'Hub %d, Port %d'.format(usbport.hub, usbport.port));
		});

		o = s.option(form.Value, 'port_mask', _('Switch Port Mask'));
		o.depends('trigger', 'switch0');
		o.depends('trigger', 'switch1');

		return m.render();
	}
});