1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/src/elements/emby-select/emby-select.js

153 lines
3.7 KiB
JavaScript
Raw Normal View History

2020-08-14 08:46:34 +02:00
import layoutManager from '../../components/layoutManager';
import browser from '../../scripts/browser';
import actionsheet from '../../components/actionSheet/actionSheet';
2021-01-26 15:42:02 -05:00
import './emby-select.scss';
2020-09-08 02:43:56 -04:00
import 'webcomponents.js/webcomponents-lite';
2023-04-19 01:56:05 -04:00
const EmbySelectPrototype = Object.create(HTMLSelectElement.prototype);
2023-04-19 01:56:05 -04:00
function enableNativeMenu() {
if (browser.edgeUwp || browser.xboxOne) {
return true;
2018-10-23 01:05:09 +03:00
}
2023-04-19 01:56:05 -04:00
// Doesn't seem to work at all
if (browser.tizen || browser.orsay || browser.web0s) {
return false;
2018-10-23 01:05:09 +03:00
}
2023-04-19 01:56:05 -04:00
// Take advantage of the native input methods
if (browser.tv) {
return true;
2018-10-23 01:05:09 +03:00
}
2023-04-19 01:56:05 -04:00
return !layoutManager.tv;
}
2023-04-19 01:56:05 -04:00
function triggerChange(select) {
const evt = document.createEvent('HTMLEvents');
evt.initEvent('change', false, true);
select.dispatchEvent(evt);
}
2023-04-19 01:56:05 -04:00
function setValue(select, value) {
select.value = value;
}
2018-10-23 01:05:09 +03:00
2023-04-19 01:56:05 -04:00
function showActionSheet(select) {
const labelElem = getLabel(select);
const title = labelElem ? (labelElem.textContent || labelElem.innerText) : null;
actionsheet.show({
items: select.options,
positionTo: select,
title: title
}).then(function (value) {
setValue(select, value);
triggerChange(select);
});
}
2018-10-23 01:05:09 +03:00
2023-04-19 01:56:05 -04:00
function getLabel(select) {
let elem = select.previousSibling;
while (elem && elem.tagName !== 'LABEL') {
elem = elem.previousSibling;
2018-10-23 01:05:09 +03:00
}
2023-04-19 01:56:05 -04:00
return elem;
}
2018-10-23 01:05:09 +03:00
2023-04-19 01:56:05 -04:00
function onFocus() {
const label = getLabel(this);
if (label) {
label.classList.add('selectLabelFocused');
2018-10-23 01:05:09 +03:00
}
2023-04-19 01:56:05 -04:00
}
2018-10-23 01:05:09 +03:00
2023-04-19 01:56:05 -04:00
function onBlur() {
const label = getLabel(this);
if (label) {
label.classList.remove('selectLabelFocused');
2018-10-23 01:05:09 +03:00
}
2023-04-19 01:56:05 -04:00
}
2018-10-23 01:05:09 +03:00
2023-04-19 01:56:05 -04:00
function onMouseDown(e) {
// 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
}
2023-04-19 01:56:05 -04:00
}
function onKeyDown(e) {
switch (e.keyCode) {
case 13:
if (!enableNativeMenu()) {
e.preventDefault();
showActionSheet(this);
}
return;
case 37:
case 38:
case 39:
case 40:
if (layoutManager.tv) {
e.preventDefault();
}
return;
default:
break;
}
}
2023-04-19 01:56:05 -04:00
let inputId = 0;
2023-04-19 01:56:05 -04:00
EmbySelectPrototype.createdCallback = function () {
if (!this.id) {
this.id = 'embyselect' + inputId;
inputId++;
}
2023-04-19 01:56:05 -04:00
this.classList.add('emby-select-withcolor');
2023-04-19 01:56:05 -04:00
if (layoutManager.tv) {
this.classList.add('emby-select-focusscale');
}
2023-04-19 01:56:05 -04:00
this.addEventListener('mousedown', onMouseDown);
this.addEventListener('keydown', onKeyDown);
2023-04-19 01:56:05 -04:00
this.addEventListener('focus', onFocus);
this.addEventListener('blur', onBlur);
};
2023-04-19 01:56:05 -04:00
EmbySelectPrototype.attachedCallback = function () {
if (this.classList.contains('emby-select')) {
return;
}
2023-04-19 01:56:05 -04:00
this.classList.add('emby-select');
2023-04-19 01:56:05 -04:00
const label = this.ownerDocument.createElement('label');
label.innerText = this.getAttribute('label') || '';
label.classList.add('selectLabel');
label.htmlFor = this.id;
this.parentNode?.insertBefore(label, this);
2023-04-19 01:56:05 -04:00
if (this.classList.contains('emby-select-withcolor')) {
this.parentNode?.insertAdjacentHTML('beforeend', '<div class="selectArrowContainer"><div style="visibility:hidden;display:none;">0</div><span class="selectArrow material-icons keyboard_arrow_down" aria-hidden="true"></span></div>');
}
};
2023-04-19 01:56:05 -04:00
EmbySelectPrototype.setLabel = function (text) {
const label = this.parentNode?.querySelector('label');
2023-04-19 01:56:05 -04:00
label.innerText = text;
};
2023-04-19 01:56:05 -04:00
document.registerElement('emby-select', {
prototype: EmbySelectPrototype,
extends: 'select'
});