2016-09-16 22:47:49 -07:00
|
|
|
|
define(['css!./emby-toggle', 'registerElement'], function () {
|
2016-10-05 21:28:10 -07:00
|
|
|
|
'use strict';
|
2016-09-16 22:47:49 -07:00
|
|
|
|
|
|
|
|
|
var EmbyTogglePrototype = Object.create(HTMLInputElement.prototype);
|
|
|
|
|
|
|
|
|
|
function onKeyDown(e) {
|
|
|
|
|
|
|
|
|
|
// Don't submit form on enter
|
2016-10-05 21:28:10 -07:00
|
|
|
|
if (e.keyCode === 13) {
|
2016-09-16 22:47:49 -07:00
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
|
|
this.checked = !this.checked;
|
|
|
|
|
|
|
|
|
|
this.dispatchEvent(new CustomEvent('change', {
|
|
|
|
|
bubbles: true
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EmbyTogglePrototype.attachedCallback = function () {
|
|
|
|
|
|
2016-10-05 21:28:10 -07:00
|
|
|
|
if (this.getAttribute('data-embytoggle') === 'true') {
|
2016-09-16 22:47:49 -07:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.setAttribute('data-embytoggle', 'true');
|
|
|
|
|
|
|
|
|
|
this.classList.add('mdl-switch__input');
|
|
|
|
|
|
|
|
|
|
var labelElement = this.parentNode;
|
|
|
|
|
labelElement.classList.add('mdl-switch');
|
|
|
|
|
labelElement.classList.add('mdl-js-switch');
|
|
|
|
|
|
|
|
|
|
var labelTextElement = labelElement.querySelector('span');
|
|
|
|
|
|
2016-09-20 09:51:16 -07:00
|
|
|
|
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>');
|
2016-09-16 22:47:49 -07:00
|
|
|
|
|
|
|
|
|
labelTextElement.classList.add('toggleButtonLabel');
|
|
|
|
|
labelTextElement.classList.add('mdl-switch__label');
|
|
|
|
|
|
|
|
|
|
this.addEventListener('keydown', onKeyDown);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
document.registerElement('emby-toggle', {
|
|
|
|
|
prototype: EmbyTogglePrototype,
|
|
|
|
|
extends: 'input'
|
|
|
|
|
});
|
|
|
|
|
});
|