define(['browser'], function (browser) { var mainAnimatedPages = document.querySelector('.mainAnimatedPages'); var allPages = []; var currentUrls = []; var pageContainerCount = 3; var selectedPageIndex = -1; function enableAnimation() { if (browser.tv) { return false; } if (browser.safari) { // Right now they don't look good. Haven't figured out why yet. return false; } return true; } function loadView(options) { if (options.cancel) { return; } cancelActiveAnimations(); var selected = selectedPageIndex; var previousAnimatable = selected == -1 ? null : allPages[selected]; var pageIndex = selected + 1; if (pageIndex >= pageContainerCount) { pageIndex = 0; } var newViewInfo = normalizeNewView(options); var newView = newViewInfo.elem; var dependencies = typeof (newView) == 'string' ? null : newView.getAttribute('data-require'); dependencies = dependencies ? dependencies.split(',') : []; var isPluginpage = options.url.toLowerCase().indexOf('/configurationpage?') != -1; if (isPluginpage) { dependencies.push('jqmpopup'); dependencies.push('jqmcollapsible'); dependencies.push('jqmcheckbox'); dependencies.push('legacy/dashboard'); dependencies.push('legacy/selectmenu'); dependencies.push('jqmcontrolgroup'); dependencies.push('jqmlistview'); } if (isPluginpage || (newView.classList && newView.classList.contains('type-interior'))) { dependencies.push('scripts/notifications'); dependencies.push('css!css/notifications.css'); dependencies.push('dashboardcss'); } return new Promise(function (resolve, reject) { require(dependencies, function () { var currentPage = allPages[pageIndex]; if (currentPage) { triggerDestroy(currentPage); } var view = newView; if (typeof (view) == 'string') { view = document.createElement('div'); view.innerHTML = newView; } view.classList.add('mainAnimatedPage'); if (currentPage) { if (newViewInfo.hasScript && window.$) { // TODO: figure this out without jQuery view = $(view).appendTo(mainAnimatedPages)[0]; mainAnimatedPages.removeChild(currentPage); } else { mainAnimatedPages.replaceChild(view, currentPage); } } else { if (newViewInfo.hasScript && window.$) { // TODO: figure this out without jQuery view = $(view).appendTo(mainAnimatedPages)[0]; } else { mainAnimatedPages.appendChild(view); } } if (typeof (newView) != 'string') { enhanceNewView(dependencies, view); } if (options.type) { view.setAttribute('data-type', options.type); } var animatable = view; allPages[pageIndex] = view; if (onBeforeChange) { onBeforeChange(view, false, options); } beforeAnimate(allPages, pageIndex, selected); // animate here animate(animatable, previousAnimatable, options.transition, options.isBack).then(function () { selectedPageIndex = pageIndex; currentUrls[pageIndex] = options.url; if (!options.cancel && previousAnimatable) { afterAnimate(allPages, pageIndex); } // Temporary hack // If a view renders UI in viewbeforeshow the lazy image loader will think the images aren't visible and won't load images // The views need to be updated to start loading data in beforeshow, but not render until show document.dispatchEvent(new CustomEvent('scroll', {})); if (window.$) { $.mobile = $.mobile || {}; $.mobile.activePage = view; } resolve(view); }); }); }); } function enhanceNewView(dependencies, newView) { var hasJqm = false; for (var i = 0, length = dependencies.length; i < length; i++) { if (dependencies[i].indexOf('jqm') == 0) { hasJqm = true; break; } } if (hasJqm && window.$) { $(newView).trigger('create'); } } function replaceAll(str, find, replace) { return str.split(find).join(replace); } function parseHtml(html, hasScript) { if (hasScript) { html = replaceAll(html, '', ''); } var wrapper = document.createElement('div'); wrapper.innerHTML = html; return 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('