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-checkbox/emby-checkbox.js

109 lines
3.3 KiB
JavaScript
Raw Normal View History

2020-08-14 08:46:34 +02:00
import browser from '../../scripts/browser';
import dom from '../../scripts/dom';
2021-01-26 15:42:02 -05:00
import './emby-checkbox.scss';
2020-09-08 02:43:56 -04:00
import 'webcomponents.js/webcomponents-lite';
2020-07-08 16:44:47 +01:00
2023-04-19 01:56:05 -04:00
const EmbyCheckboxPrototype = Object.create(HTMLInputElement.prototype);
2023-04-19 01:56:05 -04:00
function onKeyDown(e) {
// Don't submit form on enter
// Real (non-emulator) Tizen does nothing on Space
if (e.keyCode === 13 || (e.keyCode === 32 && browser.tizen)) {
e.preventDefault();
2018-10-23 01:05:09 +03:00
2023-04-19 01:56:05 -04:00
this.checked = !this.checked;
2023-04-19 01:56:05 -04:00
this.dispatchEvent(new CustomEvent('change', {
bubbles: true
}));
2020-08-12 21:06:10 +02:00
2023-04-19 01:56:05 -04:00
return false;
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-10-14 20:35:55 +05:30
const enableRefreshHack = browser.tizen || browser.orsay || browser.operaTv || browser.web0s;
2023-04-19 01:56:05 -04:00
function forceRefresh(loading) {
const elem = this.parentNode;
2023-04-19 01:56:05 -04:00
elem.style.webkitAnimationName = 'repaintChrome';
elem.style.webkitAnimationDelay = (loading === true ? '500ms' : '');
elem.style.webkitAnimationDuration = '10ms';
elem.style.webkitAnimationIterationCount = '1';
2023-04-19 01:56:05 -04:00
setTimeout(function () {
elem.style.webkitAnimationName = '';
}, (loading === true ? 520 : 20));
}
2023-04-19 01:56:05 -04:00
EmbyCheckboxPrototype.attachedCallback = function () {
if (this.getAttribute('data-embycheckbox') === 'true') {
return;
}
2023-04-19 01:56:05 -04:00
this.setAttribute('data-embycheckbox', 'true');
2023-04-19 01:56:05 -04:00
this.classList.add('emby-checkbox');
2023-04-19 01:56:05 -04:00
const labelElement = this.parentNode;
labelElement.classList.add('emby-checkbox-label');
2023-04-19 01:56:05 -04:00
const labelTextElement = labelElement.querySelector('span');
2023-04-19 01:56:05 -04:00
let outlineClass = 'checkboxOutline';
2023-04-19 01:56:05 -04:00
const customClass = this.getAttribute('data-outlineclass');
if (customClass) {
outlineClass += ' ' + customClass;
}
2023-04-19 01:56:05 -04:00
const checkedIcon = this.getAttribute('data-checkedicon') || 'check';
const uncheckedIcon = this.getAttribute('data-uncheckedicon') || '';
const checkHtml = '<span class="material-icons checkboxIcon checkboxIcon-checked ' + checkedIcon + '" aria-hidden="true"></span>';
const uncheckedHtml = '<span class="material-icons checkboxIcon checkboxIcon-unchecked ' + uncheckedIcon + '" aria-hidden="true"></span>';
labelElement.insertAdjacentHTML('beforeend', '<span class="' + outlineClass + '">' + checkHtml + uncheckedHtml + '</span>');
2023-04-19 01:56:05 -04:00
labelTextElement.classList.add('checkboxLabel');
2023-04-19 01:56:05 -04:00
this.addEventListener('keydown', onKeyDown);
2023-04-19 01:56:05 -04:00
if (enableRefreshHack) {
forceRefresh.call(this, true);
dom.addEventListener(this, 'click', forceRefresh, {
passive: true
});
2023-04-19 01:56:05 -04:00
dom.addEventListener(this, 'blur', forceRefresh, {
passive: true
});
2023-04-19 01:56:05 -04:00
dom.addEventListener(this, 'focus', forceRefresh, {
passive: true
});
2023-04-19 01:56:05 -04:00
dom.addEventListener(this, 'change', forceRefresh, {
passive: true
});
2023-04-19 01:56:05 -04:00
}
};
EmbyCheckboxPrototype.detachedCallback = function () {
this.removeEventListener('keydown', onKeyDown);
2023-04-19 01:56:05 -04:00
dom.removeEventListener(this, 'click', forceRefresh, {
passive: true
});
dom.removeEventListener(this, 'blur', forceRefresh, {
passive: true
});
2023-04-19 01:56:05 -04:00
dom.removeEventListener(this, 'focus', forceRefresh, {
passive: true
});
dom.removeEventListener(this, 'change', forceRefresh, {
passive: true
});
};
document.registerElement('emby-checkbox', {
prototype: EmbyCheckboxPrototype,
extends: 'input'
});