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

257 lines
7.7 KiB
JavaScript
Raw Normal View History

2016-03-02 23:24:46 -07:00
define(['historyManager', 'focusManager', 'browser', 'layoutManager', 'inputManager', 'paper-dialog', 'scale-up-animation', 'fade-out-animation', 'fade-in-animation', 'css!./paperdialoghelper.css'], function (historyManager, focusManager, browser, layoutManager, inputManager) {
2016-01-29 19:43:11 -07:00
2016-01-30 21:04:00 -07:00
function paperDialogHashHandler(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;
if (isBack || !dlg.opened) {
window.removeEventListener('popstate', onHashChange);
}
if (isBack) {
self.closedByBack = true;
dlg.close();
}
}
2016-03-02 23:24:46 -07:00
function onBackCommand(e) {
inputManager.off(dlg, onBackCommand);
self.closedByBack = true;
dlg.close();
e.preventDefault();
}
2016-01-29 19:43:11 -07:00
function onDialogClosed() {
2016-01-30 13:59:09 -07:00
if (removeScrollLockOnClose) {
document.body.classList.remove('noScroll');
2016-01-29 19:43:11 -07:00
}
window.removeEventListener('popstate', onHashChange);
2016-03-02 23:24:46 -07:00
inputManager.off(dlg, onBackCommand);
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();
}
}
activeElement.focus();
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);
dlg.open();
2016-02-04 11:19:10 -07:00
// It's not being positioned properly in firefox
2016-02-04 21:56:19 -07:00
if (!browser.chrome && !dlg.classList.contains('fixedSize')) {
2016-02-04 11:19:10 -07:00
setTimeout(function () {
dlg.refit();
}, 100);
}
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-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
}
}
function isHistoryEnabled(dlg) {
return dlg.getAttribute('data-history') == 'true';
2016-01-29 19:43:11 -07:00
}
function open(dlg) {
return new Promise(function (resolve, reject) {
new paperDialogHashHandler(dlg, 'dlg' + new Date().getTime(), resolve);
});
}
function close(dlg) {
if (dlg.opened) {
2016-02-01 10:02:17 -07:00
if (isHistoryEnabled(dlg)) {
history.back();
} else {
dlg.close();
}
2016-01-29 19:43:11 -07:00
}
}
function onDialogOpened(e) {
focusManager.autoFocus(e.target);
}
2016-01-30 21:04:00 -07:00
function shouldLockDocumentScroll(options) {
if (options.lockScroll != null) {
return options.lockScroll;
}
if (options.size == 'fullscreen') {
return true;
}
return browser.mobile;
}
2016-01-29 19:43:11 -07:00
function createDialog(options) {
options = options || {};
var dlg = document.createElement('paper-dialog');
dlg.setAttribute('with-backdrop', 'with-backdrop');
dlg.setAttribute('role', 'alertdialog');
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');
}
// seeing max call stack size exceeded in the debugger with this
dlg.setAttribute('noAutoFocus', 'noAutoFocus');
2016-03-02 23:24:46 -07:00
var defaultEntryAnimation = browser.animate && !browser.mobile ? 'scale-up-animation' : 'fade-in-animation';
2016-01-29 19:43:11 -07:00
dlg.entryAnimation = options.entryAnimation || defaultEntryAnimation;
dlg.exitAnimation = 'fade-out-animation';
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
var entryAnimationDuration = options.entryAnimationDuration || (options.size ? 240 : 300);
2016-01-29 19:43:11 -07:00
dlg.animationConfig = {
// scale up
'entry': {
2016-01-30 14:25:00 -07:00
name: dlg.entryAnimation,
2016-01-29 19:43:11 -07:00
node: dlg,
2016-01-30 14:15:04 -07:00
timing: { duration: entryAnimationDuration, easing: 'ease-out' }
2016-01-29 19:43:11 -07:00
},
// fade out
'exit': {
2016-01-30 14:25:00 -07:00
name: dlg.exitAnimation,
2016-01-29 19:43:11 -07:00
node: dlg,
timing: { duration: options.exitAnimationDuration || 400, easing: 'ease-in' }
}
};
2016-02-15 21:54:20 -07:00
// too buggy in IE, not even worth it
2016-02-26 21:38:30 -07:00
if (!browser.animate) {
2016-02-15 21:54:20 -07:00
dlg.animationConfig = null;
dlg.entryAnimation = null;
dlg.exitAnimation = null;
}
2016-01-29 19:43:11 -07:00
dlg.classList.add('paperDialog');
dlg.classList.add('scrollY');
2016-02-04 11:19:10 -07:00
if (layoutManager.tv || layoutManager.mobile) {
// Need scrollbars for mouse use
dlg.classList.add('hiddenScroll');
}
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) {
dlg.classList.add('fixedSize');
dlg.classList.add(options.size);
}
if (options.autoFocus !== false) {
dlg.addEventListener('iron-overlay-opened', onDialogOpened);
}
2016-01-29 19:43:11 -07:00
return dlg;
}
function positionTo(dlg, elem) {
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';
}
}
return {
open: open,
close: close,
createDialog: createDialog,
positionTo: positionTo
};
});