treewide: switch to shared isObject(), toArray() and sortedKeys() helpers
authorJo-Philipp Wich <jo@mein.io>
Sun, 7 Jul 2019 18:11:35 +0000 (20:11 +0200)
committerJo-Philipp Wich <jo@mein.io>
Sun, 7 Jul 2019 18:11:35 +0000 (20:11 +0200)
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
applications/luci-app-firewall/htdocs/luci-static/resources/tools/firewall.js
applications/luci-app-firewall/htdocs/luci-static/resources/view/firewall/forwards.js
applications/luci-app-firewall/htdocs/luci-static/resources/view/firewall/rules.js
modules/luci-base/htdocs/luci-static/resources/firewall.js
modules/luci-base/htdocs/luci-static/resources/form.js
modules/luci-base/htdocs/luci-static/resources/network.js
modules/luci-base/htdocs/luci-static/resources/tools/widgets.js

index 909540eaf9e868850d0d8d7db0d3390ada8a1b60..6785d1ca6303d2d75b51a2f322dc103b389b6068 100644 (file)
@@ -64,22 +64,6 @@ var protocols = [
        'rohc', 142, 'ROHC',
 ];
 
-function toArray(x) {
-       if (x == null)
-               return [];
-       else if (Array.isArray(x))
-               return x.map(String);
-       else if (typeof(x) == 'object')
-               return [ x ];
-
-       var s = String(x).trim();
-
-       if (s == '')
-               return [];
-
-       return s.split(/\s+/);
-}
-
 function lookupProto(x) {
        if (x == null || x == '')
                return null;
@@ -104,7 +88,7 @@ return L.Class.extend({
        },
 
        fmt_mac: function(x) {
-               var rv = E([]), l = toArray(x);
+               var rv = E([]), l = L.toArray(x);
 
                if (l.length == 0)
                        return null;
@@ -123,7 +107,7 @@ return L.Class.extend({
        },
 
        fmt_port: function(x, d) {
-               var rv = E([]), l = toArray(x);
+               var rv = E([]), l = L.toArray(x);
 
                if (l.length == 0) {
                        if (d) {
@@ -159,7 +143,7 @@ return L.Class.extend({
        },
 
        fmt_ip: function(x, d) {
-               var rv = E([]), l = toArray(x);
+               var rv = E([]), l = L.toArray(x);
 
                if (l.length == 0) {
                        if (d) {
@@ -205,7 +189,7 @@ return L.Class.extend({
        },
 
        fmt_icmp_type: function(x) {
-               var rv = E([]), l = toArray(x);
+               var rv = E([]), l = L.toArray(x);
 
                if (l.length == 0)
                        return null;
@@ -228,7 +212,7 @@ return L.Class.extend({
        },
 
        fmt_proto: function(x, icmp_types) {
-               var rv = E([]), l = toArray(x);
+               var rv = E([]), l = L.toArray(x);
 
                if (l.length == 0)
                        return null;
index 743d115e88dff0d24b6d81868bc2fa0240f21bf9..93bfc2918876f675e9e80b0d1e1622243ac8c65e 100644 (file)
@@ -6,34 +6,6 @@
 'require tools.firewall as fwtool';
 'require tools.widgets as widgets';
 
-function skeys(obj, key, mode) {
-       if (obj == null || typeof(obj) != 'object')
-               return [];
-
-       return Object.keys(obj).map(function(e) {
-               var v = (key != null) ? obj[e][key] : e;
-
-               switch (mode) {
-               case 'addr':
-                       v = (v != null) ? v.replace(/(?:^|[.:])([0-9a-fA-F]{1,4})/g,
-                               function(m0, m1) { return ('000' + m1.toLowerCase()).substr(-4) }) : null;
-                       break;
-
-               case 'num':
-                       v = (v != null) ? +v : null;
-                       break;
-               }
-
-               return [ e, v ];
-       }).filter(function(e) {
-               return (e[1] != null);
-       }).sort(function(a, b) {
-               return (a[1] > b[1]);
-       }).map(function(e) {
-               return e[0];
-       });
-}
-
 function fmt(fmt /*, ...*/) {
        var repl = [], wrap = false;
 
@@ -192,7 +164,7 @@ return L.view.extend({
                o.rmempty = true;
                o.datatype = 'neg(macaddr)';
                o.placeholder = E('em', _('any'));
-               skeys(hosts).forEach(function(mac) {
+               L.sortedKeys(hosts).forEach(function(mac) {
                        o.value(mac, '%s (%s)'.format(
                                mac,
                                hosts[mac].name || hosts[mac].ipv4 || hosts[mac].ipv6 || '?'
@@ -205,7 +177,7 @@ return L.view.extend({
                o.rmempty = true;
                o.datatype = 'neg(ipmask4)';
                o.placeholder = E('em', _('any'));
-               skeys(hosts, 'ipv4', 'addr').forEach(function(mac) {
+               L.sortedKeys(hosts, 'ipv4', 'addr').forEach(function(mac) {
                        o.value(hosts[mac].ipv4, '%s (%s)'.format(
                                hosts[mac].ipv4,
                                hosts[mac].name || mac
@@ -229,7 +201,7 @@ return L.view.extend({
                o.rmempty = true;
                o.datatype = 'neg(ipmask4)';
                o.placeholder = E('em', _('any'));
-               skeys(hosts, 'ipv4', 'addr').forEach(function(mac) {
+               L.sortedKeys(hosts, 'ipv4', 'addr').forEach(function(mac) {
                        o.value(hosts[mac].ipv4, '%s (%s)'.format(
                                hosts[mac].ipv4,
                                hosts[mac].name || mac
@@ -257,7 +229,7 @@ return L.view.extend({
                o.modalonly = true;
                o.rmempty = true;
                o.datatype = 'ipmask4';
-               skeys(hosts, 'ipv4', 'addr').forEach(function(mac) {
+               L.sortedKeys(hosts, 'ipv4', 'addr').forEach(function(mac) {
                        o.value(hosts[mac].ipv4, '%s (%s)'.format(
                                hosts[mac].ipv4,
                                hosts[mac].name || mac
index 9fa1aa252dcc61b5ac8d94942c12615132a4a24e..d2d191632243f029c51bcc82cb1cd094ad350536 100644 (file)
@@ -6,34 +6,6 @@
 'require tools.firewall as fwtool';
 'require tools.widgets as widgets';
 
-function skeys(obj, key, mode) {
-       if (obj == null || typeof(obj) != 'object')
-               return [];
-
-       return Object.keys(obj).map(function(e) {
-               var v = (key != null) ? obj[e][key] : e;
-
-               switch (mode) {
-               case 'addr':
-                       v = (v != null) ? v.replace(/(?:^|[.:])([0-9a-fA-F]{1,4})/g,
-                               function(m0, m1) { return ('000' + m1.toLowerCase()).substr(-4) }) : null;
-                       break;
-
-               case 'num':
-                       v = (v != null) ? +v : null;
-                       break;
-               }
-
-               return [ e, v ];
-       }).filter(function(e) {
-               return (e[1] != null);
-       }).sort(function(a, b) {
-               return (a[1] > b[1]);
-       }).map(function(e) {
-               return e[0];
-       });
-}
-
 function fmt(fmt /*, ...*/) {
        var repl = [], wrap = false;
 
@@ -276,7 +248,7 @@ return L.view.extend({
                o.modalonly = true;
                o.datatype = 'list(macaddr)';
                o.placeholder = _('any');
-               skeys(hosts).forEach(function(mac) {
+               L.sortedKeys(hosts).forEach(function(mac) {
                        o.value(mac, '%s (%s)'.format(
                                mac,
                                hosts[mac].name || hosts[mac].ipv4 || hosts[mac].ipv6 || '?'
@@ -287,7 +259,7 @@ return L.view.extend({
                o.modalonly = true;
                o.datatype = 'list(neg(ipmask))';
                o.placeholder = _('any');
-               skeys(hosts, 'ipv4', 'addr').forEach(function(mac) {
+               L.sortedKeys(hosts, 'ipv4', 'addr').forEach(function(mac) {
                        o.value(hosts[mac].ipv4, '%s (%s)'.format(
                                hosts[mac].ipv4,
                                hosts[mac].name || mac
@@ -324,7 +296,7 @@ return L.view.extend({
                o.modalonly = true;
                o.datatype = 'list(neg(ipmask))';
                o.placeholder = _('any');
-               skeys(hosts, 'ipv4', 'addr').forEach(function(mac) {
+               L.sortedKeys(hosts, 'ipv4', 'addr').forEach(function(mac) {
                        o.value(hosts[mac].ipv4, '%s (%s)'.format(
                                hosts[mac].ipv4,
                                hosts[mac].name || mac
index e99f8b83bcb488f206143e2cdba9c1866b7d3ca4..d034d6e01068afe2d8b5a1c297ea8bbbde270fb0 100644 (file)
@@ -8,21 +8,6 @@ function initFirewallState() {
        return uci.load('firewall');
 }
 
-function toArray(val) {
-       if (val == null)
-               return [];
-
-       if (Array.isArray(val))
-               return val;
-
-       var s = String(val).trim();
-
-       if (s == '')
-               return [];
-
-       return s.split(/\s+/);
-}
-
 function parseEnum(s, values) {
        if (s == null)
                return null;
@@ -155,7 +140,7 @@ Firewall = L.Class.extend({
                        var sections = uci.sections('firewall', 'zone');
 
                        for (var i = 0; i < sections.length; i++)
-                               if (toArray(sections[i].network || sections[i].name).indexOf(network) != -1)
+                               if (L.toArray(sections[i].network || sections[i].name).indexOf(network) != -1)
                                        return new Zone(sections[i]['.name']);
 
                        return null;
@@ -217,7 +202,7 @@ Firewall = L.Class.extend({
                                if (sections[i].name != oldName)
                                        continue;
 
-                               if (toArray(sections[i].network).length == 0)
+                               if (L.toArray(sections[i].network).length == 0)
                                        uci.set('firewall', sections[i]['.name'], 'network', oldName);
 
                                uci.set('firewall', sections[i]['.name'], 'name', newName);
@@ -383,7 +368,7 @@ Zone = AbstractFirewallItem.extend({
        },
 
        getNetworks: function() {
-               return toArray(this.get('network') || this.get('name'));
+               return L.toArray(this.get('network') || this.get('name'));
        },
 
        clearNetworks: function() {
index dd7ba6ef605c98bcb86ea6acaf7e4eb013085fac..853b01ee3d63e36af5edc90272775b71df36553a 100644 (file)
@@ -1430,12 +1430,7 @@ var CBIDynamicList = CBIValue.extend({
        renderWidget: function(section_id, option_index, cfgvalue) {
                var value = (cfgvalue != null) ? cfgvalue : this.default,
                    choices = this.transformChoices(),
-                   items = null;
-
-               if (Array.isArray(value))
-                       items = value;
-               else if (value != null)
-                       items = String(value).trim().split(/\s+/);
+                   items = L.toArray(value);
 
                var widget = new ui.DynamicList(items, choices, {
                        id: this.cbid(section_id),
@@ -1541,10 +1536,7 @@ var CBIMultiValue = CBIDynamicList.extend({
                var value = (cfgvalue != null) ? cfgvalue : this.default,
                    choices = this.transformChoices();
 
-               if (!Array.isArray(value))
-                       value = String(value).split(/\s+/);
-
-               var widget = new ui.Dropdown(value, choices, {
+               var widget = new ui.Dropdown(L.toArray(value), choices, {
                        id: this.cbid(section_id),
                        sort: this.keylist,
                        multiple: true,
index cfaec34298e3ee750f7a4c730684cca11f4b9756..1f66c6ff9bcc1eabd7083c8387c4aafdd8072c5c 100644 (file)
@@ -90,7 +90,7 @@ var _cache = {},
 function getWifiState() {
        if (_cache.wifi == null)
                return callNetworkWirelessStatus().then(function(state) {
-                       if (!isObject(state))
+                       if (!L.isObject(state))
                                throw !1;
                        return (_cache.wifi = state);
                }).catch(function() {
@@ -116,7 +116,7 @@ function getInterfaceState() {
 function getDeviceState() {
        if (_cache.devicedump == null)
                return callNetworkDeviceStatus().then(function(state) {
-                       if (!isObject(state))
+                       if (!L.isObject(state))
                                throw !1;
                        return (_cache.devicedump = state);
                }).catch(function() {
@@ -142,7 +142,7 @@ function getIfaddrState() {
 function getNetdevState() {
        if (_cache.devices == null)
                return callLuciNetdevs().then(function(state) {
-                       if (!isObject(state))
+                       if (!L.isObject(state))
                                throw !1;
                        return (_cache.devices = state);
                }).catch(function() {
@@ -155,7 +155,7 @@ function getNetdevState() {
 function getBoardState() {
        if (_cache.board == null)
                return callLuciBoardjson().then(function(state) {
-                       if (!isObject(state))
+                       if (!L.isObject(state))
                                throw !1;
                        return (_cache.board = state);
                }).catch(function() {
@@ -222,7 +222,7 @@ function getWifiIwinfoByIfname(ifname, forcePhyOnly) {
                    devstate = info[1],
                    phyonly = forcePhyOnly || !devstate[ifname] || (devstate[ifname].type != 1);
 
-               if (isObject(iwinfo)) {
+               if (L.isObject(iwinfo)) {
                        if (phyonly) {
                                delete iwinfo.bitrate;
                                delete iwinfo.quality;
@@ -266,7 +266,7 @@ function getWifiSidByIfname(ifname) {
 
        var res = getWifiStateByIfname(ifname);
 
-       if (res != null && isObject(res[2]) && typeof(res[2].section) == 'string')
+       if (res != null && L.isObject(res[2]) && typeof(res[2].section) == 'string')
                return res[2].section;
 
        return null;
@@ -330,10 +330,6 @@ function isIgnoredIfname(ifname) {
        return false;
 }
 
-function isObject(val) {
-       return (val != null && typeof(val) == 'object')
-}
-
 function appendValue(config, section, option, value) {
        var values = uci.get(config, section, option),
            isArray = Array.isArray(values),
@@ -375,21 +371,6 @@ function removeValue(config, section, option, value) {
        return rv;
 }
 
-function toArray(val) {
-       if (val == null)
-               return [];
-
-       if (Array.isArray(val))
-               return val;
-
-       var s = String(val).trim();
-
-       if (s == '')
-               return [];
-
-       return s.split(/\s+/);
-}
-
 function prefixToMask(bits, v6) {
        var w = v6 ? 128 : 32,
            m = [];
@@ -499,7 +480,7 @@ function initNetworkState() {
                                }
                        }
 
-                       if (isObject(board.switch)) {
+                       if (L.isObject(board.switch)) {
                                for (var switchname in board.switch) {
                                        var layout = board.switch[switchname],
                                            netdevs = {},
@@ -508,7 +489,7 @@ function initNetworkState() {
                                            pnum = null,
                                            role = null;
 
-                                       if (isObject(layout) && Array.isArray(layout.ports)) {
+                                       if (L.isObject(layout) && Array.isArray(layout.ports)) {
                                                for (var i = 0, port; (port = layout.ports[i]) != null; i++) {
                                                        if (typeof(port) == 'object' && typeof(port.num) == 'number' &&
                                                                (typeof(port.role) == 'string' || typeof(port.device) == 'string')) {
@@ -653,7 +634,7 @@ Network = L.Class.extend({
                                var sid = uci.add('network', 'interface', name);
 
                                if (sid != null) {
-                                       if (isObject(options))
+                                       if (L.isObject(options))
                                                for (var key in options)
                                                        if (options.hasOwnProperty(key))
                                                                uci.set('network', sid, key, options[key]);
@@ -662,7 +643,7 @@ Network = L.Class.extend({
                                }
                        }
                        else if (existingNetwork != null && existingNetwork.isEmpty()) {
-                               if (isObject(options))
+                               if (L.isObject(options))
                                        for (var key in options)
                                                if (options.hasOwnProperty(key))
                                                        existingNetwork.set(key, options[key]);
@@ -742,7 +723,7 @@ Network = L.Class.extend({
                                });
 
                                uci.sections('wireless', 'wifi-iface', function(s) {
-                                       var networks = toArray(s.network).filter(function(network) { return network != name });
+                                       var networks = L.toArray(s.network).filter(function(network) { return network != name });
 
                                        if (networks.length > 0)
                                                uci.set('wireless', s['.name'], 'network', networks.join(' '));
@@ -797,7 +778,7 @@ Network = L.Class.extend({
                        });
 
                        uci.sections('wireless', 'wifi-iface', function(s) {
-                               var networks = toArray(s.network).map(function(network) { return (network == oldName ? newName : network) });
+                               var networks = L.toArray(s.network).map(function(network) { return (network == oldName ? newName : network) });
 
                                if (networks.length > 0)
                                        uci.set('wireless', s['.name'], 'network', networks.join(' '));
@@ -832,7 +813,7 @@ Network = L.Class.extend({
                        /* find simple devices */
                        var uciInterfaces = uci.sections('network', 'interface');
                        for (var i = 0; i < uciInterfaces.length; i++) {
-                               var ifnames = toArray(uciInterfaces[i].ifname);
+                               var ifnames = L.toArray(uciInterfaces[i].ifname);
 
                                for (var j = 0; j < ifnames.length; j++) {
                                        if (ifnames[j].charAt(0) == '@')
@@ -955,7 +936,7 @@ Network = L.Class.extend({
                        var rv = [];
 
                        for (var i = 0; i < deviceNames.length; i++)
-                               if (isObject(iwinfos[i]))
+                               if (L.isObject(iwinfos[i]))
                                        rv.push(this.instantiateWifiDevice(deviceNames[i], iwinfos[i]));
 
                        rv.sort(function(a, b) { return a.getName() < b.getName() });
@@ -1248,7 +1229,7 @@ Protocol = L.Class.extend({
        getZoneName: function() {
                var d = this._ubus('data');
 
-               if (isObject(d) && typeof(d.zone) == 'string')
+               if (L.isObject(d) && typeof(d.zone) == 'string')
                        return d.zone;
 
                return null;
@@ -1339,12 +1320,12 @@ Protocol = L.Class.extend({
        getIP6Addr: function() {
                var addrs = this._ubus('ipv6-address');
 
-               if (Array.isArray(addrs) && isObject(addrs[0]))
+               if (Array.isArray(addrs) && L.isObject(addrs[0]))
                        return '%s/%d'.format(addrs[0].address, addrs[0].mask);
 
                addrs = this._ubus('ipv6-prefix-assignment');
 
-               if (Array.isArray(addrs) && isObject(addrs[0]) && isObject(addrs[0]['local-address']))
+               if (Array.isArray(addrs) && L.isObject(addrs[0]) && L.isObject(addrs[0]['local-address']))
                        return '%s/%d'.format(addrs[0]['local-address'].address, addrs[0]['local-address'].mask);
 
                return null;
@@ -1356,14 +1337,14 @@ Protocol = L.Class.extend({
 
                if (Array.isArray(addrs))
                        for (var i = 0; i < addrs.length; i++)
-                               if (isObject(addrs[i]))
+                               if (L.isObject(addrs[i]))
                                        rv.push('%s/%d'.format(addrs[i].address, addrs[i].mask));
 
                addrs = this._ubus('ipv6-prefix-assignment');
 
                if (Array.isArray(addrs))
                        for (var i = 0; i < addrs.length; i++)
-                               if (isObject(addrs[i]) && isObject(addrs[i]['local-address']))
+                               if (L.isObject(addrs[i]) && L.isObject(addrs[i]['local-address']))
                                        rv.push('%s/%d'.format(addrs[i]['local-address'].address, addrs[i]['local-address'].mask));
 
                return rv;
@@ -1384,7 +1365,7 @@ Protocol = L.Class.extend({
        getIP6Prefix: function() {
                var prefixes = this._ubus('ipv6-prefix');
 
-               if (Array.isArray(prefixes) && isObject(prefixes[0]))
+               if (Array.isArray(prefixes) && L.isObject(prefixes[0]))
                        return '%s/%d'.format(prefixes[0].address, prefixes[0].mask);
 
                return null;
@@ -1396,7 +1377,7 @@ Protocol = L.Class.extend({
 
                if (Array.isArray(errors)) {
                        for (var i = 0; i < errors.length; i++) {
-                               if (!isObject(errors[i]) || typeof(errors[i].code) != 'string')
+                               if (!L.isObject(errors[i]) || typeof(errors[i].code) != 'string')
                                        continue;
 
                                rv = rv || [];
@@ -1432,7 +1413,7 @@ Protocol = L.Class.extend({
        },
 
        isAlias: function() {
-               var ifnames = toArray(uci.get('network', this.sid, 'ifname')),
+               var ifnames = L.toArray(uci.get('network', this.sid, 'ifname')),
                    parent = null;
 
                for (var i = 0; i < ifnames.length; i++)
@@ -1514,7 +1495,7 @@ Protocol = L.Class.extend({
                        if (ifname != null)
                                return L.network.instantiateDevice(ifname, this);
 
-                       var ifnames = toArray(uci.get('network', this.sid, 'ifname'));
+                       var ifnames = L.toArray(uci.get('network', this.sid, 'ifname'));
 
                        for (var i = 0; i < ifnames.length; i++) {
                                var m = ifnames[i].match(/^([^:/]+)/);
@@ -1533,7 +1514,7 @@ Protocol = L.Class.extend({
                if (!this.isBridge() && !(this.isVirtual() && !this.isFloating()))
                        return null;
 
-               var ifnames = toArray(uci.get('network', this.sid, 'ifname'));
+               var ifnames = L.toArray(uci.get('network', this.sid, 'ifname'));
 
                for (var i = 0; i < ifnames.length; i++) {
                        if (ifnames[i].charAt(0) == '@')
@@ -1550,7 +1531,7 @@ Protocol = L.Class.extend({
                        if (typeof(uciWifiIfaces[i].device) != 'string')
                                continue;
 
-                       var networks = toArray(uciWifiIfaces[i].network);
+                       var networks = L.toArray(uciWifiIfaces[i].network);
 
                        for (var j = 0; j < networks.length; j++) {
                                if (networks[j] != this.sid)
@@ -1578,7 +1559,7 @@ Protocol = L.Class.extend({
                else if (this.isBridge() && 'br-%s'.format(this.sid) == ifname)
                        return true;
 
-               var ifnames = toArray(uci.get('network', this.sid, 'ifname'));
+               var ifnames = L.toArray(uci.get('network', this.sid, 'ifname'));
 
                for (var i = 0; i < ifnames.length; i++) {
                        var m = ifnames[i].match(/^([^:/]+)/);
@@ -1589,7 +1570,7 @@ Protocol = L.Class.extend({
                var wif = getWifiSidByIfname(ifname);
 
                if (wif != null) {
-                       var networks = toArray(uci.get('wireless', wif, 'network'));
+                       var networks = L.toArray(uci.get('wireless', wif, 'network'));
 
                        for (var i = 0; i < networks.length; i++)
                                if (networks[i] == this.sid)
@@ -1814,7 +1795,7 @@ WifiDevice = L.Class.extend({
        },
 
        getHWModes: function() {
-               if (isObject(this.iwinfo.hwmodelist))
+               if (L.isObject(this.iwinfo.hwmodelist))
                        for (var k in this.iwinfo.hwmodelist)
                                return this.iwinfo.hwmodelist;
 
@@ -1840,7 +1821,7 @@ WifiDevice = L.Class.extend({
        },
 
        isUp: function() {
-               if (isObject(_cache.wifi[this.sid]))
+               if (L.isObject(_cache.wifi[this.sid]))
                        return (_cache.wifi[this.sid].up == true);
 
                return false;
@@ -1869,7 +1850,7 @@ WifiDevice = L.Class.extend({
        },
 
        addWifiNetwork: function(options) {
-               if (!isObject(options))
+               if (!L.isObject(options))
                        options = {};
 
                options.device = this.sid;
@@ -1916,7 +1897,7 @@ WifiNetwork = L.Class.extend({
                var v = this._ubusdata;
 
                for (var i = 0; i < arguments.length; i++)
-                       if (isObject(v))
+                       if (L.isObject(v))
                                v = v[arguments[i]];
                        else
                                return null;
@@ -1945,7 +1926,7 @@ WifiNetwork = L.Class.extend({
        },
 
        getNetworkNames: function() {
-               return toArray(this.ubus('net', 'config', 'network') || this.get('network'));
+               return L.toArray(this.ubus('net', 'config', 'network') || this.get('network'));
        },
 
        getID: function() {
@@ -2020,7 +2001,7 @@ WifiNetwork = L.Class.extend({
        getActiveEncryption: function() {
                var encryption = this.iwinfo.encryption;
 
-               return (isObject(encryption) ? encryption.description || '-' : '-');
+               return (L.isObject(encryption) ? encryption.description || '-' : '-');
        },
 
        getAssocList: function() {
index 669689449f0d3a0d0294548fde9c324840c7ded5..7f2997f17367c6ef67ca35ee6135699f0cf38d13 100644 (file)
@@ -4,22 +4,6 @@
 'require network';
 'require firewall';
 
-function toArray(x) {
-       if (x == null)
-               return [];
-       else if (Array.isArray(x))
-               return x.map(String);
-       else if (typeof(x) == 'object')
-               return [ x ];
-
-       var s = String(x).trim();
-
-       if (s == '')
-               return [];
-
-       return s.split(/\s+/);
-}
-
 var CBIZoneSelect = form.ListValue.extend({
        __name__: 'CBI.ZoneSelect',
 
@@ -45,7 +29,7 @@ var CBIZoneSelect = form.ListValue.extend({
        },
 
        renderWidget: function(section_id, option_index, cfgvalue) {
-               var values = toArray((cfgvalue != null) ? cfgvalue : this.default),
+               var values = L.toArray((cfgvalue != null) ? cfgvalue : this.default),
                    choices = {};
 
                if (this.allowlocal) {
@@ -94,7 +78,7 @@ var CBIZoneSelect = form.ListValue.extend({
                                        'class': 'ifacebadge' + (network.getName() == this.network ? ' ifacebadge-active' : '')
                                }, network.getName() + ': ');
 
-                               var devices = network.isBridge() ? network.getDevices() : toArray(network.getDevice());
+                               var devices = network.isBridge() ? network.getDevices() : L.toArray(network.getDevice());
 
                                for (var k = 0; k < devices.length; k++) {
                                        span.appendChild(E('img', {
@@ -168,7 +152,7 @@ var CBIZoneForwards = form.DummyValue.extend({
                                'class': 'ifacebadge' + (network.getName() == this.network ? ' ifacebadge-active' : '')
                        }, network.getName() + ': ');
 
-                       var devices = network.isBridge() ? network.getDevices() : toArray(network.getDevice());
+                       var devices = network.isBridge() ? network.getDevices() : L.toArray(network.getDevice());
 
                        for (var k = 0; k < devices.length && devices[k]; k++) {
                                span.appendChild(E('img', {
@@ -243,7 +227,7 @@ var CBINetworkSelect = form.ListValue.extend({
 
        renderIfaceBadge: function(network) {
                var span = E('span', { 'class': 'ifacebadge' }, network.getName() + ': '),
-                   devices = network.isBridge() ? network.getDevices() : toArray(network.getDevice());
+                   devices = network.isBridge() ? network.getDevices() : L.toArray(network.getDevice());
 
                for (var j = 0; j < devices.length && devices[j]; j++) {
                        span.appendChild(E('img', {
@@ -261,7 +245,7 @@ var CBINetworkSelect = form.ListValue.extend({
        },
 
        renderWidget: function(section_id, option_index, cfgvalue) {
-               var values = toArray((cfgvalue != null) ? cfgvalue : this.default),
+               var values = L.toArray((cfgvalue != null) ? cfgvalue : this.default),
                    choices = {},
                    checked = {};
 
@@ -312,7 +296,7 @@ var CBINetworkSelect = form.ListValue.extend({
 
        textvalue: function(section_id) {
                var cfgvalue = this.cfgvalue(section_id),
-                   values = toArray((cfgvalue != null) ? cfgvalue : this.default),
+                   values = L.toArray((cfgvalue != null) ? cfgvalue : this.default),
                    rv = E([]);
 
                for (var i = 0; i < (this.networks || []).length; i++) {