Merge pull request #1735 from sumpfralle/olsr-jsoninfo-parser-handle-empty-result
[oweals/luci.git] / applications / luci-app-firewall / htdocs / luci-static / resources / view / firewall / forwards.js
index 743d115e88dff0d24b6d81868bc2fa0240f21bf9..80938711e9d416f121cd7f0ea8b886597652c3d7 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;
 
@@ -70,7 +42,8 @@ function fmt(fmt /*, ...*/) {
 }
 
 function forward_proto_txt(s) {
-       return fmt('%s-%s', _('IPv4'),
+       return fmt('%s-%s',
+               fwtool.fmt_family(uci.get('firewall', s, 'family')),
                fwtool.fmt_proto(uci.get('firewall', s, 'proto'),
                                 uci.get('firewall', s, 'icmp_type')) || 'TCP+UDP');
 }
@@ -102,7 +75,8 @@ function forward_via_txt(s) {
 return L.view.extend({
        callHostHints: rpc.declare({
                object: 'luci',
-               method: 'host_hints'
+               method: 'getHostHints',
+               expect: { '': {} }
        }),
 
        load: function() {
@@ -134,6 +108,16 @@ return L.view.extend({
                        return uci.get('firewall', section_id, 'name') || _('Unnamed forward');
                };
 
+               s.handleAdd = function(ev) {
+                       var config_name = this.uciconfig || this.map.config,
+                           section_id = uci.add(config_name, this.sectiontype);
+
+                       uci.set(config_name, section_id, 'target', 'DNAT');
+
+                       this.addedSection = section_id;
+                       this.renderMoreOptionsModal(section_id);
+               };
+
                o = s.taboption('general', form.Value, 'name', _('Name'));
                o.placeholder = _('Unnamed forward');
                o.modalonly = true;
@@ -192,7 +176,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 +189,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 +213,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 +241,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