2 LuCI - Lua Configuration Interface
4 Copyright 2008 Steven Barth <steven@midlink.org>
5 Copyright 2008-2012 Jo-Philipp Wich <jow@openwrt.org>
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
11 http://www.apache.org/licenses/LICENSE-2.0
17 var cbi_validators = {
21 return (this.match(/^-?[0-9]+$/) != null);
24 'uinteger': function()
26 return (cbi_validators.integer.apply(this) && (this >= 0));
31 return !isNaN(parseFloat(this));
36 return (cbi_validators['float'].apply(this) && (this >= 0));
41 return cbi_validators.ip4addr.apply(this) ||
42 cbi_validators.ip6addr.apply(this);
47 if (this.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})(\/(\S+))?$/))
49 return (RegExp.$1 >= 0) && (RegExp.$1 <= 255) &&
50 (RegExp.$2 >= 0) && (RegExp.$2 <= 255) &&
51 (RegExp.$3 >= 0) && (RegExp.$3 <= 255) &&
52 (RegExp.$4 >= 0) && (RegExp.$4 <= 255) &&
53 ((RegExp.$6.indexOf('.') < 0)
54 ? ((RegExp.$6 >= 0) && (RegExp.$6 <= 32))
55 : (cbi_validators.ip4addr.apply(RegExp.$6)))
64 if( this.match(/^([a-fA-F0-9:.]+)(\/(\d+))?$/) )
66 if( !RegExp.$2 || ((RegExp.$3 >= 0) && (RegExp.$3 <= 128)) )
75 if( addr.indexOf('.') > 0 )
77 var off = addr.lastIndexOf(':');
79 if( !(off && cbi_validators.ip4addr.apply(addr.substr(off+1))) )
82 addr = addr.substr(0, off) + ':0:0';
85 if( addr.indexOf('::') >= 0 )
90 for( var i = 1; i < (addr.length-1); i++ )
91 if( addr.charAt(i) == ':' )
97 for( var i = 0; i < (7 - colons); i++ )
100 if (addr.match(/^(.*?)::(.*?)$/))
101 addr = (RegExp.$1 ? RegExp.$1 + ':' : '') + fill +
102 (RegExp.$2 ? ':' + RegExp.$2 : '');
105 return (addr.match(/^(?:[a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}$/) != null);
114 return cbi_validators.integer.apply(this) &&
115 (this >= 0) && (this <= 65535);
118 'portrange': function()
120 if (this.match(/^(\d+)-(\d+)$/))
125 return cbi_validators.port.apply(p1) &&
126 cbi_validators.port.apply(p2) &&
127 (parseInt(p1) <= parseInt(p2))
132 return cbi_validators.port.apply(this);
136 'macaddr': function()
138 return (this.match(/^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$/) != null);
141 'host': function(ipv4only)
143 return cbi_validators.hostname.apply(this) ||
144 ((ipv4only != 1) && cbi_validators.ipaddr.apply(this)) ||
145 ((ipv4only == 1) && cb_validators.ip4addr.apply(this));
148 'hostname': function()
150 if (this.length <= 253)
151 return (this.match(/^[a-zA-Z0-9]+$/) != null ||
152 (this.match(/^[a-zA-Z0-9_][a-zA-Z0-9_\-.]*[a-zA-Z0-9]$/) &&
153 this.match(/[^0-9.]/)));
158 'network': function()
160 return cbi_validators.uciname.apply(this) ||
161 cbi_validators.host.apply(this);
164 'hostport': function(ipv4only)
166 var hp = this.split(/:/);
169 return (cbi_validators.host.apply(hp[0], ipv4only) &&
170 cbi_validators.port.apply(hp[1]));
175 'ip4addrport': function()
177 var hp = this.split(/:/);
180 return (cbi_validators.ipaddr.apply(hp[0]) &&
181 cbi_validators.port.apply(hp[1]));
185 'ipaddrport': function(bracket)
187 if (this.match(/^([^\[\]:]+):([^:]+)$/)) {
190 return (cbi_validators.ip4addr.apply(addr) &&
191 cbi_validators.port.apply(port));
192 } else if ((bracket == 1) && (this.match(/^\[(.+)\]:([^:]+)$/))) {
195 return (cbi_validators.ip6addr.apply(addr) &&
196 cbi_validators.port.apply(port));
197 } else if ((bracket != 1) && (this.match(/^([^\[\]]+):([^:]+)$/))) {
200 return (cbi_validators.ip6addr.apply(addr) &&
201 cbi_validators.port.apply(port));
212 return (v.match(/^[a-fA-F0-9]{64}$/) != null);
214 return (v.length >= 8) && (v.length <= 63);
221 if ( v.substr(0,2) == 's:' )
224 if( (v.length == 10) || (v.length == 26) )
225 return (v.match(/^[a-fA-F0-9]{10,26}$/) != null);
227 return (v.length == 5) || (v.length == 13);
230 'uciname': function()
232 return (this.match(/^[a-zA-Z0-9_]+$/) != null);
235 'range': function(min, max)
237 var val = parseFloat(this);
238 if (!isNaN(min) && !isNaN(max) && !isNaN(val))
239 return ((val >= min) && (val <= max));
246 var val = parseFloat(this);
247 if (!isNaN(min) && !isNaN(val))
255 var val = parseFloat(this);
256 if (!isNaN(max) && !isNaN(val))
262 'rangelength': function(min, max)
265 if (!isNaN(min) && !isNaN(max))
266 return ((val.length >= min) && (val.length <= max));
271 'minlength': function(min)
275 return (val.length >= min);
280 'maxlength': function(max)
284 return (val.length <= max);
291 for (var i = 0; i < arguments.length; i += 2)
293 if (typeof arguments[i] != 'function')
295 if (arguments[i] == this)
299 else if (arguments[i].apply(this, arguments[i+1]))
309 for (var i = 0; i < arguments.length; i += 2)
311 if (typeof arguments[i] != 'function')
313 if (arguments[i] != this)
317 else if (!arguments[i].apply(this, arguments[i+1]))
327 return cbi_validators.or.apply(
328 this.replace(/^[ \t]*![ \t]*/, ''), arguments);
331 'list': function(subvalidator, subargs)
333 if (typeof subvalidator != 'function')
336 var tokens = this.match(/[^ \t]+/g);
337 for (var i = 0; i < tokens.length; i++)
338 if (!subvalidator.apply(tokens[i], subargs))
343 'phonedigit': function()
345 return (this.match(/^[0-9\*#!\.]+$/) != null);
347 'timehhmmss': function()
349 return (this.match(/^[0-6][0-9]:[0-6][0-9]:[0-6][0-9]$/) != null);
351 'dateyyyymmdd': function()
356 if (this.match(/^(\d\d\d\d)-(\d\d)-(\d\d)/)) {
357 var year = RegExp.$1;
358 var month = RegExp.$2;
361 var days_in_month = [ 31, 28, 31, 30, 31, 30, 31, 31, 30 , 31, 30, 31 ];
362 function is_leap_year(year) {
363 return ((year % 4) == 0) && ((year % 100) != 0) || ((year % 400) == 0);
365 function get_days_in_month(month, year) {
366 if ((month == 2) && is_leap_year(year)) {
369 return days_in_month[month];
372 /* Firewall rules in the past don't make sense */
376 if ((month <= 0) || (month > 12)) {
379 if ((day <= 0) || (day > get_days_in_month(month, year))) {
391 function cbi_d_add(field, dep, index) {
392 var obj = (typeof(field) === 'string') ? document.getElementById(field) : field;
395 for (var i=0; i<cbi_d.length; i++) {
396 if (cbi_d[i].id == obj.id) {
405 "parent": obj.parentNode.id,
409 cbi_d.unshift(entry);
415 function cbi_d_checkvalue(target, ref) {
416 var t = document.getElementById(target);
420 var tl = document.getElementsByName(target);
422 if( tl.length > 0 && (tl[0].type == 'radio' || tl[0].type == 'checkbox'))
423 for( var i = 0; i < tl.length; i++ )
424 if( tl[i].checked ) {
429 value = value ? value : "";
430 } else if (!t.value) {
435 if (t.type == "checkbox") {
436 value = t.checked ? value : "";
440 return (value == ref)
443 function cbi_d_check(deps) {
446 for (var i=0; i<deps.length; i++) {
449 for (var j in deps[i]) {
450 if (j == "!reverse") {
452 } else if (j == "!default") {
456 istat = (istat && cbi_d_checkvalue(j, deps[i][j]))
466 function cbi_d_update() {
468 for (var i=0; i<cbi_d.length; i++) {
469 var entry = cbi_d[i];
470 var node = document.getElementById(entry.id);
471 var parent = document.getElementById(entry.parent);
473 if (node && node.parentNode && !cbi_d_check(entry.deps)) {
474 node.parentNode.removeChild(node);
476 } else if ((!node || !node.parentNode) && cbi_d_check(entry.deps)) {
477 var next = undefined;
479 for (next = parent.firstChild; next; next = next.nextSibling) {
480 if (next.getAttribute && parseInt(next.getAttribute('data-index'), 10) > entry.index) {
486 parent.appendChild(entry.node);
488 parent.insertBefore(entry.node, next);
495 if (entry && entry.parent) {
497 cbi_tag_last(parent);
505 function cbi_init() {
506 var nodes = document.querySelectorAll('[data-depends]');
508 for (var i = 0, node; (node = nodes[i]) !== undefined; i++) {
509 var index = parseInt(node.getAttribute('data-index'), 10);
510 var depends = JSON.parse(node.getAttribute('data-depends'));
511 if (!isNaN(index) && depends.length > 0) {
512 for (var alt = 0; alt < depends.length; alt++) {
513 cbi_d_add(node, depends[alt], index);
518 nodes = document.querySelectorAll('[data-change]');
520 for (var i = 0, node; (node = nodes[i]) !== undefined; i++) {
521 var events = node.getAttribute('data-change').split(' ');
522 for (var j = 0, event; (event = events[j]) !== undefined; j++) {
523 cbi_bind(node, event, cbi_d_update);
530 function cbi_bind(obj, type, callback, mode) {
531 if (!obj.addEventListener) {
532 obj.attachEvent('on' + type,
534 var e = window.event;
536 if (!e.target && e.srcElement)
537 e.target = e.srcElement;
539 return !!callback(e);
543 obj.addEventListener(type, callback, !!mode);
548 function cbi_combobox(id, values, def, man, focus) {
549 var selid = "cbi.combobox." + id;
550 if (document.getElementById(selid)) {
554 var obj = document.getElementById(id)
555 var sel = document.createElement("select");
557 sel.className = obj.className.replace(/cbi-input-text/, 'cbi-input-select');
559 if (obj.nextSibling) {
560 obj.parentNode.insertBefore(sel, obj.nextSibling);
562 obj.parentNode.appendChild(sel);
565 var dt = obj.getAttribute('cbi_datatype');
566 var op = obj.getAttribute('cbi_optional');
569 cbi_validate_field(sel, op == 'true', dt);
571 if (!values[obj.value]) {
572 if (obj.value == "") {
573 var optdef = document.createElement("option");
575 optdef.appendChild(document.createTextNode(def));
576 sel.appendChild(optdef);
578 var opt = document.createElement("option");
579 opt.value = obj.value;
580 opt.selected = "selected";
581 opt.appendChild(document.createTextNode(obj.value));
582 sel.appendChild(opt);
586 for (var i in values) {
587 var opt = document.createElement("option");
590 if (obj.value == i) {
591 opt.selected = "selected";
594 opt.appendChild(document.createTextNode(values[i]));
595 sel.appendChild(opt);
598 var optman = document.createElement("option");
600 optman.appendChild(document.createTextNode(man));
601 sel.appendChild(optman);
603 obj.style.display = "none";
605 cbi_bind(sel, "change", function() {
606 if (sel.selectedIndex == sel.options.length - 1) {
607 obj.style.display = "inline";
609 sel.parentNode.removeChild(sel);
612 obj.value = sel.options[sel.selectedIndex].value;
622 // Retrigger validation in select
629 function cbi_combobox_init(id, values, def, man) {
630 var obj = document.getElementById(id);
631 cbi_bind(obj, "blur", function() {
632 cbi_combobox(id, values, def, man, true);
634 cbi_combobox(id, values, def, man, false);
637 function cbi_filebrowser(id, url, defpath) {
638 var field = document.getElementById(id);
639 var browser = window.open(
640 url + ( field.value || defpath || '' ) + '?field=' + id,
641 "luci_filebrowser", "width=300,height=400,left=100,top=200,scrollbars=yes"
647 function cbi_browser_init(id, respath, url, defpath)
649 function cbi_browser_btnclick(e) {
650 cbi_filebrowser(id, url, defpath);
654 var field = document.getElementById(id);
656 var btn = document.createElement('img');
657 btn.className = 'cbi-image-button';
658 btn.src = respath + '/cbi/folder.gif';
659 field.parentNode.insertBefore(btn, field.nextSibling);
661 cbi_bind(btn, 'click', cbi_browser_btnclick);
664 function cbi_dynlist_init(name, respath, datatype, optional, url, defpath, choices)
666 var input0 = document.getElementsByName(name)[0];
667 var prefix = input0.name;
668 var parent = input0.parentNode;
669 var holder = input0.placeholder;
673 function cbi_dynlist_redraw(focus, add, del)
677 while (parent.firstChild)
679 var n = parent.firstChild;
680 var i = parseInt(n.index);
684 if (n.nodeName.toLowerCase() == 'input')
685 values.push(n.value || '');
686 else if (n.nodeName.toLowerCase() == 'select')
687 values[values.length-1] = n.options[n.selectedIndex].value;
690 parent.removeChild(n);
696 values.splice(focus, 0, '');
698 else if (values.length == 0)
704 for (var i = 0; i < values.length; i++)
706 var t = document.createElement('input');
707 t.id = prefix + '.' + (i+1);
712 t.className = 'cbi-input-text';
714 if (i == 0 && holder)
716 t.placeholder = holder;
719 var b = document.createElement('img');
720 b.src = respath + ((i+1) < values.length ? '/cbi/remove.gif' : '/cbi/add.gif');
721 b.className = 'cbi-image-button';
723 parent.appendChild(t);
724 parent.appendChild(b);
725 if (datatype == 'file')
727 cbi_browser_init(t.id, respath, url, defpath);
730 parent.appendChild(document.createElement('br'));
734 cbi_validate_field(t.id, ((i+1) == values.length) || optional, datatype);
739 cbi_combobox_init(t.id, choices[0], '', choices[1]);
742 cbi_bind(b, 'keydown', cbi_dynlist_keydown);
743 cbi_bind(b, 'keypress', cbi_dynlist_keypress);
745 if (i == focus || -i == focus)
750 cbi_bind(t, 'keydown', cbi_dynlist_keydown);
751 cbi_bind(t, 'keypress', cbi_dynlist_keypress);
757 else if (-i == focus)
761 /* force cursor to end */
768 cbi_bind(b, 'click', cbi_dynlist_btnclick);
772 function cbi_dynlist_keypress(ev)
774 ev = ev ? ev : window.event;
776 var se = ev.target ? ev.target : ev.srcElement;
778 if (se.nodeType == 3)
783 /* backspace, delete */
786 if (se.value.length == 0)
788 if (ev.preventDefault)
796 /* enter, arrow up, arrow down */
800 if (ev.preventDefault)
809 function cbi_dynlist_keydown(ev)
811 ev = ev ? ev : window.event;
813 var se = ev.target ? ev.target : ev.srcElement;
815 if (se.nodeType == 3)
818 var prev = se.previousSibling;
819 while (prev && prev.name != name)
820 prev = prev.previousSibling;
822 var next = se.nextSibling;
823 while (next && next.name != name)
824 next = next.nextSibling;
826 /* advance one further in combobox case */
827 if (next && next.nextSibling.name == name)
828 next = next.nextSibling;
832 /* backspace, delete */
835 var del = (se.nodeName.toLowerCase() == 'select')
836 ? true : (se.value.length == 0);
840 if (ev.preventDefault)
843 var focus = se.index;
847 cbi_dynlist_redraw(focus, -1, se.index);
856 cbi_dynlist_redraw(-1, se.index, -1);
877 function cbi_dynlist_btnclick(ev)
879 ev = ev ? ev : window.event;
881 var se = ev.target ? ev.target : ev.srcElement;
882 var input = se.previousSibling;
883 while (input && input.name != name) {
884 input = input.previousSibling;
887 if (se.src.indexOf('remove') > -1)
891 cbi_dynlist_keydown({
898 cbi_dynlist_keydown({
907 cbi_dynlist_redraw(NaN, -1, -1);
911 function cbi_t_add(section, tab) {
912 var t = document.getElementById('tab.' + section + '.' + tab);
913 var c = document.getElementById('container.' + section + '.' + tab);
916 cbi_t[section] = (cbi_t[section] || [ ]);
917 cbi_t[section][tab] = { 'tab': t, 'container': c, 'cid': c.id };
921 function cbi_t_switch(section, tab) {
922 if( cbi_t[section] && cbi_t[section][tab] ) {
923 var o = cbi_t[section][tab];
924 var h = document.getElementById('tab.' + section);
925 for( var tid in cbi_t[section] ) {
926 var o2 = cbi_t[section][tid];
927 if( o.tab.id != o2.tab.id ) {
928 o2.tab.className = o2.tab.className.replace(/(^| )cbi-tab( |$)/, " cbi-tab-disabled ");
929 o2.container.style.display = 'none';
933 o2.tab.className = o2.tab.className.replace(/(^| )cbi-tab-disabled( |$)/, " cbi-tab ");
934 o2.container.style.display = 'block';
941 function cbi_t_update() {
945 for( var sid in cbi_t )
946 for( var tid in cbi_t[sid] )
948 var t = cbi_t[sid][tid].tab;
949 var c = cbi_t[sid][tid].container;
950 var n = c.getElementsByTagName('div');
952 if (n.length === 0) {
953 t.style.display = 'none';
955 else if (t.style.display == 'none') {
956 t.style.display = '';
957 t.className += ' cbi-tab-highlighted';
965 if (hl_tabs.length > 0)
966 window.setTimeout(function() {
967 for( var i = 0; i < hl_tabs.length; i++ )
968 hl_tabs[i].className = hl_tabs[i].className.replace(/ cbi-tab-highlighted/g, '');
975 function cbi_validate_form(form, errmsg)
977 /* if triggered by a section removal or addition, don't validate */
978 if( form.cbi_state == 'add-section' || form.cbi_state == 'del-section' )
981 if( form.cbi_validators )
983 for( var i = 0; i < form.cbi_validators.length; i++ )
985 var validator = form.cbi_validators[i];
986 if( !validator() && errmsg )
997 function cbi_validate_reset(form)
1000 function() { cbi_validate_form(form, null) }, 100
1006 function cbi_validate_compile(code)
1015 for (var i = 0; i < code.length; i++)
1023 switch (code.charCodeAt(i))
1035 var label = code.substring(pos, i);
1036 label = label.replace(/\\(.)/g, '$1');
1037 label = label.replace(/^[ \t]+/g, '');
1038 label = label.replace(/[ \t]+$/g, '');
1040 if (label && !isNaN(label))
1042 stack.push(parseFloat(label));
1044 else if (label.match(/^(['"]).*\1$/))
1046 stack.push(label.replace(/^(['"])(.*)\1$/, '$2'));
1048 else if (typeof cbi_validators[label] == 'function')
1050 stack.push(cbi_validators[label]);
1055 throw "Syntax error, unhandled token '"+label+"'";
1060 depth += (code.charCodeAt(i) == 40);
1066 if (typeof stack[stack.length-2] != 'function')
1067 throw "Syntax error, argument list follows non-function";
1069 stack[stack.length-1] =
1070 arguments.callee(code.substring(pos, i));
1081 function cbi_validate_field(cbid, optional, type)
1083 var field = (typeof cbid == "string") ? document.getElementById(cbid) : cbid;
1084 var vstack; try { vstack = cbi_validate_compile(type); } catch(e) { };
1086 if (field && vstack && typeof vstack[0] == "function")
1088 var validator = function()
1093 field.className = field.className.replace(/ cbi-input-invalid/g, '');
1096 var value = (field.options && field.options.selectedIndex > -1)
1097 ? field.options[field.options.selectedIndex].value : field.value;
1099 if (!(((value.length == 0) && optional) || vstack[0].apply(value, vstack[1])))
1102 field.className += ' cbi-input-invalid';
1110 if( ! field.form.cbi_validators )
1111 field.form.cbi_validators = [ ];
1113 field.form.cbi_validators.push(validator);
1115 cbi_bind(field, "blur", validator);
1116 cbi_bind(field, "keyup", validator);
1118 if (field.nodeName == 'SELECT')
1120 cbi_bind(field, "change", validator);
1121 cbi_bind(field, "click", validator);
1124 field.setAttribute("cbi_validate", validator);
1125 field.setAttribute("cbi_datatype", type);
1126 field.setAttribute("cbi_optional", (!!optional).toString());
1130 var fcbox = document.getElementById('cbi.combobox.' + field.id);
1132 cbi_validate_field(fcbox, optional, type);
1136 function cbi_row_swap(elem, up, store)
1138 var tr = elem.parentNode;
1139 while (tr && tr.nodeName.toLowerCase() != 'tr')
1145 var table = tr.parentNode;
1146 while (table && table.nodeName.toLowerCase() != 'table')
1147 table = table.parentNode;
1153 var e = up ? table.rows.length : table.rows.length - 1;
1155 for (var idx = s; idx < e; idx++)
1157 if (table.rows[idx] == tr)
1160 tr.parentNode.insertBefore(table.rows[idx], table.rows[idx-1]);
1162 tr.parentNode.insertBefore(table.rows[idx+1], table.rows[idx]);
1169 for (idx = 2; idx < table.rows.length; idx++)
1171 table.rows[idx].className = table.rows[idx].className.replace(
1172 /cbi-rowstyle-[12]/, 'cbi-rowstyle-' + (1 + (idx % 2))
1175 if (table.rows[idx].id && table.rows[idx].id.match(/-([^\-]+)$/) )
1176 ids.push(RegExp.$1);
1179 var input = document.getElementById(store);
1181 input.value = ids.join(' ');
1186 function cbi_tag_last(container)
1190 for (var i = 0; i < container.childNodes.length; i++)
1192 var c = container.childNodes[i];
1193 if (c.nodeType == 1 && c.nodeName.toLowerCase() == 'div')
1195 c.className = c.className.replace(/ cbi-value-last$/, '');
1202 last.className += ' cbi-value-last';
1206 String.prototype.serialize = function()
1223 for( var i = 0; i < o.length; i++ )
1224 s += (s ? ', ' : '') + String.serialize(o[i]);
1226 return '[ ' + s + ' ]';
1235 s += (s ? ', ' : '') + k + ': ' + String.serialize(o[k]);
1237 return '{ ' + s + ' }';
1244 if( o.match(/[^a-zA-Z0-9_,.: -]/) )
1245 return 'decodeURIComponent("' + encodeURIComponent(o) + '")';
1249 return '"' + o + '"';
1254 return o.toString();
1258 String.prototype.format = function()
1263 var html_esc = [/&/g, '&', /"/g, '"', /'/g, ''', /</g, '<', />/g, '>'];
1264 var quot_esc = [/"/g, '"', /'/g, '''];
1266 function esc(s, r) {
1267 for( var i = 0; i < r.length; i += 2 )
1268 s = s.replace(r[i], r[i+1]);
1274 var re = /^(([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X|q|h|j|t|m))/;
1275 var a = b = [], numSubstitutions = 0, numMatches = 0;
1277 while( a = re.exec(str) )
1280 var leftpart = a[2], pPad = a[3], pJustify = a[4], pMinLength = a[5];
1281 var pPrecision = a[6], pType = a[7];
1291 if (numSubstitutions < arguments.length)
1293 var param = arguments[numSubstitutions++];
1296 if (pPad && pPad.substr(0,1) == "'")
1297 pad = leftpart.substr(1,1);
1301 var justifyRight = true;
1302 if (pJustify && pJustify === "-")
1303 justifyRight = false;
1307 minLength = parseInt(pMinLength);
1310 if (pPrecision && pType == 'f')
1311 precision = parseInt(pPrecision.substring(1));
1318 subst = (parseInt(param) || 0).toString(2);
1322 subst = String.fromCharCode(parseInt(param) || 0);
1326 subst = (parseInt(param) || 0);
1330 subst = Math.abs(parseInt(param) || 0);
1334 subst = (precision > -1)
1335 ? ((parseFloat(param) || 0.0)).toFixed(precision)
1336 : (parseFloat(param) || 0.0);
1340 subst = (parseInt(param) || 0).toString(8);
1348 subst = ('' + (parseInt(param) || 0).toString(16)).toLowerCase();
1352 subst = ('' + (parseInt(param) || 0).toString(16)).toUpperCase();
1356 subst = esc(param, html_esc);
1360 subst = esc(param, quot_esc);
1364 subst = String.serialize(param);
1371 var ts = (param || 0);
1374 tm = Math.floor(ts / 60);
1379 th = Math.floor(tm / 60);
1384 td = Math.floor(th / 24);
1389 ? String.format('%dd %dh %dm %ds', td, th, tm, ts)
1390 : String.format('%dh %dm %ds', th, tm, ts);
1395 var mf = pMinLength ? parseInt(pMinLength) : 1000;
1396 var pr = pPrecision ? Math.floor(10*parseFloat('0'+pPrecision)) : 2;
1399 var val = parseFloat(param || 0);
1400 var units = [ '', 'K', 'M', 'G', 'T', 'P', 'E' ];
1402 for (i = 0; (i < units.length) && (val > mf); i++)
1405 subst = val.toFixed(pr) + ' ' + units[i];
1411 out += leftpart + subst;
1412 str = str.substr(m.length);
1418 String.prototype.nobr = function()
1420 return this.replace(/[\s\n]+/g, ' ');
1423 String.serialize = function()
1426 for (var i = 1; i < arguments.length; i++)
1427 a.push(arguments[i]);
1428 return ''.serialize.apply(arguments[0], a);
1431 String.format = function()
1434 for (var i = 1; i < arguments.length; i++)
1435 a.push(arguments[i]);
1436 return ''.format.apply(arguments[0], a);
1439 String.nobr = function()
1442 for (var i = 1; i < arguments.length; i++)
1443 a.push(arguments[i]);
1444 return ''.nobr.apply(arguments[0], a);