luci-mod-system: leds.js: fix crash when no usb devices or ports are present
authorJo-Philipp Wich <jo@mein.io>
Thu, 25 Jul 2019 06:48:56 +0000 (08:48 +0200)
committerJo-Philipp Wich <jo@mein.io>
Thu, 25 Jul 2019 06:49:18 +0000 (08:49 +0200)
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
modules/luci-mod-system/htdocs/luci-static/resources/view/system/leds.js

index adcf4a4f8b09c3bcf76e7e00bf831114bdd23838..c1109b5d64ac7783590a62b4057db6d117aa16a8 100644 (file)
@@ -72,8 +72,10 @@ return L.view.extend({
 
                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');
+               if (usb.devices && usb.devices.length)
+                       trigger.value('usbdev');
+               if (usb.ports && usb.ports.length)
+                       trigger.value('usbport');
 
                o = s.option(form.Value, 'delayon', _('On-State Delay'));
                o.depends('trigger', 'timer');
@@ -100,42 +102,46 @@ return L.view.extend({
                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');
+               if (usb.devices && usb.devices.length) {
+                       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.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));
-               });
+               if (usb.ports && usb.ports.length) {
+                       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');