jellyfin-web/dashboard-ui/components/viewcontainer-lite.js
2017-01-27 22:16:59 -05:00

1 line
7.3 KiB
JavaScript

define(["browser"],function(browser){"use strict";function enableAnimation(){return!browser.tv&&!browser.safari}function loadView(options){if(!options.cancel){cancelActiveAnimations();var selected=selectedPageIndex,previousAnimatable=selected==-1?null:allPages[selected],pageIndex=selected+1;pageIndex>=pageContainerCount&&(pageIndex=0);var newViewInfo=normalizeNewView(options),newView=newViewInfo.elem,dependencies="string"==typeof newView?null:newView.getAttribute("data-require");dependencies=dependencies?dependencies.split(","):[];var isPluginpage=options.url.toLowerCase().indexOf("/configurationpage?")!=-1;return isPluginpage&&(dependencies.push("jqmpopup"),dependencies.push("legacy/dashboard"),dependencies.push("legacy/selectmenu"),dependencies.push("jqmlistview"),dependencies.push("fnchecked")),(isPluginpage||newView.classList&&newView.classList.contains("type-interior"))&&(dependencies.push("scripts/notifications"),dependencies.push("css!css/notifications.css"),dependencies.push("dashboardcss")),new Promise(function(resolve,reject){require(dependencies,function(){var currentPage=allPages[pageIndex];currentPage&&triggerDestroy(currentPage);var view=newView;"string"==typeof view&&(view=document.createElement("div"),view.innerHTML=newView),view.classList.add("mainAnimatedPage"),currentPage?newViewInfo.hasScript&&window.$?(view=$(view).appendTo(mainAnimatedPages)[0],mainAnimatedPages.removeChild(currentPage)):mainAnimatedPages.replaceChild(view,currentPage):newViewInfo.hasScript&&window.$?view=$(view).appendTo(mainAnimatedPages)[0]:mainAnimatedPages.appendChild(view),"string"!=typeof newView&&enhanceNewView(dependencies,view),options.type&&view.setAttribute("data-type",options.type);var properties=[];options.fullscreen&&properties.push("fullscreen"),properties.length&&view.setAttribute("data-properties",properties.join(","));var animatable=view;allPages[pageIndex]=view,onBeforeChange&&onBeforeChange(view,!1,options),beforeAnimate(allPages,pageIndex,selected),animate(animatable,previousAnimatable,options.transition,options.isBack).then(function(){selectedPageIndex=pageIndex,currentUrls[pageIndex]=options.url,!options.cancel&&previousAnimatable&&afterAnimate(allPages,pageIndex),document.dispatchEvent(new CustomEvent("scroll",{})),window.$&&($.mobile=$.mobile||{},$.mobile.activePage=view),resolve(view)})})})}}function enhanceNewView(dependencies,newView){for(var hasJqm=!1,i=0,length=dependencies.length;i<length;i++)if(0==dependencies[i].indexOf("jqm")){hasJqm=!0;break}hasJqm&&window.$&&$(newView).trigger("create")}function replaceAll(str,find,replace){return str.split(find).join(replace)}function parseHtml(html,hasScript){hasScript&&(html=replaceAll(html,"<!--<script","<script"),html=replaceAll(html,"</script>-->","</script>"));var wrapper=document.createElement("div");return wrapper.innerHTML=html,wrapper.querySelector('div[data-role="page"]')}function normalizeNewView(options){if(options.view.indexOf('data-role="page"')==-1)return options.view;var hasScript=options.view.indexOf("<script")!=-1,elem=parseHtml(options.view,hasScript);return hasScript&&(hasScript=null!=elem.querySelector("script")),{elem:elem,hasScript:hasScript}}function beforeAnimate(allPages,newPageIndex,oldPageIndex){for(var i=0,length=allPages.length;i<length;i++)newPageIndex==i||oldPageIndex==i||allPages[i].classList.add("hide")}function afterAnimate(allPages,newPageIndex){for(var i=0,length=allPages.length;i<length;i++)newPageIndex==i||allPages[i].classList.add("hide")}function animate(newAnimatedPage,oldAnimatedPage,transition,isBack){if(transition=transition||"fade",enableAnimation()&&oldAnimatedPage&&newAnimatedPage.animate){if("slide"==transition)return slideLeft(newAnimatedPage,oldAnimatedPage,transition,isBack);if("slidedown"==transition)return slideDown(newAnimatedPage,oldAnimatedPage,transition,isBack);if("fade"==transition)return fade(newAnimatedPage,oldAnimatedPage,transition,isBack)}return Promise.resolve()}function slideLeft(newAnimatedPage,oldAnimatedPage,transition,isBack){return new Promise(function(resolve,reject){var timings={duration:450,iterations:1,easing:"ease-out"};browser.chrome||(timings.fill="both");var animations=[];if(oldAnimatedPage){var destination=isBack?"100%":"-100%";animations.push(oldAnimatedPage.animate([{transform:"none",offset:0},{transform:"translate3d("+destination+", 0, 0)",offset:1}],timings))}var start=isBack?"-100%":"100%";animations.push(newAnimatedPage.animate([{transform:"translate3d("+start+", 0, 0)",offset:0},{transform:"none",offset:1}],timings)),currentAnimations=animations,animations[animations.length-1].onfinish=resolve})}function slideDown(newAnimatedPage,oldAnimatedPage,transition,isBack){return new Promise(function(resolve,reject){var timings={duration:450,iterations:1,easing:"ease-out"};browser.chrome||(timings.fill="both");var animations=[];if(oldAnimatedPage){var destination=isBack?"100%":"-100%";animations.push(oldAnimatedPage.animate([{transform:"none",offset:0},{transform:"translate3d("+destination+", 0, 0)",offset:1}],timings))}var start=isBack?"100%":"-100%";animations.push(newAnimatedPage.animate([{transform:"translate3d(0, "+start+", 0)",offset:0},{transform:"none",offset:1}],timings)),currentAnimations=animations,animations[animations.length-1].onfinish=resolve})}function fade(newAnimatedPage,oldAnimatedPage,transition,isBack){return new Promise(function(resolve,reject){var timings={duration:160,iterations:1,easing:"ease-out"};browser.chrome||(timings.fill="both");var animations=[];oldAnimatedPage&&animations.push(oldAnimatedPage.animate([{opacity:1,offset:0},{opacity:0,offset:1}],timings)),animations.push(newAnimatedPage.animate([{opacity:0,offset:0},{opacity:1,offset:1}],timings)),currentAnimations=animations,animations[animations.length-1].onfinish=resolve})}function cancelActiveAnimations(){for(var animations=currentAnimations,i=0,length=animations.length;i<length;i++)cancelAnimation(animations[i])}function cancelAnimation(animation){try{animation.cancel()}catch(err){console.log("Error canceling animation: "+err)}}function setOnBeforeChange(fn){onBeforeChange=fn}function tryRestoreView(options){var url=options.url,index=currentUrls.indexOf(url);if(index!=-1){var animatable=allPages[index],view=animatable;if(view){if(options.cancel)return;cancelActiveAnimations();var selected=selectedPageIndex,previousAnimatable=selected==-1?null:allPages[selected];return onBeforeChange&&onBeforeChange(view,!0,options),beforeAnimate(allPages,index,selected),animatable.classList.remove("hide"),animate(animatable,previousAnimatable,options.transition,options.isBack).then(function(){return selectedPageIndex=index,!options.cancel&&previousAnimatable&&afterAnimate(allPages,index),document.dispatchEvent(new CustomEvent("scroll",{})),window.$&&($.mobile=$.mobile||{},$.mobile.activePage=view),view})}}return Promise.reject()}function triggerDestroy(view){view.dispatchEvent(new CustomEvent("viewdestroy",{}))}function reset(){allPages=[],currentUrls=[],mainAnimatedPages.innerHTML="",selectedPageIndex=-1}var onBeforeChange,mainAnimatedPages=document.querySelector(".mainAnimatedPages"),allPages=[],currentUrls=[],pageContainerCount=3,selectedPageIndex=-1,currentAnimations=[];return enableAnimation()&&!document.documentElement.animate&&require(["webAnimations"]),reset(),mainAnimatedPages.classList.remove("hide"),{loadView:loadView,tryRestoreView:tryRestoreView,reset:reset,setOnBeforeChange:setOnBeforeChange}});