luci-base: cbi.js: fix handling of inverse-depends, combobox validation
[project/luci.git] / modules / luci-base / htdocs / luci-static / resources / cbi.js
index 5790e30..b819230 100644 (file)
@@ -118,6 +118,50 @@ var cbi_validators = {
                return false;
        },
 
+       'ipmask': function()
+       {
+               return cbi_validators.ipmask4.apply(this) ||
+                       cbi_validators.ipmask6.apply(this);
+       },
+
+       'ipmask4': function()
+       {
+               var ip = this, mask = 32;
+
+               if (ip.match(/^(\S+)\/(\S+)$/))
+               {
+                       ip = RegExp.$1;
+                       mask = RegExp.$2;
+               }
+
+               if (!isNaN(mask) && (mask < 0 || mask > 32))
+                       return false;
+
+               if (isNaN(mask) && !cbi_validators.ip4addr.apply(mask))
+                       return false;
+
+               return cbi_validators.ip4addr.apply(ip);
+       },
+
+       'ipmask6': function()
+       {
+               var ip = this, mask = 128;
+
+               if (ip.match(/^(\S+)\/(\S+)$/))
+               {
+                       ip = RegExp.$1;
+                       mask = RegExp.$2;
+               }
+
+               if (!isNaN(mask) && (mask < 0 || mask > 128))
+                       return false;
+
+               if (isNaN(mask) && !cbi_validators.ip6addr.apply(mask))
+                       return false;
+
+               return cbi_validators.ip6addr.apply(ip);
+       },
+
        'port': function()
        {
                var p = Int(this);
@@ -437,8 +481,9 @@ function cbi_d_check(deps) {
                                istat = (istat && cbi_d_checkvalue(j, deps[i][j]))
                        }
                }
-               if (istat) {
-                       return !reverse;
+
+               if (istat ^ reverse) {
+                       return true;
                }
        }
        return def;
@@ -523,13 +568,6 @@ function cbi_init() {
                }
        }
 
-       nodes = document.querySelectorAll('[data-type]');
-
-       for (var i = 0, node; (node = nodes[i]) !== undefined; i++) {
-               cbi_validate_field(node, node.getAttribute('data-optional') === 'true',
-                                  node.getAttribute('data-type'));
-       }
-
        nodes = document.querySelectorAll('[data-choices]');
 
        for (var i = 0, node; (node = nodes[i]) !== undefined; i++) {
@@ -562,6 +600,13 @@ function cbi_init() {
                cbi_dynlist_init(node, choices[2], choices[3], options);
        }
 
+       nodes = document.querySelectorAll('[data-type]');
+
+       for (var i = 0, node; (node = nodes[i]) !== undefined; i++) {
+               cbi_validate_field(node, node.getAttribute('data-optional') === 'true',
+                                  node.getAttribute('data-type'));
+       }
+
        cbi_d_update();
 }
 
@@ -604,9 +649,6 @@ function cbi_combobox(id, values, def, man, focus) {
        var dt = obj.getAttribute('cbi_datatype');
        var op = obj.getAttribute('cbi_optional');
 
-       if (dt)
-               cbi_validate_field(sel, op == 'true', dt);
-
        if (!values[obj.value]) {
                if (obj.value == "") {
                        var optdef = document.createElement("option");
@@ -641,6 +683,9 @@ function cbi_combobox(id, values, def, man, focus) {
 
        obj.style.display = "none";
 
+       if (dt)
+               cbi_validate_field(sel, op == 'true', dt);
+
        cbi_bind(sel, "change", function() {
                if (sel.selectedIndex == sel.options.length - 1) {
                        obj.style.display = "inline";
@@ -683,7 +728,7 @@ function cbi_filebrowser(id, defpath) {
        browser.focus();
 }
 
-function cbi_browser_init(id, defpath)
+function cbi_browser_init(id, resource, defpath)
 {
        function cbi_browser_btnclick(e) {
                cbi_filebrowser(id, defpath);
@@ -694,7 +739,7 @@ function cbi_browser_init(id, defpath)
 
        var btn = document.createElement('img');
        btn.className = 'cbi-image-button';
-       btn.src = cbi_strings.path.resource + '/cbi/folder.gif';
+       btn.src = (resource || cbi_strings.path.resource) + '/cbi/folder.gif';
        field.parentNode.insertBefore(btn, field.nextSibling);
 
        cbi_bind(btn, 'click', cbi_browser_btnclick);
@@ -761,7 +806,7 @@ function cbi_dynlist_init(parent, datatype, optional, choices)
                        parent.appendChild(b);
                        if (datatype == 'file')
                        {
-                               cbi_browser_init(t.id, parent.getAttribute('data-browser-path'));
+                               cbi_browser_init(t.id, null, parent.getAttribute('data-browser-path'));
                        }
 
                        parent.appendChild(document.createElement('br'));