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

1 line
7.4 KiB
JavaScript
Raw Normal View History

2017-04-20 13:16:45 -07:00
define(["historyManager","focusManager","browser","layoutManager","inputManager","dom","css!./dialoghelper.css","scrollStyles"],function(historyManager,focusManager,browser,layoutManager,inputManager,dom){"use strict";function enableAnimation(){return!browser.tv&&(!browser.noAnimation&&browser.supportsCssAnimation())}function removeCenterFocus(dlg){layoutManager.tv&&(dlg.classList.contains("smoothScrollX")?centerFocus(dlg,!0,!1):dlg.classList.contains("smoothScrollY")&&centerFocus(dlg,!1,!1))}function tryRemoveElement(elem){var parentNode=elem.parentNode;if(parentNode)try{parentNode.removeChild(elem)}catch(err){console.log("Error removing dialog element: "+err)}}function DialogHashHandler(dlg,hash,resolve){function onHashChange(e){var isBack=self.originalUrl===window.location.href;!isBack&&isOpened(dlg)||window.removeEventListener("popstate",onHashChange),isBack&&(self.closedByBack=!0,closeDialog(dlg))}function onBackCommand(e){"back"===e.detail.command&&(self.closedByBack=!0,e.preventDefault(),e.stopPropagation(),closeDialog(dlg))}function onDialogClosed(){if(isHistoryEnabled(dlg)||inputManager.off(dlg,onBackCommand),window.removeEventListener("popstate",onHashChange),removeBackdrop(dlg),dlg.classList.remove("opened"),removeScrollLockOnClose&&document.body.classList.remove("noScroll"),!self.closedByBack&&isHistoryEnabled(dlg)){var state=history.state||{};state.dialogId===hash&&history.back()}if(layoutManager.tv&&activeElement.focus(),"false"!==dlg.getAttribute("data-removeonclose")){removeCenterFocus(dlg);var dialogContainer=dlg.dialogContainer;dialogContainer?(tryRemoveElement(dialogContainer),dlg.dialogContainer=null):tryRemoveElement(dlg)}setTimeout(function(){resolve({element:dlg,closedByBack:self.closedByBack})},1)}var self=this;self.originalUrl=window.location.href;var activeElement=document.activeElement,removeScrollLockOnClose=!1;dlg.addEventListener("close",onDialogClosed);var center=!dlg.classList.contains("dialog-fixedSize");center&&dlg.classList.add("centeredDialog"),dlg.classList.remove("hide"),addBackdropOverlay(dlg),dlg.classList.add("opened"),dlg.dispatchEvent(new CustomEvent("open",{bubbles:!1,cancelable:!1})),"true"!==dlg.getAttribute("data-lockscroll")||document.body.classList.contains("noScroll")||(document.body.classList.add("noScroll"),removeScrollLockOnClose=!0),animateDialogOpen(dlg),isHistoryEnabled(dlg)?(historyManager.pushState({dialogId:hash},"Dialog","#"+hash),window.addEventListener("popstate",onHashChange)):inputManager.on(dlg,onBackCommand)}function addBackdropOverlay(dlg){var backdrop=document.createElement("div");backdrop.classList.add("dialogBackdrop");var backdropParent=dlg.dialogContainer||dlg;backdropParent.parentNode.insertBefore(backdrop,backdropParent),dlg.backdrop=backdrop,void backdrop.offsetWidth,backdrop.classList.add("dialogBackdropOpened"),dom.addEventListener(dlg.dialogContainer||backdrop,"click",function(e){e.target===dlg.dialogContainer&&close(dlg)},{passive:!0})}function isHistoryEnabled(dlg){return"true"===dlg.getAttribute("data-history")}function open(dlg){globalOnOpenCallback&&globalOnOpenCallback(dlg);var parent=dlg.parentNode;parent&&parent.removeChild(dlg);var dialogContainer=document.createElement("div");return dialogContainer.classList.add("dialogContainer"),dialogContainer.appendChild(dlg),dlg.dialogContainer=dialogContainer,document.body.appendChild(dialogContainer),new Promise(function(resolve,reject){new DialogHashHandler(dlg,"dlg"+(new Date).getTime(),resolve)})}function isOpened(dlg){return!dlg.classList.contains("hide")}function close(dlg){isOpened(dlg)&&(isHistoryEnabled(dlg)?history.back():closeDialog(dlg))}function closeDialog(dlg){if(!dlg.classList.contains("hide")){dlg.dispatchEvent(new CustomEvent("closing",{bubbles:!1,cancelable:!1}));var onAnimationFinish=function(){focusManager.popScope(dlg),dlg.classList.add("hide"),dlg.dispatchEvent(new CustomEvent("close",{bubbles:!1,cancelable:!1}))};animateDialogClose(dlg,onAnimationFinish)}}function animateDialogOpen(dlg){var onAnimationFinish=function(){focusManager.pushScope(dlg),"true"===dlg.getAttribut