define(["browser","dom","layoutManager"],function(browser,dom,layoutManager){"use strict";var mainAnimatedPages,onBeforeChange,allPages=[],currentUrls=[],selectedPageIndex=-1;function setControllerClass(view,options){if(options.controllerFactory)return Promise.resolve();var controllerUrl=view.getAttribute("data-controller");return controllerUrl?(0===controllerUrl.indexOf("__plugin/")&&(controllerUrl=controllerUrl.substring("__plugin/".length)),controllerUrl=Dashboard.getConfigurationResourceUrl(controllerUrl),require([controllerUrl]).then(function(deps){options.controllerFactory=deps[0]})):Promise.resolve()}function replaceAll(str,find,replace){return str.split(find).join(replace)}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")}return{loadView:function(options){if(!options.cancel){var selected=selectedPageIndex,previousAnimatable=-1===selected?null:allPages[selected],pageIndex=selected+1;3<=pageIndex&&(pageIndex=0);var isPluginpage=options.isPluginPage,newViewInfo=function(options,isPluginpage){var viewHtml=options.view,hasScript=!!isPluginpage&&-1!==viewHtml.indexOf("<script"),elem=function(html,hasScript){hasScript&&(html=replaceAll(html=replaceAll(html,"\x3c!--<script","<script"),"<\/script>--\x3e","<\/script>"));var wrapper=document.createElement("div");return wrapper.innerHTML=html,wrapper.querySelector('div[data-role="page"],.view')}(viewHtml,hasScript);hasScript=hasScript&&null!=elem.querySelector("script");var hasjQuery=!1;isPluginpage&&(hasjQuery=-1!==viewHtml.indexOf("jQuery")||-1!==viewHtml.indexOf("$(")||-1!==viewHtml.indexOf("$."));return{elem:elem,hasScript:hasScript,hasjQuery:hasjQuery}}(options,isPluginpage),newView=newViewInfo.elem,dependencies="string"==typeof newView?null:newView.getAttribute("data-require");dependencies=dependencies?dependencies.split(","):[],isPluginpage&&dependencies.push("legacy/dashboard"),newViewInfo.hasjQuery&&dependencies.push("jQuery"),(isPluginpage||newView.classList&&newView.classList.contains("type-interior"))&&dependencies.push("dashboardcss");dependencies.join(",");return require(dependencies).then(function(){var currentPage=allPages[pageIndex];currentPage&&function(view){view.dispatchEvent(new CustomEvent("viewdestroy",{cancelable:!1}))}(currentPage);var view=newView;"string"==typeof view&&((view=document.createElement("div")).innerHTML=newView),view.classList.add("page"),function(detail){var windowScroll=detail.windowScroll;return!(2!==windowScroll||layoutManager.tv||!browser.iOS)||!0===windowScroll}(options)&&view.classList.add("page-windowScroll"),mainAnimatedPages||((mainAnimatedPages=document.querySelector(".mainAnimatedPages")).innerHTML=""),currentPage?newViewInfo.hasScript&&window.$?(view=window.$(view).appendTo(mainAnimatedPages)[0],mainAnimatedPages.removeChild(currentPage)):mainAnimatedPages.replaceChild(view,currentPage):newViewInfo.hasScript&&window.$?view=window.$(view).appendTo(mainAnimatedPages)[0]:mainAnimatedPages.appendChild(view),options.type&&view.setAttribute("data-type",options.type);var properties=[];options.fullscreen&&properties.push("fullscreen"),properties.length&&view.setAttribute("data-properties",properties.join(","));return setControllerClass(allPages[pageIndex]=view,options).then(function(){return onBeforeChange&&onBeforeChange(view,!1,options),beforeAnimate(allPages,pageIndex,selected),currentUrls[selectedPageIndex=pageIndex]=options.url,!options.cancel&&previousAnimatable&&afterAnimate(allPages,pageIndex),window.$&&(window.$.mobile=window.$.mobile||{},window.$.mobile.activePage=view),view})})}},tryRestoreView:function(options){var url=options.url,index=currentUrls.indexOf(url);if(-1!==index){var animatable=allPages[index],view=animatable;if(view){if(options.cancel)return;var selected=selectedPageIndex,previousAnimatable=-1===selected?null:allPages[selected];return setControllerClass(view,options).then(function(){return onBeforeChange&&onBeforeChange(view,!0,options),beforeAnimate(allPages,index,selected),animatable.classList.remove("hide"),selectedPageIndex=index,!options.cancel&&previousAnimatable&&afterAnimate(allPages,index),window.$&&(window.$.mobile=window.$.mobile||{},window.$.mobile.activePage=view),view})}}return Promise.reject()},setOnBeforeChange:function(fn){onBeforeChange=fn}}});