jellyfin-web/dashboard-ui/bower_components/emby-webcomponents/emby-checkbox/emby-checkbox.js

90 lines
2.7 KiB
JavaScript
Raw Normal View History

2017-01-23 12:06:13 -07:00
define(['browser', 'dom', 'css!./emby-checkbox', 'registerElement'], function (browser, dom) {
2016-10-03 22:15:39 -07:00
'use strict';
2016-06-11 08:55:39 -07:00
var EmbyCheckboxPrototype = Object.create(HTMLInputElement.prototype);
function onKeyDown(e) {
// Don't submit form on enter
2016-10-03 22:15:39 -07:00
if (e.keyCode === 13) {
2016-06-11 08:55:39 -07:00
e.preventDefault();
this.checked = !this.checked;
2016-09-05 22:02:05 -07:00
this.dispatchEvent(new CustomEvent('change', {
bubbles: true
}));
2016-06-11 08:55:39 -07:00
return false;
}
}
2017-01-23 12:06:13 -07:00
var enableRefreshHack = browser.tizen || browser.orsay || browser.operaTv || browser.web0s ? true : false;
function forceRefresh(loading) {
2017-01-24 12:54:18 -07:00
var elem = this.parentNode;
2017-01-23 12:06:13 -07:00
elem.style.webkitAnimationName = 'repaintChrome';
elem.style.webkitAnimationDelay = (loading === true ? '500ms' : '');
elem.style.webkitAnimationDuration = '10ms';
elem.style.webkitAnimationIterationCount = '1';
setTimeout(function () {
elem.style.webkitAnimationName = '';
}, (loading === true ? 520 : 20));
}
2016-06-11 08:55:39 -07:00
EmbyCheckboxPrototype.attachedCallback = function () {
2016-10-03 22:15:39 -07:00
if (this.getAttribute('data-embycheckbox') === 'true') {
2016-06-11 08:55:39 -07:00
return;
}
this.setAttribute('data-embycheckbox', 'true');
this.classList.add('mdl-checkbox__input');
var labelElement = this.parentNode;
labelElement.classList.add('mdl-checkbox');
labelElement.classList.add('mdl-js-checkbox');
var labelTextElement = labelElement.querySelector('span');
2016-08-02 21:30:22 -07:00
var outlineClass = 'checkboxOutline';
var customClass = this.getAttribute('data-outlineclass');
if (customClass) {
outlineClass += ' ' + customClass;
}
labelElement.insertAdjacentHTML('beforeend', '<span class="mdl-checkbox__focus-helper"></span><span class="' + outlineClass + '"><span class="mdl-checkbox__tick-outline"></span></span>');
2016-06-11 08:55:39 -07:00
labelTextElement.classList.add('checkboxLabel');
this.addEventListener('keydown', onKeyDown);
2017-01-23 12:06:13 -07:00
if (enableRefreshHack) {
2017-01-24 12:54:18 -07:00
forceRefresh.call(this, true);
2017-01-23 12:06:13 -07:00
dom.addEventListener(this, 'click', forceRefresh, {
passive: true
});
dom.addEventListener(this, 'blur', forceRefresh, {
passive: true
});
dom.addEventListener(this, 'focus', forceRefresh, {
passive: true
});
dom.addEventListener(this, 'change', forceRefresh, {
passive: true
});
}
2016-06-11 08:55:39 -07:00
};
document.registerElement('emby-checkbox', {
prototype: EmbyCheckboxPrototype,
extends: 'input'
});
2017-01-23 12:06:13 -07:00
});