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

170 lines
4.4 KiB
JavaScript
Raw Normal View History

2016-06-09 23:54:03 -07:00
define(['layoutManager', 'browser', 'actionsheet', 'css!./emby-select', 'registerElement'], function (layoutManager, browser, actionsheet) {
2016-10-05 21:28:10 -07:00
'use strict';
2016-05-21 19:28:47 -07:00
var EmbySelectPrototype = Object.create(HTMLSelectElement.prototype);
function enableNativeMenu() {
if (browser.edgeUwp || browser.xboxOne) {
return true;
2016-05-24 09:58:12 -07:00
}
2016-06-25 13:18:23 -07:00
// Doesn't seem to work at all
if (browser.tizen) {
return false;
}
2016-05-21 19:28:47 -07:00
// Take advantage of the native input methods
if (browser.tv) {
return true;
}
if (layoutManager.tv) {
return false;
}
return true;
}
2016-05-21 21:17:28 -07:00
function triggerChange(select) {
var evt = document.createEvent("HTMLEvents");
evt.initEvent("change", false, true);
select.dispatchEvent(evt);
}
2016-05-23 10:54:38 -07:00
function setValue(select, value) {
select.value = value;
}
function showActionSheet(select) {
2016-05-21 19:28:47 -07:00
2016-05-21 21:17:28 -07:00
var labelElem = getLabel(select);
var title = labelElem ? (labelElem.textContent || labelElem.innerText) : null;
2016-05-21 19:28:47 -07:00
actionsheet.show({
items: select.options,
2016-05-21 21:17:28 -07:00
positionTo: select,
title: title
2016-05-21 19:28:47 -07:00
}).then(function (value) {
2016-05-23 10:54:38 -07:00
setValue(select, value);
2016-05-21 21:17:28 -07:00
triggerChange(select);
2016-05-21 19:28:47 -07:00
});
}
function getLabel(select) {
var elem = select.previousSibling;
2016-10-05 21:28:10 -07:00
while (elem && elem.tagName !== 'LABEL') {
2016-05-21 19:28:47 -07:00
elem = elem.previousSibling;
}
return elem;
}
function onFocus(e) {
var label = getLabel(this);
if (label) {
2016-05-21 21:17:28 -07:00
label.classList.add('selectLabelFocused');
label.classList.remove('selectLabelUnfocused');
2016-05-21 19:28:47 -07:00
}
}
function onBlur(e) {
var label = getLabel(this);
if (label) {
2016-05-21 21:17:28 -07:00
label.classList.add('selectLabelUnfocused');
label.classList.remove('selectLabelFocused');
2016-05-21 19:28:47 -07:00
}
}
function onMouseDown(e) {
2016-05-21 21:17:28 -07:00
// e.button=0 for primary (left) mouse button click
if (!e.button && !enableNativeMenu()) {
2016-05-21 19:28:47 -07:00
e.preventDefault();
showActionSheet(this);
2016-05-21 19:28:47 -07:00
}
}
function onKeyDown(e) {
switch (e.keyCode) {
case 13:
if (!enableNativeMenu()) {
e.preventDefault();
showActionSheet(this);
2016-05-21 19:28:47 -07:00
}
return;
case 37:
case 38:
case 39:
case 40:
if (layoutManager.tv) {
e.preventDefault();
}
return;
default:
break;
}
}
2016-06-03 12:32:10 -07:00
var inputId = 0;
2016-05-21 19:28:47 -07:00
EmbySelectPrototype.createdCallback = function () {
if (!this.id) {
2016-06-03 12:32:10 -07:00
this.id = 'embyselect' + inputId;
inputId++;
2016-05-21 19:28:47 -07:00
}
2016-05-26 12:20:24 -07:00
2016-05-21 19:28:47 -07:00
this.addEventListener('mousedown', onMouseDown);
this.addEventListener('keydown', onKeyDown);
2016-06-25 13:18:23 -07:00
2016-05-21 19:28:47 -07:00
this.addEventListener('focus', onFocus);
2016-05-23 20:06:51 -07:00
this.addEventListener('blur', onBlur);
2016-05-21 19:28:47 -07:00
};
EmbySelectPrototype.attachedCallback = function () {
2016-08-02 18:32:16 -07:00
if (this.classList.contains('emby-select')) {
return;
2016-05-26 12:20:24 -07:00
}
2016-08-02 18:32:16 -07:00
this.classList.add('emby-select');
var label = this.ownerDocument.createElement('label');
label.innerHTML = this.getAttribute('label') || '';
label.classList.add('selectLabel');
label.classList.add('selectLabelUnfocused');
label.htmlFor = this.id;
this.parentNode.insertBefore(label, this);
var div = document.createElement('div');
div.classList.add('emby-select-selectionbar');
this.parentNode.insertBefore(div, this.nextSibling);
var arrowContainer = document.createElement('div');
arrowContainer.classList.add('selectArrowContainer');
arrowContainer.innerHTML = '<div style="visibility:hidden;">0</div>';
this.parentNode.appendChild(arrowContainer);
var arrow = document.createElement('i');
arrow.classList.add('md-icon');
arrow.classList.add('selectArrow');
arrow.innerHTML = '&#xE313;';
arrowContainer.appendChild(arrow);
2016-05-21 19:28:47 -07:00
};
2016-08-26 12:31:56 -07:00
EmbySelectPrototype.setLabel = function (text) {
var label = this.parentNode.querySelector('label');
label.innerHTML = text;
};
2016-05-21 19:28:47 -07:00
document.registerElement('emby-select', {
prototype: EmbySelectPrototype,
extends: 'select'
});
});