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

472 lines
14 KiB
JavaScript
Raw Normal View History

2016-07-26 22:19:56 -07:00
define(['historyManager', 'focusManager', 'browser', 'layoutManager', 'inputManager', 'dom', 'css!./dialoghelper.css', 'scrollStyles'], function (historyManager, focusManager, browser, layoutManager, inputManager, dom) {
2016-01-29 19:43:11 -07:00
2016-05-13 11:28:05 -07:00
var globalOnOpenCallback;
2016-05-21 21:17:28 -07:00
function enableAnimation() {
2016-05-21 23:08:44 -07:00
2016-05-21 21:17:28 -07:00
if (browser.animate) {
return true;
}
2016-06-05 13:46:19 -07:00
if (browser.edge) {
return true;
}
2016-05-21 23:08:44 -07:00
return false;
2016-05-21 21:17:28 -07:00
}
2016-07-26 22:19:56 -07:00
function removeCenterFocus(dlg) {
if (layoutManager.tv) {
if (dlg.classList.contains('smoothScrollX')) {
centerFocus(dlg, true, false);
}
else if (dlg.classList.contains('smoothScrollY')) {
centerFocus(dlg, false, false);
}
}
}
2016-03-23 12:03:17 -07:00
function dialogHashHandler(dlg, hash, resolve) {
2016-01-29 19:43:11 -07:00
var self = this;
self.originalUrl = window.location.href;
var activeElement = document.activeElement;
2016-01-30 13:59:09 -07:00
var removeScrollLockOnClose = false;
2016-01-29 19:43:11 -07:00
function onHashChange(e) {
var isBack = self.originalUrl == window.location.href;
2016-03-22 10:46:57 -07:00
if (isBack || !isOpened(dlg)) {
2016-01-29 19:43:11 -07:00
window.removeEventListener('popstate', onHashChange);
}
if (isBack) {
self.closedByBack = true;
2016-03-22 10:46:57 -07:00
closeDialog(dlg);
2016-01-29 19:43:11 -07:00
}
}
2016-03-02 23:24:46 -07:00
function onBackCommand(e) {
2016-03-04 20:39:49 -07:00
if (e.detail.command == 'back') {
self.closedByBack = true;
2016-03-22 10:46:57 -07:00
closeDialog(dlg);
2016-03-04 20:39:49 -07:00
e.preventDefault();
}
2016-03-02 23:24:46 -07:00
}
2016-01-29 19:43:11 -07:00
function onDialogClosed() {
2016-07-11 10:18:52 -07:00
if (!isHistoryEnabled(dlg)) {
inputManager.off(dlg, onBackCommand);
}
2016-04-28 20:31:52 -07:00
window.removeEventListener('popstate', onHashChange);
2016-03-22 10:46:57 -07:00
removeBackdrop(dlg);
2016-03-23 09:01:49 -07:00
dlg.classList.remove('opened');
2016-03-22 10:46:57 -07:00
2016-01-30 13:59:09 -07:00
if (removeScrollLockOnClose) {
document.body.classList.remove('noScroll');
2016-01-29 19:43:11 -07:00
}
2016-02-01 10:02:17 -07:00
if (!self.closedByBack && isHistoryEnabled(dlg)) {
2016-01-29 19:43:11 -07:00
var state = history.state || {};
if (state.dialogId == hash) {
history.back();
}
}
2016-09-09 09:58:08 -07:00
if (layoutManager.tv) {
activeElement.focus();
}
2016-01-29 19:43:11 -07:00
2016-09-08 13:32:30 -07:00
if (dlg.getAttribute('data-removeonclose') != 'false') {
2016-07-26 22:19:56 -07:00
removeCenterFocus(dlg);
2016-09-08 13:32:30 -07:00
var dialogContainer = dlg.dialogContainer;
if (dialogContainer) {
dialogContainer.parentNode.removeChild(dialogContainer);
dlg.dialogContainer = null;
} else {
dlg.parentNode.removeChild(dlg);
}
2016-01-29 19:43:11 -07:00
}
//resolve();
// if we just called history.back(), then use a timeout to allow the history events to fire first
setTimeout(function () {
resolve({
element: dlg,
closedByBack: self.closedByBack
});
}, 1);
}
2016-03-22 10:46:57 -07:00
dlg.addEventListener('close', onDialogClosed);
2016-08-01 22:55:52 -07:00
var center = !dlg.classList.contains('dialog-fixedSize');
2016-03-22 10:46:57 -07:00
if (center) {
2016-04-03 10:34:52 -07:00
dlg.classList.add('centeredDialog');
2016-03-22 10:46:57 -07:00
}
dlg.classList.remove('hide');
2016-09-08 13:32:30 -07:00
addBackdropOverlay(dlg);
2016-03-22 10:46:57 -07:00
2016-03-23 09:01:49 -07:00
dlg.classList.add('opened');
2016-09-20 09:51:16 -07:00
dlg.dispatchEvent(new CustomEvent('open', {
bubbles: false,
cancelable: false
}));
2016-03-22 10:46:57 -07:00
2016-01-30 21:04:00 -07:00
if (dlg.getAttribute('data-lockscroll') == 'true' && !document.body.classList.contains('noScroll')) {
2016-01-30 13:59:09 -07:00
document.body.classList.add('noScroll');
removeScrollLockOnClose = true;
2016-01-29 19:43:11 -07:00
}
2016-05-10 11:13:26 -07:00
animateDialogOpen(dlg);
2016-02-01 10:02:17 -07:00
if (isHistoryEnabled(dlg)) {
historyManager.pushState({ dialogId: hash }, "Dialog", hash);
2016-01-29 19:43:11 -07:00
2016-02-01 10:02:17 -07:00
window.addEventListener('popstate', onHashChange);
2016-03-02 23:24:46 -07:00
} else {
inputManager.on(dlg, onBackCommand);
2016-02-01 10:02:17 -07:00
}
}
2016-09-08 13:32:30 -07:00
function addBackdropOverlay(dlg) {
var backdrop = document.createElement('div');
backdrop.classList.add('dialogBackdrop');
2016-03-23 19:44:27 -07:00
2016-09-08 13:32:30 -07:00
var backdropParent = dlg.dialogContainer || dlg;
backdropParent.parentNode.insertBefore(backdrop, backdropParent);
dlg.backdrop = backdrop;
2016-03-24 18:08:05 -07:00
2016-09-08 13:32:30 -07:00
// Doing this immediately causes the opacity to jump immediately without animating
setTimeout(function () {
backdrop.classList.add('dialogBackdropOpened');
}, 0);
2016-03-24 18:08:05 -07:00
2016-09-08 13:32:30 -07:00
dom.addEventListener((dlg.dialogContainer || backdrop), 'click', function (e) {
2016-09-09 19:21:00 -07:00
if (e.target == dlg.dialogContainer) {
2016-03-23 19:44:27 -07:00
close(dlg);
}
2016-09-08 13:32:30 -07:00
}, {
passive: true
2016-03-23 19:44:27 -07:00
});
}
2016-02-01 10:02:17 -07:00
function isHistoryEnabled(dlg) {
return dlg.getAttribute('data-history') == 'true';
2016-01-29 19:43:11 -07:00
}
function open(dlg) {
2016-05-13 11:28:05 -07:00
if (globalOnOpenCallback) {
globalOnOpenCallback(dlg);
}
2016-09-08 13:32:30 -07:00
var parent = dlg.parentNode;
if (parent) {
parent.removeChild(dlg);
}
var dialogContainer = document.createElement('div');
dialogContainer.classList.add('dialogContainer');
dialogContainer.appendChild(dlg);
dlg.dialogContainer = dialogContainer;
document.body.appendChild(dialogContainer);
2016-01-29 19:43:11 -07:00
return new Promise(function (resolve, reject) {
2016-03-23 12:03:17 -07:00
new dialogHashHandler(dlg, 'dlg' + new Date().getTime(), resolve);
2016-01-29 19:43:11 -07:00
});
}
2016-03-22 10:46:57 -07:00
function isOpened(dlg) {
//return dlg.opened;
return !dlg.classList.contains('hide');
}
2016-01-29 19:43:11 -07:00
function close(dlg) {
2016-03-22 10:46:57 -07:00
if (isOpened(dlg)) {
2016-02-01 10:02:17 -07:00
if (isHistoryEnabled(dlg)) {
history.back();
} else {
2016-03-22 10:46:57 -07:00
closeDialog(dlg);
2016-02-01 10:02:17 -07:00
}
2016-01-29 19:43:11 -07:00
}
}
2016-04-03 10:34:52 -07:00
function scaleUp(elem, onFinish) {
2016-03-22 10:46:57 -07:00
var keyframes = [
{ transform: 'scale(0)', offset: 0 },
2016-05-18 14:46:56 -07:00
{ transform: 'none', offset: 1 }];
var timing = elem.animationConfig.entry.timing;
return elem.animate(keyframes, timing).onfinish = onFinish;
}
function slideUp(elem, onFinish) {
var keyframes = [
{ transform: 'translate3d(0,30%,0)', opacity: 0, offset: 0 },
{ transform: 'none', opacity: 1, offset: 1 }];
2016-03-22 10:46:57 -07:00
var timing = elem.animationConfig.entry.timing;
2016-04-03 10:34:52 -07:00
return elem.animate(keyframes, timing).onfinish = onFinish;
2016-03-22 10:46:57 -07:00
}
2016-04-03 10:34:52 -07:00
function fadeIn(elem, onFinish) {
2016-03-22 10:46:57 -07:00
var keyframes = [
{ opacity: '0', offset: 0 },
{ opacity: '1', offset: 1 }];
var timing = elem.animationConfig.entry.timing;
2016-04-03 10:34:52 -07:00
return elem.animate(keyframes, timing).onfinish = onFinish;
2016-03-22 10:46:57 -07:00
}
2016-05-28 11:03:38 -07:00
function scaleDown(elem) {
var keyframes = [
{ transform: 'none', opacity: 1, offset: 0 },
{ transform: 'scale(0)', opacity: 0, offset: 1 }];
var timing = elem.animationConfig.exit.timing;
return elem.animate(keyframes, timing);
}
2016-03-22 10:46:57 -07:00
function fadeOut(elem) {
2016-01-29 19:43:11 -07:00
2016-03-22 10:46:57 -07:00
var keyframes = [
{ opacity: '1', offset: 0 },
{ opacity: '0', offset: 1 }];
var timing = elem.animationConfig.exit.timing;
return elem.animate(keyframes, timing);
}
2016-05-18 14:46:56 -07:00
function slideDown(elem, onFinish) {
var keyframes = [
{ transform: 'none', opacity: 1, offset: 0 },
{ transform: 'translate3d(0,30%,0)', opacity: 0, offset: 1 }];
var timing = elem.animationConfig.entry.timing;
return elem.animate(keyframes, timing);
}
2016-03-22 10:46:57 -07:00
function closeDialog(dlg) {
if (!dlg.classList.contains('hide')) {
2016-08-13 13:54:29 -07:00
dlg.dispatchEvent(new CustomEvent('closing', {
bubbles: false,
cancelable: false
}));
2016-03-22 10:46:57 -07:00
var onAnimationFinish = function () {
2016-08-11 20:23:12 -07:00
focusManager.popScope(dlg);
2016-03-22 10:46:57 -07:00
dlg.classList.add('hide');
if (dlg.close) {
dlg.close();
} else {
dlg.dispatchEvent(new CustomEvent('close', {
bubbles: false,
cancelable: false
}));
}
};
if (!dlg.animationConfig || !dlg.animate) {
onAnimationFinish();
return;
}
2016-05-18 14:46:56 -07:00
var animation;
if (dlg.animationConfig.exit.name == 'fadeout') {
animation = fadeOut(dlg);
2016-05-28 11:03:38 -07:00
} else if (dlg.animationConfig.exit.name == 'scaledown') {
animation = scaleDown(dlg);
2016-05-18 14:46:56 -07:00
} else if (dlg.animationConfig.exit.name == 'slidedown') {
animation = slideDown(dlg);
} else {
onAnimationFinish();
return;
}
animation.onfinish = onAnimationFinish;
2016-03-22 10:46:57 -07:00
}
}
function animateDialogOpen(dlg) {
2016-05-09 20:36:43 -07:00
var onAnimationFinish = function () {
2016-08-11 20:23:12 -07:00
focusManager.pushScope(dlg);
2016-05-10 11:13:26 -07:00
if (dlg.getAttribute('data-autofocus') == 'true') {
2016-09-08 13:32:30 -07:00
focusManager.autoFocus(dlg);
2016-05-10 11:13:26 -07:00
}
2016-04-03 10:34:52 -07:00
};
2016-03-22 10:46:57 -07:00
if (!dlg.animationConfig || !dlg.animate) {
2016-04-03 10:34:52 -07:00
onAnimationFinish();
2016-03-22 10:46:57 -07:00
return;
}
2016-05-18 14:46:56 -07:00
if (dlg.animationConfig.entry.name == 'fadein') {
2016-04-03 10:34:52 -07:00
fadeIn(dlg, onAnimationFinish);
2016-05-18 14:46:56 -07:00
} else if (dlg.animationConfig.entry.name == 'scaleup') {
2016-04-03 10:34:52 -07:00
scaleUp(dlg, onAnimationFinish);
2016-05-18 14:46:56 -07:00
} else if (dlg.animationConfig.entry.name == 'slideup') {
slideUp(dlg, onAnimationFinish);
2016-03-22 10:46:57 -07:00
}
2016-01-29 19:43:11 -07:00
}
2016-01-30 21:04:00 -07:00
function shouldLockDocumentScroll(options) {
if (options.lockScroll != null) {
return options.lockScroll;
}
if (options.size == 'fullscreen') {
return true;
}
2016-08-14 21:36:17 -07:00
if (options.size) {
return true;
}
2016-08-06 19:11:39 -07:00
return browser.touch;
2016-01-30 21:04:00 -07:00
}
2016-03-22 10:46:57 -07:00
function removeBackdrop(dlg) {
var backdrop = dlg.backdrop;
if (backdrop) {
dlg.backdrop = null;
2016-08-01 22:55:52 -07:00
backdrop.classList.remove('dialogBackdropOpened');
2016-03-22 10:46:57 -07:00
setTimeout(function () {
backdrop.parentNode.removeChild(backdrop);
}, 300);
}
}
2016-07-26 22:19:56 -07:00
function centerFocus(elem, horiz, on) {
require(['scrollHelper'], function (scrollHelper) {
var fn = on ? 'on' : 'off';
scrollHelper.centerFocus[fn](elem, horiz);
});
}
2016-01-29 19:43:11 -07:00
function createDialog(options) {
options = options || {};
2016-03-23 12:03:17 -07:00
// If there's no native dialog support, use a plain div
2016-03-23 12:03:36 -07:00
// Also not working well in samsung tizen browser, content inside not clickable
2016-08-01 22:55:52 -07:00
// Just go ahead and always use a plain div because we're seeing issues overlaying absoltutely positioned content over a modal dialog
var dlg = document.createElement('div');
2016-03-23 12:03:17 -07:00
2016-05-09 20:36:43 -07:00
dlg.classList.add('focuscontainer');
2016-03-22 10:46:57 -07:00
dlg.classList.add('hide');
2016-01-29 19:43:11 -07:00
2016-01-30 21:04:00 -07:00
if (shouldLockDocumentScroll(options)) {
dlg.setAttribute('data-lockscroll', 'true');
}
2016-03-02 21:31:15 -07:00
if (options.enableHistory !== false && historyManager.enableNativeHistory()) {
2016-02-01 10:02:17 -07:00
dlg.setAttribute('data-history', 'true');
}
2016-01-29 19:43:11 -07:00
// without this safari will scroll the background instead of the dialog contents
// but not needed here since this is already on top of an existing dialog
// but skip it in IE because it's causing the entire browser to hang
// Also have to disable for firefox because it's causing select elements to not be clickable
2016-02-17 14:24:01 -07:00
if (options.modal !== false) {
2016-01-29 19:43:11 -07:00
dlg.setAttribute('modal', 'modal');
}
2016-03-22 10:46:57 -07:00
if (options.autoFocus !== false) {
dlg.setAttribute('data-autofocus', 'true');
}
2016-01-29 19:43:11 -07:00
var defaultEntryAnimation = 'scaleup';
2016-05-28 11:03:38 -07:00
var entryAnimation = options.entryAnimation || defaultEntryAnimation;
var defaultExitAnimation = 'scaledown';
2016-05-28 11:03:38 -07:00
var exitAnimation = options.exitAnimation || defaultExitAnimation;
2016-01-29 19:43:11 -07:00
2016-01-30 14:15:04 -07:00
// If it's not fullscreen then lower the default animation speed to make it open really fast
2016-09-08 13:32:30 -07:00
var entryAnimationDuration = options.entryAnimationDuration || (options.size !== 'fullscreen' ? 180 : 280);
var exitAnimationDuration = options.exitAnimationDuration || (options.size !== 'fullscreen' ? 180 : 280);
2016-01-30 14:15:04 -07:00
2016-01-29 19:43:11 -07:00
dlg.animationConfig = {
// scale up
'entry': {
2016-05-28 11:03:38 -07:00
name: entryAnimation,
2016-01-29 19:43:11 -07:00
node: dlg,
2016-06-04 22:06:56 -07:00
timing: {
duration: entryAnimationDuration,
easing: 'ease-out'
}
2016-01-29 19:43:11 -07:00
},
// fade out
'exit': {
2016-05-28 11:03:38 -07:00
name: exitAnimation,
2016-01-29 19:43:11 -07:00
node: dlg,
2016-06-04 22:06:56 -07:00
timing: {
duration: exitAnimationDuration,
easing: 'ease-out',
fill: 'both'
}
2016-01-29 19:43:11 -07:00
}
};
2016-02-15 21:54:20 -07:00
// too buggy in IE, not even worth it
2016-05-21 21:17:28 -07:00
if (!enableAnimation()) {
2016-03-22 11:29:33 -07:00
dlg.animationConfig = null;
2016-02-15 21:54:20 -07:00
}
2016-03-23 12:03:17 -07:00
dlg.classList.add('dialog');
2016-01-29 19:43:11 -07:00
2016-07-02 11:05:40 -07:00
if (options.scrollX) {
dlg.classList.add('smoothScrollX');
if (layoutManager.tv) {
2016-07-26 22:19:56 -07:00
centerFocus(dlg, true, true);
2016-07-02 11:05:40 -07:00
}
}
else if (options.scrollY !== false) {
2016-05-12 23:22:02 -07:00
dlg.classList.add('smoothScrollY');
2016-01-29 19:43:11 -07:00
2016-05-12 23:22:02 -07:00
if (layoutManager.tv) {
2016-07-26 22:19:56 -07:00
centerFocus(dlg, false, true);
2016-05-12 23:22:02 -07:00
}
2016-02-04 11:19:10 -07:00
}
2016-01-29 19:43:11 -07:00
if (options.removeOnClose) {
dlg.setAttribute('data-removeonclose', 'true');
}
2016-01-30 13:59:09 -07:00
if (options.size) {
2016-08-01 22:55:52 -07:00
dlg.classList.add('dialog-fixedSize');
dlg.classList.add('dialog-' + options.size);
2016-01-30 13:59:09 -07:00
}
2016-01-29 19:43:11 -07:00
return dlg;
}
return {
open: open,
close: close,
2016-05-13 11:28:05 -07:00
createDialog: createDialog,
setOnOpen: function (val) {
globalOnOpenCallback = val;
}
2016-01-29 19:43:11 -07:00
};
});