2020-07-08 16:40:30 +01:00
|
|
|
import layoutManager from 'layoutManager';
|
|
|
|
import browser from 'browser';
|
|
|
|
import actionsheet from 'actionsheet';
|
|
|
|
import 'css!./emby-select';
|
|
|
|
import 'registerElement';
|
|
|
|
|
|
|
|
/* eslint-disable indent */
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-07-11 09:52:35 +01:00
|
|
|
const EmbySelectPrototype = Object.create(HTMLSelectElement.prototype);
|
2018-10-23 01:05:09 +03:00
|
|
|
|
|
|
|
function enableNativeMenu() {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
if (browser.edgeUwp || browser.xboxOne) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Doesn't seem to work at all
|
|
|
|
if (browser.tizen || browser.orsay || browser.web0s) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Take advantage of the native input methods
|
|
|
|
if (browser.tv) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (layoutManager.tv) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function triggerChange(select) {
|
2020-07-11 09:52:35 +01:00
|
|
|
const evt = document.createEvent('HTMLEvents');
|
2020-05-04 12:44:12 +02:00
|
|
|
evt.initEvent('change', false, true);
|
2019-01-10 15:39:37 +03:00
|
|
|
select.dispatchEvent(evt);
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function setValue(select, value) {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
select.value = value;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function showActionSheet(select) {
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-07-11 09:52:35 +01:00
|
|
|
const labelElem = getLabel(select);
|
|
|
|
const title = labelElem ? (labelElem.textContent || labelElem.innerText) : null;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2018-10-23 01:05:09 +03:00
|
|
|
actionsheet.show({
|
|
|
|
items: select.options,
|
|
|
|
positionTo: select,
|
|
|
|
title: title
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
}).then(function (value) {
|
|
|
|
setValue(select, value);
|
|
|
|
triggerChange(select);
|
|
|
|
});
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function getLabel(select) {
|
2020-07-11 09:52:35 +01:00
|
|
|
let elem = select.previousSibling;
|
2019-01-10 15:39:37 +03:00
|
|
|
while (elem && elem.tagName !== 'LABEL') {
|
|
|
|
elem = elem.previousSibling;
|
|
|
|
}
|
|
|
|
return elem;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function onFocus(e) {
|
2020-07-11 09:52:35 +01:00
|
|
|
const label = getLabel(this);
|
2019-01-10 15:39:37 +03:00
|
|
|
if (label) {
|
|
|
|
label.classList.add('selectLabelFocused');
|
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function onBlur(e) {
|
2020-07-11 09:52:35 +01:00
|
|
|
const label = getLabel(this);
|
2019-01-10 15:39:37 +03:00
|
|
|
if (label) {
|
|
|
|
label.classList.remove('selectLabelFocused');
|
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function onMouseDown(e) {
|
2019-01-10 15:39:37 +03:00
|
|
|
// e.button=0 for primary (left) mouse button click
|
|
|
|
if (!e.button && !enableNativeMenu()) {
|
|
|
|
e.preventDefault();
|
|
|
|
showActionSheet(this);
|
|
|
|
}
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function onKeyDown(e) {
|
|
|
|
switch (e.keyCode) {
|
|
|
|
case 13:
|
2019-01-10 15:39:37 +03:00
|
|
|
if (!enableNativeMenu()) {
|
|
|
|
e.preventDefault();
|
|
|
|
showActionSheet(this);
|
|
|
|
}
|
|
|
|
return;
|
2018-10-23 01:05:09 +03:00
|
|
|
case 37:
|
|
|
|
case 38:
|
|
|
|
case 39:
|
|
|
|
case 40:
|
2019-01-10 15:39:37 +03:00
|
|
|
if (layoutManager.tv) {
|
|
|
|
e.preventDefault();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
break;
|
2018-10-23 01:05:09 +03:00
|
|
|
}
|
|
|
|
}
|
2019-01-10 15:39:37 +03:00
|
|
|
|
2020-07-11 09:52:35 +01:00
|
|
|
let inputId = 0;
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
EmbySelectPrototype.createdCallback = function () {
|
|
|
|
|
|
|
|
if (!this.id) {
|
|
|
|
this.id = 'embyselect' + inputId;
|
|
|
|
inputId++;
|
|
|
|
}
|
|
|
|
|
2019-06-02 02:43:05 +03:00
|
|
|
this.classList.add('emby-select-withcolor');
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
if (layoutManager.tv) {
|
|
|
|
this.classList.add('emby-select-focusscale');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.addEventListener('mousedown', onMouseDown);
|
|
|
|
this.addEventListener('keydown', onKeyDown);
|
|
|
|
|
|
|
|
this.addEventListener('focus', onFocus);
|
|
|
|
this.addEventListener('blur', onBlur);
|
|
|
|
};
|
|
|
|
|
|
|
|
EmbySelectPrototype.attachedCallback = function () {
|
|
|
|
|
|
|
|
if (this.classList.contains('emby-select')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.classList.add('emby-select');
|
|
|
|
|
2020-07-11 09:52:35 +01:00
|
|
|
const label = this.ownerDocument.createElement('label');
|
2019-01-10 15:39:37 +03:00
|
|
|
label.innerHTML = this.getAttribute('label') || '';
|
|
|
|
label.classList.add('selectLabel');
|
|
|
|
label.htmlFor = this.id;
|
|
|
|
this.parentNode.insertBefore(label, this);
|
|
|
|
|
|
|
|
if (this.classList.contains('emby-select-withcolor')) {
|
2020-04-26 02:37:28 +03:00
|
|
|
this.parentNode.insertAdjacentHTML('beforeend', '<div class="selectArrowContainer"><div style="visibility:hidden;display:none;">0</div><span class="selectArrow material-icons keyboard_arrow_down"></span></div>');
|
2019-01-10 15:39:37 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
EmbySelectPrototype.setLabel = function (text) {
|
|
|
|
|
2020-07-11 09:52:35 +01:00
|
|
|
const label = this.parentNode.querySelector('label');
|
2019-01-10 15:39:37 +03:00
|
|
|
|
|
|
|
label.innerHTML = text;
|
|
|
|
};
|
|
|
|
|
|
|
|
document.registerElement('emby-select', {
|
2018-10-23 01:05:09 +03:00
|
|
|
prototype: EmbySelectPrototype,
|
2019-01-10 15:39:37 +03:00
|
|
|
extends: 'select'
|
|
|
|
});
|
2020-07-08 16:40:30 +01:00
|
|
|
|
|
|
|
/* eslint-enable indent */
|