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
]))
485 if (istat
^ reverse
) {
492 function cbi_d_update() {
494 for (var i
=0; i
<cbi_d
.length
; i
++) {
495 var entry
= cbi_d
[i
];
496 var node
= document
.getElementById(entry
.id
);
497 var parent
= document
.getElementById(entry
.parent
);
499 if (node
&& node
.parentNode
&& !cbi_d_check(entry
.deps
)) {
500 node
.parentNode
.removeChild(node
);
502 } else if (parent
&& (!node
|| !node
.parentNode
) && cbi_d_check(entry
.deps
)) {
503 var next
= undefined;
505 for (next
= parent
.firstChild
; next
; next
= next
.nextSibling
) {
506 if (next
.getAttribute
&& parseInt(next
.getAttribute('data-index'), 10) > entry
.index
) {
512 parent
.appendChild(entry
.node
);
514 parent
.insertBefore(entry
.node
, next
);
520 // hide optionals widget if no choices remaining
521 if (parent
&& parent
.parentNode
&& parent
.getAttribute('data-optionals'))
522 parent
.parentNode
.style
.display
= (parent
.options
.length
<= 1) ? 'none' : '';
525 if (entry
&& entry
.parent
) {
527 cbi_tag_last(parent
);
535 function cbi_init() {
538 nodes
= document
.querySelectorAll('[data-strings]');
540 for (var i
= 0, node
; (node
= nodes
[i
]) !== undefined; i
++) {
541 var str
= JSON
.parse(node
.getAttribute('data-strings'));
542 for (var key
in str
) {
543 for (var key2
in str
[key
]) {
544 var dst
= cbi_strings
[key
] || (cbi_strings
[key
] = { });
545 dst
[key2
] = str
[key
][key2
];
550 nodes
= document
.querySelectorAll('[data-depends]');
552 for (var i
= 0, node
; (node
= nodes
[i
]) !== undefined; i
++) {
553 var index
= parseInt(node
.getAttribute('data-index'), 10);
554 var depends
= JSON
.parse(node
.getAttribute('data-depends'));
555 if (!isNaN(index
) && depends
.length
> 0) {
556 for (var alt
= 0; alt
< depends
.length
; alt
++) {
557 cbi_d_add(node
, depends
[alt
], index
);
562 nodes
= document
.querySelectorAll('[data-update]');
564 for (var i
= 0, node
; (node
= nodes
[i
]) !== undefined; i
++) {
565 var events
= node
.getAttribute('data-update').split(' ');
566 for (var j
= 0, event
; (event
= events
[j
]) !== undefined; j
++) {
567 cbi_bind(node
, event
, cbi_d_update
);
571 nodes
= document
.querySelectorAll('[data-choices]');
573 for (var i
= 0, node
; (node
= nodes
[i
]) !== undefined; i
++) {
574 var choices
= JSON
.parse(node
.getAttribute('data-choices'));
577 for (var j
= 0; j
< choices
[0].length
; j
++)
578 options
[choices
[0][j
]] = choices
[1][j
];
580 var def
= (node
.getAttribute('data-optional') === 'true')
581 ? node
.placeholder
|| '' : null;
583 cbi_combobox_init(node
, options
, def
,
584 node
.getAttribute('data-manual'));
587 nodes
= document
.querySelectorAll('[data-dynlist]');
589 for (var i
= 0, node
; (node
= nodes
[i
]) !== undefined; i
++) {
590 var choices
= JSON
.parse(node
.getAttribute('data-dynlist'));
593 if (choices
[0] && choices
[0].length
) {
596 for (var j
= 0; j
< choices
[0].length
; j
++)
597 options
[choices
[0][j
]] = choices
[1][j
];
600 cbi_dynlist_init(node
, choices
[2], choices
[3], options
);
603 nodes
= document
.querySelectorAll('[data-type]');
605 for (var i
= 0, node
; (node
= nodes
[i
]) !== undefined; i
++) {
606 cbi_validate_field(node
, node
.getAttribute('data-optional') === 'true',
607 node
.getAttribute('data-type'));
613 function cbi_bind(obj
, type
, callback
, mode
) {
614 if (!obj
.addEventListener
) {
615 obj
.attachEvent('on' + type
,
617 var e
= window
.event
;
619 if (!e
.target
&& e
.srcElement
)
620 e
.target
= e
.srcElement
;
622 return !!callback(e
);
626 obj
.addEventListener(type
, callback
, !!mode
);
631 function cbi_combobox(id
, values
, def
, man
, focus
) {
632 var selid
= "cbi.combobox." + id
;
633 if (document
.getElementById(selid
)) {
637 var obj
= document
.getElementById(id
)
638 var sel
= document
.createElement("select");
640 sel
.index
= obj
.index
;
641 sel
.className
= obj
.className
.replace(/cbi-input-text/, 'cbi-input-select');
643 if (obj
.nextSibling
) {
644 obj
.parentNode
.insertBefore(sel
, obj
.nextSibling
);
646 obj
.parentNode
.appendChild(sel
);
649 var dt
= obj
.getAttribute('cbi_datatype');
650 var op
= obj
.getAttribute('cbi_optional');
652 if (!values
[obj
.value
]) {
653 if (obj
.value
== "") {
654 var optdef
= document
.createElement("option");
656 optdef
.appendChild(document
.createTextNode(typeof(def
) === 'string' ? def
: cbi_strings
.label
.choose
));
657 sel
.appendChild(optdef
);
659 var opt
= document
.createElement("option");
660 opt
.value
= obj
.value
;
661 opt
.selected
= "selected";
662 opt
.appendChild(document
.createTextNode(obj
.value
));
663 sel
.appendChild(opt
);
667 for (var i
in values
) {
668 var opt
= document
.createElement("option");
671 if (obj
.value
== i
) {
672 opt
.selected
= "selected";
675 opt
.appendChild(document
.createTextNode(values
[i
]));
676 sel
.appendChild(opt
);
679 var optman
= document
.createElement("option");
681 optman
.appendChild(document
.createTextNode(typeof(man
) === 'string' ? man
: cbi_strings
.label
.custom
));
682 sel
.appendChild(optman
);
684 obj
.style
.display
= "none";
687 cbi_validate_field(sel
, op
== 'true', dt
);
689 cbi_bind(sel
, "change", function() {
690 if (sel
.selectedIndex
== sel
.options
.length
- 1) {
691 obj
.style
.display
= "inline";
693 sel
.parentNode
.removeChild(sel
);
696 obj
.value
= sel
.options
[sel
.selectedIndex
].value
;
706 // Retrigger validation in select
713 function cbi_combobox_init(id
, values
, def
, man
) {
714 var obj
= (typeof(id
) === 'string') ? document
.getElementById(id
) : id
;
715 cbi_bind(obj
, "blur", function() {
716 cbi_combobox(obj
.id
, values
, def
, man
, true);
718 cbi_combobox(obj
.id
, values
, def
, man
, false);
721 function cbi_filebrowser(id
, defpath
) {
722 var field
= document
.getElementById(id
);
723 var browser
= window
.open(
724 cbi_strings
.path
.browser
+ ( field
.value
|| defpath
|| '' ) + '?field=' + id
,
725 "luci_filebrowser", "width=300,height=400,left=100,top=200,scrollbars=yes"
731 function cbi_browser_init(id
, resource
, defpath
)
733 function cbi_browser_btnclick(e
) {
734 cbi_filebrowser(id
, defpath
);
738 var field
= document
.getElementById(id
);
740 var btn
= document
.createElement('img');
741 btn
.className
= 'cbi-image-button';
742 btn
.src
= (resource
|| cbi_strings
.path
.resource
) + '/cbi/folder.gif';
743 field
.parentNode
.insertBefore(btn
, field
.nextSibling
);
745 cbi_bind(btn
, 'click', cbi_browser_btnclick
);
748 function cbi_dynlist_init(parent
, datatype
, optional
, choices
)
750 var prefix
= parent
.getAttribute('data-prefix');
751 var holder
= parent
.getAttribute('data-placeholder');
755 function cbi_dynlist_redraw(focus
, add
, del
)
759 while (parent
.firstChild
)
761 var n
= parent
.firstChild
;
766 if (n
.nodeName
.toLowerCase() == 'input')
767 values
.push(n
.value
|| '');
768 else if (n
.nodeName
.toLowerCase() == 'select')
769 values
[values
.length
-1] = n
.options
[n
.selectedIndex
].value
;
772 parent
.removeChild(n
);
778 values
.splice(focus
, 0, '');
780 else if (values
.length
== 0)
786 for (var i
= 0; i
< values
.length
; i
++)
788 var t
= document
.createElement('input');
789 t
.id
= prefix
+ '.' + (i
+1);
794 t
.className
= 'cbi-input-text';
796 if (i
== 0 && holder
)
798 t
.placeholder
= holder
;
801 var b
= document
.createElement('img');
802 b
.src
= cbi_strings
.path
.resource
+ ((i
+1) < values
.length
? '/cbi/remove.gif' : '/cbi/add.gif');
803 b
.className
= 'cbi-image-button';
805 parent
.appendChild(t
);
806 parent
.appendChild(b
);
807 if (datatype
== 'file')
809 cbi_browser_init(t
.id
, null, parent
.getAttribute('data-browser-path'));
812 parent
.appendChild(document
.createElement('br'));
816 cbi_validate_field(t
.id
, ((i
+1) == values
.length
) || optional
, datatype
);
821 cbi_combobox_init(t
.id
, choices
, '', cbi_strings
.label
.custom
);
824 cbi_bind(b
, 'keydown', cbi_dynlist_keydown
);
825 cbi_bind(b
, 'keypress', cbi_dynlist_keypress
);
827 if (i
== focus
|| -i
== focus
)
832 cbi_bind(t
, 'keydown', cbi_dynlist_keydown
);
833 cbi_bind(t
, 'keypress', cbi_dynlist_keypress
);
839 else if (-i
== focus
)
843 /* force cursor to end */
850 cbi_bind(b
, 'click', cbi_dynlist_btnclick
);
854 function cbi_dynlist_keypress(ev
)
856 ev
= ev
? ev
: window
.event
;
858 var se
= ev
.target
? ev
.target
: ev
.srcElement
;
860 if (se
.nodeType
== 3)
865 /* backspace, delete */
868 if (se
.value
.length
== 0)
870 if (ev
.preventDefault
)
878 /* enter, arrow up, arrow down */
882 if (ev
.preventDefault
)
891 function cbi_dynlist_keydown(ev
)
893 ev
= ev
? ev
: window
.event
;
895 var se
= ev
.target
? ev
.target
: ev
.srcElement
;
897 if (se
.nodeType
== 3)
900 var prev
= se
.previousSibling
;
901 while (prev
&& prev
.name
!= prefix
)
902 prev
= prev
.previousSibling
;
904 var next
= se
.nextSibling
;
905 while (next
&& next
.name
!= prefix
)
906 next
= next
.nextSibling
;
908 /* advance one further in combobox case */
909 if (next
&& next
.nextSibling
.name
== prefix
)
910 next
= next
.nextSibling
;
914 /* backspace, delete */
917 var del
= (se
.nodeName
.toLowerCase() == 'select')
918 ? true : (se
.value
.length
== 0);
922 if (ev
.preventDefault
)
925 var focus
= se
.index
;
929 cbi_dynlist_redraw(focus
, -1, se
.index
);
938 cbi_dynlist_redraw(-1, se
.index
, -1);
959 function cbi_dynlist_btnclick(ev
)
961 ev
= ev
? ev
: window
.event
;
963 var se
= ev
.target
? ev
.target
: ev
.srcElement
;
964 var input
= se
.previousSibling
;
965 while (input
&& input
.name
!= prefix
) {
966 input
= input
.previousSibling
;
969 if (se
.src
.indexOf('remove') > -1)
973 cbi_dynlist_keydown({
980 cbi_dynlist_keydown({
989 cbi_dynlist_redraw(NaN
, -1, -1);
993 function cbi_t_add(section
, tab
) {
994 var t
= document
.getElementById('tab.' + section
+ '.' + tab
);
995 var c
= document
.getElementById('container.' + section
+ '.' + tab
);
998 cbi_t
[section
] = (cbi_t
[section
] || [ ]);
999 cbi_t
[section
][tab
] = { 'tab': t
, 'container': c
, 'cid': c
.id
};
1003 function cbi_t_switch(section
, tab
) {
1004 if( cbi_t
[section
] && cbi_t
[section
][tab
] ) {
1005 var o
= cbi_t
[section
][tab
];
1006 var h
= document
.getElementById('tab.' + section
);
1007 for( var tid
in cbi_t
[section
] ) {
1008 var o2
= cbi_t
[section
][tid
];
1009 if( o
.tab
.id
!= o2
.tab
.id
) {
1010 o2
.tab
.className
= o2
.tab
.className
.replace(/(^| )cbi-tab( |$)/, " cbi-tab-disabled ");
1011 o2
.container
.style
.display
= 'none';
1014 if(h
) h
.value
= tab
;
1015 o2
.tab
.className
= o2
.tab
.className
.replace(/(^| )cbi-tab-disabled( |$)/, " cbi-tab ");
1016 o2
.container
.style
.display
= 'block';
1023 function cbi_t_update() {
1025 var updated
= false;
1027 for( var sid
in cbi_t
)
1028 for( var tid
in cbi_t
[sid
] )
1030 var t
= cbi_t
[sid
][tid
].tab
;
1031 var c
= cbi_t
[sid
][tid
].container
;
1033 if (!c
.firstElementChild
) {
1034 t
.style
.display
= 'none';
1036 else if (t
.style
.display
== 'none') {
1037 t
.style
.display
= '';
1038 t
.className
+= ' cbi-tab-highlighted';
1046 if (hl_tabs
.length
> 0)
1047 window
.setTimeout(function() {
1048 for( var i
= 0; i
< hl_tabs
.length
; i
++ )
1049 hl_tabs
[i
].className
= hl_tabs
[i
].className
.replace(/ cbi
-tab
-highlighted
/g
, '');
1056 function cbi_validate_form(form
, errmsg
)
1058 /* if triggered by a section removal or addition, don't validate */
1059 if( form
.cbi_state
== 'add-section' || form
.cbi_state
== 'del-section' )
1062 if( form
.cbi_validators
)
1064 for( var i
= 0; i
< form
.cbi_validators
.length
; i
++ )
1066 var validator
= form
.cbi_validators
[i
];
1067 if( !validator() && errmsg
)
1078 function cbi_validate_reset(form
)
1081 function() { cbi_validate_form(form
, null) }, 100
1087 function cbi_validate_compile(code
)
1096 for (var i
= 0; i
< code
.length
; i
++)
1104 switch (code
.charCodeAt(i
))
1116 var label
= code
.substring(pos
, i
);
1117 label
= label
.replace(/\\(.)/g, '$1');
1118 label
= label
.replace(/^[ \t]+/g, '');
1119 label
= label
.replace(/[ \t]+$/g, '');
1121 if (label
&& !isNaN(label
))
1123 stack
.push(parseFloat(label
));
1125 else if (label
.match(/^(['"]).*\1$/))
1127 stack
.push(label
.replace(/^(['"])(.*)\1$/, '$2'));
1129 else if (typeof cbi_validators
[label
] == 'function')
1131 stack
.push(cbi_validators
[label
]);
1136 throw "Syntax error, unhandled token '"+label
+"'";
1141 depth
+= (code
.charCodeAt(i
) == 40);
1147 if (typeof stack
[stack
.length
-2] != 'function')
1148 throw "Syntax error, argument list follows non-function";
1150 stack
[stack
.length
-1] =
1151 arguments
.callee(code
.substring(pos
, i
));
1162 function cbi_validate_field(cbid
, optional
, type
)
1164 var field
= (typeof cbid
== "string") ? document
.getElementById(cbid
) : cbid
;
1165 var vstack
; try { vstack
= cbi_validate_compile(type
); } catch(e
) { };
1167 if (field
&& vstack
&& typeof vstack
[0] == "function")
1169 var validator = function()
1174 field
.className
= field
.className
.replace(/ cbi
-input
-invalid
/g
, '');
1177 var value
= (field
.options
&& field
.options
.selectedIndex
> -1)
1178 ? field
.options
[field
.options
.selectedIndex
].value
: field
.value
;
1180 if (!(((value
.length
== 0) && optional
) || vstack
[0].apply(value
, vstack
[1])))
1183 field
.className
+= ' cbi-input-invalid';
1191 if( ! field
.form
.cbi_validators
)
1192 field
.form
.cbi_validators
= [ ];
1194 field
.form
.cbi_validators
.push(validator
);
1196 cbi_bind(field
, "blur", validator
);
1197 cbi_bind(field
, "keyup", validator
);
1199 if (field
.nodeName
== 'SELECT')
1201 cbi_bind(field
, "change", validator
);
1202 cbi_bind(field
, "click", validator
);
1205 field
.setAttribute("cbi_validate", validator
);
1206 field
.setAttribute("cbi_datatype", type
);
1207 field
.setAttribute("cbi_optional", (!!optional
).toString());
1211 var fcbox
= document
.getElementById('cbi.combobox.' + field
.id
);
1213 cbi_validate_field(fcbox
, optional
, type
);
1217 function cbi_row_swap(elem
, up
, store
)
1219 var tr
= elem
.parentNode
;
1220 while (tr
&& tr
.nodeName
.toLowerCase() != 'tr')
1226 var table
= tr
.parentNode
;
1227 while (table
&& table
.nodeName
.toLowerCase() != 'table')
1228 table
= table
.parentNode
;
1234 var e
= up
? table
.rows
.length
: table
.rows
.length
- 1;
1236 for (var idx
= s
; idx
< e
; idx
++)
1238 if (table
.rows
[idx
] == tr
)
1241 tr
.parentNode
.insertBefore(table
.rows
[idx
], table
.rows
[idx
-1]);
1243 tr
.parentNode
.insertBefore(table
.rows
[idx
+1], table
.rows
[idx
]);
1250 for (idx
= 2; idx
< table
.rows
.length
; idx
++)
1252 table
.rows
[idx
].className
= table
.rows
[idx
].className
.replace(
1253 /cbi-rowstyle-[12]/, 'cbi-rowstyle-' + (1 + (idx
% 2))
1256 if (table
.rows
[idx
].id
&& table
.rows
[idx
].id
.match(/-([^\-]+)$/) )
1257 ids
.push(RegExp
.$1);
1260 var input
= document
.getElementById(store
);
1262 input
.value
= ids
.join(' ');
1267 function cbi_tag_last(container
)
1271 for (var i
= 0; i
< container
.childNodes
.length
; i
++)
1273 var c
= container
.childNodes
[i
];
1274 if (c
.nodeType
== 1 && c
.nodeName
.toLowerCase() == 'div')
1276 c
.className
= c
.className
.replace(/ cbi
-value
-last
$/, '');
1283 last
.className
+= ' cbi-value-last';
1287 String
.prototype.serialize = function()
1304 for( var i
= 0; i
< o
.length
; i
++ )
1305 s
+= (s
? ', ' : '') + String
.serialize(o
[i
]);
1307 return '[ ' + s
+ ' ]';
1316 s
+= (s
? ', ' : '') + k
+ ': ' + String
.serialize(o
[k
]);
1318 return '{ ' + s
+ ' }';
1325 if( o
.match(/[^a-zA-Z0-9_,.: -]/) )
1326 return 'decodeURIComponent("' + encodeURIComponent(o
) + '")';
1330 return '"' + o
+ '"';
1335 return o
.toString();
1339 String
.prototype.format = function()
1344 var html_esc
= [/&/g, '&', /"/g, '"', /'/g, ''', /</g, '<', />/g, '>'];
1345 var quot_esc = [/"/g
, '"', /'/g, '''];
1347 function esc(s, r) {
1348 if (typeof(s) !== 'string
' && !(s instanceof String))
1351 for( var i = 0; i < r.length; i += 2 )
1352 s = s.replace(r[i], r[i+1]);
1358 var re = /^(([^%]*)%('.|0|\x20)?(-)?(\d
+)?(\.\d
+)?(%|b
|c
|d
|u
|f
|o
|s
|x
|X
|q
|h
|j
|t
|m
))/;
1359 var a
= b
= [], numSubstitutions
= 0, numMatches
= 0;
1361 while (a
= re
.exec(str
))
1364 var leftpart
= a
[2], pPad
= a
[3], pJustify
= a
[4], pMinLength
= a
[5];
1365 var pPrecision
= a
[6], pType
= a
[7];
1375 if (numSubstitutions
< arguments
.length
)
1377 var param
= arguments
[numSubstitutions
++];
1380 if (pPad
&& pPad
.substr(0,1) == "'")
1381 pad
= leftpart
.substr(1,1);
1387 var justifyRight
= true;
1388 if (pJustify
&& pJustify
=== "-")
1389 justifyRight
= false;
1393 minLength
= +pMinLength
;
1396 if (pPrecision
&& pType
== 'f')
1397 precision
= +pPrecision
.substring(1);
1404 subst
= (+param
|| 0).toString(2);
1408 subst
= String
.fromCharCode(+param
|| 0);
1412 subst
= ~~(+param
|| 0);
1416 subst
= ~~Math
.abs(+param
|| 0);
1420 subst
= (precision
> -1)
1421 ? ((+param
|| 0.0)).toFixed(precision
)
1426 subst
= (+param
|| 0).toString(8);
1434 subst
= ('' + (+param
|| 0).toString(16)).toLowerCase();
1438 subst
= ('' + (+param
|| 0).toString(16)).toUpperCase();
1442 subst
= esc(param
, html_esc
);
1446 subst
= esc(param
, quot_esc
);
1450 subst
= String
.serialize(param
);
1457 var ts
= (param
|| 0);
1460 tm
= Math
.floor(ts
/ 60);
1465 th
= Math
.floor(tm
/ 60);
1470 td
= Math
.floor(th
/ 24);
1475 ? String
.format('%dd %dh %dm %ds', td
, th
, tm
, ts
)
1476 : String
.format('%dh %dm %ds', th
, tm
, ts
);
1481 var mf
= pMinLength
? +pMinLength
: 1000;
1482 var pr
= pPrecision
? ~~(10 * +('0' + pPrecision
)) : 2;
1485 var val
= (+param
|| 0);
1486 var units
= [ ' ', ' K', ' M', ' G', ' T', ' P', ' E' ];
1488 for (i
= 0; (i
< units
.length
) && (val
> mf
); i
++)
1491 subst
= (i
? val
.toFixed(pr
) : val
) + units
[i
];
1499 subst
= subst
.toString();
1500 for (var i
= subst
.length
; i
< pMinLength
; i
++)
1501 if (pJustify
== '-')
1502 subst
= subst
+ ' ';
1504 subst
= pad
+ subst
;
1507 out
+= leftpart
+ subst
;
1508 str
= str
.substr(m
.length
);
1514 String
.prototype.nobr = function()
1516 return this.replace(/[\s\n]+/g, ' ');
1519 String
.serialize = function()
1522 for (var i
= 1; i
< arguments
.length
; i
++)
1523 a
.push(arguments
[i
]);
1524 return ''.serialize
.apply(arguments
[0], a
);
1527 String
.format = function()
1530 for (var i
= 1; i
< arguments
.length
; i
++)
1531 a
.push(arguments
[i
]);
1532 return ''.format
.apply(arguments
[0], a
);
1535 String
.nobr = function()
1538 for (var i
= 1; i
< arguments
.length
; i
++)
1539 a
.push(arguments
[i
]);
1540 return ''.nobr
.apply(arguments
[0], a
);