2016-12-14 13:58:55 -07:00
|
|
|
|
define(['browser', 'dom', 'layoutManager', 'css!./emby-button', 'registerElement'], function (browser, dom, layoutManager) {
|
2016-10-03 22:15:39 -07:00
|
|
|
|
'use strict';
|
2016-06-04 17:17:35 -07:00
|
|
|
|
|
|
|
|
|
var EmbyButtonPrototype = Object.create(HTMLButtonElement.prototype);
|
|
|
|
|
|
2016-06-05 13:46:19 -07:00
|
|
|
|
function animateButtonInternal(e, btn) {
|
2016-06-04 17:17:35 -07:00
|
|
|
|
|
|
|
|
|
var div = document.createElement('div');
|
|
|
|
|
|
2016-08-14 09:53:23 -07:00
|
|
|
|
for (var i = 0, length = btn.classList.length; i < length; i++) {
|
|
|
|
|
div.classList.add(btn.classList[i] + '-ripple-effect');
|
|
|
|
|
}
|
2016-06-04 17:17:35 -07:00
|
|
|
|
|
|
|
|
|
var offsetX = e.offsetX || 0;
|
|
|
|
|
var offsetY = e.offsetY || 0;
|
|
|
|
|
|
|
|
|
|
if (offsetX > 0 && offsetY > 0) {
|
|
|
|
|
div.style.left = offsetX + 'px';
|
|
|
|
|
div.style.top = offsetY + 'px';
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-14 09:53:23 -07:00
|
|
|
|
var firstChild = btn.firstChild;
|
|
|
|
|
if (firstChild) {
|
|
|
|
|
btn.insertBefore(div, btn.firstChild);
|
|
|
|
|
} else {
|
|
|
|
|
btn.appendChild(div);
|
|
|
|
|
}
|
2016-06-04 17:17:35 -07:00
|
|
|
|
|
2016-11-26 22:00:20 -07:00
|
|
|
|
div.addEventListener(dom.whichAnimationEvent(), function () {
|
2016-06-04 17:17:35 -07:00
|
|
|
|
div.parentNode.removeChild(div);
|
2016-06-04 20:50:07 -07:00
|
|
|
|
}, false);
|
2016-06-04 17:17:35 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-05 21:28:10 -07:00
|
|
|
|
function animateButton(e, btn) {
|
2016-06-05 13:46:19 -07:00
|
|
|
|
|
|
|
|
|
requestAnimationFrame(function () {
|
|
|
|
|
animateButtonInternal(e, btn);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-04 17:17:35 -07:00
|
|
|
|
function onKeyDown(e) {
|
|
|
|
|
|
2016-10-03 22:15:39 -07:00
|
|
|
|
if (e.keyCode === 13) {
|
2016-10-05 21:28:10 -07:00
|
|
|
|
animateButton(e, this);
|
2016-06-04 17:17:35 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-04 20:50:07 -07:00
|
|
|
|
function onMouseDown(e) {
|
|
|
|
|
|
2016-10-03 22:15:39 -07:00
|
|
|
|
if (e.button === 0) {
|
2016-10-05 21:28:10 -07:00
|
|
|
|
animateButton(e, this);
|
2016-06-04 20:50:07 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-05 21:28:10 -07:00
|
|
|
|
function onClick(e) {
|
|
|
|
|
|
|
|
|
|
animateButton(e, this);
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-25 13:18:23 -07:00
|
|
|
|
function enableAnimation() {
|
|
|
|
|
if (browser.tv) {
|
|
|
|
|
// too slow
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-12 22:49:00 -07:00
|
|
|
|
EmbyButtonPrototype.createdCallback = function () {
|
2016-06-04 17:17:35 -07:00
|
|
|
|
|
2016-08-04 23:10:24 -07:00
|
|
|
|
if (this.classList.contains('emby-button')) {
|
2016-06-04 17:17:35 -07:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-04 23:10:24 -07:00
|
|
|
|
this.classList.add('emby-button');
|
2016-06-04 17:17:35 -07:00
|
|
|
|
|
2016-11-29 12:11:51 -07:00
|
|
|
|
// Even though they support flex, it doesn't quite work with button elements
|
|
|
|
|
if (browser.firefox || browser.safari) {
|
2016-08-04 23:10:24 -07:00
|
|
|
|
this.classList.add('emby-button-noflex');
|
2016-06-06 14:12:44 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-14 13:58:55 -07:00
|
|
|
|
if (layoutManager.tv) {
|
|
|
|
|
this.classList.add('emby-button-focusscale');
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-25 13:18:23 -07:00
|
|
|
|
if (enableAnimation()) {
|
2016-08-07 12:43:52 -07:00
|
|
|
|
dom.addEventListener(this, 'keydown', onKeyDown, {
|
|
|
|
|
passive: true
|
|
|
|
|
});
|
2016-06-25 13:18:23 -07:00
|
|
|
|
if (browser.safari) {
|
2016-10-05 21:28:10 -07:00
|
|
|
|
dom.addEventListener(this, 'click', onClick, {
|
2016-08-07 12:43:52 -07:00
|
|
|
|
passive: true
|
|
|
|
|
});
|
2016-06-25 13:18:23 -07:00
|
|
|
|
} else {
|
2016-08-07 12:43:52 -07:00
|
|
|
|
dom.addEventListener(this, 'mousedown', onMouseDown, {
|
|
|
|
|
passive: true
|
|
|
|
|
});
|
2016-06-25 13:18:23 -07:00
|
|
|
|
//this.addEventListener('touchstart', animateButton);
|
|
|
|
|
}
|
2016-06-04 21:49:29 -07:00
|
|
|
|
}
|
2016-06-04 17:17:35 -07:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
document.registerElement('emby-button', {
|
|
|
|
|
prototype: EmbyButtonPrototype,
|
|
|
|
|
extends: 'button'
|
|
|
|
|
});
|
|
|
|
|
});
|