jellyfin-web/dashboard-ui/bower_components/emby-webcomponents/alphapicker/alphapicker.js

283 lines
8.5 KiB
JavaScript
Raw Normal View History

2016-08-03 10:26:42 -07:00
define(['focusManager', 'css!./style.css', 'paper-icon-button-light', 'material-icons'], function (focusManager) {
2016-10-01 23:46:32 -07:00
'use strict';
2016-06-06 22:42:26 -07:00
2016-08-02 21:30:22 -07:00
var selectedButtonClass = 'alphaPickerButton-selected';
2016-06-06 22:42:26 -07:00
function focus() {
2016-10-01 23:46:32 -07:00
var scope = this;
var selected = scope.querySelector('.' + selectedButtonClass);
2016-06-06 22:42:26 -07:00
if (selected) {
focusManager.focus(selected);
} else {
2016-10-01 23:46:32 -07:00
focusManager.autoFocus(scope, true);
2016-06-06 22:42:26 -07:00
}
}
function getLetterButton(l) {
2016-08-03 10:26:42 -07:00
return '<button data-value="' + l + '" class="alphaPickerButton">' + l + '</button>';
2016-06-06 22:42:26 -07:00
}
function render(element, options) {
element.classList.add('alphaPicker');
element.classList.add('focuscontainer-x');
var html = '';
var letters;
html += '<div class="alphaPickerRow">';
2016-10-01 23:46:32 -07:00
if (options.mode === 'keyboard') {
2016-07-06 10:44:44 -07:00
// space_bar icon
2016-10-01 23:46:32 -07:00
html += '<button data-value=" " is="paper-icon-button-light" class="alphaPickerButton autoSize"><i class="md-icon alphaPickerButtonIcon">&#xE256;</i></button>';
2016-06-06 22:42:26 -07:00
} else {
letters = ['#'];
html += letters.map(getLetterButton).join('');
}
letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
html += letters.map(getLetterButton).join('');
2016-10-01 23:46:32 -07:00
if (options.mode === 'keyboard') {
2016-07-06 10:44:44 -07:00
// backspace icon
2016-10-01 23:46:32 -07:00
html += '<button data-value="backspace" is="paper-icon-button-light" class="alphaPickerButton autoSize"><i class="md-icon alphaPickerButtonIcon">&#xE14A;</i></button>';
2016-06-06 22:42:26 -07:00
html += '</div>';
letters = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
html += '<div class="alphaPickerRow">';
html += '<br/>';
html += letters.map(getLetterButton).join('');
html += '</div>';
} else {
html += '</div>';
}
element.innerHTML = html;
element.classList.add('focusable');
element.focus = focus;
}
2016-10-01 23:46:32 -07:00
function AlphaPicker(options) {
2016-06-06 22:42:26 -07:00
var self = this;
var element = options.element;
var itemsContainer = options.itemsContainer;
var itemClass = options.itemClass;
var itemFocusValue;
var itemFocusTimeout;
function onItemFocusTimeout() {
itemFocusTimeout = null;
self.value(itemFocusValue);
}
var alphaFocusedElement;
var alphaFocusTimeout;
function onAlphaFocusTimeout() {
alphaFocusTimeout = null;
2016-10-01 23:46:32 -07:00
if (document.activeElement === alphaFocusedElement) {
2016-06-06 22:42:26 -07:00
var value = alphaFocusedElement.getAttribute('data-value');
self.value(value, true);
}
}
function parentWithClass(elem, className) {
while (!elem.classList || !elem.classList.contains(className)) {
elem = elem.parentNode;
if (!elem) {
return null;
}
}
return elem;
}
2016-06-07 22:24:25 -07:00
function onAlphaPickerInKeyboardModeClick(e) {
2016-06-06 22:42:26 -07:00
var alphaPickerButton = parentWithClass(e.target, 'alphaPickerButton');
if (alphaPickerButton) {
var value = alphaPickerButton.getAttribute('data-value');
element.dispatchEvent(new CustomEvent("alphavalueclicked", {
2016-08-07 12:43:52 -07:00
cancelable: false,
2016-06-06 22:42:26 -07:00
detail: {
value: value
}
}));
}
}
2016-06-07 22:24:25 -07:00
function onAlphaPickerClick(e) {
var alphaPickerButton = parentWithClass(e.target, 'alphaPickerButton');
if (alphaPickerButton) {
var value = alphaPickerButton.getAttribute('data-value');
2016-10-01 23:46:32 -07:00
if (currentValue === value.toUpperCase()) {
2016-06-07 22:24:25 -07:00
self.value(null, true);
} else {
self.value(value, true);
}
}
}
2016-06-06 22:42:26 -07:00
function onAlphaPickerFocusIn(e) {
if (alphaFocusTimeout) {
clearTimeout(alphaFocusTimeout);
alphaFocusTimeout = null;
}
var alphaPickerButton = parentWithClass(e.target, 'alphaPickerButton');
if (alphaPickerButton) {
alphaFocusedElement = alphaPickerButton;
alphaFocusTimeout = setTimeout(onAlphaFocusTimeout, 100);
}
}
function onItemsFocusIn(e) {
var item = parentWithClass(e.target, itemClass);
if (item) {
var prefix = item.getAttribute('data-prefix');
if (prefix && prefix.length) {
itemFocusValue = prefix[0];
if (itemFocusTimeout) {
clearTimeout(itemFocusTimeout);
}
itemFocusTimeout = setTimeout(onItemFocusTimeout, 100);
}
}
}
self.enabled = function (enabled) {
if (enabled) {
if (itemsContainer) {
itemsContainer.addEventListener('focus', onItemsFocusIn, true);
}
2016-10-01 23:46:32 -07:00
if (options.mode === 'keyboard') {
2016-06-07 22:24:25 -07:00
element.addEventListener('click', onAlphaPickerInKeyboardModeClick);
2016-06-06 22:42:26 -07:00
}
2016-06-07 22:24:25 -07:00
if (options.valueChangeEvent !== 'click') {
2016-06-06 22:42:26 -07:00
element.addEventListener('focus', onAlphaPickerFocusIn, true);
} else {
2016-06-07 22:24:25 -07:00
element.addEventListener('click', onAlphaPickerClick);
2016-06-06 22:42:26 -07:00
}
} else {
if (itemsContainer) {
itemsContainer.removeEventListener('focus', onItemsFocusIn, true);
}
2016-06-07 22:24:25 -07:00
element.removeEventListener('click', onAlphaPickerInKeyboardModeClick);
2016-06-06 22:42:26 -07:00
element.removeEventListener('focus', onAlphaPickerFocusIn, true);
2016-06-07 22:24:25 -07:00
element.removeEventListener('click', onAlphaPickerClick);
2016-06-06 22:42:26 -07:00
}
};
self.on = function (name, fn) {
element.addEventListener(name, fn);
};
self.off = function (name, fn) {
element.removeEventListener(name, fn);
};
self.destroy = function () {
self.enabled(false);
element.classList.remove('focuscontainer-x');
};
self.visible = function (visible) {
element.style.visibility = visible ? 'visible' : 'hidden';
};
var currentValue;
self.value = function (value, applyValue) {
2016-06-07 22:24:25 -07:00
var btn, selected;
2016-06-06 22:42:26 -07:00
2016-06-07 22:24:25 -07:00
if (value !== undefined) {
if (value != null) {
2016-06-06 22:42:26 -07:00
2016-06-07 22:24:25 -07:00
value = value.toUpperCase();
currentValue = value;
2016-06-06 22:42:26 -07:00
2016-10-01 23:46:32 -07:00
if (options.mode !== 'keyboard') {
2016-08-02 21:30:22 -07:00
selected = element.querySelector('.' + selectedButtonClass);
2016-06-07 22:24:25 -07:00
btn = element.querySelector('.alphaPickerButton[data-value=\'' + value + '\']');
2016-10-01 23:46:32 -07:00
if (btn && btn !== selected) {
2016-08-02 21:30:22 -07:00
btn.classList.add(selectedButtonClass);
2016-06-07 22:24:25 -07:00
}
2016-10-01 23:46:32 -07:00
if (selected && selected !== btn) {
2016-08-02 21:30:22 -07:00
selected.classList.remove(selectedButtonClass);
2016-06-07 22:24:25 -07:00
}
2016-06-06 22:42:26 -07:00
}
2016-06-07 22:24:25 -07:00
} else {
currentValue = value;
2016-08-02 21:30:22 -07:00
selected = element.querySelector('.' + selectedButtonClass);
2016-06-07 22:24:25 -07:00
if (selected) {
2016-08-02 21:30:22 -07:00
selected.classList.remove(selectedButtonClass);
2016-06-06 22:42:26 -07:00
}
}
2016-06-07 22:24:25 -07:00
}
2016-06-06 22:42:26 -07:00
2016-06-07 22:24:25 -07:00
if (applyValue) {
element.dispatchEvent(new CustomEvent("alphavaluechanged", {
2016-08-07 12:43:52 -07:00
cancelable: false,
2016-06-07 22:24:25 -07:00
detail: {
value: value
}
}));
2016-06-06 22:42:26 -07:00
}
return currentValue;
};
self.values = function () {
var elems = element.querySelectorAll('.alphaPickerButton');
var values = [];
for (var i = 0, length = elems.length; i < length; i++) {
values.push(elems[i].getAttribute('data-value'));
}
return values;
};
self.focus = function () {
focusManager.autoFocus(element, true);
};
render(element, options);
self.enabled(true);
self.visible(true);
}
2016-10-01 23:46:32 -07:00
return AlphaPicker;
2016-06-06 22:42:26 -07:00
});