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

466 lines
13 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-10-03 22:15:39 -07:00
'use strict';
2016-01-29 19:43:11 -07:00
2016-05-13 11:28:05 -07:00
var globalOnOpenCallback;
function enableAnimation() {
if (browser.animate) {
return true;
}
if (browser.edge) {
return true;
}
// An indication of an older browser
if (browser.noFlex) {
return false;
}
return true;
}
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-10-03 22:15:39 -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) {
2016-10-03 22:15:39 -07:00
var isBack = self.originalUrl === window.location.href;
2016-01-29 19:43:11 -07:00
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-10-03 22:15:39 -07:00
if (e.detail.command === 'back') {
2016-03-04 20:39:49 -07:00
self.closedByBack = true;
e.preventDefault();
2016-09-29 05:55:49 -07:00
e.stopPropagation();
closeDialog(dlg);
2016-03-04 20:39:49 -07:00
}
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 || {};
2016-10-03 22:15:39 -07:00
if (state.dialogId === hash) {
2016-01-29 19:43:11 -07:00
history.back();
}
}
2016-09-09 09:58:08 -07:00
if (layoutManager.tv) {
activeElement.focus();
}
2016-01-29 19:43:11 -07:00
2016-10-03 22:15:39 -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-10-03 22:15:39 -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)) {
2016-10-02 23:28:45 -07:00
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
// trigger reflow or the backdrop will not animate
void backdrop.offsetWidth;
backdrop.classList.add('dialogBackdropOpened');
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-10-03 22:15:39 -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) {
2016-10-03 22:15:39 -07:00
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-10-03 22:15:39 -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-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');
2016-10-14 09:22:04 -07:00
dlg.dispatchEvent(new CustomEvent('close', {
bubbles: false,
cancelable: false
}));
2016-03-22 10:46:57 -07:00
};
2016-10-14 09:22:04 -07:00
animateDialogClose(dlg, 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-10-03 22:15:39 -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
};
if (enableAnimation()) {
var onFinish = function () {
dom.removeEventListener(dlg, 'animationend', onFinish, {
once: true
});
onAnimationFinish();
};
dom.addEventListener(dlg, 'animationend', onFinish, {
once: true
});
return;
}
onAnimationFinish();
2016-10-14 09:22:04 -07:00
}
function animateDialogClose(dlg, onAnimationFinish) {
if (enableAnimation()) {
var animated = true;
switch (dlg.animationConfig.exit.name) {
case 'fadeout':
dlg.style.animation = 'fadeout ' + dlg.animationConfig.exit.timing.duration + 'ms ease-out normal both';
break;
case 'scaledown':
dlg.style.animation = 'scaledown ' + dlg.animationConfig.exit.timing.duration + 'ms ease-out normal both';
break;
case 'slidedown':
dlg.style.animation = 'slidedown ' + dlg.animationConfig.exit.timing.duration + 'ms ease-out normal both';
break;
default:
animated = false;
break;
}
var onFinish = function () {
dom.removeEventListener(dlg, 'animationend', onFinish, {
once: true
});
onAnimationFinish();
};
dom.addEventListener(dlg, 'animationend', onFinish, {
once: true
});
if (animated) {
return;
}
2016-03-22 10:46:57 -07:00
}
2016-10-14 09:22:04 -07:00
onAnimationFinish();
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;
}
2016-10-03 22:15:39 -07:00
if (options.size === 'fullscreen') {
2016-01-30 21:04:00 -07:00
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) {
return;
}
dlg.backdrop = null;
var onAnimationFinish = function () {
backdrop.parentNode.removeChild(backdrop);
};
if (enableAnimation()) {
2016-03-22 10:46:57 -07:00
2016-08-01 22:55:52 -07:00
backdrop.classList.remove('dialogBackdropOpened');
2016-03-22 10:46:57 -07:00
// this is not firing animatonend
setTimeout(onAnimationFinish, 300);
return;
2016-03-22 10:46:57 -07:00
}
onAnimationFinish();
2016-03-22 10:46:57 -07:00
}
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-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-06-04 22:06:56 -07:00
timing: {
duration: exitAnimationDuration,
easing: 'ease-out',
fill: 'both'
}
2016-01-29 19:43:11 -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
}
if (enableAnimation()) {
switch (dlg.animationConfig.entry.name) {
case 'fadein':
dlg.style.animation = 'fadein ' + entryAnimationDuration + 'ms ease-out normal';
break;
case 'scaleup':
dlg.style.animation = 'scaleup ' + entryAnimationDuration + 'ms ease-out normal both';
break;
case 'slideup':
dlg.style.animation = 'slideup ' + entryAnimationDuration + 'ms ease-out normal';
break;
default:
break;
}
2016-10-14 09:22:04 -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
};
});