'class': 'cbi-dropdown',
'multiple': this.options.multiple ? '' : null,
'optional': this.options.optional ? '' : null,
- 'disabled': this.options.disabled ? '' : null
+ 'disabled': this.options.disabled ? '' : null,
+ 'tabindex': -1
}, E('ul'));
var keys = Object.keys(this.choices);
}
else {
sb.addEventListener('mouseover', this.handleMouseover.bind(this));
+ sb.addEventListener('mouseout', this.handleMouseout.bind(this));
sb.addEventListener('focus', this.handleFocus.bind(this));
canary.addEventListener('focus', this.handleCanaryFocus.bind(this));
- window.addEventListener('mouseover', this.setFocus);
window.addEventListener('click', this.closeAllDropdowns);
}
sb.lastElementChild.setAttribute('tabindex', 0);
- this.setFocus(sb, sel || li[0], true);
+ var focusFn = L.bind(function(el) {
+ this.setFocus(sb, el, true);
+ ul.removeEventListener('transitionend', focusFn);
+ }, this, sel || li[0]);
+
+ ul.addEventListener('transitionend', focusFn);
},
/** @private */
/** @private */
setFocus: function(sb, elem, scroll) {
- if (sb && sb.hasAttribute && sb.hasAttribute('locked-in'))
+ if (sb.hasAttribute('locked-in'))
return;
- if (sb.target && findParent(sb.target, 'ul.dropdown'))
+ sb.querySelectorAll('.focus').forEach(function(e) {
+ e.classList.remove('focus');
+ });
+
+ elem.classList.add('focus');
+
+ if (scroll)
+ elem.parentNode.scrollTop = elem.offsetTop - elem.parentNode.offsetTop;
+
+ elem.focus();
+ },
+
+ /** @private */
+ handleMouseout: function(ev) {
+ var sb = ev.currentTarget;
+
+ if (!sb.hasAttribute('open'))
return;
- document.querySelectorAll('.focus').forEach(function(e) {
- if (!matchesElem(e, 'input')) {
- e.classList.remove('focus');
- e.blur();
- }
+ sb.querySelectorAll('.focus').forEach(function(e) {
+ e.classList.remove('focus');
});
- if (elem) {
- elem.focus();
- elem.classList.add('focus');
-
- if (scroll)
- elem.parentNode.scrollTop = elem.offsetTop - elem.parentNode.offsetTop;
- }
+ sb.querySelector('ul.dropdown').focus();
},
/** @private */
/** @private */
handleKeydown: function(ev) {
- var sb = ev.currentTarget;
+ var sb = ev.currentTarget,
+ ul = sb.querySelector('ul.dropdown');
if (matchesElem(ev.target, 'input'))
return;
switch (ev.keyCode) {
case 27:
this.closeDropdown(sb);
+ ev.stopPropagation();
break;
case 13:
this.setFocus(sb, active.previousElementSibling);
ev.preventDefault();
}
+ else if (document.activeElement === ul) {
+ this.setFocus(sb, ul.lastElementChild);
+ ev.preventDefault();
+ }
break;
case 40:
this.setFocus(sb, active.nextElementSibling);
ev.preventDefault();
}
+ else if (document.activeElement === ul) {
+ this.setFocus(sb, ul.firstElementChild);
+ ev.preventDefault();
+ }
break;
}
}