X-Git-Url: http://git.archive.openwrt.org/?a=blobdiff_plain;f=modules%2Fluci-base%2Fhtdocs%2Fluci-static%2Fresources%2Fcbi.js;h=b334703bdfb421964a57d6c7e64f4abda64f5dd1;hb=b2274e5d6d27cdd0819d9e35058ebcd2cb8b55f5;hp=b27c564922cfd6f4b3dd0ca73d31422053b332db;hpb=6d126649f01ac817149d4f36324c161bb1a4650b;p=project%2Fluci.git diff --git a/modules/luci-base/htdocs/luci-static/resources/cbi.js b/modules/luci-base/htdocs/luci-static/resources/cbi.js index b27c56492..b334703bd 100644 --- a/modules/luci-base/htdocs/luci-static/resources/cbi.js +++ b/modules/luci-base/htdocs/luci-static/resources/cbi.js @@ -118,6 +118,90 @@ var cbi_validators = { return false; }, + 'ip4prefix': function() + { + return !isNaN(this) && this >= 0 && this <= 32; + }, + + 'ip6prefix': function() + { + return !isNaN(this) && this >= 0 && this <= 128; + }, + + 'cidr': function() + { + return cbi_validators.cidr4.apply(this) || + cbi_validators.cidr6.apply(this); + }, + + 'cidr4': function() + { + if (this.match(/^(\S+)\/(\S+)$/)) + { + ip = RegExp.$1; + mask = RegExp.$2; + return cbi_validators.ip4addr.apply(ip) && + cbi_validators.ip4prefix.apply(mask); + } + return false; + }, + + 'cidr6': function() + { + if (this.match(/^(\S+)\/(\S+)$/)) + { + ip = RegExp.$1; + mask = RegExp.$2; + return cbi_validators.ip6addr.apply(ip) && + cbi_validators.ip6prefix.apply(mask); + } + return false; + }, + + 'ipnet4': function() + { + if (this.match(/^(\S+)\/(\S+)$/)) + { + ip = RegExp.$1; + net = RegExp.$2; + return cbi_validators.ip4addr.apply(ip) && + cbi_validators.ip4addr.apply(net); + } + return false; + }, + + 'ipnet6': function() + { + if (this.match(/^(\S+)\/(\S+)$/)) + { + ip = RegExp.$1; + net = RegExp.$2; + return cbi_validators.ip6addr.apply(ip) && + cbi_validators.ip6addr.apply(net); + } + return false; + }, + + 'ipmask': function() + { + return cbi_validators.ipmask4.apply(this) || + cbi_validators.ipmask6.apply(this); + }, + + 'ipmask4': function() + { + return cbi_validators.cidr4.apply(this) || + cbi_validators.ipnet4.apply(this) || + cbi_validators.ip4addr.apply(this); + }, + + 'ipmask6': function() + { + return cbi_validators.cidr6.apply(this) || + cbi_validators.ipnet6.apply(this) || + cbi_validators.ip6addr.apply(this); + }, + 'port': function() { var p = Int(this); @@ -145,7 +229,7 @@ var cbi_validators = { { return cbi_validators.hostname.apply(this) || ((ipv4only != 1) && cbi_validators.ipaddr.apply(this)) || - ((ipv4only == 1) && cb_validators.ip4addr.apply(this)); + ((ipv4only == 1) && cbi_validators.ip4addr.apply(this)); }, 'hostname': function() @@ -437,8 +521,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; @@ -454,7 +539,7 @@ function cbi_d_update() { if (node && node.parentNode && !cbi_d_check(entry.deps)) { node.parentNode.removeChild(node); state = true; - } else if ((!node || !node.parentNode) && cbi_d_check(entry.deps)) { + } else if (parent && (!node || !node.parentNode) && cbi_d_check(entry.deps)) { var next = undefined; for (next = parent.firstChild; next; next = next.nextSibling) { @@ -473,7 +558,7 @@ function cbi_d_update() { } // hide optionals widget if no choices remaining - if (parent.parentNode && parent.getAttribute('data-optionals')) + if (parent && parent.parentNode && parent.getAttribute('data-optionals')) parent.parentNode.style.display = (parent.options.length <= 1) ? 'none' : ''; } @@ -523,13 +608,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++) { @@ -550,14 +628,25 @@ function cbi_init() { for (var i = 0, node; (node = nodes[i]) !== undefined; i++) { var choices = JSON.parse(node.getAttribute('data-dynlist')); - var options = {}; + var options = null; - for (var j = 0; j < choices[0].length; j++) - options[choices[0][j]] = choices[1][j]; + if (choices[0] && choices[0].length) { + options = {}; + + for (var j = 0; j < choices[0].length; j++) + options[choices[0][j]] = choices[1][j]; + } 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(); } @@ -588,6 +677,7 @@ function cbi_combobox(id, values, def, man, focus) { var obj = document.getElementById(id) var sel = document.createElement("select"); sel.id = selid; + sel.index = obj.index; sel.className = obj.className.replace(/cbi-input-text/, 'cbi-input-select'); if (obj.nextSibling) { @@ -599,9 +689,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"); @@ -636,6 +723,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"; @@ -678,7 +768,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); @@ -689,7 +779,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); @@ -756,7 +846,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')); @@ -921,14 +1011,14 @@ function cbi_dynlist_init(parent, datatype, optional, choices) input.value = ''; cbi_dynlist_keydown({ - target: se, + target: input, keyCode: 8 }); } else { cbi_dynlist_keydown({ - target: se, + target: input, keyCode: 13 }); } @@ -1295,6 +1385,9 @@ String.prototype.format = function() var quot_esc = [/"/g, '"', /'/g, ''']; function esc(s, r) { + if (typeof(s) !== 'string' && !(s instanceof String)) + return ''; + for( var i = 0; i < r.length; i += 2 ) s = s.replace(r[i], r[i+1]); return s; @@ -1305,7 +1398,7 @@ String.prototype.format = function() var re = /^(([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X|q|h|j|t|m))/; var a = b = [], numSubstitutions = 0, numMatches = 0; - while( a = re.exec(str) ) + while (a = re.exec(str)) { var m = a[1]; var leftpart = a[2], pPad = a[3], pJustify = a[4], pMinLength = a[5]; @@ -1328,6 +1421,8 @@ String.prototype.format = function() pad = leftpart.substr(1,1); else if (pPad) pad = pPad; + else + pad = ' '; var justifyRight = true; if (pJustify && pJustify === "-") @@ -1354,11 +1449,11 @@ String.prototype.format = function() break; case 'd': - subst = (+param || 0); + subst = ~~(+param || 0); break; case 'u': - subst = Math.abs(+param || 0); + subst = ~~Math.abs(+param || 0); break; case 'f': @@ -1428,17 +1523,27 @@ String.prototype.format = function() var i = 0; var val = (+param || 0); - var units = [ '', 'K', 'M', 'G', 'T', 'P', 'E' ]; + var units = [ ' ', ' K', ' M', ' G', ' T', ' P', ' E' ]; for (i = 0; (i < units.length) && (val > mf); i++) val /= mf; - subst = val.toFixed(pr) + ' ' + units[i]; + subst = (i ? val.toFixed(pr) : val) + units[i]; + pMinLength = null; break; } } } + if (pMinLength) { + subst = subst.toString(); + for (var i = subst.length; i < pMinLength; i++) + if (pJustify == '-') + subst = subst + ' '; + else + subst = pad + subst; + } + out += leftpart + subst; str = str.substr(m.length); }