jellyfin-web/dashboard-ui/components/paperdialoghelper.js

207 lines
6.1 KiB
JavaScript
Raw Normal View History

2015-12-14 08:43:03 -07:00
define(['paper-dialog', 'scale-up-animation', 'fade-out-animation', 'fade-in-animation'], function () {
2015-09-17 09:04:04 -07:00
2015-12-14 08:43:03 -07:00
function paperDialogHashHandler(dlg, hash, resolve, lockDocumentScroll) {
2015-09-17 09:04:04 -07:00
2015-12-14 08:43:03 -07:00
var self = this;
self.originalUrl = window.location.href;
var activeElement = document.activeElement;
2015-09-17 09:04:04 -07:00
2015-12-14 08:43:03 -07:00
function onHashChange(e) {
2015-09-17 09:04:04 -07:00
2015-12-14 08:43:03 -07:00
var isBack = self.originalUrl == window.location.href;
if (isBack || !dlg.opened) {
window.removeEventListener('popstate', onHashChange);
}
if (isBack) {
self.closedByBack = true;
dlg.close();
2015-09-17 09:04:04 -07:00
}
}
function onDialogClosed() {
if (lockDocumentScroll !== false) {
Dashboard.onPopupClose();
}
2015-09-17 14:26:06 -07:00
2015-12-14 08:43:03 -07:00
window.removeEventListener('popstate', onHashChange);
2015-09-17 09:04:04 -07:00
2015-12-14 08:43:03 -07:00
if (!self.closedByBack) {
var state = history.state || {};
if (state.dialogId == hash) {
2015-09-29 09:29:06 -07:00
history.back();
}
2015-09-17 09:04:04 -07:00
}
2015-12-14 08:43:03 -07:00
activeElement.focus();
2015-09-17 09:04:04 -07:00
2015-12-14 08:43:03 -07:00
if (dlg.getAttribute('data-removeonclose') == 'true') {
dlg.parentNode.removeChild(dlg);
}
//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);
}
dlg.addEventListener('iron-overlay-closed', onDialogClosed);
2015-09-17 09:04:04 -07:00
dlg.open();
if (lockDocumentScroll !== false) {
Dashboard.onPopupOpen();
}
2015-09-17 09:04:04 -07:00
2015-12-14 08:43:03 -07:00
var state = {
dialogId: hash,
navigate: false
};
history.pushState(state, "Dialog", hash);
2015-09-29 09:29:06 -07:00
2015-12-14 08:43:03 -07:00
jQuery.onStatePushed(state);
2015-09-29 09:29:06 -07:00
2015-12-14 08:43:03 -07:00
window.addEventListener('popstate', onHashChange);
2015-09-29 09:29:06 -07:00
}
2015-09-17 09:04:04 -07:00
2015-12-14 08:43:03 -07:00
function open(dlg) {
2015-09-17 09:04:04 -07:00
2015-12-14 08:43:03 -07:00
return new Promise(function (resolve, reject) {
2015-09-17 09:04:04 -07:00
2015-12-14 08:43:03 -07:00
new paperDialogHashHandler(dlg, 'dlg' + new Date().getTime(), resolve);
});
2015-09-17 09:04:04 -07:00
}
2015-09-29 09:29:06 -07:00
function close(dlg) {
2015-12-14 08:43:03 -07:00
if (dlg.opened) {
history.back();
}
}
2015-10-01 23:14:04 -07:00
2015-12-14 08:43:03 -07:00
function onDialogOpened(e) {
2015-10-01 23:14:04 -07:00
2015-12-14 08:43:03 -07:00
//Emby.FocusManager.autoFocus(e.target, true);
2015-09-29 09:29:06 -07:00
}
2015-10-13 12:22:45 -07:00
function createDialog(options) {
options = options || {};
2015-10-01 23:14:04 -07:00
var dlg = document.createElement('paper-dialog');
dlg.setAttribute('with-backdrop', 'with-backdrop');
dlg.setAttribute('role', 'alertdialog');
// 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
2015-10-17 18:18:29 -07:00
// but skip it in IE because it's causing the entire browser to hang
2015-10-25 13:10:59 -07:00
// Also have to disable for firefox because it's causing select elements to not be clickable
2015-12-14 08:43:03 -07:00
if (!browserInfo.msie && !browserInfo.mozilla) {
2015-10-26 11:12:20 -07:00
if (options.modal !== false) {
dlg.setAttribute('modal', 'modal');
}
2015-10-17 18:18:29 -07:00
}
2015-10-01 23:14:04 -07:00
2015-12-14 08:43:03 -07:00
// seeing max call stack size exceeded in the debugger with this
2015-10-01 23:14:04 -07:00
dlg.setAttribute('noAutoFocus', 'noAutoFocus');
2015-12-14 08:43:03 -07:00
// These don't seem to perform well on mobile
var defaultEntryAnimation = browserInfo.mobile ? 'fade-in-animation' : 'scale-up-animation';
dlg.entryAnimation = options.entryAnimation || defaultEntryAnimation;
2015-10-01 23:14:04 -07:00
dlg.exitAnimation = 'fade-out-animation';
2015-10-13 12:22:45 -07:00
2015-12-14 08:43:03 -07:00
dlg.animationConfig = {
// scale up
'entry': {
name: options.entryAnimation || defaultEntryAnimation,
node: dlg,
timing: { duration: options.entryAnimationDuration || 300, easing: 'ease-out' }
},
// fade out
'exit': {
name: 'fade-out-animation',
node: dlg,
timing: { duration: options.exitAnimationDuration || 400, easing: 'ease-in' }
}
};
2015-10-13 12:22:45 -07:00
2015-12-14 08:43:03 -07:00
if (options.size != 'auto') {
dlg.classList.add('popupEditor');
if (options.size == 'small') {
dlg.classList.add('small-paper-dialog');
}
else if (options.size == 'medium') {
dlg.classList.add('medium-paper-dialog');
} else {
dlg.classList.add('fullscreen-paper-dialog');
}
2015-10-13 12:22:45 -07:00
}
var theme = options.theme || 'b';
dlg.classList.add('ui-body-' + theme);
dlg.classList.add('background-theme-' + theme);
2015-10-01 23:14:04 -07:00
dlg.classList.add('smoothScrollY');
2015-12-14 08:43:03 -07:00
if (options.removeOnClose) {
dlg.setAttribute('data-removeonclose', 'true');
}
2015-10-01 23:14:04 -07:00
return dlg;
}
2015-10-08 10:28:26 -07:00
function positionTo(dlg, elem) {
2015-10-13 22:46:11 -07:00
2015-10-08 10:28:26 -07:00
var windowHeight = $(window).height();
// If the window height is under a certain amount, don't bother trying to position
// based on an element.
if (windowHeight >= 540) {
var pos = $(elem).offset();
pos.top += elem.offsetHeight / 2;
pos.left += elem.offsetWidth / 2;
// Account for margins
pos.top -= 24;
pos.left -= 24;
// Account for popup size - we can't predict this yet so just estimate
pos.top -= $(dlg).height() / 2;
pos.left -= $(dlg).width() / 2;
// Account for scroll position
pos.top -= $(window).scrollTop();
pos.left -= $(window).scrollLeft();
// Avoid showing too close to the bottom
pos.top = Math.min(pos.top, windowHeight - 300);
pos.left = Math.min(pos.left, $(window).width() - 300);
// Do some boundary checking
pos.top = Math.max(pos.top, 0);
pos.left = Math.max(pos.left, 0);
dlg.style.position = 'fixed';
dlg.style.left = pos.left + 'px';
dlg.style.top = pos.top + 'px';
}
}
2015-12-14 08:43:03 -07:00
window.PaperDialogHelper = {
open: open,
2015-10-01 23:14:04 -07:00
close: close,
2015-10-08 10:28:26 -07:00
createDialog: createDialog,
positionTo: positionTo
2015-09-17 09:04:04 -07:00
};
2015-12-14 08:43:03 -07:00
return PaperDialogHelper;
});