From: Jo-Philipp Wich Date: Tue, 19 Oct 2010 04:06:46 +0000 (+0000) Subject: libs/web: add String.serialize() and String.format() to cbi.js X-Git-Tag: 0.10.0~593 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=1a3533cb8693ef81e9ce9e638e4fb24fa67b67fd;p=oweals%2Fluci.git libs/web: add String.serialize() and String.format() to cbi.js --- diff --git a/libs/web/htdocs/luci-static/resources/cbi.js b/libs/web/htdocs/luci-static/resources/cbi.js index 4af6e58d9..a00254c76 100644 --- a/libs/web/htdocs/luci-static/resources/cbi.js +++ b/libs/web/htdocs/luci-static/resources/cbi.js @@ -94,7 +94,7 @@ var cbi_validators = { fields++; last = i + 1; - } + } } return (fields == 8); @@ -264,7 +264,7 @@ function cbi_d_update() { } } - if (entry.parent) { + if (entry && entry.parent) { cbi_t_update(); } @@ -516,3 +516,175 @@ function cbi_validate_field(cbid, optional, type) } } +if( ! String.serialize ) + String.serialize = function(o) + { + switch(typeof(o)) + { + case 'object': + // null + if( o == null ) + { + return 'null'; + } + + // array + else if( o.length ) + { + var i, s = ''; + + for( var i = 0; i < o.length; i++ ) + s += (s ? ', ' : '') + String.serialize(o[i]); + + return '[ ' + s + ' ]'; + } + + // object + else + { + var k, s = ''; + + for( k in o ) + s += (s ? ', ' : '') + k + ': ' + String.serialize(o[k]); + + return '{ ' + s + ' }'; + } + + break; + + case 'string': + // complex string + if( o.match(/[^a-zA-Z0-9_,.: -]/) ) + return 'decodeURIComponent("' + encodeURIComponent(o) + '")'; + + // simple string + else + return '"' + o + '"'; + + break; + + default: + return o.toString(); + } + } + + +if( ! String.format ) + String.format = function() + { + if (!arguments || arguments.length < 1 || !RegExp) + return; + + var html_esc = [/&/g, '&', /"/g, '"', /'/g, ''', //g, '>']; + var quot_esc = [/"/g, '"', /'/g, ''']; + + function esc(s, r) { + for( var i = 0; i < r.length; i += 2 ) + s = s.replace(r[i], r[i+1]); + return s; + } + + 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 a = b = [], numSubstitutions = 0, numMatches = 0; + + while( a = re.exec(str) ) + { + var m = a[1]; + var leftpart = a[2], pPad = a[3], pJustify = a[4], pMinLength = a[5]; + var pPrecision = a[6], pType = a[7]; + + numMatches++; + + if (pType == '%') + { + subst = '%'; + } + else + { + if (numSubstitutions++ < arguments.length) + { + var param = arguments[numSubstitutions]; + + var pad = ''; + if (pPad && pPad.substr(0,1) == "'") + pad = leftpart.substr(1,1); + else if (pPad) + pad = pPad; + + var justifyRight = true; + if (pJustify && pJustify === "-") + justifyRight = false; + + var minLength = -1; + if (pMinLength) + minLength = parseInt(pMinLength); + + var precision = -1; + if (pPrecision && pType == 'f') + precision = parseInt(pPrecision.substring(1)); + + var subst = param; + + switch(pType) + { + case 'b': + subst = (parseInt(param) || 0).toString(2); + break; + + case 'c': + subst = String.fromCharCode(parseInt(param) || 0); + break; + + case 'd': + subst = (parseInt(param) || 0); + break; + + case 'u': + subst = Math.abs(parseInt(param) || 0); + break; + + case 'f': + subst = (precision > -1) + ? Math.round((parseFloat(param) || 0.0) * Math.pow(10, precision)) / Math.pow(10, precision) + : (parseFloat(param) || 0.0); + break; + + case 'o': + subst = (parseInt(param) || 0).toString(8); + break; + + case 's': + subst = param; + break; + + case 'x': + subst = ('' + (parseInt(param) || 0).toString(16)).toLowerCase(); + break; + + case 'X': + subst = ('' + (parseInt(param) || 0).toString(16)).toUpperCase(); + break; + + case 'h': + subst = esc(param, html_esc); + break; + + case 'q': + subst = esc(param, quot_esc); + break; + + case 'j': + subst = String.serialize(param); + break; + } + } + } + + out += leftpart + subst; + str = str.substr(m.length); + } + + return out + str; + }