X-Git-Url: https://git.archive.openwrt.org/?a=blobdiff_plain;f=libs%2Fweb%2Fhtdocs%2Fluci-static%2Fresources%2Fcbi.js;h=ac4eaa9b231c8cb08644c8185c4e1aaf371bbfc1;hb=80e1900b03b2a5315c791f9240f9d7641ae67562;hp=d5355f904bdf50ee97ab5709c8947e386185aff1;hpb=70706cf388f8ac100778831e9ef9d7b1eb74c752;p=project%2Fluci.git diff --git a/libs/web/htdocs/luci-static/resources/cbi.js b/libs/web/htdocs/luci-static/resources/cbi.js index d5355f904..ac4eaa9b2 100644 --- a/libs/web/htdocs/luci-static/resources/cbi.js +++ b/libs/web/htdocs/luci-static/resources/cbi.js @@ -27,6 +27,16 @@ var cbi_validators = { return (cbi_validators.integer(v) && (v >= 0)); }, + 'float': function(v) + { + return !isNaN(parseFloat(v)); + }, + + 'ufloat': function(v) + { + return (cbi_validators['float'](v) && (v >= 0)); + }, + 'ipaddr': function(v) { return cbi_validators.ip4addr(v) || cbi_validators.ip6addr(v); @@ -75,7 +85,7 @@ var cbi_validators = { var colons = 0; var fill = '0'; - for( var i = 0; i < addr.length; i++ ) + for( var i = 1; i < (addr.length-1); i++ ) if( addr.charAt(i) == ':' ) colons++; @@ -85,7 +95,9 @@ var cbi_validators = { for( var i = 0; i < (7 - colons); i++ ) fill += ':0'; - addr = addr.replace(/::/, ':' + fill + ':'); + if (addr.match(/^(.*?)::(.*?)$/)) + addr = (RegExp.$1 ? RegExp.$1 + ':' : '') + fill + + (RegExp.$2 ? ':' + RegExp.$2 : ''); } return (addr.match(/^(?:[a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}$/) != null); @@ -152,6 +164,22 @@ var cbi_validators = { return (v.length == 5) || (v.length == 13); }, + 'uciname': function(v) + { + return (v.match(/^[a-zA-Z0-9_]+$/) != null); + }, + + 'range': function(v, args) + { + var min = parseInt(args[0]); + var max = parseInt(args[1]); + var val = parseInt(v); + + if (!isNaN(min) && !isNaN(max) && !isNaN(val)) + return ((val >= min) && (val <= max)); + + return false; + } }; @@ -290,17 +318,21 @@ function cbi_combobox(id, values, def, man) { var obj = document.getElementById(id) var sel = document.createElement("select"); - sel.id = selid; - sel.className = 'cbi-input-select'; - if (obj.className && obj.className.match(/cbi-input-invalid/)) { - sel.className += ' cbi-input-invalid'; - } + sel.id = selid; + sel.className = 'cbi-input-select'; + if (obj.nextSibling) { obj.parentNode.insertBefore(sel, obj.nextSibling); } else { obj.parentNode.appendChild(sel); } + 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"); @@ -342,10 +374,6 @@ function cbi_combobox(id, values, def, man) { obj.focus(); } else { obj.value = sel.options[sel.selectedIndex].value; - - var vld = obj.getAttribute("cbi_validate"); - sel.className = (!vld || vld()) - ? 'cbi-input-select' : 'cbi-input-select cbi-input-invalid'; } try { @@ -630,6 +658,14 @@ function cbi_validate_reset(form) function cbi_validate_field(cbid, optional, type) { var field = (typeof cbid == "string") ? document.getElementById(cbid) : cbid; + var vargs; + + if( type.match(/^(\w+)\(([^\(\)]+)\)/) ) + { + type = RegExp.$1; + vargs = RegExp.$2.split(/\s*,\s*/); + } + var vldcb = cbi_validators[type]; if( field && vldcb ) @@ -642,8 +678,10 @@ function cbi_validate_field(cbid, optional, type) field.className = field.className.replace(/ cbi-input-invalid/g, ''); // validate value - var value = (field.options) ? field.options[field.options.selectedIndex].value : field.value; - if( !(((value.length == 0) && optional) || vldcb(value)) ) + var value = (field.options && field.options.selectedIndex > -1) + ? field.options[field.options.selectedIndex].value : field.value; + + if( !(((value.length == 0) && optional) || vldcb(value, vargs)) ) { // invalid field.className += ' cbi-input-invalid'; @@ -662,14 +700,68 @@ function cbi_validate_field(cbid, optional, type) cbi_bind(field, "blur", validator); cbi_bind(field, "keyup", validator); + if (field.nodeName == 'SELECT') + { + cbi_bind(field, "change", validator); + cbi_bind(field, "click", validator); + } + field.setAttribute("cbi_validate", validator); field.setAttribute("cbi_datatype", type); field.setAttribute("cbi_optional", (!!optional).toString()); validator(); + + var fcbox = document.getElementById('cbi.combobox.' + field.id); + if (fcbox) + cbi_validate_field(fcbox, optional, type); } } +function cbi_row_swap(elem, up, store) +{ + var tr = elem.parentNode; + while (tr && tr.nodeName != 'tr') + tr = tr.parentNode; + + var table = tr.parentNode; + while (table && table.nodeName != 'table') + table = table.parentNode; + + var s = up ? 3 : 2; + var e = up ? table.rows.length : table.rows.length - 1; + + for (var idx = s; idx < e; idx++) + { + if (table.rows[idx] == tr) + { + if (up) + tr.parentNode.insertBefore(table.rows[idx], table.rows[idx-1]); + else + tr.parentNode.insertBefore(table.rows[idx+1], table.rows[idx]); + + break; + } + } + + var ids = [ ]; + for (idx = 2; idx < table.rows.length; idx++) + { + table.rows[idx].className = table.rows[idx].className.replace( + /cbi-rowstyle-[12]/, 'cbi-rowstyle-' + (1 + (idx % 2)) + ); + + if (table.rows[idx].id && table.rows[idx].id.match(/-(cfg[0-9a-f]+)$/) ) + ids.push(RegExp.$1); + } + + var input = document.getElementById(store); + if (input) + input.value = ids.join(' '); + + return false; +} + if( ! String.serialize ) String.serialize = function(o) { @@ -740,7 +832,7 @@ if( ! String.format ) var str = arguments[0]; var out = ''; - var re = /^(([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X|q|h|j))/; + var re = /^(([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X|q|h|j|t))/; var a = b = [], numSubstitutions = 0, numMatches = 0; while( a = re.exec(str) ) @@ -801,7 +893,7 @@ if( ! String.format ) case 'f': subst = (precision > -1) - ? Math.round((parseFloat(param) || 0.0) * Math.pow(10, precision)) / Math.pow(10, precision) + ? ((parseFloat(param) || 0.0)).toFixed(precision) : (parseFloat(param) || 0.0); break; @@ -832,6 +924,33 @@ if( ! String.format ) case 'j': subst = String.serialize(param); break; + + case 't': + var td = 0; + var th = 0; + var tm = 0; + var ts = (param || 0); + + if (ts > 60) { + tm = Math.floor(ts / 60); + ts = (ts % 60); + } + + if (tm > 60) { + th = Math.floor(tm / 60); + tm = (tm % 60); + } + + if (th > 24) { + td = Math.floor(th / 24); + th = (th % 24); + } + + subst = (td > 0) + ? String.format('%dd %dh %dm %ds', td, th, tm, ts) + : String.format('%dh %dm %ds', th, tm, ts); + + break; } } }