treewide: Update JS using luci-rpc getHostHints
authorNiels Widger <[email protected]>
Wed, 31 Mar 2021 12:23:12 +0000 (08:23 -0400)
committerJo-Philipp Wich <[email protected]>
Thu, 3 Jun 2021 07:51:09 +0000 (09:51 +0200)
Update frontend JS code which uses luci-rpc getHostHints to support the new
response format which removes the `ipv4` and `ipv6` host hint string fields
and replaces them with `ipaddrs` and `ip6addrs` weighted string list fields.

Signed-off-by: Niels Widger <[email protected]>
[rework code to be forwards/backwards compatible, fix some Network.Hosts
 methods, fix IP choice ordering, change commit subject, rewrap commit
 message]
Signed-off-by: Jo-Philipp Wich <[email protected]>
applications/luci-app-firewall/htdocs/luci-static/resources/tools/firewall.js
applications/luci-app-wol/htdocs/luci-static/resources/view/wol.js
modules/luci-base/htdocs/luci-static/resources/network.js
modules/luci-mod-network/htdocs/luci-static/resources/view/network/dhcp.js
modules/luci-mod-network/htdocs/luci-static/resources/view/network/hosts.js

index ebdd345c983436d583ea43cf26149097bdef1f81..1f2eff3747d5e0bff7489190260082c973972e25 100644 (file)
@@ -392,12 +392,25 @@ return baseclass.extend({
        },
 
        transformHostHints: function(family, hosts) {
-               var choice_values = [], choice_labels = {};
+               var choice_values = [],
+                   choice_labels = {},
+                   ip6addrs = {},
+                   ipaddrs = {};
+
+               for (var mac in hosts) {
+                       L.toArray(hosts[mac].ipaddrs).forEach(function(ip) {
+                               ipaddrs[ip] = mac;
+                       });
+
+                       L.toArray(hosts[mac].ip6addrs).forEach(function(ip) {
+                               ip6addrs[ip] = mac;
+                       });
+               }
 
                if (!family || family == 'ipv4') {
-                       L.sortedKeys(hosts, 'ipv4', 'addr').forEach(function(mac) {
-                               var val = hosts[mac].ipv4,
-                                   txt = hosts[mac].name || mac;
+                       L.sortedKeys(ipaddrs, null, 'addr').forEach(function(ip) {
+                               var val = ip,
+                                   txt = hosts[ipaddrs[ip]].name || ipaddrs[ip];
 
                                choice_values.push(val);
                                choice_labels[val] = E([], [ val, ' (', E('strong', {}, [txt]), ')' ]);
@@ -405,9 +418,9 @@ return baseclass.extend({
                }
 
                if (!family || family == 'ipv6') {
-                       L.sortedKeys(hosts, 'ipv6', 'addr').forEach(function(mac) {
-                               var val = hosts[mac].ipv6,
-                                   txt = hosts[mac].name || mac;
+                       L.sortedKeys(ip6addrs, null, 'addr').forEach(function(ip) {
+                               var val = ip,
+                                   txt = hosts[ip6addrs[ip]].name || ip6addrs[ip];
 
                                choice_values.push(val);
                                choice_labels[val] = E([], [ val, ' (', E('strong', {}, [txt]), ')' ]);
@@ -497,7 +510,9 @@ return baseclass.extend({
 
                L.sortedKeys(hosts).forEach(function(mac) {
                        o.value(mac, E([], [ mac, ' (', E('strong', {}, [
-                               hosts[mac].name || hosts[mac].ipv4 || hosts[mac].ipv6 || '?'
+                               hosts[mac].name ||
+                                       (hosts[mac].ipaddrs && hosts[mac].ipaddrs.length && hosts[mac].ipaddrs[0]) ||
+                                       (hosts[mac].ip6addrs && hosts[mac].ip6addrs.length && hosts[mac].ip6addrs[0]) || '?'
                        ]), ')' ]));
                });
 
index 3e0c70a924458401f54fa101befe1151f6090649..b7e251a68961fccef5c779926ec471ccea7ef784 100644 (file)
@@ -66,8 +66,13 @@ return view.extend({
 
                o.rmempty = false;
 
-               Object.keys(hosts).sort().forEach(function(mac) {
-                       o.value(mac, E([], [ mac, ' (', E('strong', [hosts[mac].name || hosts[mac].ipv4 || hosts[mac].ipv6 || '?']), ')' ]));
+               L.sortedKeys(hosts).forEach(function(mac) {
+                       o.value(mac, E([], [ mac, ' (', E('strong', [
+                               hosts[mac].name ||
+                               L.toArray(hosts[mac].ipaddrs || hosts[mac].ipv4)[0] ||
+                               L.toArray(hosts[mac].ip6addrs || hosts[mac].ipv6)[0] ||
+                               '?'
+                       ]), ')' ]));
                });
 
                if (has_ewk) {
index fccb72b3d28145c01f1f7a82b3bb9fda9f7dac42..40f9eaaa5b97aa3fdfa5fdf215bf955415bd538b 100644 (file)
@@ -1805,7 +1805,9 @@ Hosts = baseclass.extend(/** @lends LuCI.network.Hosts.prototype */ {
         * the corresponding host.
         */
        getHostnameByMACAddr: function(mac) {
-               return this.hosts[mac] ? this.hosts[mac].name : null;
+               return this.hosts[mac]
+                       ? (this.hosts[mac].name || null)
+                       : null;
        },
 
        /**
@@ -1820,7 +1822,9 @@ Hosts = baseclass.extend(/** @lends LuCI.network.Hosts.prototype */ {
         * the corresponding host.
         */
        getIPAddrByMACAddr: function(mac) {
-               return this.hosts[mac] ? this.hosts[mac].ipv4 : null;
+               return this.hosts[mac]
+                       ? (L.toArray(this.hosts[mac].ipaddrs || this.hosts[mac].ipv4)[0] || null)
+                       : null;
        },
 
        /**
@@ -1835,7 +1839,9 @@ Hosts = baseclass.extend(/** @lends LuCI.network.Hosts.prototype */ {
         * the corresponding host.
         */
        getIP6AddrByMACAddr: function(mac) {
-               return this.hosts[mac] ? this.hosts[mac].ipv6 : null;
+               return this.hosts[mac]
+                       ? (L.toArray(this.hosts[mac].ip6addrs || this.hosts[mac].ipv6)[0] || null)
+                       : null;
        },
 
        /**
@@ -1850,9 +1856,17 @@ Hosts = baseclass.extend(/** @lends LuCI.network.Hosts.prototype */ {
         * the corresponding host.
         */
        getHostnameByIPAddr: function(ipaddr) {
-               for (var mac in this.hosts)
-                       if (this.hosts[mac].ipv4 == ipaddr && this.hosts[mac].name != null)
-                               return this.hosts[mac].name;
+               for (var mac in this.hosts) {
+                       if (this.hosts[mac].name == null)
+                               continue;
+
+                       var addrs = L.toArray(this.hosts[mac].ipaddrs || this.hosts[mac].ipv4);
+
+                       for (var i = 0; i < addrs.length; i++)
+                               if (addrs[i] == ipaddr)
+                                       return this.hosts[mac].name;
+               }
+
                return null;
        },
 
@@ -1868,16 +1882,21 @@ Hosts = baseclass.extend(/** @lends LuCI.network.Hosts.prototype */ {
         * the corresponding host.
         */
        getMACAddrByIPAddr: function(ipaddr) {
-               for (var mac in this.hosts)
-                       if (this.hosts[mac].ipv4 == ipaddr)
-                               return mac;
+               for (var mac in this.hosts) {
+                       var addrs = L.toArray(this.hosts[mac].ipaddrs || this.hosts[mac].ipv4);
+
+                       for (var i = 0; i < addrs.length; i++)
+                               if (addrs[i] == ipaddr)
+                                       return mac;
+               }
+
                return null;
        },
 
        /**
         * Lookup the hostname associated with the given IPv6 address.
         *
-        * @param {string} ipaddr
+        * @param {string} ip6addr
         * The IPv6 address to lookup.
         *
         * @returns {null|string}
@@ -1886,16 +1905,24 @@ Hosts = baseclass.extend(/** @lends LuCI.network.Hosts.prototype */ {
         * the corresponding host.
         */
        getHostnameByIP6Addr: function(ip6addr) {
-               for (var mac in this.hosts)
-                       if (this.hosts[mac].ipv6 == ip6addr && this.hosts[mac].name != null)
-                               return this.hosts[mac].name;
+               for (var mac in this.hosts) {
+                       if (this.hosts[mac].name == null)
+                               continue;
+
+                       var addrs = L.toArray(this.hosts[mac].ip6addrs || this.hosts[mac].ipv6);
+
+                       for (var i = 0; i < addrs.length; i++)
+                               if (addrs[i] == ip6addr)
+                                       return this.hosts[mac].name;
+               }
+
                return null;
        },
 
        /**
         * Lookup the MAC address associated with the given IPv6 address.
         *
-        * @param {string} ipaddr
+        * @param {string} ip6addr
         * The IPv6 address to lookup.
         *
         * @returns {null|string}
@@ -1904,9 +1931,14 @@ Hosts = baseclass.extend(/** @lends LuCI.network.Hosts.prototype */ {
         * the corresponding host.
         */
        getMACAddrByIP6Addr: function(ip6addr) {
-               for (var mac in this.hosts)
-                       if (this.hosts[mac].ipv6 == ip6addr)
-                               return mac;
+               for (var mac in this.hosts) {
+                       var addrs = L.toArray(this.hosts[mac].ip6addrs || this.hosts[mac].ipv6);
+
+                       for (var i = 0; i < addrs.length; i++)
+                               if (addrs[i] == ip6addr)
+                                       return mac;
+               }
+
                return null;
        },
 
@@ -1933,13 +1965,15 @@ Hosts = baseclass.extend(/** @lends LuCI.network.Hosts.prototype */ {
         */
        getMACHints: function(preferIp6) {
                var rv = [];
+
                for (var mac in this.hosts) {
                        var hint = this.hosts[mac].name ||
-                               this.hosts[mac][preferIp6 ? 'ipv6' : 'ipv4'] ||
-                               this.hosts[mac][preferIp6 ? 'ipv4' : 'ipv6'];
+                               L.toArray(this.hosts[mac][preferIp6 ? 'ip6addrs' : 'ipaddrs'] || this.hosts[mac][preferIp6 ? 'ipv6' : 'ipv4'])[0] ||
+                               L.toArray(this.hosts[mac][preferIp6 ? 'ipaddrs' : 'ip6addrs'] || this.hosts[mac][preferIp6 ? 'ipv4' : 'ipv6'])[0];
 
                        rv.push([mac, hint]);
                }
+
                return rv.sort(function(a, b) { return a[0] > b[0] });
        }
 });
index 253b37b846abdd25dfcbcab8d20dc43b30e4ae0f..0d1420772ebc56041e3d8d23b2a4e95c5c982167 100644 (file)
@@ -450,7 +450,11 @@ return view.extend({
 
                        node.addEventListener('cbi-dropdown-change', L.bind(function(ipopt, section_id, ev) {
                                var mac = ev.detail.value.value;
-                               if (mac == null || mac == '' || !hosts[mac] || !hosts[mac].ipv4)
+                               if (mac == null || mac == '' || !hosts[mac])
+                                       return;
+
+                               var iphint = L.toArray(hosts[mac].ipaddrs || hosts[mac].ipv4)[0];
+                               if (iphint == null)
                                        return;
 
                                var ip = ipopt.formvalue(section_id);
@@ -459,13 +463,13 @@ return view.extend({
 
                                var node = ipopt.map.findElement('id', ipopt.cbid(section_id));
                                if (node)
-                                       dom.callClassMethod(node, 'setValue', hosts[mac].ipv4);
+                                       dom.callClassMethod(node, 'setValue', iphint);
                        }, this, ipopt, section_id));
 
                        return node;
                };
                Object.keys(hosts).forEach(function(mac) {
-                       var hint = hosts[mac].name || hosts[mac].ipv4;
+                       var hint = hosts[mac].name || L.toArray(hosts[mac].ipaddrs || hosts[mac].ipv4)[0];
                        so.value(mac, hint ? '%s (%s)'.format(mac, hint) : mac);
                });
 
@@ -501,11 +505,18 @@ return view.extend({
 
                        return true;
                };
+
+               var ipaddrs = {};
+
                Object.keys(hosts).forEach(function(mac) {
-                       if (hosts[mac].ipv4) {
-                               var hint = hosts[mac].name;
-                               so.value(hosts[mac].ipv4, hint ? '%s (%s)'.format(hosts[mac].ipv4, hint) : hosts[mac].ipv4);
-                       }
+                       var addrs = L.toArray(hosts[mac].ipaddrs || hosts[mac].ipv4);
+
+                       for (var i = 0; i < addrs.length; i++)
+                               ipaddrs[addrs[i]] = hosts[mac].name;
+               });
+
+               L.sortedKeys(ipaddrs, null, 'addr').forEach(function(ipv4) {
+                       so.value(ipv4, ipaddrs[ipv4] ? '%s (%s)'.format(ipv4, ipaddrs[ipv4]) : ipv4);
                });
 
                so = ss.option(form.Value, 'leasetime', _('Lease time'));
@@ -563,7 +574,7 @@ return view.extend({
                                                                        exp = '%t'.format(lease.expires);
 
                                                                var hint = lease.macaddr ? hosts[lease.macaddr] : null,
-                                                                   name = hint ? (hint.name || hint.ipv4 || hint.ipv6) : null,
+                                                                   name = hint ? (hint.name || L.toArray(hint.ipaddrs || hint.ipv4)[0] || L.toArray(hint.ip6addrs || hint.ipv6)[0]) : null,
                                                                    host = null;
 
                                                                if (name && lease.hostname && lease.hostname != name && lease.ip6addr != name)
index cd0dacbf67cba3165e64d5b2fe6e5611c33b9b78..93ebf5ba688eff3fd8a98954becbc4efbaad6740 100644 (file)
@@ -31,11 +31,18 @@ return view.extend({
                o = s.option(form.Value, 'ip', _('IP address'));
                o.datatype = 'ipaddr';
                o.rmempty = true;
-               L.sortedKeys(hosts, 'ipv4', 'addr').forEach(function(mac) {
-                       o.value(hosts[mac].ipv4, '%s (%s)'.format(
-                               hosts[mac].ipv4,
-                               hosts[mac].name || mac
-                       ));
+
+               var ipaddrs = {};
+
+               Object.keys(hosts).forEach(function(mac) {
+                       var addrs = L.toArray(hosts[mac].ipaddrs || hosts[mac].ipv4);
+
+                       for (var i = 0; i < addrs.length; i++)
+                               ipaddrs[addrs[i]] = hosts[mac].name || mac;
+               });
+
+               L.sortedKeys(ipaddrs, null, 'addr').forEach(function(ipv4) {
+                       o.value(ipv4, '%s (%s)'.format(ipv4, ipaddrs[ipv4]));
                });
 
                return m.render();