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);
121 'ip4prefix': function()
123 return !isNaN(this) && this >= 0 && this <= 32;
126 'ip6prefix': function()
128 return !isNaN(this) && this >= 0 && this <= 128;
133 return cbi_validators
.cidr4
.apply(this) ||
134 cbi_validators
.cidr6
.apply(this);
139 if (this.match(/^(\S+)\/(\S+)$/))
143 return cbi_validators
.ip4addr
.apply(ip
) &&
144 cbi_validators
.ip4prefix
.apply(mask
);
151 if (this.match(/^(\S+)\/(\S+)$/))
155 return cbi_validators
.ip6addr
.apply(ip
) &&
156 cbi_validators
.ip6prefix
.apply(mask
);
163 if (this.match(/^(\S+)\/(\S+)$/))
167 return cbi_validators
.ip4addr
.apply(ip
) &&
168 cbi_validators
.ip4addr
.apply(net
);
175 if (this.match(/^(\S+)\/(\S+)$/))
179 return cbi_validators
.ip6addr
.apply(ip
) &&
180 cbi_validators
.ip6addr
.apply(net
);
187 return cbi_validators
.ipmask4
.apply(this) ||
188 cbi_validators
.ipmask6
.apply(this);
191 'ipmask4': function()
193 return cbi_validators
.cidr4
.apply(this) ||
194 cbi_validators
.ipnet4
.apply(this) ||
195 cbi_validators
.ip4addr
.apply(this);
198 'ipmask6': function()
200 return cbi_validators
.cidr6
.apply(this) ||
201 cbi_validators
.ipnet6
.apply(this) ||
202 cbi_validators
.ip6addr
.apply(this);
208 return (p
>= 0 && p
<= 65535);
211 'portrange': function()
213 if (this.match(/^(\d+)-(\d+)$/))
217 return (p1
<= p2
&& p2
<= 65535);
220 return cbi_validators
.port
.apply(this);
223 'macaddr': function()
225 return (this.match(/^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$/) != null);
228 'host': function(ipv4only
)
230 return cbi_validators
.hostname
.apply(this) ||
231 ((ipv4only
!= 1) && cbi_validators
.ipaddr
.apply(this)) ||
232 ((ipv4only
== 1) && cb_validators
.ip4addr
.apply(this));
235 'hostname': function()
237 if (this.length
<= 253)
238 return (this.match(/^[a-zA-Z0-9]+$/) != null ||
239 (this.match(/^[a-zA-Z0-9_][a-zA-Z0-9_\-.]*[a-zA-Z0-9]$/) &&
240 this.match(/[^0-9.]/)));
245 'network': function()
247 return cbi_validators
.uciname
.apply(this) ||
248 cbi_validators
.host
.apply(this);
251 'hostport': function(ipv4only
)
253 var hp
= this.split(/:/);
256 return (cbi_validators
.host
.apply(hp
[0], ipv4only
) &&
257 cbi_validators
.port
.apply(hp
[1]));
262 'ip4addrport': function()
264 var hp
= this.split(/:/);
267 return (cbi_validators
.ipaddr
.apply(hp
[0]) &&
268 cbi_validators
.port
.apply(hp
[1]));
272 'ipaddrport': function(bracket
)
274 if (this.match(/^([^\[\]:]+):([^:]+)$/)) {
277 return (cbi_validators
.ip4addr
.apply(addr
) &&
278 cbi_validators
.port
.apply(port
));
279 } else if ((bracket
== 1) && (this.match(/^\[(.+)\]:([^:]+)$/))) {
282 return (cbi_validators
.ip6addr
.apply(addr
) &&
283 cbi_validators
.port
.apply(port
));
284 } else if ((bracket
!= 1) && (this.match(/^([^\[\]]+):([^:]+)$/))) {
287 return (cbi_validators
.ip6addr
.apply(addr
) &&
288 cbi_validators
.port
.apply(port
));
299 return (v
.match(/^[a-fA-F0-9]{64}$/) != null);
301 return (v
.length
>= 8) && (v
.length
<= 63);
308 if ( v
.substr(0,2) == 's:' )
311 if( (v
.length
== 10) || (v
.length
== 26) )
312 return (v
.match(/^[a-fA-F0-9]{10,26}$/) != null);
314 return (v
.length
== 5) || (v
.length
== 13);
317 'uciname': function()
319 return (this.match(/^[a-zA-Z0-9_]+$/) != null);
322 'range': function(min
, max
)
325 return (val
>= +min
&& val
<= +max
);
330 return (Dec(this) >= +min
);
335 return (Dec(this) <= +max
);
338 'rangelength': function(min
, max
)
341 return ((val
.length
>= +min
) && (val
.length
<= +max
));
344 'minlength': function(min
)
346 return ((''+this).length
>= +min
);
349 'maxlength': function(max
)
351 return ((''+this).length
<= +max
);
356 for (var i
= 0; i
< arguments
.length
; i
+= 2)
358 if (typeof arguments
[i
] != 'function')
360 if (arguments
[i
] == this)
364 else if (arguments
[i
].apply(this, arguments
[i
+1]))
374 for (var i
= 0; i
< arguments
.length
; i
+= 2)
376 if (typeof arguments
[i
] != 'function')
378 if (arguments
[i
] != this)
382 else if (!arguments
[i
].apply(this, arguments
[i
+1]))
392 return cbi_validators
.or
.apply(
393 this.replace(/^[ \t]*![ \t]*/, ''), arguments
);
396 'list': function(subvalidator
, subargs
)
398 if (typeof subvalidator
!= 'function')
401 var tokens
= this.match(/[^ \t]+/g);
402 for (var i
= 0; i
< tokens
.length
; i
++)
403 if (!subvalidator
.apply(tokens
[i
], subargs
))
408 'phonedigit': function()
410 return (this.match(/^[0-9\*#!\.]+$/) != null);
412 'timehhmmss': function()
414 return (this.match(/^[0-6][0-9]:[0-6][0-9]:[0-6][0-9]$/) != null);
416 'dateyyyymmdd': function()
421 if (this.match(/^(\d\d\d\d)-(\d\d)-(\d\d)/)) {
422 var year
= RegExp
.$1;
423 var month
= RegExp
.$2;
426 var days_in_month
= [ 31, 28, 31, 30, 31, 30, 31, 31, 30 , 31, 30, 31 ];
427 function is_leap_year(year
) {
428 return ((year
% 4) == 0) && ((year
% 100) != 0) || ((year
% 400) == 0);
430 function get_days_in_month(month
, year
) {
431 if ((month
== 2) && is_leap_year(year
)) {
434 return days_in_month
[month
];
437 /* Firewall rules in the past don't make sense */
441 if ((month
<= 0) || (month
> 12)) {
444 if ((day
<= 0) || (day
> get_days_in_month(month
, year
))) {
456 function cbi_d_add(field
, dep
, index
) {
457 var obj
= (typeof(field
) === 'string') ? document
.getElementById(field
) : field
;
460 for (var i
=0; i
<cbi_d
.length
; i
++) {
461 if (cbi_d
[i
].id
== obj
.id
) {
470 "parent": obj
.parentNode
.id
,
474 cbi_d
.unshift(entry
);
480 function cbi_d_checkvalue(target
, ref
) {
481 var t
= document
.getElementById(target
);
485 var tl
= document
.getElementsByName(target
);
487 if( tl
.length
> 0 && (tl
[0].type
== 'radio' || tl
[0].type
== 'checkbox'))
488 for( var i
= 0; i
< tl
.length
; i
++ )
489 if( tl
[i
].checked
) {
494 value
= value
? value
: "";
495 } else if (!t
.value
) {
500 if (t
.type
== "checkbox") {
501 value
= t
.checked
? value
: "";
505 return (value
== ref
)
508 function cbi_d_check(deps
) {
511 for (var i
=0; i
<deps
.length
; i
++) {
514 for (var j
in deps
[i
]) {
515 if (j
== "!reverse") {
517 } else if (j
== "!default") {
521 istat
= (istat
&& cbi_d_checkvalue(j
, deps
[i
][j
]))
525 if (istat
^ reverse
) {
532 function cbi_d_update() {
534 for (var i
=0; i
<cbi_d
.length
; i
++) {
535 var entry
= cbi_d
[i
];
536 var node
= document
.getElementById(entry
.id
);
537 var parent
= document
.getElementById(entry
.parent
);
539 if (node
&& node
.parentNode
&& !cbi_d_check(entry
.deps
)) {
540 node
.parentNode
.removeChild(node
);
542 } else if (parent
&& (!node
|| !node
.parentNode
) && cbi_d_check(entry
.deps
)) {
543 var next
= undefined;
545 for (next
= parent
.firstChild
; next
; next
= next
.nextSibling
) {
546 if (next
.getAttribute
&& parseInt(next
.getAttribute('data-index'), 10) > entry
.index
) {
552 parent
.appendChild(entry
.node
);
554 parent
.insertBefore(entry
.node
, next
);
560 // hide optionals widget if no choices remaining
561 if (parent
&& parent
.parentNode
&& parent
.getAttribute('data-optionals'))
562 parent
.parentNode
.style
.display
= (parent
.options
.length
<= 1) ? 'none' : '';
565 if (entry
&& entry
.parent
) {
567 cbi_tag_last(parent
);
575 function cbi_init() {
578 nodes
= document
.querySelectorAll('[data-strings]');
580 for (var i
= 0, node
; (node
= nodes
[i
]) !== undefined; i
++) {
581 var str
= JSON
.parse(node
.getAttribute('data-strings'));
582 for (var key
in str
) {
583 for (var key2
in str
[key
]) {
584 var dst
= cbi_strings
[key
] || (cbi_strings
[key
] = { });
585 dst
[key2
] = str
[key
][key2
];
590 nodes
= document
.querySelectorAll('[data-depends]');
592 for (var i
= 0, node
; (node
= nodes
[i
]) !== undefined; i
++) {
593 var index
= parseInt(node
.getAttribute('data-index'), 10);
594 var depends
= JSON
.parse(node
.getAttribute('data-depends'));
595 if (!isNaN(index
) && depends
.length
> 0) {
596 for (var alt
= 0; alt
< depends
.length
; alt
++) {
597 cbi_d_add(node
, depends
[alt
], index
);
602 nodes
= document
.querySelectorAll('[data-update]');
604 for (var i
= 0, node
; (node
= nodes
[i
]) !== undefined; i
++) {
605 var events
= node
.getAttribute('data-update').split(' ');
606 for (var j
= 0, event
; (event
= events
[j
]) !== undefined; j
++) {
607 cbi_bind(node
, event
, cbi_d_update
);
611 nodes
= document
.querySelectorAll('[data-choices]');
613 for (var i
= 0, node
; (node
= nodes
[i
]) !== undefined; i
++) {
614 var choices
= JSON
.parse(node
.getAttribute('data-choices'));
617 for (var j
= 0; j
< choices
[0].length
; j
++)
618 options
[choices
[0][j
]] = choices
[1][j
];
620 var def
= (node
.getAttribute('data-optional') === 'true')
621 ? node
.placeholder
|| '' : null;
623 cbi_combobox_init(node
, options
, def
,
624 node
.getAttribute('data-manual'));
627 nodes
= document
.querySelectorAll('[data-dynlist]');
629 for (var i
= 0, node
; (node
= nodes
[i
]) !== undefined; i
++) {
630 var choices
= JSON
.parse(node
.getAttribute('data-dynlist'));
633 if (choices
[0] && choices
[0].length
) {
636 for (var j
= 0; j
< choices
[0].length
; j
++)
637 options
[choices
[0][j
]] = choices
[1][j
];
640 cbi_dynlist_init(node
, choices
[2], choices
[3], options
);
643 nodes
= document
.querySelectorAll('[data-type]');
645 for (var i
= 0, node
; (node
= nodes
[i
]) !== undefined; i
++) {
646 cbi_validate_field(node
, node
.getAttribute('data-optional') === 'true',
647 node
.getAttribute('data-type'));
653 function cbi_bind(obj
, type
, callback
, mode
) {
654 if (!obj
.addEventListener
) {
655 obj
.attachEvent('on' + type
,
657 var e
= window
.event
;
659 if (!e
.target
&& e
.srcElement
)
660 e
.target
= e
.srcElement
;
662 return !!callback(e
);
666 obj
.addEventListener(type
, callback
, !!mode
);
671 function cbi_combobox(id
, values
, def
, man
, focus
) {
672 var selid
= "cbi.combobox." + id
;
673 if (document
.getElementById(selid
)) {
677 var obj
= document
.getElementById(id
)
678 var sel
= document
.createElement("select");
680 sel
.index
= obj
.index
;
681 sel
.className
= obj
.className
.replace(/cbi-input-text/, 'cbi-input-select');
683 if (obj
.nextSibling
) {
684 obj
.parentNode
.insertBefore(sel
, obj
.nextSibling
);
686 obj
.parentNode
.appendChild(sel
);
689 var dt
= obj
.getAttribute('cbi_datatype');
690 var op
= obj
.getAttribute('cbi_optional');
692 if (!values
[obj
.value
]) {
693 if (obj
.value
== "") {
694 var optdef
= document
.createElement("option");
696 optdef
.appendChild(document
.createTextNode(typeof(def
) === 'string' ? def
: cbi_strings
.label
.choose
));
697 sel
.appendChild(optdef
);
699 var opt
= document
.createElement("option");
700 opt
.value
= obj
.value
;
701 opt
.selected
= "selected";
702 opt
.appendChild(document
.createTextNode(obj
.value
));
703 sel
.appendChild(opt
);
707 for (var i
in values
) {
708 var opt
= document
.createElement("option");
711 if (obj
.value
== i
) {
712 opt
.selected
= "selected";
715 opt
.appendChild(document
.createTextNode(values
[i
]));
716 sel
.appendChild(opt
);
719 var optman
= document
.createElement("option");
721 optman
.appendChild(document
.createTextNode(typeof(man
) === 'string' ? man
: cbi_strings
.label
.custom
));
722 sel
.appendChild(optman
);
724 obj
.style
.display
= "none";
727 cbi_validate_field(sel
, op
== 'true', dt
);
729 cbi_bind(sel
, "change", function() {
730 if (sel
.selectedIndex
== sel
.options
.length
- 1) {
731 obj
.style
.display
= "inline";
733 sel
.parentNode
.removeChild(sel
);
736 obj
.value
= sel
.options
[sel
.selectedIndex
].value
;
746 // Retrigger validation in select
753 function cbi_combobox_init(id
, values
, def
, man
) {
754 var obj
= (typeof(id
) === 'string') ? document
.getElementById(id
) : id
;
755 cbi_bind(obj
, "blur", function() {
756 cbi_combobox(obj
.id
, values
, def
, man
, true);
758 cbi_combobox(obj
.id
, values
, def
, man
, false);
761 function cbi_filebrowser(id
, defpath
) {
762 var field
= document
.getElementById(id
);
763 var browser
= window
.open(
764 cbi_strings
.path
.browser
+ ( field
.value
|| defpath
|| '' ) + '?field=' + id
,
765 "luci_filebrowser", "width=300,height=400,left=100,top=200,scrollbars=yes"
771 function cbi_browser_init(id
, resource
, defpath
)
773 function cbi_browser_btnclick(e
) {
774 cbi_filebrowser(id
, defpath
);
778 var field
= document
.getElementById(id
);
780 var btn
= document
.createElement('img');
781 btn
.className
= 'cbi-image-button';
782 btn
.src
= (resource
|| cbi_strings
.path
.resource
) + '/cbi/folder.gif';
783 field
.parentNode
.insertBefore(btn
, field
.nextSibling
);
785 cbi_bind(btn
, 'click', cbi_browser_btnclick
);
788 function cbi_dynlist_init(parent
, datatype
, optional
, choices
)
790 var prefix
= parent
.getAttribute('data-prefix');
791 var holder
= parent
.getAttribute('data-placeholder');
795 function cbi_dynlist_redraw(focus
, add
, del
)
799 while (parent
.firstChild
)
801 var n
= parent
.firstChild
;
806 if (n
.nodeName
.toLowerCase() == 'input')
807 values
.push(n
.value
|| '');
808 else if (n
.nodeName
.toLowerCase() == 'select')
809 values
[values
.length
-1] = n
.options
[n
.selectedIndex
].value
;
812 parent
.removeChild(n
);
818 values
.splice(focus
, 0, '');
820 else if (values
.length
== 0)
826 for (var i
= 0; i
< values
.length
; i
++)
828 var t
= document
.createElement('input');
829 t
.id
= prefix
+ '.' + (i
+1);
834 t
.className
= 'cbi-input-text';
836 if (i
== 0 && holder
)
838 t
.placeholder
= holder
;
841 var b
= document
.createElement('img');
842 b
.src
= cbi_strings
.path
.resource
+ ((i
+1) < values
.length
? '/cbi/remove.gif' : '/cbi/add.gif');
843 b
.className
= 'cbi-image-button';
845 parent
.appendChild(t
);
846 parent
.appendChild(b
);
847 if (datatype
== 'file')
849 cbi_browser_init(t
.id
, null, parent
.getAttribute('data-browser-path'));
852 parent
.appendChild(document
.createElement('br'));
856 cbi_validate_field(t
.id
, ((i
+1) == values
.length
) || optional
, datatype
);
861 cbi_combobox_init(t
.id
, choices
, '', cbi_strings
.label
.custom
);
864 cbi_bind(b
, 'keydown', cbi_dynlist_keydown
);
865 cbi_bind(b
, 'keypress', cbi_dynlist_keypress
);
867 if (i
== focus
|| -i
== focus
)
872 cbi_bind(t
, 'keydown', cbi_dynlist_keydown
);
873 cbi_bind(t
, 'keypress', cbi_dynlist_keypress
);
879 else if (-i
== focus
)
883 /* force cursor to end */
890 cbi_bind(b
, 'click', cbi_dynlist_btnclick
);
894 function cbi_dynlist_keypress(ev
)
896 ev
= ev
? ev
: window
.event
;
898 var se
= ev
.target
? ev
.target
: ev
.srcElement
;
900 if (se
.nodeType
== 3)
905 /* backspace, delete */
908 if (se
.value
.length
== 0)
910 if (ev
.preventDefault
)
918 /* enter, arrow up, arrow down */
922 if (ev
.preventDefault
)
931 function cbi_dynlist_keydown(ev
)
933 ev
= ev
? ev
: window
.event
;
935 var se
= ev
.target
? ev
.target
: ev
.srcElement
;
937 if (se
.nodeType
== 3)
940 var prev
= se
.previousSibling
;
941 while (prev
&& prev
.name
!= prefix
)
942 prev
= prev
.previousSibling
;
944 var next
= se
.nextSibling
;
945 while (next
&& next
.name
!= prefix
)
946 next
= next
.nextSibling
;
948 /* advance one further in combobox case */
949 if (next
&& next
.nextSibling
.name
== prefix
)
950 next
= next
.nextSibling
;
954 /* backspace, delete */
957 var del
= (se
.nodeName
.toLowerCase() == 'select')
958 ? true : (se
.value
.length
== 0);
962 if (ev
.preventDefault
)
965 var focus
= se
.index
;
969 cbi_dynlist_redraw(focus
, -1, se
.index
);
978 cbi_dynlist_redraw(-1, se
.index
, -1);
999 function cbi_dynlist_btnclick(ev
)
1001 ev
= ev
? ev
: window
.event
;
1003 var se
= ev
.target
? ev
.target
: ev
.srcElement
;
1004 var input
= se
.previousSibling
;
1005 while (input
&& input
.name
!= prefix
) {
1006 input
= input
.previousSibling
;
1009 if (se
.src
.indexOf('remove') > -1)
1013 cbi_dynlist_keydown({
1020 cbi_dynlist_keydown({
1029 cbi_dynlist_redraw(NaN
, -1, -1);
1033 function cbi_t_add(section
, tab
) {
1034 var t
= document
.getElementById('tab.' + section
+ '.' + tab
);
1035 var c
= document
.getElementById('container.' + section
+ '.' + tab
);
1038 cbi_t
[section
] = (cbi_t
[section
] || [ ]);
1039 cbi_t
[section
][tab
] = { 'tab': t
, 'container': c
, 'cid': c
.id
};
1043 function cbi_t_switch(section
, tab
) {
1044 if( cbi_t
[section
] && cbi_t
[section
][tab
] ) {
1045 var o
= cbi_t
[section
][tab
];
1046 var h
= document
.getElementById('tab.' + section
);
1047 for( var tid
in cbi_t
[section
] ) {
1048 var o2
= cbi_t
[section
][tid
];
1049 if( o
.tab
.id
!= o2
.tab
.id
) {
1050 o2
.tab
.className
= o2
.tab
.className
.replace(/(^| )cbi-tab( |$)/, " cbi-tab-disabled ");
1051 o2
.container
.style
.display
= 'none';
1054 if(h
) h
.value
= tab
;
1055 o2
.tab
.className
= o2
.tab
.className
.replace(/(^| )cbi-tab-disabled( |$)/, " cbi-tab ");
1056 o2
.container
.style
.display
= 'block';
1063 function cbi_t_update() {
1065 var updated
= false;
1067 for( var sid
in cbi_t
)
1068 for( var tid
in cbi_t
[sid
] )
1070 var t
= cbi_t
[sid
][tid
].tab
;
1071 var c
= cbi_t
[sid
][tid
].container
;
1073 if (!c
.firstElementChild
) {
1074 t
.style
.display
= 'none';
1076 else if (t
.style
.display
== 'none') {
1077 t
.style
.display
= '';
1078 t
.className
+= ' cbi-tab-highlighted';
1086 if (hl_tabs
.length
> 0)
1087 window
.setTimeout(function() {
1088 for( var i
= 0; i
< hl_tabs
.length
; i
++ )
1089 hl_tabs
[i
].className
= hl_tabs
[i
].className
.replace(/ cbi
-tab
-highlighted
/g
, '');
1096 function cbi_validate_form(form
, errmsg
)
1098 /* if triggered by a section removal or addition, don't validate */
1099 if( form
.cbi_state
== 'add-section' || form
.cbi_state
== 'del-section' )
1102 if( form
.cbi_validators
)
1104 for( var i
= 0; i
< form
.cbi_validators
.length
; i
++ )
1106 var validator
= form
.cbi_validators
[i
];
1107 if( !validator() && errmsg
)
1118 function cbi_validate_reset(form
)
1121 function() { cbi_validate_form(form
, null) }, 100
1127 function cbi_validate_compile(code
)
1136 for (var i
= 0; i
< code
.length
; i
++)
1144 switch (code
.charCodeAt(i
))
1156 var label
= code
.substring(pos
, i
);
1157 label
= label
.replace(/\\(.)/g, '$1');
1158 label
= label
.replace(/^[ \t]+/g, '');
1159 label
= label
.replace(/[ \t]+$/g, '');
1161 if (label
&& !isNaN(label
))
1163 stack
.push(parseFloat(label
));
1165 else if (label
.match(/^(['"]).*\1$/))
1167 stack
.push(label
.replace(/^(['"])(.*)\1$/, '$2'));
1169 else if (typeof cbi_validators
[label
] == 'function')
1171 stack
.push(cbi_validators
[label
]);
1176 throw "Syntax error, unhandled token '"+label
+"'";
1181 depth
+= (code
.charCodeAt(i
) == 40);
1187 if (typeof stack
[stack
.length
-2] != 'function')
1188 throw "Syntax error, argument list follows non-function";
1190 stack
[stack
.length
-1] =
1191 arguments
.callee(code
.substring(pos
, i
));
1202 function cbi_validate_field(cbid
, optional
, type
)
1204 var field
= (typeof cbid
== "string") ? document
.getElementById(cbid
) : cbid
;
1205 var vstack
; try { vstack
= cbi_validate_compile(type
); } catch(e
) { };
1207 if (field
&& vstack
&& typeof vstack
[0] == "function")
1209 var validator = function()
1214 field
.className
= field
.className
.replace(/ cbi
-input
-invalid
/g
, '');
1217 var value
= (field
.options
&& field
.options
.selectedIndex
> -1)
1218 ? field
.options
[field
.options
.selectedIndex
].value
: field
.value
;
1220 if (!(((value
.length
== 0) && optional
) || vstack
[0].apply(value
, vstack
[1])))
1223 field
.className
+= ' cbi-input-invalid';
1231 if( ! field
.form
.cbi_validators
)
1232 field
.form
.cbi_validators
= [ ];
1234 field
.form
.cbi_validators
.push(validator
);
1236 cbi_bind(field
, "blur", validator
);
1237 cbi_bind(field
, "keyup", validator
);
1239 if (field
.nodeName
== 'SELECT')
1241 cbi_bind(field
, "change", validator
);
1242 cbi_bind(field
, "click", validator
);
1245 field
.setAttribute("cbi_validate", validator
);
1246 field
.setAttribute("cbi_datatype", type
);
1247 field
.setAttribute("cbi_optional", (!!optional
).toString());
1251 var fcbox
= document
.getElementById('cbi.combobox.' + field
.id
);
1253 cbi_validate_field(fcbox
, optional
, type
);
1257 function cbi_row_swap(elem
, up
, store
)
1259 var tr
= elem
.parentNode
;
1260 while (tr
&& tr
.nodeName
.toLowerCase() != 'tr')
1266 var table
= tr
.parentNode
;
1267 while (table
&& table
.nodeName
.toLowerCase() != 'table')
1268 table
= table
.parentNode
;
1274 var e
= up
? table
.rows
.length
: table
.rows
.length
- 1;
1276 for (var idx
= s
; idx
< e
; idx
++)
1278 if (table
.rows
[idx
] == tr
)
1281 tr
.parentNode
.insertBefore(table
.rows
[idx
], table
.rows
[idx
-1]);
1283 tr
.parentNode
.insertBefore(table
.rows
[idx
+1], table
.rows
[idx
]);
1290 for (idx
= 2; idx
< table
.rows
.length
; idx
++)
1292 table
.rows
[idx
].className
= table
.rows
[idx
].className
.replace(
1293 /cbi-rowstyle-[12]/, 'cbi-rowstyle-' + (1 + (idx
% 2))
1296 if (table
.rows
[idx
].id
&& table
.rows
[idx
].id
.match(/-([^\-]+)$/) )
1297 ids
.push(RegExp
.$1);
1300 var input
= document
.getElementById(store
);
1302 input
.value
= ids
.join(' ');
1307 function cbi_tag_last(container
)
1311 for (var i
= 0; i
< container
.childNodes
.length
; i
++)
1313 var c
= container
.childNodes
[i
];
1314 if (c
.nodeType
== 1 && c
.nodeName
.toLowerCase() == 'div')
1316 c
.className
= c
.className
.replace(/ cbi
-value
-last
$/, '');
1323 last
.className
+= ' cbi-value-last';
1327 String
.prototype.serialize = function()
1344 for( var i
= 0; i
< o
.length
; i
++ )
1345 s
+= (s
? ', ' : '') + String
.serialize(o
[i
]);
1347 return '[ ' + s
+ ' ]';
1356 s
+= (s
? ', ' : '') + k
+ ': ' + String
.serialize(o
[k
]);
1358 return '{ ' + s
+ ' }';
1365 if( o
.match(/[^a-zA-Z0-9_,.: -]/) )
1366 return 'decodeURIComponent("' + encodeURIComponent(o
) + '")';
1370 return '"' + o
+ '"';
1375 return o
.toString();
1379 String
.prototype.format = function()
1384 var html_esc
= [/&/g, '&', /"/g, '"', /'/g, ''', /</g, '<', />/g, '>'];
1385 var quot_esc = [/"/g
, '"', /'/g, '''];
1387 function esc(s, r) {
1388 if (typeof(s) !== 'string
' && !(s instanceof String))
1391 for( var i = 0; i < r.length; i += 2 )
1392 s = s.replace(r[i], r[i+1]);
1398 var re = /^(([^%]*)%('.|0|\x20)?(-)?(\d
+)?(\.\d
+)?(%|b
|c
|d
|u
|f
|o
|s
|x
|X
|q
|h
|j
|t
|m
))/;
1399 var a
= b
= [], numSubstitutions
= 0, numMatches
= 0;
1401 while (a
= re
.exec(str
))
1404 var leftpart
= a
[2], pPad
= a
[3], pJustify
= a
[4], pMinLength
= a
[5];
1405 var pPrecision
= a
[6], pType
= a
[7];
1415 if (numSubstitutions
< arguments
.length
)
1417 var param
= arguments
[numSubstitutions
++];
1420 if (pPad
&& pPad
.substr(0,1) == "'")
1421 pad
= leftpart
.substr(1,1);
1427 var justifyRight
= true;
1428 if (pJustify
&& pJustify
=== "-")
1429 justifyRight
= false;
1433 minLength
= +pMinLength
;
1436 if (pPrecision
&& pType
== 'f')
1437 precision
= +pPrecision
.substring(1);
1444 subst
= (+param
|| 0).toString(2);
1448 subst
= String
.fromCharCode(+param
|| 0);
1452 subst
= ~~(+param
|| 0);
1456 subst
= ~~Math
.abs(+param
|| 0);
1460 subst
= (precision
> -1)
1461 ? ((+param
|| 0.0)).toFixed(precision
)
1466 subst
= (+param
|| 0).toString(8);
1474 subst
= ('' + (+param
|| 0).toString(16)).toLowerCase();
1478 subst
= ('' + (+param
|| 0).toString(16)).toUpperCase();
1482 subst
= esc(param
, html_esc
);
1486 subst
= esc(param
, quot_esc
);
1490 subst
= String
.serialize(param
);
1497 var ts
= (param
|| 0);
1500 tm
= Math
.floor(ts
/ 60);
1505 th
= Math
.floor(tm
/ 60);
1510 td
= Math
.floor(th
/ 24);
1515 ? String
.format('%dd %dh %dm %ds', td
, th
, tm
, ts
)
1516 : String
.format('%dh %dm %ds', th
, tm
, ts
);
1521 var mf
= pMinLength
? +pMinLength
: 1000;
1522 var pr
= pPrecision
? ~~(10 * +('0' + pPrecision
)) : 2;
1525 var val
= (+param
|| 0);
1526 var units
= [ ' ', ' K', ' M', ' G', ' T', ' P', ' E' ];
1528 for (i
= 0; (i
< units
.length
) && (val
> mf
); i
++)
1531 subst
= (i
? val
.toFixed(pr
) : val
) + units
[i
];
1539 subst
= subst
.toString();
1540 for (var i
= subst
.length
; i
< pMinLength
; i
++)
1541 if (pJustify
== '-')
1542 subst
= subst
+ ' ';
1544 subst
= pad
+ subst
;
1547 out
+= leftpart
+ subst
;
1548 str
= str
.substr(m
.length
);
1554 String
.prototype.nobr = function()
1556 return this.replace(/[\s\n]+/g, ' ');
1559 String
.serialize = function()
1562 for (var i
= 1; i
< arguments
.length
; i
++)
1563 a
.push(arguments
[i
]);
1564 return ''.serialize
.apply(arguments
[0], a
);
1567 String
.format = function()
1570 for (var i
= 1; i
< arguments
.length
; i
++)
1571 a
.push(arguments
[i
]);
1572 return ''.format
.apply(arguments
[0], a
);
1575 String
.nobr = function()
1578 for (var i
= 1; i
< arguments
.length
; i
++)
1579 a
.push(arguments
[i
]);
1580 return ''.nobr
.apply(arguments
[0], a
);