147 linhas
6,2 KiB
JavaScript
147 linhas
6,2 KiB
JavaScript
/* Styled select boxes for the plugin settings: the native select stays in the form, hidden, and a button with a list sits on top. */
|
|
(function () {
|
|
'use strict';
|
|
|
|
var openBox = null;
|
|
|
|
function close(box, focus) {
|
|
if (!box) { return; }
|
|
box.classList.remove('is-open');
|
|
box.querySelector('.interweb-ao-email-center-select__button').setAttribute('aria-expanded', 'false');
|
|
if (focus) { box.querySelector('.interweb-ao-email-center-select__button').focus(); }
|
|
if (openBox === box) { openBox = null; }
|
|
}
|
|
|
|
function label(sel) {
|
|
var o = sel.options[sel.selectedIndex];
|
|
return o ? o.text : '';
|
|
}
|
|
|
|
function paint(box, sel) {
|
|
box.querySelector('.interweb-ao-email-center-select__text').textContent = label(sel);
|
|
var items = box.querySelectorAll('.interweb-ao-email-center-select__option');
|
|
for (var i = 0; i < items.length; i++) {
|
|
var on = i === sel.selectedIndex;
|
|
items[i].classList.toggle('is-selected', on);
|
|
items[i].setAttribute('aria-selected', on ? 'true' : 'false');
|
|
}
|
|
}
|
|
|
|
function move(box, step) {
|
|
var items = box.querySelectorAll('.interweb-ao-email-center-select__option:not(.is-disabled)');
|
|
if (!items.length) { return; }
|
|
var cur = box.querySelector('.interweb-ao-email-center-select__option.is-active');
|
|
var idx = Array.prototype.indexOf.call(items, cur);
|
|
idx = idx < 0 ? 0 : Math.max(0, Math.min(items.length - 1, idx + step));
|
|
activate(box, items[idx]);
|
|
}
|
|
|
|
function activate(box, item) {
|
|
var cur = box.querySelector('.interweb-ao-email-center-select__option.is-active');
|
|
if (cur) { cur.classList.remove('is-active'); }
|
|
item.classList.add('is-active');
|
|
box.querySelector('.interweb-ao-email-center-select__list').setAttribute('aria-activedescendant', item.id);
|
|
item.scrollIntoView({ block: 'nearest' });
|
|
}
|
|
|
|
function choose(box, sel, index) {
|
|
if (sel.selectedIndex !== index) {
|
|
sel.selectedIndex = index;
|
|
sel.dispatchEvent(new Event('change', { bubbles: true }));
|
|
}
|
|
paint(box, sel);
|
|
close(box, true);
|
|
}
|
|
|
|
function open(box, sel) {
|
|
if (openBox && openBox !== box) { close(openBox, false); }
|
|
box.classList.add('is-open');
|
|
box.querySelector('.interweb-ao-email-center-select__button').setAttribute('aria-expanded', 'true');
|
|
openBox = box;
|
|
var item = box.querySelectorAll('.interweb-ao-email-center-select__option')[sel.selectedIndex];
|
|
if (item) { activate(box, item); }
|
|
}
|
|
|
|
function build(sel, n) {
|
|
if (sel.classList.contains('interweb-ao-email-center-select__native')) { return; }
|
|
var box = document.createElement('div');
|
|
box.className = 'interweb-ao-email-center-select';
|
|
var id = 'interweb-ao-email-center-select-' + n;
|
|
var btn = document.createElement('button');
|
|
btn.type = 'button';
|
|
btn.className = 'interweb-ao-email-center-select__button';
|
|
btn.setAttribute('aria-haspopup', 'listbox');
|
|
btn.setAttribute('aria-expanded', 'false');
|
|
if (sel.id) {
|
|
var lab = document.querySelector('label[for="' + sel.id + '"]');
|
|
if (lab) { btn.setAttribute('aria-label', lab.textContent); }
|
|
}
|
|
btn.innerHTML = '<span class="interweb-ao-email-center-select__text"></span><span class="interweb-ao-email-center-select__arrow" aria-hidden="true"></span>';
|
|
var list = document.createElement('ul');
|
|
list.className = 'interweb-ao-email-center-select__list';
|
|
list.setAttribute('role', 'listbox');
|
|
list.tabIndex = -1;
|
|
for (var i = 0; i < sel.options.length; i++) {
|
|
var li = document.createElement('li');
|
|
li.className = 'interweb-ao-email-center-select__option' + (sel.options[i].disabled ? ' is-disabled' : '');
|
|
li.id = id + '-' + i;
|
|
li.setAttribute('role', 'option');
|
|
li.textContent = sel.options[i].text;
|
|
li.dataset.index = String(i);
|
|
list.appendChild(li);
|
|
}
|
|
sel.parentNode.insertBefore(box, sel);
|
|
box.appendChild(sel);
|
|
box.appendChild(btn);
|
|
box.appendChild(list);
|
|
sel.classList.add('interweb-ao-email-center-select__native');
|
|
sel.tabIndex = -1;
|
|
sel.setAttribute('aria-hidden', 'true');
|
|
paint(box, sel);
|
|
|
|
btn.addEventListener('click', function () {
|
|
if (box.classList.contains('is-open')) { close(box, false); } else { open(box, sel); }
|
|
});
|
|
list.addEventListener('click', function (e) {
|
|
var li = e.target.closest('.interweb-ao-email-center-select__option');
|
|
if (li && !li.classList.contains('is-disabled')) { choose(box, sel, parseInt(li.dataset.index, 10)); }
|
|
});
|
|
list.addEventListener('mousemove', function (e) {
|
|
var li = e.target.closest('.interweb-ao-email-center-select__option');
|
|
if (li && !li.classList.contains('is-active') && !li.classList.contains('is-disabled')) { activate(box, li); }
|
|
});
|
|
btn.addEventListener('keydown', function (e) {
|
|
var isOpen = box.classList.contains('is-open');
|
|
if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
|
|
e.preventDefault();
|
|
if (!isOpen) { open(box, sel); } else { move(box, e.key === 'ArrowDown' ? 1 : -1); }
|
|
} else if (e.key === 'Enter' || e.key === ' ') {
|
|
e.preventDefault();
|
|
if (!isOpen) { open(box, sel); return; }
|
|
var cur = box.querySelector('.interweb-ao-email-center-select__option.is-active');
|
|
if (cur) { choose(box, sel, parseInt(cur.dataset.index, 10)); }
|
|
} else if (e.key === 'Escape' && isOpen) {
|
|
e.preventDefault();
|
|
close(box, true);
|
|
} else if (e.key === 'Tab' && isOpen) {
|
|
close(box, false);
|
|
} else if (e.key.length === 1 && isOpen) {
|
|
var items = box.querySelectorAll('.interweb-ao-email-center-select__option:not(.is-disabled)');
|
|
for (var j = 0; j < items.length; j++) {
|
|
if (items[j].textContent.toLowerCase().indexOf(e.key.toLowerCase()) === 0) { activate(box, items[j]); break; }
|
|
}
|
|
}
|
|
});
|
|
sel.addEventListener('change', function () { paint(box, sel); });
|
|
}
|
|
|
|
document.addEventListener('click', function (e) {
|
|
if (openBox && !openBox.contains(e.target)) { close(openBox, false); }
|
|
});
|
|
|
|
function init() {
|
|
var sels = document.querySelectorAll('select[data-interweb-ao-email-center-select]');
|
|
for (var i = 0; i < sels.length; i++) { build(sels[i], i); }
|
|
}
|
|
if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', init); } else { init(); }
|
|
})();
|