summaryrefslogtreecommitdiffhomepage
path: root/libs/web/htdocs/luci-static/resources/cbi.js
diff options
context:
space:
mode:
authorJo-Philipp Wich <jow@openwrt.org>2010-10-19 04:06:46 +0000
committerJo-Philipp Wich <jow@openwrt.org>2010-10-19 04:06:46 +0000
commit1a3533cb8693ef81e9ce9e638e4fb24fa67b67fd (patch)
tree5ace53a364a2e650d36f5fdd76b19f3ba88449ad /libs/web/htdocs/luci-static/resources/cbi.js
parentdb46443478b27646fe58730ab2900d549509d0e1 (diff)
libs/web: add String.serialize() and String.format() to cbi.js
Diffstat (limited to 'libs/web/htdocs/luci-static/resources/cbi.js')
-rw-r--r--libs/web/htdocs/luci-static/resources/cbi.js176
1 files changed, 174 insertions, 2 deletions
diff --git a/libs/web/htdocs/luci-static/resources/cbi.js b/libs/web/htdocs/luci-static/resources/cbi.js
index 4af6e58d9b..a00254c76e 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, '&#38;', /"/g, '&#34;', /'/g, '&#39;', /</g, '&#60;', />/g, '&#62;'];
+ var quot_esc = [/"/g, '&#34;', /'/g, '&#39;'];
+
+ 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;
+ }