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
16 var cbi_strings
= { path
: {}, label
: {} };
19 return (/^-?\d+$/.test(x
) ? +x
: NaN
);
23 return (/^-?\d+(?:\.\d+)?$/.test(x
) ? +x
: NaN
);
26 var cbi_validators
= {
33 'uinteger': function()
35 return (Int(this) >= 0);
45 return (Dec(this) >= 0);
50 return cbi_validators
.ip4addr
.apply(this) ||
51 cbi_validators
.ip6addr
.apply(this);
56 if (this.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})(\/(\S+))?$/))
58 return (RegExp
.$1 >= 0) && (RegExp
.$1 <= 255) &&
59 (RegExp
.$2 >= 0) && (RegExp
.$2 <= 255) &&
60 (RegExp
.$3 >= 0) && (RegExp
.$3 <= 255) &&
61 (RegExp
.$4 >= 0) && (RegExp
.$4 <= 255) &&
62 ((RegExp
.$6.indexOf('.') < 0)
63 ? ((RegExp
.$6 >= 0) && (RegExp
.$6 <= 32))
64 : (cbi_validators
.ip4addr
.apply(RegExp
.$6)))
73 if( this.match(/^([a-fA-F0-9:.]+)(\/(\d+))?$/) )
75 if( !RegExp
.$2 || ((RegExp
.$3 >= 0) && (RegExp
.$3 <= 128)) )
84 if( addr
.indexOf('.') > 0 )
86 var off
= addr
.lastIndexOf(':');
88 if( !(off
&& cbi_validators
.ip4addr
.apply(addr
.substr(off
+1))) )
91 addr
= addr
.substr(0, off
) + ':0:0';
94 if( addr
.indexOf('::') >= 0 )
99 for( var i
= 1; i
< (addr
.length
-1); i
++ )
100 if( addr
.charAt(i
) == ':' )
106 for( var i
= 0; i
< (7 - colons
); i
++ )
109 if (addr
.match(/^(.*?)::(.*?)$/))
110 addr
= (RegExp
.$1 ? RegExp
.$1 + ':' : '') + fill
+
111 (RegExp
.$2 ? ':' + RegExp
.$2 : '');
114 return (addr
.match(/^(?:[a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}$/) != null);
123 return cbi_validators
.ipmask4
.apply(this) ||
124 cbi_validators
.ipmask6
.apply(this);
127 'ipmask4': function()
129 var ip
= this, mask
= 32;
131 if (ip
.match(/^(\S+)\/(\S+)$/))
137 if (!isNaN(mask
) && (mask
< 0 || mask
> 32))
140 if (isNaN(mask
) && !cbi_validators
.ip4addr
.apply(mask
))
143 return cbi_validators
.ip4addr
.apply(ip
);
146 'ipmask6': function()
148 var ip
= this, mask
= 128;
150 if (ip
.match(/^(\S+)\/(\S+)$/))
156 if (!isNaN(mask
) && (mask
< 0 || mask
> 128))
159 if (isNaN(mask
) && !cbi_validators
.ip6addr
.apply(mask
))
162 return cbi_validators
.ip6addr
.apply(ip
);
168 return (p
>= 0 && p
<= 65535);
171 'portrange': function()
173 if (this.match(/^(\d+)-(\d+)$/))
177 return (p1
<= p2
&& p2
<= 65535);
180 return cbi_validators
.port
.apply(this);
183 'macaddr': function()
185 return (this.match(/^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$/) != null);
188 'host': function(ipv4only
)
190 return cbi_validators
.hostname
.apply(this) ||
191 ((ipv4only
!= 1) && cbi_validators
.ipaddr
.apply(this)) ||
192 ((ipv4only
== 1) && cb_validators
.ip4addr
.apply(this));
195 'hostname': function()
197 if (this.length
<= 253)
198 return (this.match(/^[a-zA-Z0-9]+$/) != null ||
199 (this.match(/^[a-zA-Z0-9_][a-zA-Z0-9_\-.]*[a-zA-Z0-9]$/) &&
200 this.match(/[^0-9.]/)));
205 'network': function()
207 return cbi_validators
.uciname
.apply(this) ||
208 cbi_validators
.host
.apply(this);
211 'hostport': function(ipv4only
)
213 var hp
= this.split(/:/);
216 return (cbi_validators
.host
.apply(hp
[0], ipv4only
) &&
217 cbi_validators
.port
.apply(hp
[1]));
222 'ip4addrport': function()
224 var hp
= this.split(/:/);
227 return (cbi_validators
.ipaddr
.apply(hp
[0]) &&
228 cbi_validators
.port
.apply(hp
[1]));
232 'ipaddrport': function(bracket
)
234 if (this.match(/^([^\[\]:]+):([^:]+)$/)) {
237 return (cbi_validators
.ip4addr
.apply(addr
) &&
238 cbi_validators
.port
.apply(port
));
239 } else if ((bracket
== 1) && (this.match(/^\[(.+)\]:([^:]+)$/))) {
242 return (cbi_validators
.ip6addr
.apply(addr
) &&
243 cbi_validators
.port
.apply(port
));
244 } else if ((bracket
!= 1) && (this.match(/^([^\[\]]+):([^:]+)$/))) {
247 return (cbi_validators
.ip6addr
.apply(addr
) &&
248 cbi_validators
.port
.apply(port
));
259 return (v
.match(/^[a-fA-F0-9]{64}$/) != null);
261 return (v
.length
>= 8) && (v
.length
<= 63);
268 if ( v
.substr(0,2) == 's:' )
271 if( (v
.length
== 10) || (v
.length
== 26) )
272 return (v
.match(/^[a-fA-F0-9]{10,26}$/) != null);
274 return (v
.length
== 5) || (v
.length
== 13);
277 'uciname': function()
279 return (this.match(/^[a-zA-Z0-9_]+$/) != null);
282 'range': function(min
, max
)
285 return (val
>= +min
&& val
<= +max
);
290 return (Dec(this) >= +min
);
295 return (Dec(this) <= +max
);
298 'rangelength': function(min
, max
)
301 return ((val
.length
>= +min
) && (val
.length
<= +max
));
304 'minlength': function(min
)
306 return ((''+this).length
>= +min
);
309 'maxlength': function(max
)
311 return ((''+this).length
<= +max
);
316 for (var i
= 0; i
< arguments
.length
; i
+= 2)
318 if (typeof arguments
[i
] != 'function')
320 if (arguments
[i
] == this)
324 else if (arguments
[i
].apply(this, arguments
[i
+1]))
334 for (var i
= 0; i
< arguments
.length
; i
+= 2)
336 if (typeof arguments
[i
] != 'function')
338 if (arguments
[i
] != this)
342 else if (!arguments
[i
].apply(this, arguments
[i
+1]))
352 return cbi_validators
.or
.apply(
353 this.replace(/^[ \t]*![ \t]*/, ''), arguments
);
356 'list': function(subvalidator
, subargs
)
358 if (typeof subvalidator
!= 'function')
361 var tokens
= this.match(/[^ \t]+/g);
362 for (var i
= 0; i
< tokens
.length
; i
++)
363 if (!subvalidator
.apply(tokens
[i
], subargs
))
368 'phonedigit': function()
370 return (this.match(/^[0-9\*#!\.]+$/) != null);
372 'timehhmmss': function()
374 return (this.match(/^[0-6][0-9]:[0-6][0-9]:[0-6][0-9]$/) != null);
376 'dateyyyymmdd': function()
381 if (this.match(/^(\d\d\d\d)-(\d\d)-(\d\d)/)) {
382 var year
= RegExp
.$1;
383 var month
= RegExp
.$2;
386 var days_in_month
= [ 31, 28, 31, 30, 31, 30, 31, 31, 30 , 31, 30, 31 ];
387 function is_leap_year(year
) {
388 return ((year
% 4) == 0) && ((year
% 100) != 0) || ((year
% 400) == 0);
390 function get_days_in_month(month
, year
) {
391 if ((month
== 2) && is_leap_year(year
)) {
394 return days_in_month
[month
];
397 /* Firewall rules in the past don't make sense */
401 if ((month
<= 0) || (month
> 12)) {
404 if ((day
<= 0) || (day
> get_days_in_month(month
, year
))) {
416 function cbi_d_add(field
, dep
, index
) {
417 var obj
= (typeof(field
) === 'string') ? document
.getElementById(field
) : field
;
420 for (var i
=0; i
<cbi_d
.length
; i
++) {
421 if (cbi_d
[i
].id
== obj
.id
) {
430 "parent": obj
.parentNode
.id
,
434 cbi_d
.unshift(entry
);
440 function cbi_d_checkvalue(target
, ref
) {
441 var t
= document
.getElementById(target
);
445 var tl
= document
.getElementsByName(target
);
447 if( tl
.length
> 0 && (tl
[0].type
== 'radio' || tl
[0].type
== 'checkbox'))
448 for( var i
= 0; i
< tl
.length
; i
++ )
449 if( tl
[i
].checked
) {
454 value
= value
? value
: "";
455 } else if (!t
.value
) {
460 if (t
.type
== "checkbox") {
461 value
= t
.checked
? value
: "";
465 return (value
== ref
)
468 function cbi_d_check(deps
) {
471 for (var i
=0; i
<deps
.length
; i
++) {
474 for (var j
in deps
[i
]) {
475 if (j
== "!reverse") {
477 } else if (j
== "!default") {
481 istat
= (istat
&& cbi_d_checkvalue(j
, deps
[i
][j
]))
491 function cbi_d_update() {
493 for (var i
=0; i
<cbi_d
.length
; i
++) {
494 var entry
= cbi_d
[i
];
495 var node
= document
.getElementById(entry
.id
);
496 var parent
= document
.getElementById(entry
.parent
);
498 if (node
&& node
.parentNode
&& !cbi_d_check(entry
.deps
)) {
499 node
.parentNode
.removeChild(node
);
501 } else if (parent
&& (!node
|| !node
.parentNode
) && cbi_d_check(entry
.deps
)) {
502 var next
= undefined;
504 for (next
= parent
.firstChild
; next
; next
= next
.nextSibling
) {
505 if (next
.getAttribute
&& parseInt(next
.getAttribute('data-index'), 10) > entry
.index
) {
511 parent
.appendChild(entry
.node
);
513 parent
.insertBefore(entry
.node
, next
);
519 // hide optionals widget if no choices remaining
520 if (parent
&& parent
.parentNode
&& parent
.getAttribute('data-optionals'))
521 parent
.parentNode
.style
.display
= (parent
.options
.length
<= 1) ? 'none' : '';
524 if (entry
&& entry
.parent
) {
526 cbi_tag_last(parent
);
534 function cbi_init() {
537 nodes
= document
.querySelectorAll('[data-strings]');
539 for (var i
= 0, node
; (node
= nodes
[i
]) !== undefined; i
++) {
540 var str
= JSON
.parse(node
.getAttribute('data-strings'));
541 for (var key
in str
) {
542 for (var key2
in str
[key
]) {
543 var dst
= cbi_strings
[key
] || (cbi_strings
[key
] = { });
544 dst
[key2
] = str
[key
][key2
];
549 nodes
= document
.querySelectorAll('[data-depends]');
551 for (var i
= 0, node
; (node
= nodes
[i
]) !== undefined; i
++) {
552 var index
= parseInt(node
.getAttribute('data-index'), 10);
553 var depends
= JSON
.parse(node
.getAttribute('data-depends'));
554 if (!isNaN(index
) && depends
.length
> 0) {
555 for (var alt
= 0; alt
< depends
.length
; alt
++) {
556 cbi_d_add(node
, depends
[alt
], index
);
561 nodes
= document
.querySelectorAll('[data-update]');
563 for (var i
= 0, node
; (node
= nodes
[i
]) !== undefined; i
++) {
564 var events
= node
.getAttribute('data-update').split(' ');
565 for (var j
= 0, event
; (event
= events
[j
]) !== undefined; j
++) {
566 cbi_bind(node
, event
, cbi_d_update
);
570 nodes
= document
.querySelectorAll('[data-choices]');
572 for (var i
= 0, node
; (node
= nodes
[i
]) !== undefined; i
++) {
573 var choices
= JSON
.parse(node
.getAttribute('data-choices'));
576 for (var j
= 0; j
< choices
[0].length
; j
++)
577 options
[choices
[0][j
]] = choices
[1][j
];
579 var def
= (node
.getAttribute('data-optional') === 'true')
580 ? node
.placeholder
|| '' : null;
582 cbi_combobox_init(node
, options
, def
,
583 node
.getAttribute('data-manual'));
586 nodes
= document
.querySelectorAll('[data-dynlist]');
588 for (var i
= 0, node
; (node
= nodes
[i
]) !== undefined; i
++) {
589 var choices
= JSON
.parse(node
.getAttribute('data-dynlist'));
592 if (choices
[0] && choices
[0].length
) {
595 for (var j
= 0; j
< choices
[0].length
; j
++)
596 options
[choices
[0][j
]] = choices
[1][j
];
599 cbi_dynlist_init(node
, choices
[2], choices
[3], options
);
602 nodes
= document
.querySelectorAll('[data-type]');
604 for (var i
= 0, node
; (node
= nodes
[i
]) !== undefined; i
++) {
605 cbi_validate_field(node
, node
.getAttribute('data-optional') === 'true',
606 node
.getAttribute('data-type'));
612 function cbi_bind(obj
, type
, callback
, mode
) {
613 if (!obj
.addEventListener
) {
614 obj
.attachEvent('on' + type
,
616 var e
= window
.event
;
618 if (!e
.target
&& e
.srcElement
)
619 e
.target
= e
.srcElement
;
621 return !!callback(e
);
625 obj
.addEventListener(type
, callback
, !!mode
);
630 function cbi_combobox(id
, values
, def
, man
, focus
) {
631 var selid
= "cbi.combobox." + id
;
632 if (document
.getElementById(selid
)) {
636 var obj
= document
.getElementById(id
)
637 var sel
= document
.createElement("select");
639 sel
.index
= obj
.index
;
640 sel
.className
= obj
.className
.replace(/cbi-input-text/, 'cbi-input-select');
642 if (obj
.nextSibling
) {
643 obj
.parentNode
.insertBefore(sel
, obj
.nextSibling
);
645 obj
.parentNode
.appendChild(sel
);
648 var dt
= obj
.getAttribute('cbi_datatype');
649 var op
= obj
.getAttribute('cbi_optional');
652 cbi_validate_field(sel
, op
== 'true', dt
);
654 if (!values
[obj
.value
]) {
655 if (obj
.value
== "") {
656 var optdef
= document
.createElement("option");
658 optdef
.appendChild(document
.createTextNode(typeof(def
) === 'string' ? def
: cbi_strings
.label
.choose
));
659 sel
.appendChild(optdef
);
661 var opt
= document
.createElement("option");
662 opt
.value
= obj
.value
;
663 opt
.selected
= "selected";
664 opt
.appendChild(document
.createTextNode(obj
.value
));
665 sel
.appendChild(opt
);
669 for (var i
in values
) {
670 var opt
= document
.createElement("option");
673 if (obj
.value
== i
) {
674 opt
.selected
= "selected";
677 opt
.appendChild(document
.createTextNode(values
[i
]));
678 sel
.appendChild(opt
);
681 var optman
= document
.createElement("option");
683 optman
.appendChild(document
.createTextNode(typeof(man
) === 'string' ? man
: cbi_strings
.label
.custom
));
684 sel
.appendChild(optman
);
686 obj
.style
.display
= "none";
688 cbi_bind(sel
, "change", function() {
689 if (sel
.selectedIndex
== sel
.options
.length
- 1) {
690 obj
.style
.display
= "inline";
692 sel
.parentNode
.removeChild(sel
);
695 obj
.value
= sel
.options
[sel
.selectedIndex
].value
;
705 // Retrigger validation in select
712 function cbi_combobox_init(id
, values
, def
, man
) {
713 var obj
= (typeof(id
) === 'string') ? document
.getElementById(id
) : id
;
714 cbi_bind(obj
, "blur", function() {
715 cbi_combobox(obj
.id
, values
, def
, man
, true);
717 cbi_combobox(obj
.id
, values
, def
, man
, false);
720 function cbi_filebrowser(id
, defpath
) {
721 var field
= document
.getElementById(id
);
722 var browser
= window
.open(
723 cbi_strings
.path
.browser
+ ( field
.value
|| defpath
|| '' ) + '?field=' + id
,
724 "luci_filebrowser", "width=300,height=400,left=100,top=200,scrollbars=yes"
730 function cbi_browser_init(id
, resource
, defpath
)
732 function cbi_browser_btnclick(e
) {
733 cbi_filebrowser(id
, defpath
);
737 var field
= document
.getElementById(id
);
739 var btn
= document
.createElement('img');
740 btn
.className
= 'cbi-image-button';
741 btn
.src
= (resource
|| cbi_strings
.path
.resource
) + '/cbi/folder.gif';
742 field
.parentNode
.insertBefore(btn
, field
.nextSibling
);
744 cbi_bind(btn
, 'click', cbi_browser_btnclick
);
747 function cbi_dynlist_init(parent
, datatype
, optional
, choices
)
749 var prefix
= parent
.getAttribute('data-prefix');
750 var holder
= parent
.getAttribute('data-placeholder');
754 function cbi_dynlist_redraw(focus
, add
, del
)
758 while (parent
.firstChild
)
760 var n
= parent
.firstChild
;
765 if (n
.nodeName
.toLowerCase() == 'input')
766 values
.push(n
.value
|| '');
767 else if (n
.nodeName
.toLowerCase() == 'select')
768 values
[values
.length
-1] = n
.options
[n
.selectedIndex
].value
;
771 parent
.removeChild(n
);
777 values
.splice(focus
, 0, '');
779 else if (values
.length
== 0)
785 for (var i
= 0; i
< values
.length
; i
++)
787 var t
= document
.createElement('input');
788 t
.id
= prefix
+ '.' + (i
+1);
793 t
.className
= 'cbi-input-text';
795 if (i
== 0 && holder
)
797 t
.placeholder
= holder
;
800 var b
= document
.createElement('img');
801 b
.src
= cbi_strings
.path
.resource
+ ((i
+1) < values
.length
? '/cbi/remove.gif' : '/cbi/add.gif');
802 b
.className
= 'cbi-image-button';
804 parent
.appendChild(t
);
805 parent
.appendChild(b
);
806 if (datatype
== 'file')
808 cbi_browser_init(t
.id
, null, parent
.getAttribute('data-browser-path'));
811 parent
.appendChild(document
.createElement('br'));
815 cbi_validate_field(t
.id
, ((i
+1) == values
.length
) || optional
, datatype
);
820 cbi_combobox_init(t
.id
, choices
, '', cbi_strings
.label
.custom
);
823 cbi_bind(b
, 'keydown', cbi_dynlist_keydown
);
824 cbi_bind(b
, 'keypress', cbi_dynlist_keypress
);
826 if (i
== focus
|| -i
== focus
)
831 cbi_bind(t
, 'keydown', cbi_dynlist_keydown
);
832 cbi_bind(t
, 'keypress', cbi_dynlist_keypress
);
838 else if (-i
== focus
)
842 /* force cursor to end */
849 cbi_bind(b
, 'click', cbi_dynlist_btnclick
);
853 function cbi_dynlist_keypress(ev
)
855 ev
= ev
? ev
: window
.event
;
857 var se
= ev
.target
? ev
.target
: ev
.srcElement
;
859 if (se
.nodeType
== 3)
864 /* backspace, delete */
867 if (se
.value
.length
== 0)
869 if (ev
.preventDefault
)
877 /* enter, arrow up, arrow down */
881 if (ev
.preventDefault
)
890 function cbi_dynlist_keydown(ev
)
892 ev
= ev
? ev
: window
.event
;
894 var se
= ev
.target
? ev
.target
: ev
.srcElement
;
896 if (se
.nodeType
== 3)
899 var prev
= se
.previousSibling
;
900 while (prev
&& prev
.name
!= prefix
)
901 prev
= prev
.previousSibling
;
903 var next
= se
.nextSibling
;
904 while (next
&& next
.name
!= prefix
)
905 next
= next
.nextSibling
;
907 /* advance one further in combobox case */
908 if (next
&& next
.nextSibling
.name
== prefix
)
909 next
= next
.nextSibling
;
913 /* backspace, delete */
916 var del
= (se
.nodeName
.toLowerCase() == 'select')
917 ? true : (se
.value
.length
== 0);
921 if (ev
.preventDefault
)
924 var focus
= se
.index
;
928 cbi_dynlist_redraw(focus
, -1, se
.index
);
937 cbi_dynlist_redraw(-1, se
.index
, -1);
958 function cbi_dynlist_btnclick(ev
)
960 ev
= ev
? ev
: window
.event
;
962 var se
= ev
.target
? ev
.target
: ev
.srcElement
;
963 var input
= se
.previousSibling
;
964 while (input
&& input
.name
!= prefix
) {
965 input
= input
.previousSibling
;
968 if (se
.src
.indexOf('remove') > -1)
972 cbi_dynlist_keydown({
979 cbi_dynlist_keydown({
988 cbi_dynlist_redraw(NaN
, -1, -1);
992 function cbi_t_add(section
, tab
) {
993 var t
= document
.getElementById('tab.' + section
+ '.' + tab
);
994 var c
= document
.getElementById('container.' + section
+ '.' + tab
);
997 cbi_t
[section
] = (cbi_t
[section
] || [ ]);
998 cbi_t
[section
][tab
] = { 'tab': t
, 'container': c
, 'cid': c
.id
};
1002 function cbi_t_switch(section
, tab
) {
1003 if( cbi_t
[section
] && cbi_t
[section
][tab
] ) {
1004 var o
= cbi_t
[section
][tab
];
1005 var h
= document
.getElementById('tab.' + section
);
1006 for( var tid
in cbi_t
[section
] ) {
1007 var o2
= cbi_t
[section
][tid
];
1008 if( o
.tab
.id
!= o2
.tab
.id
) {
1009 o2
.tab
.className
= o2
.tab
.className
.replace(/(^| )cbi-tab( |$)/, " cbi-tab-disabled ");
1010 o2
.container
.style
.display
= 'none';
1013 if(h
) h
.value
= tab
;
1014 o2
.tab
.className
= o2
.tab
.className
.replace(/(^| )cbi-tab-disabled( |$)/, " cbi-tab ");
1015 o2
.container
.style
.display
= 'block';
1022 function cbi_t_update() {
1024 var updated
= false;
1026 for( var sid
in cbi_t
)
1027 for( var tid
in cbi_t
[sid
] )
1029 var t
= cbi_t
[sid
][tid
].tab
;
1030 var c
= cbi_t
[sid
][tid
].container
;
1032 if (!c
.firstElementChild
) {
1033 t
.style
.display
= 'none';
1035 else if (t
.style
.display
== 'none') {
1036 t
.style
.display
= '';
1037 t
.className
+= ' cbi-tab-highlighted';
1045 if (hl_tabs
.length
> 0)
1046 window
.setTimeout(function() {
1047 for( var i
= 0; i
< hl_tabs
.length
; i
++ )
1048 hl_tabs
[i
].className
= hl_tabs
[i
].className
.replace(/ cbi
-tab
-highlighted
/g
, '');
1055 function cbi_validate_form(form
, errmsg
)
1057 /* if triggered by a section removal or addition, don't validate */
1058 if( form
.cbi_state
== 'add-section' || form
.cbi_state
== 'del-section' )
1061 if( form
.cbi_validators
)
1063 for( var i
= 0; i
< form
.cbi_validators
.length
; i
++ )
1065 var validator
= form
.cbi_validators
[i
];
1066 if( !validator() && errmsg
)
1077 function cbi_validate_reset(form
)
1080 function() { cbi_validate_form(form
, null) }, 100
1086 function cbi_validate_compile(code
)
1095 for (var i
= 0; i
< code
.length
; i
++)
1103 switch (code
.charCodeAt(i
))
1115 var label
= code
.substring(pos
, i
);
1116 label
= label
.replace(/\\(.)/g, '$1');
1117 label
= label
.replace(/^[ \t]+/g, '');
1118 label
= label
.replace(/[ \t]+$/g, '');
1120 if (label
&& !isNaN(label
))
1122 stack
.push(parseFloat(label
));
1124 else if (label
.match(/^(['"]).*\1$/))
1126 stack
.push(label
.replace(/^(['"])(.*)\1$/, '$2'));
1128 else if (typeof cbi_validators
[label
] == 'function')
1130 stack
.push(cbi_validators
[label
]);
1135 throw "Syntax error, unhandled token '"+label
+"'";
1140 depth
+= (code
.charCodeAt(i
) == 40);
1146 if (typeof stack
[stack
.length
-2] != 'function')
1147 throw "Syntax error, argument list follows non-function";
1149 stack
[stack
.length
-1] =
1150 arguments
.callee(code
.substring(pos
, i
));
1161 function cbi_validate_field(cbid
, optional
, type
)
1163 var field
= (typeof cbid
== "string") ? document
.getElementById(cbid
) : cbid
;
1164 var vstack
; try { vstack
= cbi_validate_compile(type
); } catch(e
) { };
1166 if (field
&& vstack
&& typeof vstack
[0] == "function")
1168 var validator = function()
1173 field
.className
= field
.className
.replace(/ cbi
-input
-invalid
/g
, '');
1176 var value
= (field
.options
&& field
.options
.selectedIndex
> -1)
1177 ? field
.options
[field
.options
.selectedIndex
].value
: field
.value
;
1179 if (!(((value
.length
== 0) && optional
) || vstack
[0].apply(value
, vstack
[1])))
1182 field
.className
+= ' cbi-input-invalid';
1190 if( ! field
.form
.cbi_validators
)
1191 field
.form
.cbi_validators
= [ ];
1193 field
.form
.cbi_validators
.push(validator
);
1195 cbi_bind(field
, "blur", validator
);
1196 cbi_bind(field
, "keyup", validator
);
1198 if (field
.nodeName
== 'SELECT')
1200 cbi_bind(field
, "change", validator
);
1201 cbi_bind(field
, "click", validator
);
1204 field
.setAttribute("cbi_validate", validator
);
1205 field
.setAttribute("cbi_datatype", type
);
1206 field
.setAttribute("cbi_optional", (!!optional
).toString());
1210 var fcbox
= document
.getElementById('cbi.combobox.' + field
.id
);
1212 cbi_validate_field(fcbox
, optional
, type
);
1216 function cbi_row_swap(elem
, up
, store
)
1218 var tr
= elem
.parentNode
;
1219 while (tr
&& tr
.nodeName
.toLowerCase() != 'tr')
1225 var table
= tr
.parentNode
;
1226 while (table
&& table
.nodeName
.toLowerCase() != 'table')
1227 table
= table
.parentNode
;
1233 var e
= up
? table
.rows
.length
: table
.rows
.length
- 1;
1235 for (var idx
= s
; idx
< e
; idx
++)
1237 if (table
.rows
[idx
] == tr
)
1240 tr
.parentNode
.insertBefore(table
.rows
[idx
], table
.rows
[idx
-1]);
1242 tr
.parentNode
.insertBefore(table
.rows
[idx
+1], table
.rows
[idx
]);
1249 for (idx
= 2; idx
< table
.rows
.length
; idx
++)
1251 table
.rows
[idx
].className
= table
.rows
[idx
].className
.replace(
1252 /cbi-rowstyle-[12]/, 'cbi-rowstyle-' + (1 + (idx
% 2))
1255 if (table
.rows
[idx
].id
&& table
.rows
[idx
].id
.match(/-([^\-]+)$/) )
1256 ids
.push(RegExp
.$1);
1259 var input
= document
.getElementById(store
);
1261 input
.value
= ids
.join(' ');
1266 function cbi_tag_last(container
)
1270 for (var i
= 0; i
< container
.childNodes
.length
; i
++)
1272 var c
= container
.childNodes
[i
];
1273 if (c
.nodeType
== 1 && c
.nodeName
.toLowerCase() == 'div')
1275 c
.className
= c
.className
.replace(/ cbi
-value
-last
$/, '');
1282 last
.className
+= ' cbi-value-last';
1286 String
.prototype.serialize = function()
1303 for( var i
= 0; i
< o
.length
; i
++ )
1304 s
+= (s
? ', ' : '') + String
.serialize(o
[i
]);
1306 return '[ ' + s
+ ' ]';
1315 s
+= (s
? ', ' : '') + k
+ ': ' + String
.serialize(o
[k
]);
1317 return '{ ' + s
+ ' }';
1324 if( o
.match(/[^a-zA-Z0-9_,.: -]/) )
1325 return 'decodeURIComponent("' + encodeURIComponent(o
) + '")';
1329 return '"' + o
+ '"';
1334 return o
.toString();
1338 String
.prototype.format = function()
1343 var html_esc
= [/&/g, '&', /"/g, '"', /'/g, ''', /</g, '<', />/g, '>'];
1344 var quot_esc = [/"/g
, '"', /'/g, '''];
1346 function esc(s, r) {
1347 if (typeof(s) !== 'string
' && !(s instanceof String))
1350 for( var i = 0; i < r.length; i += 2 )
1351 s = s.replace(r[i], r[i+1]);
1357 var re = /^(([^%]*)%('.|0|\x20)?(-)?(\d
+)?(\.\d
+)?(%|b
|c
|d
|u
|f
|o
|s
|x
|X
|q
|h
|j
|t
|m
))/;
1358 var a
= b
= [], numSubstitutions
= 0, numMatches
= 0;
1360 while (a
= re
.exec(str
))
1363 var leftpart
= a
[2], pPad
= a
[3], pJustify
= a
[4], pMinLength
= a
[5];
1364 var pPrecision
= a
[6], pType
= a
[7];
1374 if (numSubstitutions
< arguments
.length
)
1376 var param
= arguments
[numSubstitutions
++];
1379 if (pPad
&& pPad
.substr(0,1) == "'")
1380 pad
= leftpart
.substr(1,1);
1386 var justifyRight
= true;
1387 if (pJustify
&& pJustify
=== "-")
1388 justifyRight
= false;
1392 minLength
= +pMinLength
;
1395 if (pPrecision
&& pType
== 'f')
1396 precision
= +pPrecision
.substring(1);
1403 subst
= (+param
|| 0).toString(2);
1407 subst
= String
.fromCharCode(+param
|| 0);
1411 subst
= ~~(+param
|| 0);
1415 subst
= ~~Math
.abs(+param
|| 0);
1419 subst
= (precision
> -1)
1420 ? ((+param
|| 0.0)).toFixed(precision
)
1425 subst
= (+param
|| 0).toString(8);
1433 subst
= ('' + (+param
|| 0).toString(16)).toLowerCase();
1437 subst
= ('' + (+param
|| 0).toString(16)).toUpperCase();
1441 subst
= esc(param
, html_esc
);
1445 subst
= esc(param
, quot_esc
);
1449 subst
= String
.serialize(param
);
1456 var ts
= (param
|| 0);
1459 tm
= Math
.floor(ts
/ 60);
1464 th
= Math
.floor(tm
/ 60);
1469 td
= Math
.floor(th
/ 24);
1474 ? String
.format('%dd %dh %dm %ds', td
, th
, tm
, ts
)
1475 : String
.format('%dh %dm %ds', th
, tm
, ts
);
1480 var mf
= pMinLength
? +pMinLength
: 1000;
1481 var pr
= pPrecision
? ~~(10 * +('0' + pPrecision
)) : 2;
1484 var val
= (+param
|| 0);
1485 var units
= [ ' ', ' K', ' M', ' G', ' T', ' P', ' E' ];
1487 for (i
= 0; (i
< units
.length
) && (val
> mf
); i
++)
1490 subst
= (i
? val
.toFixed(pr
) : val
) + units
[i
];
1498 subst
= subst
.toString();
1499 for (var i
= subst
.length
; i
< pMinLength
; i
++)
1500 if (pJustify
== '-')
1501 subst
= subst
+ ' ';
1503 subst
= pad
+ subst
;
1506 out
+= leftpart
+ subst
;
1507 str
= str
.substr(m
.length
);
1513 String
.prototype.nobr = function()
1515 return this.replace(/[\s\n]+/g, ' ');
1518 String
.serialize = function()
1521 for (var i
= 1; i
< arguments
.length
; i
++)
1522 a
.push(arguments
[i
]);
1523 return ''.serialize
.apply(arguments
[0], a
);
1526 String
.format = function()
1529 for (var i
= 1; i
< arguments
.length
; i
++)
1530 a
.push(arguments
[i
]);
1531 return ''.format
.apply(arguments
[0], a
);
1534 String
.nobr = function()
1537 for (var i
= 1; i
< arguments
.length
; i
++)
1538 a
.push(arguments
[i
]);
1539 return ''.nobr
.apply(arguments
[0], a
);