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=b4137d48654c99c995b11e82785c7fee3f00943a;hpb=85a5d628ecded0d773ed09ec5e3509b3d8f818d4;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 b4137d486..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; + } }; @@ -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 ) @@ -645,7 +681,7 @@ function cbi_validate_field(cbid, optional, type) var value = (field.options && field.options.selectedIndex > -1) ? field.options[field.options.selectedIndex].value : field.value; - if( !(((value.length == 0) && optional) || vldcb(value)) ) + if( !(((value.length == 0) && optional) || vldcb(value, vargs)) ) { // invalid field.className += ' cbi-input-invalid'; @@ -682,6 +718,50 @@ function cbi_validate_field(cbid, 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) { @@ -752,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) ) @@ -813,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; @@ -844,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; } } }