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

52 lines
1.5 KiB
JavaScript
Raw Normal View History

2021-01-26 15:42:02 -05:00
import './emby-toggle.scss';
2020-09-08 02:43:56 -04:00
import 'webcomponents.js/webcomponents-lite';
/* eslint-disable indent */
2020-07-10 20:05:00 +01:00
const EmbyTogglePrototype = Object.create(HTMLInputElement.prototype);
2018-10-23 01:05:09 +03:00
function onKeyDown(e) {
// Don't submit form on enter
if (e.keyCode === 13) {
e.preventDefault();
this.checked = !this.checked;
this.dispatchEvent(new CustomEvent('change', {
bubbles: true
}));
return false;
}
2018-10-23 01:05:09 +03:00
}
EmbyTogglePrototype.attachedCallback = function () {
if (this.getAttribute('data-embytoggle') === 'true') {
return;
2018-10-23 01:05:09 +03:00
}
this.setAttribute('data-embytoggle', 'true');
this.classList.add('mdl-switch__input');
2020-07-10 20:05:00 +01:00
const labelElement = this.parentNode;
labelElement.classList.add('mdl-switch');
labelElement.classList.add('mdl-js-switch');
2020-07-10 20:05:00 +01:00
const labelTextElement = labelElement.querySelector('span');
labelElement.insertAdjacentHTML('beforeend', '<div class="mdl-switch__trackContainer"><div class="mdl-switch__track"></div><div class="mdl-switch__thumb"><span class="mdl-switch__focus-helper"></span></div></div>');
labelTextElement.classList.add('toggleButtonLabel');
labelTextElement.classList.add('mdl-switch__label');
this.addEventListener('keydown', onKeyDown);
};
document.registerElement('emby-toggle', {
2018-10-23 01:05:09 +03:00
prototype: EmbyTogglePrototype,
extends: 'input'
});
/* eslint-enable indent */