1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/src/components/dialogHelper/dialogHelper.js

487 lines
15 KiB
JavaScript
Raw Normal View History

2020-08-16 20:24:45 +02:00
import { appRouter } from '../appRouter';
2020-08-14 08:46:34 +02:00
import focusManager from '../focusManager';
import browser from '../../scripts/browser';
import layoutManager from '../layoutManager';
import inputManager from '../../scripts/inputManager';
import dom from '../../scripts/dom';
2021-01-26 16:25:38 -05:00
import './dialoghelper.scss';
2021-01-26 15:31:58 -05:00
import '../../assets/css/scrollstyles.scss';
2020-06-17 01:19:59 +03:00
/* eslint-disable indent */
let globalOnOpenCallback;
2018-10-23 01:05:09 +03:00
function enableAnimation() {
// too slow
if (browser.tv) {
return false;
}
return browser.supportsCssAnimation();
2018-10-23 01:05:09 +03:00
}
function removeCenterFocus(dlg) {
if (layoutManager.tv) {
if (dlg.classList.contains('scrollX')) {
centerFocus(dlg, true, false);
} else if (dlg.classList.contains('smoothScrollY')) {
centerFocus(dlg, false, false);
}
}
2018-10-23 01:05:09 +03:00
}
function tryRemoveElement(elem) {
2020-06-17 01:19:59 +03:00
const parentNode = elem.parentNode;
if (parentNode) {
// Seeing crashes in edge webview
try {
parentNode.removeChild(elem);
} catch (err) {
2020-06-26 23:16:10 +03:00
console.error('error removing dialog element: ' + err);
}
2018-10-23 01:05:09 +03:00
}
}
function DialogHashHandler(dlg, hash, resolve) {
2020-06-17 01:19:59 +03:00
const self = this;
self.originalUrl = window.location.href;
2020-06-17 01:19:59 +03:00
const activeElement = document.activeElement;
let removeScrollLockOnClose = false;
2021-01-26 22:20:12 -05:00
function onHashChange() {
2020-06-17 01:19:59 +03:00
const isBack = self.originalUrl === window.location.href;
if (isBack || !isOpened(dlg)) {
window.removeEventListener('popstate', onHashChange);
}
if (isBack) {
self.closedByBack = true;
closeDialog(dlg);
}
2018-10-23 01:05:09 +03:00
}
function onBackCommand(e) {
if (e.detail.command === 'back') {
self.closedByBack = true;
e.preventDefault();
e.stopPropagation();
closeDialog(dlg);
}
2018-10-23 01:05:09 +03:00
}
function onDialogClosed() {
if (!isHistoryEnabled(dlg)) {
inputManager.off(dlg, onBackCommand);
}
window.removeEventListener('popstate', onHashChange);
removeBackdrop(dlg);
dlg.classList.remove('opened');
if (removeScrollLockOnClose) {
document.body.classList.remove('noScroll');
}
if (!self.closedByBack && isHistoryEnabled(dlg)) {
2020-08-25 10:12:35 +09:00
const state = window.history.state || {};
if (state.dialogId === hash) {
2021-09-06 16:47:46 +03:00
appRouter.back();
}
2018-10-23 01:05:09 +03:00
}
if (layoutManager.tv) {
focusManager.focus(activeElement);
}
if (dlg.getAttribute('data-removeonclose') !== 'false') {
2018-10-23 01:05:09 +03:00
removeCenterFocus(dlg);
2020-06-17 01:19:59 +03:00
const dialogContainer = dlg.dialogContainer;
if (dialogContainer) {
tryRemoveElement(dialogContainer);
dlg.dialogContainer = null;
} else {
tryRemoveElement(dlg);
}
2018-10-23 01:05:09 +03:00
}
2021-03-09 16:17:18 -05:00
//resolve();
// if we just called history.back(), then use a timeout to allow the history events to fire first
setTimeout(() => {
resolve({
element: dlg,
closedByBack: self.closedByBack
});
}, 1);
}
dlg.addEventListener('close', onDialogClosed);
2020-06-17 01:19:59 +03:00
const center = !dlg.classList.contains('dialog-fixedSize');
if (center) {
dlg.classList.add('centeredDialog');
}
dlg.classList.remove('hide');
addBackdropOverlay(dlg);
dlg.classList.add('opened');
dlg.dispatchEvent(new CustomEvent('open', {
bubbles: false,
cancelable: false
}));
if (dlg.getAttribute('data-lockscroll') === 'true' && !document.body.classList.contains('noScroll')) {
document.body.classList.add('noScroll');
removeScrollLockOnClose = true;
}
animateDialogOpen(dlg);
if (isHistoryEnabled(dlg)) {
2021-09-06 16:49:22 +03:00
appRouter.show(`/dialog?dlg=${hash}`, { dialogId: hash });
window.addEventListener('popstate', onHashChange);
} else {
inputManager.on(dlg, onBackCommand);
2018-10-23 01:05:09 +03:00
}
}
function addBackdropOverlay(dlg) {
2020-06-17 01:19:59 +03:00
const backdrop = document.createElement('div');
backdrop.classList.add('dialogBackdrop');
2020-06-17 01:19:59 +03:00
const backdropParent = dlg.dialogContainer || dlg;
backdropParent.parentNode.insertBefore(backdrop, backdropParent);
dlg.backdrop = backdrop;
// trigger reflow or the backdrop will not animate
void backdrop.offsetWidth;
backdrop.classList.add('dialogBackdropOpened');
2020-06-17 01:19:59 +03:00
dom.addEventListener((dlg.dialogContainer || backdrop), 'click', e => {
if (e.target === dlg.dialogContainer) {
close(dlg);
}
2018-10-23 01:05:09 +03:00
}, {
passive: true
});
2020-06-17 01:19:59 +03:00
dom.addEventListener((dlg.dialogContainer || backdrop), 'contextmenu', e => {
if (e.target === dlg.dialogContainer) {
// Close the application dialog menu
close(dlg);
// Prevent the default browser context menu from appearing
e.preventDefault();
}
});
2018-10-23 01:05:09 +03:00
}
function isHistoryEnabled(dlg) {
return dlg.getAttribute('data-history') === 'true';
2018-10-23 01:05:09 +03:00
}
2020-06-17 01:19:59 +03:00
export function open(dlg) {
if (globalOnOpenCallback) {
globalOnOpenCallback(dlg);
}
2020-06-17 01:19:59 +03:00
const parent = dlg.parentNode;
if (parent) {
parent.removeChild(dlg);
}
2020-06-17 01:19:59 +03:00
const dialogContainer = document.createElement('div');
dialogContainer.classList.add('dialogContainer');
dialogContainer.appendChild(dlg);
dlg.dialogContainer = dialogContainer;
document.body.appendChild(dialogContainer);
2021-01-26 22:20:12 -05:00
return new Promise((resolve) => {
2020-06-17 01:19:59 +03:00
new DialogHashHandler(dlg, `dlg${new Date().getTime()}`, resolve);
});
2018-10-23 01:05:09 +03:00
}
function isOpened(dlg) {
//return dlg.opened;
return !dlg.classList.contains('hide');
2018-10-23 01:05:09 +03:00
}
2020-06-17 01:19:59 +03:00
export function close(dlg) {
if (isOpened(dlg)) {
if (isHistoryEnabled(dlg)) {
2021-09-06 16:47:46 +03:00
appRouter.back();
} else {
closeDialog(dlg);
}
}
2018-10-23 01:05:09 +03:00
}
function closeDialog(dlg) {
if (!dlg.classList.contains('hide')) {
dlg.dispatchEvent(new CustomEvent('closing', {
bubbles: false,
cancelable: false
2018-10-23 01:05:09 +03:00
}));
2020-06-17 01:19:59 +03:00
const onAnimationFinish = () => {
focusManager.popScope(dlg);
dlg.classList.add('hide');
dlg.dispatchEvent(new CustomEvent('close', {
bubbles: false,
cancelable: false
}));
};
animateDialogClose(dlg, onAnimationFinish);
2018-10-23 01:05:09 +03:00
}
}
function animateDialogOpen(dlg) {
2020-06-17 01:19:59 +03:00
const onAnimationFinish = () => {
focusManager.pushScope(dlg);
2020-03-15 02:18:27 +03:00
if (dlg.getAttribute('data-autofocus') === 'true') {
focusManager.autoFocus(dlg);
2020-03-15 02:18:27 +03:00
}
if (document.activeElement && !dlg.contains(document.activeElement)) {
// Blur foreign element to prevent triggering of an action from the previous scope
document.activeElement.blur();
}
2018-10-23 01:05:09 +03:00
};
2018-10-23 01:05:09 +03:00
if (enableAnimation()) {
2020-06-17 01:19:59 +03:00
const onFinish = () => {
2018-10-23 01:05:09 +03:00
dom.removeEventListener(dlg, dom.whichAnimationEvent(), onFinish, {
once: true
});
onAnimationFinish();
2018-10-23 01:05:09 +03:00
};
dom.addEventListener(dlg, dom.whichAnimationEvent(), onFinish, {
once: true
});
return;
2018-10-23 01:05:09 +03:00
}
onAnimationFinish();
2018-10-23 01:05:09 +03:00
}
function animateDialogClose(dlg, onAnimationFinish) {
if (enableAnimation()) {
2020-06-17 01:19:59 +03:00
let animated = true;
2018-10-23 01:05:09 +03:00
switch (dlg.animationConfig.exit.name) {
case 'fadeout':
2020-06-17 01:19:59 +03:00
dlg.style.animation = `fadeout ${dlg.animationConfig.exit.timing.duration}ms ease-out normal both`;
2018-10-23 01:05:09 +03:00
break;
case 'scaledown':
2020-06-17 01:19:59 +03:00
dlg.style.animation = `scaledown ${dlg.animationConfig.exit.timing.duration}ms ease-out normal both`;
2018-10-23 01:05:09 +03:00
break;
case 'slidedown':
2020-06-17 01:19:59 +03:00
dlg.style.animation = `slidedown ${dlg.animationConfig.exit.timing.duration}ms ease-out normal both`;
2018-10-23 01:05:09 +03:00
break;
default:
animated = false;
break;
2018-10-23 01:05:09 +03:00
}
2020-06-17 01:19:59 +03:00
const onFinish = () => {
2018-10-23 01:05:09 +03:00
dom.removeEventListener(dlg, dom.whichAnimationEvent(), onFinish, {
once: true
});
onAnimationFinish();
2018-10-23 01:05:09 +03:00
};
dom.addEventListener(dlg, dom.whichAnimationEvent(), onFinish, {
once: true
});
if (animated) {
return;
}
2018-10-23 01:05:09 +03:00
}
onAnimationFinish();
2018-10-23 01:05:09 +03:00
}
2020-06-17 01:19:59 +03:00
const supportsOverscrollBehavior = 'overscroll-behavior-y' in document.body.style;
2018-10-23 01:05:09 +03:00
function shouldLockDocumentScroll(options) {
if (options.lockScroll != null) {
return options.lockScroll;
}
if (options.size === 'fullscreen') {
return true;
}
if (supportsOverscrollBehavior && (options.size || !browser.touch)) {
return false;
}
if (options.size) {
return true;
}
return browser.touch;
2018-10-23 01:05:09 +03:00
}
function removeBackdrop(dlg) {
2020-06-17 01:19:59 +03:00
const backdrop = dlg.backdrop;
if (!backdrop) {
return;
}
dlg.backdrop = null;
2020-06-17 01:19:59 +03:00
const onAnimationFinish = () => {
tryRemoveElement(backdrop);
};
if (enableAnimation()) {
backdrop.classList.remove('dialogBackdropOpened');
// this is not firing animatonend
setTimeout(onAnimationFinish, 300);
return;
2018-10-23 01:05:09 +03:00
}
onAnimationFinish();
2018-10-23 01:05:09 +03:00
}
function centerFocus(elem, horiz, on) {
2020-08-14 08:46:34 +02:00
import('../../scripts/scrollHelper').then((scrollHelper) => {
2020-06-17 01:19:59 +03:00
const fn = on ? 'on' : 'off';
scrollHelper.centerFocus[fn](elem, horiz);
});
2018-10-23 01:05:09 +03:00
}
2020-12-01 13:31:04 -05:00
export function createDialog(options = {}) {
// If there's no native dialog support, use a plain div
// Also not working well in samsung tizen browser, content inside not clickable
// Just go ahead and always use a plain div because we're seeing issues overlaying absoltutely positioned content over a modal dialog
2020-06-17 01:19:59 +03:00
const dlg = document.createElement('div');
2020-12-01 13:31:04 -05:00
// Add an id so we can access the dialog element
if (options.id) {
dlg.id = options.id;
}
dlg.classList.add('focuscontainer');
dlg.classList.add('hide');
if (shouldLockDocumentScroll(options)) {
dlg.setAttribute('data-lockscroll', 'true');
}
2021-05-05 23:40:51 +03:00
if (options.enableHistory !== false) {
dlg.setAttribute('data-history', 'true');
}
// 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
if (options.modal !== false) {
dlg.setAttribute('modal', 'modal');
}
if (options.autoFocus !== false) {
dlg.setAttribute('data-autofocus', 'true');
}
2020-07-30 20:34:21 +02:00
const defaultEntryAnimation = 'scaleup';
const defaultExitAnimation = 'scaledown';
2020-06-17 01:19:59 +03:00
const entryAnimation = options.entryAnimation || defaultEntryAnimation;
const exitAnimation = options.exitAnimation || defaultExitAnimation;
// If it's not fullscreen then lower the default animation speed to make it open really fast
2020-06-17 01:19:59 +03:00
const entryAnimationDuration = options.entryAnimationDuration || (options.size !== 'fullscreen' ? 180 : 280);
const exitAnimationDuration = options.exitAnimationDuration || (options.size !== 'fullscreen' ? 120 : 220);
dlg.animationConfig = {
// scale up
'entry': {
name: entryAnimation,
timing: {
duration: entryAnimationDuration,
easing: 'ease-out'
}
},
// fade out
'exit': {
name: exitAnimation,
timing: {
duration: exitAnimationDuration,
easing: 'ease-out',
fill: 'both'
2018-10-23 01:05:09 +03:00
}
}
};
dlg.classList.add('dialog');
if (options.scrollX) {
dlg.classList.add('scrollX');
dlg.classList.add('smoothScrollX');
if (layoutManager.tv) {
centerFocus(dlg, true, true);
}
} else if (options.scrollY !== false) {
dlg.classList.add('smoothScrollY');
if (layoutManager.tv) {
centerFocus(dlg, false, true);
}
}
if (options.removeOnClose) {
dlg.setAttribute('data-removeonclose', 'true');
}
if (options.size) {
dlg.classList.add('dialog-fixedSize');
2020-06-17 01:19:59 +03:00
dlg.classList.add(`dialog-${options.size}`);
}
if (enableAnimation()) {
switch (dlg.animationConfig.entry.name) {
case 'fadein':
2020-06-17 01:19:59 +03:00
dlg.style.animation = `fadein ${entryAnimationDuration}ms ease-out normal`;
break;
case 'scaleup':
2020-06-17 01:19:59 +03:00
dlg.style.animation = `scaleup ${entryAnimationDuration}ms ease-out normal both`;
break;
case 'slideup':
2020-06-17 01:19:59 +03:00
dlg.style.animation = `slideup ${entryAnimationDuration}ms ease-out normal`;
break;
case 'slidedown':
2020-06-17 01:19:59 +03:00
dlg.style.animation = `slidedown ${entryAnimationDuration}ms ease-out normal`;
break;
default:
break;
}
}
return dlg;
}
2020-06-26 23:16:10 +03:00
export function setOnOpen(val) {
globalOnOpenCallback = val;
}
2020-06-17 01:19:59 +03:00
/* eslint-enable indent */
export default {
open: open,
close: close,
createDialog: createDialog,
2020-06-26 23:16:10 +03:00
setOnOpen: setOnOpen
2020-06-17 01:19:59 +03:00
};