2016-05-21 19:28:47 -07:00
|
|
|
|
define(['layoutManager', 'browser', 'actionsheet', 'css!./emby-select'], function (layoutManager, browser, actionsheet) {
|
|
|
|
|
|
|
|
|
|
var EmbySelectPrototype = Object.create(HTMLSelectElement.prototype);
|
|
|
|
|
|
|
|
|
|
function enableNativeMenu() {
|
|
|
|
|
|
|
|
|
|
// 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-21 19:28:47 -07:00
|
|
|
|
function showActionSheeet(select) {
|
|
|
|
|
|
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) {
|
|
|
|
|
select.value = 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;
|
|
|
|
|
while (elem && elem.tagName != 'LABEL') {
|
|
|
|
|
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();
|
|
|
|
|
showActionSheeet(this);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function onKeyDown(e) {
|
|
|
|
|
|
|
|
|
|
switch (e.keyCode) {
|
|
|
|
|
|
|
|
|
|
case 13:
|
|
|
|
|
if (!enableNativeMenu()) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
showActionSheeet(this);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
case 37:
|
|
|
|
|
case 38:
|
|
|
|
|
case 39:
|
|
|
|
|
case 40:
|
|
|
|
|
if (layoutManager.tv) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EmbySelectPrototype.createdCallback = function () {
|
|
|
|
|
|
|
|
|
|
if (!this.id) {
|
|
|
|
|
this.id = 'select' + new Date().getTime();
|
|
|
|
|
}
|
|
|
|
|
this.addEventListener('mousedown', onMouseDown);
|
|
|
|
|
this.addEventListener('keydown', onKeyDown);
|
|
|
|
|
this.addEventListener('focus', onFocus);
|
|
|
|
|
this.addEventListener('keydown', onBlur);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
EmbySelectPrototype.attachedCallback = function () {
|
|
|
|
|
|
|
|
|
|
var label = this.ownerDocument.createElement('label');
|
|
|
|
|
label.innerHTML = this.getAttribute('label') || '';
|
|
|
|
|
label.classList.add('selectLabel');
|
|
|
|
|
label.htmlFor = this.id;
|
|
|
|
|
this.parentNode.insertBefore(label, this);
|
|
|
|
|
|
|
|
|
|
var div = document.createElement('div');
|
|
|
|
|
div.classList.add('emby-select-selectionbar');
|
|
|
|
|
div.innerHTML = '<div class="emby-select-selectionbarInner"></div>';
|
|
|
|
|
this.parentNode.insertBefore(div, this.nextSibling);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
document.registerElement('emby-select', {
|
|
|
|
|
prototype: EmbySelectPrototype,
|
|
|
|
|
extends: 'select'
|
|
|
|
|
});
|
|
|
|
|
});
|