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
);
27 if (!x
.match(/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/))
30 if (RegExp
.$1 > 255 || RegExp
.$2 > 255 || RegExp
.$3 > 255 || RegExp
.$4 > 255)
33 return [ +RegExp
.$1, +RegExp
.$2, +RegExp
.$3, +RegExp
.$4 ];
37 if (x
.match(/^([a-fA-F0-9:]+):(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/)) {
38 var v6
= RegExp
.$1, v4
= IPv4(RegExp
.$2);
43 x
= v6
+ ':' + (v4
[0] * 256 + v4
[1]).toString(16)
44 + ':' + (v4
[2] * 256 + v4
[3]).toString(16);
47 if (!x
.match(/^[a-fA-F0-9:]+$/))
50 var prefix_suffix
= x
.split(/::/);
52 if (prefix_suffix
.length
> 2)
55 var prefix
= (prefix_suffix
[0] || '0').split(/:/);
56 var suffix
= prefix_suffix
.length
> 1 ? (prefix_suffix
[1] || '0').split(/:/) : [];
58 if (suffix
.length
? (prefix
.length
+ suffix
.length
> 7) : (prefix
.length
> 8))
64 for (i
= 0, word
= parseInt(prefix
[0], 16); i
< prefix
.length
; word
= parseInt(prefix
[++i
], 16))
65 if (prefix
[i
].length
<= 4 && !isNaN(word
) && word
<= 0xFFFF)
70 for (i
= 0; i
< (8 - prefix
.length
- suffix
.length
); i
++)
73 for (i
= 0, word
= parseInt(suffix
[0], 16); i
< suffix
.length
; word
= parseInt(suffix
[++i
], 16))
74 if (suffix
[i
].length
<= 4 && !isNaN(word
) && word
<= 0xFFFF)
82 var cbi_validators
= {
89 'uinteger': function()
91 return (Int(this) >= 0);
101 return (Dec(this) >= 0);
106 return cbi_validators
.ip4addr
.apply(this) ||
107 cbi_validators
.ip6addr
.apply(this);
110 'ip4addr': function()
112 var m
= this.match(/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(?:\/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})|\/(\d{1,2}))?$/);
113 return !!(m
&& IPv4(m
[1]) && (m
[2] ? IPv4(m
[2]) : (m
[3] ? cbi_validators
.ip4prefix
.apply(m
[3]) : true)));
116 'ip6addr': function()
118 var m
= this.match(/^([0-9a-fA-F:.]+)(?:\/(\d{1,3}))?$/);
119 return !!(m
&& IPv6(m
[1]) && (m
[2] ? cbi_validators
.ip6prefix
.apply(m
[2]) : true));
122 'ip4prefix': function()
124 return !isNaN(this) && this >= 0 && this <= 32;
127 'ip6prefix': function()
129 return !isNaN(this) && this >= 0 && this <= 128;
134 return cbi_validators
.cidr4
.apply(this) ||
135 cbi_validators
.cidr6
.apply(this);
140 var m
= this.match(/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\/(\d{1,2})$/);
141 return !!(m
&& IPv4(m
[1]) && cbi_validators
.ip4prefix
.apply(m
[2]));
146 var m
= this.match(/^([0-9a-fA-F:.]+)\/(\d{1,3})$/);
147 return !!(m
&& IPv6(m
[1]) && cbi_validators
.ip6prefix
.apply(m
[2]));
152 var m
= this.match(/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/);
153 return !!(m
&& IPv4(m
[1]) && IPv4(m
[2]));
158 var m
= this.match(/^([0-9a-fA-F:.]+)\/([0-9a-fA-F:.]+)$/);
159 return !!(m
&& IPv6(m
[1]) && IPv6(m
[2]));
162 'ip6hostid': function()
164 if (this == "eui64" || this == "random")
168 return !(!v6
|| v6
[0] || v6
[1] || v6
[2] || v6
[3]);
173 return cbi_validators
.ipmask4
.apply(this) ||
174 cbi_validators
.ipmask6
.apply(this);
177 'ipmask4': function()
179 return cbi_validators
.cidr4
.apply(this) ||
180 cbi_validators
.ipnet4
.apply(this) ||
181 cbi_validators
.ip4addr
.apply(this);
184 'ipmask6': function()
186 return cbi_validators
.cidr6
.apply(this) ||
187 cbi_validators
.ipnet6
.apply(this) ||
188 cbi_validators
.ip6addr
.apply(this);
194 return (p
>= 0 && p
<= 65535);
197 'portrange': function()
199 if (this.match(/^(\d+)-(\d+)$/))
203 return (p1
<= p2
&& p2
<= 65535);
206 return cbi_validators
.port
.apply(this);
209 'macaddr': function()
211 return (this.match(/^([a-fA-F0-9]{2}:){5}[a-fA-F0-9]{2}$/) != null);
214 'host': function(ipv4only
)
216 return cbi_validators
.hostname
.apply(this) ||
217 ((ipv4only
!= 1) && cbi_validators
.ipaddr
.apply(this)) ||
218 ((ipv4only
== 1) && cbi_validators
.ip4addr
.apply(this));
221 'hostname': function(strict
)
223 if (this.length
<= 253)
224 return (this.match(/^[a-zA-Z0-9_]+$/) != null ||
225 (this.match(/^[a-zA-Z0-9_][a-zA-Z0-9_\-.]*[a-zA-Z0-9]$/) &&
226 this.match(/[^0-9.]/))) &&
227 (!strict
|| !this.match(/^_/));
232 'network': function()
234 return cbi_validators
.uciname
.apply(this) ||
235 cbi_validators
.host
.apply(this);
238 'hostport': function(ipv4only
)
240 var hp
= this.split(/:/);
243 return (cbi_validators
.host
.apply(hp
[0], ipv4only
) &&
244 cbi_validators
.port
.apply(hp
[1]));
249 'ip4addrport': function()
251 var hp
= this.split(/:/);
254 return (cbi_validators
.ipaddr
.apply(hp
[0]) &&
255 cbi_validators
.port
.apply(hp
[1]));
259 'ipaddrport': function(bracket
)
261 if (this.match(/^([^\[\]:]+):([^:]+)$/)) {
264 return (cbi_validators
.ip4addr
.apply(addr
) &&
265 cbi_validators
.port
.apply(port
));
266 } else if ((bracket
== 1) && (this.match(/^\[(.+)\]:([^:]+)$/))) {
269 return (cbi_validators
.ip6addr
.apply(addr
) &&
270 cbi_validators
.port
.apply(port
));
271 } else if ((bracket
!= 1) && (this.match(/^([^\[\]]+):([^:]+)$/))) {
274 return (cbi_validators
.ip6addr
.apply(addr
) &&
275 cbi_validators
.port
.apply(port
));
286 return (v
.match(/^[a-fA-F0-9]{64}$/) != null);
288 return (v
.length
>= 8) && (v
.length
<= 63);
295 if ( v
.substr(0,2) == 's:' )
298 if( (v
.length
== 10) || (v
.length
== 26) )
299 return (v
.match(/^[a-fA-F0-9]{10,26}$/) != null);
301 return (v
.length
== 5) || (v
.length
== 13);
304 'uciname': function()
306 return (this.match(/^[a-zA-Z0-9_]+$/) != null);
309 'range': function(min
, max
)
312 return (val
>= +min
&& val
<= +max
);
317 return (Dec(this) >= +min
);
322 return (Dec(this) <= +max
);
325 'rangelength': function(min
, max
)
328 return ((val
.length
>= +min
) && (val
.length
<= +max
));
331 'minlength': function(min
)
333 return ((''+this).length
>= +min
);
336 'maxlength': function(max
)
338 return ((''+this).length
<= +max
);
343 for (var i
= 0; i
< arguments
.length
; i
+= 2)
345 if (typeof arguments
[i
] != 'function')
347 if (arguments
[i
] == this)
351 else if (arguments
[i
].apply(this, arguments
[i
+1]))
361 for (var i
= 0; i
< arguments
.length
; i
+= 2)
363 if (typeof arguments
[i
] != 'function')
365 if (arguments
[i
] != this)
369 else if (!arguments
[i
].apply(this, arguments
[i
+1]))
379 return cbi_validators
.or
.apply(
380 this.replace(/^[ \t]*![ \t]*/, ''), arguments
);
383 'list': function(subvalidator
, subargs
)
385 if (typeof subvalidator
!= 'function')
388 var tokens
= this.match(/[^ \t]+/g);
389 for (var i
= 0; i
< tokens
.length
; i
++)
390 if (!subvalidator
.apply(tokens
[i
], subargs
))
395 'phonedigit': function()
397 return (this.match(/^[0-9\*#!\.]+$/) != null);
399 'timehhmmss': function()
401 return (this.match(/^[0-6][0-9]:[0-6][0-9]:[0-6][0-9]$/) != null);
403 'dateyyyymmdd': function()
408 if (this.match(/^(\d\d\d\d)-(\d\d)-(\d\d)/)) {
409 var year
= RegExp
.$1;
410 var month
= RegExp
.$2;
413 var days_in_month
= [ 31, 28, 31, 30, 31, 30, 31, 31, 30 , 31, 30, 31 ];
414 function is_leap_year(year
) {
415 return ((year
% 4) == 0) && ((year
% 100) != 0) || ((year
% 400) == 0);
417 function get_days_in_month(month
, year
) {
418 if ((month
== 2) && is_leap_year(year
)) {
421 return days_in_month
[month
];
424 /* Firewall rules in the past don't make sense */
428 if ((month
<= 0) || (month
> 12)) {
431 if ((day
<= 0) || (day
> get_days_in_month(month
, year
))) {
443 function cbi_d_add(field
, dep
, index
) {
444 var obj
= (typeof(field
) === 'string') ? document
.getElementById(field
) : field
;
447 for (var i
=0; i
<cbi_d
.length
; i
++) {
448 if (cbi_d
[i
].id
== obj
.id
) {
457 "parent": obj
.parentNode
.id
,
461 cbi_d
.unshift(entry
);
467 function cbi_d_checkvalue(target
, ref
) {
469 query
= 'input[id="'+target
+'"], input[name="'+target
+'"], ' +
470 'select[id="'+target
+'"], select[name="'+target
+'"]';
472 document
.querySelectorAll(query
).forEach(function(i
) {
473 if (value
=== null && ((i
.type
!== 'radio' && i
.type
!== 'checkbox') || i
.checked
=== true))
477 return (((value
!== null) ? value
: "") == ref
);
480 function cbi_d_check(deps
) {
483 for (var i
=0; i
<deps
.length
; i
++) {
486 for (var j
in deps
[i
]) {
487 if (j
== "!reverse") {
489 } else if (j
== "!default") {
493 istat
= (istat
&& cbi_d_checkvalue(j
, deps
[i
][j
]))
497 if (istat
^ reverse
) {
504 function cbi_d_update() {
506 for (var i
=0; i
<cbi_d
.length
; i
++) {
507 var entry
= cbi_d
[i
];
508 var node
= document
.getElementById(entry
.id
);
509 var parent
= document
.getElementById(entry
.parent
);
511 if (node
&& node
.parentNode
&& !cbi_d_check(entry
.deps
)) {
512 node
.parentNode
.removeChild(node
);
514 } else if (parent
&& (!node
|| !node
.parentNode
) && cbi_d_check(entry
.deps
)) {
515 var next
= undefined;
517 for (next
= parent
.firstChild
; next
; next
= next
.nextSibling
) {
518 if (next
.getAttribute
&& parseInt(next
.getAttribute('data-index'), 10) > entry
.index
) {
524 parent
.appendChild(entry
.node
);
526 parent
.insertBefore(entry
.node
, next
);
532 // hide optionals widget if no choices remaining
533 if (parent
&& parent
.parentNode
&& parent
.getAttribute('data-optionals'))
534 parent
.parentNode
.style
.display
= (parent
.options
.length
<= 1) ? 'none' : '';
537 if (entry
&& entry
.parent
) {
539 cbi_tag_last(parent
);
547 function cbi_init() {
550 nodes
= document
.querySelectorAll('[data-strings]');
552 for (var i
= 0, node
; (node
= nodes
[i
]) !== undefined; i
++) {
553 var str
= JSON
.parse(node
.getAttribute('data-strings'));
554 for (var key
in str
) {
555 for (var key2
in str
[key
]) {
556 var dst
= cbi_strings
[key
] || (cbi_strings
[key
] = { });
557 dst
[key2
] = str
[key
][key2
];
562 nodes
= document
.querySelectorAll('[data-depends]');
564 for (var i
= 0, node
; (node
= nodes
[i
]) !== undefined; i
++) {
565 var index
= parseInt(node
.getAttribute('data-index'), 10);
566 var depends
= JSON
.parse(node
.getAttribute('data-depends'));
567 if (!isNaN(index
) && depends
.length
> 0) {
568 for (var alt
= 0; alt
< depends
.length
; alt
++) {
569 cbi_d_add(node
, depends
[alt
], index
);
574 nodes
= document
.querySelectorAll('[data-update]');
576 for (var i
= 0, node
; (node
= nodes
[i
]) !== undefined; i
++) {
577 var events
= node
.getAttribute('data-update').split(' ');
578 for (var j
= 0, event
; (event
= events
[j
]) !== undefined; j
++) {
579 cbi_bind(node
, event
, cbi_d_update
);
583 nodes
= document
.querySelectorAll('[data-choices]');
585 for (var i
= 0, node
; (node
= nodes
[i
]) !== undefined; i
++) {
586 var choices
= JSON
.parse(node
.getAttribute('data-choices'));
589 for (var j
= 0; j
< choices
[0].length
; j
++)
590 options
[choices
[0][j
]] = choices
[1][j
];
592 var def
= (node
.getAttribute('data-optional') === 'true')
593 ? node
.placeholder
|| '' : null;
595 cbi_combobox_init(node
, options
, def
,
596 node
.getAttribute('data-manual'));
599 nodes
= document
.querySelectorAll('[data-dynlist]');
601 for (var i
= 0, node
; (node
= nodes
[i
]) !== undefined; i
++) {
602 var choices
= JSON
.parse(node
.getAttribute('data-dynlist'));
605 if (choices
[0] && choices
[0].length
) {
608 for (var j
= 0; j
< choices
[0].length
; j
++)
609 options
[choices
[0][j
]] = choices
[1][j
];
612 cbi_dynlist_init(node
, choices
[2], choices
[3], options
);
615 nodes
= document
.querySelectorAll('[data-type]');
617 for (var i
= 0, node
; (node
= nodes
[i
]) !== undefined; i
++) {
618 cbi_validate_field(node
, node
.getAttribute('data-optional') === 'true',
619 node
.getAttribute('data-type'));
622 document
.querySelectorAll('.cbi-dropdown').forEach(function(s
) {
623 cbi_dropdown_init(s
);
626 document
.querySelectorAll('.cbi-tooltip:not(:empty)').forEach(function(s
) {
627 s
.parentNode
.classList
.add('cbi-tooltip-container');
630 document
.querySelectorAll('.cbi-section-remove > input[name^="cbi.rts"]').forEach(function(i
) {
631 var handler = function(ev
) {
632 var bits
= this.name
.split(/\./),
633 section
= document
.getElementById('cbi-' + bits
[2] + '-' + bits
[3]);
635 section
.style
.opacity
= (ev
.type
=== 'mouseover') ? 0.5 : '';
638 i
.addEventListener('mouseover', handler
);
639 i
.addEventListener('mouseout', handler
);
645 function cbi_bind(obj
, type
, callback
, mode
) {
646 if (!obj
.addEventListener
) {
647 obj
.attachEvent('on' + type
,
649 var e
= window
.event
;
651 if (!e
.target
&& e
.srcElement
)
652 e
.target
= e
.srcElement
;
654 return !!callback(e
);
658 obj
.addEventListener(type
, callback
, !!mode
);
663 function cbi_combobox(id
, values
, def
, man
, focus
) {
664 var selid
= "cbi.combobox." + id
;
665 if (document
.getElementById(selid
)) {
669 var obj
= document
.getElementById(id
)
670 var sel
= document
.createElement("select");
672 sel
.index
= obj
.index
;
673 sel
.className
= obj
.className
.replace(/cbi-input-text/, 'cbi-input-select');
675 if (obj
.nextSibling
) {
676 obj
.parentNode
.insertBefore(sel
, obj
.nextSibling
);
678 obj
.parentNode
.appendChild(sel
);
681 var dt
= obj
.getAttribute('cbi_datatype');
682 var op
= obj
.getAttribute('cbi_optional');
684 if (!values
[obj
.value
]) {
685 if (obj
.value
== "") {
686 var optdef
= document
.createElement("option");
688 optdef
.appendChild(document
.createTextNode(typeof(def
) === 'string' ? def
: cbi_strings
.label
.choose
));
689 sel
.appendChild(optdef
);
691 var opt
= document
.createElement("option");
692 opt
.value
= obj
.value
;
693 opt
.selected
= "selected";
694 opt
.appendChild(document
.createTextNode(obj
.value
));
695 sel
.appendChild(opt
);
699 for (var i
in values
) {
700 var opt
= document
.createElement("option");
703 if (obj
.value
== i
) {
704 opt
.selected
= "selected";
707 opt
.appendChild(document
.createTextNode(values
[i
]));
708 sel
.appendChild(opt
);
711 var optman
= document
.createElement("option");
713 optman
.appendChild(document
.createTextNode(typeof(man
) === 'string' ? man
: cbi_strings
.label
.custom
));
714 sel
.appendChild(optman
);
716 obj
.style
.display
= "none";
719 cbi_validate_field(sel
, op
== 'true', dt
);
721 cbi_bind(sel
, "change", function() {
722 if (sel
.selectedIndex
== sel
.options
.length
- 1) {
723 obj
.style
.display
= "inline";
725 sel
.parentNode
.removeChild(sel
);
728 obj
.value
= sel
.options
[sel
.selectedIndex
].value
;
738 // Retrigger validation in select
745 function cbi_combobox_init(id
, values
, def
, man
) {
746 var obj
= (typeof(id
) === 'string') ? document
.getElementById(id
) : id
;
747 cbi_bind(obj
, "blur", function() {
748 cbi_combobox(obj
.id
, values
, def
, man
, true);
750 cbi_combobox(obj
.id
, values
, def
, man
, false);
753 function cbi_filebrowser(id
, defpath
) {
754 var field
= document
.getElementById(id
);
755 var browser
= window
.open(
756 cbi_strings
.path
.browser
+ ( field
.value
|| defpath
|| '' ) + '?field=' + id
,
757 "luci_filebrowser", "width=300,height=400,left=100,top=200,scrollbars=yes"
763 function cbi_browser_init(id
, resource
, defpath
)
765 function cbi_browser_btnclick(e
) {
766 cbi_filebrowser(id
, defpath
);
770 var field
= document
.getElementById(id
);
772 var btn
= document
.createElement('img');
773 btn
.className
= 'cbi-image-button';
774 btn
.src
= (resource
|| cbi_strings
.path
.resource
) + '/cbi/folder.gif';
775 field
.parentNode
.insertBefore(btn
, field
.nextSibling
);
777 cbi_bind(btn
, 'click', cbi_browser_btnclick
);
780 function cbi_dynlist_init(parent
, datatype
, optional
, choices
)
782 var prefix
= parent
.getAttribute('data-prefix');
783 var holder
= parent
.getAttribute('data-placeholder');
787 function cbi_dynlist_redraw(focus
, add
, del
)
791 while (parent
.firstChild
)
793 var n
= parent
.firstChild
;
798 if (n
.nodeName
.toLowerCase() == 'input')
799 values
.push(n
.value
|| '');
800 else if (n
.nodeName
.toLowerCase() == 'select')
801 values
[values
.length
-1] = n
.options
[n
.selectedIndex
].value
;
804 parent
.removeChild(n
);
810 values
.splice(focus
, 0, '');
812 else if (values
.length
== 0)
818 for (var i
= 0; i
< values
.length
; i
++)
820 var t
= document
.createElement('input');
821 t
.id
= prefix
+ '.' + (i
+1);
826 t
.className
= 'cbi-input-text';
828 if (i
== 0 && holder
)
830 t
.placeholder
= holder
;
834 class: 'cbi-button cbi-button-' + ((i
+1) < values
.length
? 'remove' : 'add')
835 }, (i
+1) < values
.length
? '×' : '+');
837 parent
.appendChild(t
);
838 parent
.appendChild(b
);
839 if (datatype
== 'file')
841 cbi_browser_init(t
.id
, null, parent
.getAttribute('data-browser-path'));
844 parent
.appendChild(document
.createElement('br'));
848 cbi_validate_field(t
.id
, ((i
+1) == values
.length
) || optional
, datatype
);
853 cbi_combobox_init(t
.id
, choices
, '', cbi_strings
.label
.custom
);
856 cbi_bind(b
, 'keydown', cbi_dynlist_keydown
);
857 cbi_bind(b
, 'keypress', cbi_dynlist_keypress
);
859 if (i
== focus
|| -i
== focus
)
864 cbi_bind(t
, 'keydown', cbi_dynlist_keydown
);
865 cbi_bind(t
, 'keypress', cbi_dynlist_keypress
);
871 else if (-i
== focus
)
875 /* force cursor to end */
882 cbi_bind(b
, 'click', cbi_dynlist_btnclick
);
886 function cbi_dynlist_keypress(ev
)
888 ev
= ev
? ev
: window
.event
;
890 var se
= ev
.target
? ev
.target
: ev
.srcElement
;
892 if (se
.nodeType
== 3)
897 /* backspace, delete */
900 if (se
.value
.length
== 0)
902 if (ev
.preventDefault
)
910 /* enter, arrow up, arrow down */
914 if (ev
.preventDefault
)
923 function cbi_dynlist_keydown(ev
)
925 ev
= ev
? ev
: window
.event
;
927 var se
= ev
.target
? ev
.target
: ev
.srcElement
;
929 if (se
.nodeType
== 3)
932 var prev
= se
.previousSibling
;
933 while (prev
&& prev
.name
!= prefix
)
934 prev
= prev
.previousSibling
;
936 var next
= se
.nextSibling
;
937 while (next
&& next
.name
!= prefix
)
938 next
= next
.nextSibling
;
940 /* advance one further in combobox case */
941 if (next
&& next
.nextSibling
.name
== prefix
)
942 next
= next
.nextSibling
;
946 /* backspace, delete */
949 var del
= (se
.nodeName
.toLowerCase() == 'select')
950 ? true : (se
.value
.length
== 0);
954 if (ev
.preventDefault
)
957 var focus
= se
.index
;
961 cbi_dynlist_redraw(focus
, -1, se
.index
);
970 cbi_dynlist_redraw(-1, se
.index
, -1);
991 function cbi_dynlist_btnclick(ev
)
993 ev
= ev
? ev
: window
.event
;
995 var se
= ev
.target
? ev
.target
: ev
.srcElement
;
996 var input
= se
.previousSibling
;
997 while (input
&& input
.name
!= prefix
) {
998 input
= input
.previousSibling
;
1001 if (se
.classList
.contains('cbi-button-remove')) {
1004 cbi_dynlist_keydown({
1010 cbi_dynlist_keydown({
1019 cbi_dynlist_redraw(NaN
, -1, -1);
1023 function cbi_t_add(section
, tab
) {
1024 var t
= document
.getElementById('tab.' + section
+ '.' + tab
);
1025 var c
= document
.getElementById('container.' + section
+ '.' + tab
);
1028 cbi_t
[section
] = (cbi_t
[section
] || [ ]);
1029 cbi_t
[section
][tab
] = { 'tab': t
, 'container': c
, 'cid': c
.id
};
1033 function cbi_t_switch(section
, tab
) {
1034 if( cbi_t
[section
] && cbi_t
[section
][tab
] ) {
1035 var o
= cbi_t
[section
][tab
];
1036 var h
= document
.getElementById('tab.' + section
);
1037 for( var tid
in cbi_t
[section
] ) {
1038 var o2
= cbi_t
[section
][tid
];
1039 if( o
.tab
.id
!= o2
.tab
.id
) {
1040 o2
.tab
.className
= o2
.tab
.className
.replace(/(^| )cbi-tab( |$)/, " cbi-tab-disabled ");
1041 o2
.container
.style
.display
= 'none';
1044 if(h
) h
.value
= tab
;
1045 o2
.tab
.className
= o2
.tab
.className
.replace(/(^| )cbi-tab-disabled( |$)/, " cbi-tab ");
1046 o2
.container
.style
.display
= 'block';
1053 function cbi_t_update() {
1055 var updated
= false;
1057 for( var sid
in cbi_t
)
1058 for( var tid
in cbi_t
[sid
] )
1060 var t
= cbi_t
[sid
][tid
].tab
;
1061 var c
= cbi_t
[sid
][tid
].container
;
1063 if (!c
.firstElementChild
) {
1064 t
.style
.display
= 'none';
1066 else if (t
.style
.display
== 'none') {
1067 t
.style
.display
= '';
1068 t
.className
+= ' cbi-tab-highlighted';
1076 if (hl_tabs
.length
> 0)
1077 window
.setTimeout(function() {
1078 for( var i
= 0; i
< hl_tabs
.length
; i
++ )
1079 hl_tabs
[i
].className
= hl_tabs
[i
].className
.replace(/ cbi
-tab
-highlighted
/g
, '');
1086 function cbi_validate_form(form
, errmsg
)
1088 /* if triggered by a section removal or addition, don't validate */
1089 if( form
.cbi_state
== 'add-section' || form
.cbi_state
== 'del-section' )
1092 if( form
.cbi_validators
)
1094 for( var i
= 0; i
< form
.cbi_validators
.length
; i
++ )
1096 var validator
= form
.cbi_validators
[i
];
1097 if( !validator() && errmsg
)
1108 function cbi_validate_reset(form
)
1111 function() { cbi_validate_form(form
, null) }, 100
1117 function cbi_validate_compile(code
)
1126 for (var i
= 0; i
< code
.length
; i
++)
1134 switch (code
.charCodeAt(i
))
1146 var label
= code
.substring(pos
, i
);
1147 label
= label
.replace(/\\(.)/g, '$1');
1148 label
= label
.replace(/^[ \t]+/g, '');
1149 label
= label
.replace(/[ \t]+$/g, '');
1151 if (label
&& !isNaN(label
))
1153 stack
.push(parseFloat(label
));
1155 else if (label
.match(/^(['"]).*\1$/))
1157 stack
.push(label
.replace(/^(['"])(.*)\1$/, '$2'));
1159 else if (typeof cbi_validators
[label
] == 'function')
1161 stack
.push(cbi_validators
[label
]);
1166 throw "Syntax error, unhandled token '"+label
+"'";
1171 depth
+= (code
.charCodeAt(i
) == 40);
1177 if (typeof stack
[stack
.length
-2] != 'function')
1178 throw "Syntax error, argument list follows non-function";
1180 stack
[stack
.length
-1] =
1181 arguments
.callee(code
.substring(pos
, i
));
1192 function cbi_validate_field(cbid
, optional
, type
)
1194 var field
= (typeof cbid
== "string") ? document
.getElementById(cbid
) : cbid
;
1195 var vstack
; try { vstack
= cbi_validate_compile(type
); } catch(e
) { };
1197 if (field
&& vstack
&& typeof vstack
[0] == "function")
1199 var validator = function()
1204 field
.className
= field
.className
.replace(/ cbi
-input
-invalid
/g
, '');
1207 var value
= (field
.options
&& field
.options
.selectedIndex
> -1)
1208 ? field
.options
[field
.options
.selectedIndex
].value
: field
.value
;
1210 if (!(((value
.length
== 0) && optional
) || vstack
[0].apply(value
, vstack
[1])))
1213 field
.className
+= ' cbi-input-invalid';
1221 if( ! field
.form
.cbi_validators
)
1222 field
.form
.cbi_validators
= [ ];
1224 field
.form
.cbi_validators
.push(validator
);
1226 cbi_bind(field
, "blur", validator
);
1227 cbi_bind(field
, "keyup", validator
);
1229 if (field
.nodeName
== 'SELECT')
1231 cbi_bind(field
, "change", validator
);
1232 cbi_bind(field
, "click", validator
);
1235 field
.setAttribute("cbi_validate", validator
);
1236 field
.setAttribute("cbi_datatype", type
);
1237 field
.setAttribute("cbi_optional", (!!optional
).toString());
1241 var fcbox
= document
.getElementById('cbi.combobox.' + field
.id
);
1243 cbi_validate_field(fcbox
, optional
, type
);
1247 function cbi_row_swap(elem
, up
, store
)
1249 var tr
= findParent(elem
.parentNode
, '.cbi-section-table-row');
1254 tr
.classList
.remove('flash');
1257 var prev
= tr
.previousElementSibling
;
1259 if (prev
&& prev
.classList
.contains('cbi-section-table-row'))
1260 tr
.parentNode
.insertBefore(tr
, prev
);
1265 var next
= tr
.nextElementSibling
? tr
.nextElementSibling
.nextElementSibling
: null;
1267 if (next
&& next
.classList
.contains('cbi-section-table-row'))
1268 tr
.parentNode
.insertBefore(tr
, next
);
1270 tr
.parentNode
.appendChild(tr
);
1277 for (var i
= 0, n
= 0; i
< tr
.parentNode
.childNodes
.length
; i
++) {
1278 var node
= tr
.parentNode
.childNodes
[i
];
1279 if (node
.classList
&& node
.classList
.contains('cbi-section-table-row')) {
1280 node
.classList
.remove('cbi-rowstyle-1');
1281 node
.classList
.remove('cbi-rowstyle-2');
1282 node
.classList
.add((n
++ % 2) ? 'cbi-rowstyle-2' : 'cbi-rowstyle-1');
1284 if (/-([^\-]+)$/.test(node
.id
))
1285 ids
.push(RegExp
.$1);
1289 var input
= document
.getElementById(store
);
1291 input
.value
= ids
.join(' ');
1293 window
.scrollTo(0, tr
.offsetTop
);
1294 window
.setTimeout(function() { tr
.classList
.add('flash'); }, 1);
1299 function cbi_tag_last(container
)
1303 for (var i
= 0; i
< container
.childNodes
.length
; i
++)
1305 var c
= container
.childNodes
[i
];
1306 if (c
.nodeType
== 1 && c
.nodeName
.toLowerCase() == 'div')
1308 c
.className
= c
.className
.replace(/ cbi
-value
-last
$/, '');
1315 last
.className
+= ' cbi-value-last';
1319 function cbi_submit(elem
, name
, value
, action
)
1321 var form
= elem
.form
|| findParent(elem
, 'form');
1327 form
.action
= action
;
1330 var hidden
= form
.querySelector('input[type="hidden"][name="%s"]'.format(name
)) ||
1331 E('input', { type
: 'hidden', name
: name
});
1333 hidden
.value
= value
|| '1';
1334 form
.appendChild(hidden
);
1341 String
.prototype.format = function()
1346 var html_esc
= [/&/g, '&', /"/g, '"', /'/g, ''', /</g, '<', />/g, '>'];
1347 var quot_esc = [/"/g
, '"', /'/g, '''];
1349 function esc(s, r) {
1350 if (typeof(s) !== 'string
' && !(s instanceof String))
1353 for( var i = 0; i < r.length; i += 2 )
1354 s = s.replace(r[i], r[i+1]);
1360 var re = /^(([^%]*)%('.|0|\x20)?(-)?(\d
+)?(\.\d
+)?(%|b
|c
|d
|u
|f
|o
|s
|x
|X
|q
|h
|j
|t
|m
))/;
1361 var a
= b
= [], numSubstitutions
= 0, numMatches
= 0;
1363 while (a
= re
.exec(str
))
1366 var leftpart
= a
[2], pPad
= a
[3], pJustify
= a
[4], pMinLength
= a
[5];
1367 var pPrecision
= a
[6], pType
= a
[7];
1377 if (numSubstitutions
< arguments
.length
)
1379 var param
= arguments
[numSubstitutions
++];
1382 if (pPad
&& pPad
.substr(0,1) == "'")
1383 pad
= leftpart
.substr(1,1);
1389 var justifyRight
= true;
1390 if (pJustify
&& pJustify
=== "-")
1391 justifyRight
= false;
1395 minLength
= +pMinLength
;
1398 if (pPrecision
&& pType
== 'f')
1399 precision
= +pPrecision
.substring(1);
1406 subst
= Math
.floor(+param
|| 0).toString(2);
1410 subst
= String
.fromCharCode(+param
|| 0);
1414 subst
= Math
.floor(+param
|| 0).toFixed(0);
1418 var n
= +param
|| 0;
1419 subst
= Math
.floor((n
< 0) ? 0x100000000 + n
: n
).toFixed(0);
1423 subst
= (precision
> -1)
1424 ? ((+param
|| 0.0)).toFixed(precision
)
1429 subst
= Math
.floor(+param
|| 0).toString(8);
1437 subst
= Math
.floor(+param
|| 0).toString(16).toLowerCase();
1441 subst
= Math
.floor(+param
|| 0).toString(16).toUpperCase();
1445 subst
= esc(param
, html_esc
);
1449 subst
= esc(param
, quot_esc
);
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
.format = function()
1521 for (var i
= 1; i
< arguments
.length
; i
++)
1522 a
.push(arguments
[i
]);
1523 return ''.format
.apply(arguments
[0], a
);
1526 String
.nobr = function()
1529 for (var i
= 1; i
< arguments
.length
; i
++)
1530 a
.push(arguments
[i
]);
1531 return ''.nobr
.apply(arguments
[0], a
);
1534 if (window
.NodeList
&& !NodeList
.prototype.forEach
) {
1535 NodeList
.prototype.forEach = function (callback
, thisArg
) {
1536 thisArg
= thisArg
|| window
;
1537 for (var i
= 0; i
< this.length
; i
++) {
1538 callback
.call(thisArg
, this[i
], i
, this);
1544 var dummyElem
, domParser
;
1548 return (typeof(e
) === 'object' && e
!== null && 'nodeType' in e
);
1556 domParser
= domParser
|| new DOMParser();
1557 elem
= domParser
.parseFromString(s
, 'text/html').body
.firstChild
;
1563 dummyElem
= dummyElem
|| document
.createElement('div');
1564 dummyElem
.innerHTML
= s
;
1565 elem
= dummyElem
.firstChild
;
1570 return elem
|| null;
1573 function findParent(node
, selector
)
1576 if (node
.msMatchesSelector
&& node
.msMatchesSelector(selector
))
1578 else if (node
.matches
&& node
.matches(selector
))
1581 node
= node
.parentNode
;
1588 var html
= arguments
[0],
1589 attr
= (arguments
[1] instanceof Object
&& !Array
.isArray(arguments
[1])) ? arguments
[1] : null,
1590 data
= attr
? arguments
[2] : arguments
[1],
1595 else if (html
.charCodeAt(0) === 60)
1596 elem
= toElem(html
);
1598 elem
= document
.createElement(html
);
1604 for (var key
in attr
)
1605 if (attr
.hasOwnProperty(key
) && attr
[key
] !== null && attr
[key
] !== undefined)
1606 elem
.setAttribute(key
, attr
[key
]);
1608 if (typeof(data
) === 'function')
1612 elem
.appendChild(data
);
1614 else if (Array
.isArray(data
)) {
1615 for (var i
= 0; i
< data
.length
; i
++)
1616 if (isElem(data
[i
]))
1617 elem
.appendChild(data
[i
]);
1619 elem
.appendChild(document
.createTextNode('' + data
[i
]));
1621 else if (data
!== null && data
!== undefined) {
1622 elem
.innerHTML
= '' + data
;
1628 if (typeof(window
.CustomEvent
) !== 'function') {
1629 function CustomEvent(event
, params
) {
1630 params
= params
|| { bubbles
: false, cancelable
: false, detail
: undefined };
1631 var evt
= document
.createEvent('CustomEvent');
1632 evt
.initCustomEvent( event
, params
.bubbles
, params
.cancelable
, params
.detail
);
1636 CustomEvent
.prototype = window
.Event
.prototype;
1637 window
.CustomEvent
= CustomEvent
;
1641 openDropdown: function(sb
) {
1642 var st
= window
.getComputedStyle(sb
, null),
1643 ul
= sb
.querySelector('ul'),
1644 li
= ul
.querySelectorAll('li'),
1645 sel
= ul
.querySelector('[selected]'),
1646 rect
= sb
.getBoundingClientRect(),
1647 h
= sb
.clientHeight
- parseFloat(st
.paddingTop
) - parseFloat(st
.paddingBottom
),
1648 mh
= this.dropdown_items
* h
,
1649 eh
= Math
.min(mh
, li
.length
* h
);
1651 document
.querySelectorAll('.cbi-dropdown[open]').forEach(function(s
) {
1652 s
.dispatchEvent(new CustomEvent('cbi-dropdown-close', {}));
1655 ul
.style
.maxHeight
= mh
+ 'px';
1656 sb
.setAttribute('open', '');
1658 ul
.scrollTop
= sel
? Math
.max(sel
.offsetTop
- sel
.offsetHeight
, 0) : 0;
1659 ul
.querySelectorAll('[selected] input[type="checkbox"]').forEach(function(c
) {
1663 ul
.style
.top
= ul
.style
.bottom
= '';
1664 ul
.style
[((sb
.getBoundingClientRect().top
+ eh
) > window
.innerHeight
) ? 'bottom' : 'top'] = rect
.height
+ 'px';
1665 ul
.classList
.add('dropdown');
1667 var pv
= ul
.cloneNode(true);
1668 pv
.classList
.remove('dropdown');
1669 pv
.classList
.add('preview');
1671 sb
.insertBefore(pv
, ul
.nextElementSibling
);
1673 li
.forEach(function(l
) {
1674 l
.setAttribute('tabindex', 0);
1677 sb
.lastElementChild
.setAttribute('tabindex', 0);
1679 this.setFocus(sb
, sel
|| li
[0], true);
1682 closeDropdown: function(sb
, no_focus
) {
1683 if (!sb
.hasAttribute('open'))
1686 var pv
= sb
.querySelector('ul.preview'),
1687 ul
= sb
.querySelector('ul.dropdown'),
1688 li
= ul
.querySelectorAll('li');
1690 li
.forEach(function(l
) { l
.removeAttribute('tabindex'); });
1691 sb
.lastElementChild
.removeAttribute('tabindex');
1694 sb
.removeAttribute('open');
1695 sb
.style
.width
= sb
.style
.height
= '';
1697 ul
.classList
.remove('dropdown');
1700 this.setFocus(sb
, sb
);
1702 this.saveValues(sb
, ul
);
1705 toggleItem: function(sb
, li
, force_state
) {
1706 if (li
.hasAttribute('unselectable'))
1710 var cbox
= li
.querySelector('input[type="checkbox"]'),
1711 items
= li
.parentNode
.querySelectorAll('li'),
1712 label
= sb
.querySelector('ul.preview'),
1713 sel
= li
.parentNode
.querySelectorAll('[selected]').length
,
1714 more
= sb
.querySelector('.more'),
1715 ndisplay
= this.display_items
,
1718 if (li
.hasAttribute('selected')) {
1719 if (force_state
!== true) {
1720 if (sel
> 1 || this.optional
) {
1721 li
.removeAttribute('selected');
1722 cbox
.checked
= cbox
.disabled
= false;
1726 cbox
.disabled
= true;
1731 if (force_state
!== false) {
1732 li
.setAttribute('selected', '');
1733 cbox
.checked
= true;
1734 cbox
.disabled
= false;
1739 while (label
.firstElementChild
)
1740 label
.removeChild(label
.firstElementChild
);
1742 for (var i
= 0; i
< items
.length
; i
++) {
1743 items
[i
].removeAttribute('display');
1744 if (items
[i
].hasAttribute('selected')) {
1745 if (ndisplay
-- > 0) {
1746 items
[i
].setAttribute('display', n
++);
1747 label
.appendChild(items
[i
].cloneNode(true));
1749 var c
= items
[i
].querySelector('input[type="checkbox"]');
1751 c
.disabled
= (sel
== 1 && !this.optional
);
1756 sb
.setAttribute('more', '');
1758 sb
.removeAttribute('more');
1760 if (ndisplay
=== this.display_items
)
1761 sb
.setAttribute('empty', '');
1763 sb
.removeAttribute('empty');
1765 more
.innerHTML
= (ndisplay
=== this.display_items
) ? this.placeholder
: '···';
1768 var sel
= li
.parentNode
.querySelector('[selected]');
1770 sel
.removeAttribute('display');
1771 sel
.removeAttribute('selected');
1774 li
.setAttribute('display', 0);
1775 li
.setAttribute('selected', '');
1777 this.closeDropdown(sb
, true);
1780 this.saveValues(sb
, li
.parentNode
);
1783 transformItem: function(sb
, li
) {
1784 var cbox
= E('form', {}, E('input', { type
: 'checkbox', tabindex
: -1, onclick
: 'event.preventDefault()' })),
1787 while (li
.firstChild
)
1788 label
.appendChild(li
.firstChild
);
1790 li
.appendChild(cbox
);
1791 li
.appendChild(label
);
1794 saveValues: function(sb
, ul
) {
1795 var sel
= ul
.querySelectorAll('[selected]'),
1796 div
= sb
.lastElementChild
;
1798 while (div
.lastElementChild
)
1799 div
.removeChild(div
.lastElementChild
);
1801 sel
.forEach(function (s
) {
1802 div
.appendChild(E('input', {
1804 name
: s
.hasAttribute('name') ? s
.getAttribute('name') : (sb
.getAttribute('name') || ''),
1805 value
: s
.hasAttribute('data-value') ? s
.getAttribute('data-value') : s
.innerText
1812 setFocus: function(sb
, elem
, scroll
) {
1813 if (sb
&& sb
.hasAttribute
&& sb
.hasAttribute('locked-in'))
1816 document
.querySelectorAll('.focus').forEach(function(e
) {
1817 if (e
.nodeName
.toLowerCase() !== 'input') {
1818 e
.classList
.remove('focus');
1825 elem
.classList
.add('focus');
1828 elem
.parentNode
.scrollTop
= elem
.offsetTop
- elem
.parentNode
.offsetTop
;
1832 createItems: function(sb
, value
) {
1834 val
= (value
|| '').trim().split(/\s+/),
1835 ul
= sb
.querySelector('ul');
1838 val
.length
= Math
.min(val
.length
, 1);
1840 val
.forEach(function(item
) {
1841 var new_item
= null;
1843 ul
.childNodes
.forEach(function(li
) {
1844 if (li
.getAttribute
&& li
.getAttribute('data-value') === item
)
1850 tpl
= sb
.querySelector(sbox
.template
);
1853 markup
= (tpl
.textContent
|| tpl
.innerHTML
|| tpl
.firstChild
.data
).replace(/^<!--|-->$/, '').trim();
1855 markup
= '<li data-value="{{value}}">{{value}}</li>';
1857 new_item
= E(markup
.replace(/{{value}}/g, item
));
1860 sbox
.transformItem(sb
, new_item
);
1863 var old
= ul
.querySelector('li[created]');
1865 ul
.removeChild(old
);
1867 new_item
.setAttribute('created', '');
1870 new_item
= ul
.insertBefore(new_item
, ul
.lastElementChild
);
1873 sbox
.toggleItem(sb
, new_item
, true);
1874 sbox
.setFocus(sb
, new_item
, true);
1878 closeAllDropdowns: function() {
1879 document
.querySelectorAll('.cbi-dropdown[open]').forEach(function(s
) {
1880 s
.dispatchEvent(new CustomEvent('cbi-dropdown-close', {}));
1885 function cbi_dropdown_init(sb
) {
1886 if (!(this instanceof cbi_dropdown_init
))
1887 return new cbi_dropdown_init(sb
);
1889 this.multi
= sb
.hasAttribute('multiple');
1890 this.optional
= sb
.hasAttribute('optional');
1891 this.placeholder
= sb
.getAttribute('placeholder') || '---';
1892 this.display_items
= parseInt(sb
.getAttribute('display-items') || 3);
1893 this.dropdown_items
= parseInt(sb
.getAttribute('dropdown-items') || 5);
1894 this.create
= sb
.getAttribute('item-create') || '.create-item-input';
1895 this.template
= sb
.getAttribute('item-template') || 'script[type="item-template"]';
1898 ul
= sb
.querySelector('ul'),
1899 items
= ul
.querySelectorAll('li'),
1900 more
= sb
.appendChild(E('span', { class: 'more', tabindex
: -1 }, '···')),
1901 open
= sb
.appendChild(E('span', { class: 'open', tabindex
: -1 }, 'â–¾')),
1902 canary
= sb
.appendChild(E('div')),
1903 create
= sb
.querySelector(this.create
),
1904 ndisplay
= this.display_items
,
1908 for (var i
= 0; i
< items
.length
; i
++) {
1909 sbox
.transformItem(sb
, items
[i
]);
1911 if (items
[i
].hasAttribute('selected') && ndisplay
-- > 0)
1912 items
[i
].setAttribute('display', n
++);
1916 var sel
= sb
.querySelectorAll('[selected]');
1918 sel
.forEach(function(s
) {
1919 s
.removeAttribute('selected');
1922 var s
= sel
[0] || items
[0];
1924 s
.setAttribute('selected', '');
1925 s
.setAttribute('display', n
++);
1930 if (this.optional
&& !ul
.querySelector('li[data-value=""]')) {
1931 var placeholder
= E('li', { placeholder
: '' }, this.placeholder
);
1932 ul
.firstChild
? ul
.insertBefore(placeholder
, ul
.firstChild
) : ul
.appendChild(placeholder
);
1936 sbox
.saveValues(sb
, ul
);
1938 ul
.setAttribute('tabindex', -1);
1939 sb
.setAttribute('tabindex', 0);
1942 sb
.setAttribute('more', '')
1944 sb
.removeAttribute('more');
1946 if (ndisplay
=== this.display_items
)
1947 sb
.setAttribute('empty', '')
1949 sb
.removeAttribute('empty');
1951 more
.innerHTML
= (ndisplay
=== this.display_items
) ? this.placeholder
: '···';
1954 sb
.addEventListener('click', function(ev
) {
1955 if (!this.hasAttribute('open')) {
1956 if (ev
.target
.nodeName
.toLowerCase() !== 'input')
1957 sbox
.openDropdown(this);
1960 var li
= findParent(ev
.target
, 'li');
1961 if (li
&& li
.parentNode
.classList
.contains('dropdown'))
1962 sbox
.toggleItem(this, li
);
1965 ev
.preventDefault();
1966 ev
.stopPropagation();
1969 sb
.addEventListener('keydown', function(ev
) {
1970 if (ev
.target
.nodeName
.toLowerCase() === 'input')
1973 if (!this.hasAttribute('open')) {
1974 switch (ev
.keyCode
) {
1979 sbox
.openDropdown(this);
1980 ev
.preventDefault();
1985 var active
= findParent(document
.activeElement
, 'li');
1987 switch (ev
.keyCode
) {
1989 sbox
.closeDropdown(this);
1994 if (!active
.hasAttribute('selected'))
1995 sbox
.toggleItem(this, active
);
1996 sbox
.closeDropdown(this);
1997 ev
.preventDefault();
2003 sbox
.toggleItem(this, active
);
2004 ev
.preventDefault();
2009 if (active
&& active
.previousElementSibling
) {
2010 sbox
.setFocus(this, active
.previousElementSibling
);
2011 ev
.preventDefault();
2016 if (active
&& active
.nextElementSibling
) {
2017 sbox
.setFocus(this, active
.nextElementSibling
);
2018 ev
.preventDefault();
2025 sb
.addEventListener('cbi-dropdown-close', function(ev
) {
2026 sbox
.closeDropdown(this, true);
2029 if ('ontouchstart' in window
) {
2030 sb
.addEventListener('touchstart', function(ev
) { ev
.stopPropagation(); });
2031 window
.addEventListener('touchstart', sbox
.closeAllDropdowns
);
2034 sb
.addEventListener('mouseover', function(ev
) {
2035 if (!this.hasAttribute('open'))
2038 var li
= findParent(ev
.target
, 'li');
2040 if (li
.parentNode
.classList
.contains('dropdown'))
2041 sbox
.setFocus(this, li
);
2043 ev
.stopPropagation();
2047 sb
.addEventListener('focus', function(ev
) {
2048 document
.querySelectorAll('.cbi-dropdown[open]').forEach(function(s
) {
2049 if (s
!== this || this.hasAttribute('open'))
2050 s
.dispatchEvent(new CustomEvent('cbi-dropdown-close', {}));
2054 canary
.addEventListener('focus', function(ev
) {
2055 sbox
.closeDropdown(this.parentNode
);
2058 window
.addEventListener('mouseover', sbox
.setFocus
);
2059 window
.addEventListener('click', sbox
.closeAllDropdowns
);
2063 create
.addEventListener('keydown', function(ev
) {
2064 switch (ev
.keyCode
) {
2066 sbox
.createItems(sb
, this.value
);
2067 ev
.preventDefault();
2074 create
.addEventListener('focus', function(ev
) {
2075 var cbox
= findParent(this, 'li').querySelector('input[type="checkbox"]');
2076 if (cbox
) cbox
.checked
= true;
2077 sb
.setAttribute('locked-in', '');
2080 create
.addEventListener('blur', function(ev
) {
2081 var cbox
= findParent(this, 'li').querySelector('input[type="checkbox"]');
2082 if (cbox
) cbox
.checked
= false;
2083 sb
.removeAttribute('locked-in');
2086 var li
= findParent(create
, 'li');
2088 li
.setAttribute('unselectable', '');
2089 li
.addEventListener('click', function(ev
) {
2090 this.querySelector(sbox
.create
).focus();
2095 cbi_dropdown_init
.prototype = CBIDropdown
;
2097 function cbi_update_table(table
, data
, placeholder
) {
2098 target
= isElem(table
) ? table
: document
.querySelector(table
);
2100 if (!isElem(target
))
2103 target
.querySelectorAll('.tr.table-titles, .cbi-section-table-titles').forEach(function(thead
) {
2106 thead
.querySelectorAll('.th').forEach(function(th
) {
2110 if (Array
.isArray(data
)) {
2111 var n
= 0, rows
= target
.querySelectorAll('.tr');
2113 data
.forEach(function(row
) {
2114 var trow
= E('div', { 'class': 'tr' });
2116 for (var i
= 0; i
< titles
.length
; i
++) {
2117 var text
= (titles
[i
].innerText
|| '').trim();
2118 var td
= trow
.appendChild(E('div', {
2119 'class': titles
[i
].className
,
2120 'data-title': (text
!== '') ? text
: null
2123 td
.classList
.remove('th');
2124 td
.classList
.add('td');
2127 trow
.classList
.add('cbi-rowstyle-%d'.format((n
++ % 2) ? 2 : 1));
2130 target
.replaceChild(trow
, rows
[n
]);
2132 target
.appendChild(trow
);
2136 target
.removeChild(rows
[n
]);
2138 if (placeholder
&& target
.firstElementChild
=== target
.lastElementChild
) {
2139 var trow
= target
.appendChild(E('div', { 'class': 'tr placeholder' }));
2140 var td
= trow
.appendChild(E('div', { 'class': titles
[0].className
}, placeholder
));
2142 td
.classList
.remove('th');
2143 td
.classList
.add('td');
2147 thead
.parentNode
.style
.display
= 'none';
2149 thead
.parentNode
.querySelectorAll('.tr, .cbi-section-table-row').forEach(function(trow
) {
2150 if (trow
!== thead
) {
2152 trow
.querySelectorAll('.th, .td').forEach(function(td
) {
2153 if (n
< titles
.length
) {
2154 var text
= (titles
[n
++].innerText
|| '').trim();
2156 td
.setAttribute('data-title', text
);
2162 thead
.parentNode
.style
.display
= '';
2167 document
.addEventListener('DOMContentLoaded', function() {
2168 document
.querySelectorAll('.table').forEach(cbi_update_table
);