2016-05-15 18:49:01 -07:00
|
|
|
define(['browser'], function (browser) {
|
2016-03-14 13:27:35 -07:00
|
|
|
|
2016-05-15 18:49:01 -07:00
|
|
|
var allPages = document.querySelectorAll('.mainAnimatedPage');
|
2016-05-16 08:47:28 -07:00
|
|
|
var currentUrls = [];
|
2016-05-15 18:49:01 -07:00
|
|
|
var pageContainerCount = allPages.length;
|
|
|
|
var allowAnimation = true;
|
|
|
|
var selectedPageIndex = -1;
|
|
|
|
|
|
|
|
function enableAnimation() {
|
|
|
|
|
|
|
|
if (!allowAnimation) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (browser.tv) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2016-03-14 13:27:35 -07:00
|
|
|
|
|
|
|
function loadView(options) {
|
|
|
|
|
2016-05-15 18:49:01 -07:00
|
|
|
if (options.cancel) {
|
|
|
|
return;
|
|
|
|
}
|
2016-03-14 13:27:35 -07:00
|
|
|
|
2016-05-15 18:49:01 -07:00
|
|
|
cancelActiveAnimations();
|
2016-03-14 13:27:35 -07:00
|
|
|
|
2016-05-15 18:49:01 -07:00
|
|
|
var selected = getSelectedIndex(allPages);
|
|
|
|
var previousAnimatable = selected == -1 ? null : allPages[selected];
|
|
|
|
var pageIndex = selected + 1;
|
2016-03-14 13:27:35 -07:00
|
|
|
|
2016-05-15 18:49:01 -07:00
|
|
|
if (pageIndex >= pageContainerCount) {
|
|
|
|
pageIndex = 0;
|
|
|
|
}
|
2016-03-14 13:27:35 -07:00
|
|
|
|
2016-05-15 18:49:01 -07:00
|
|
|
var newViewInfo = normalizeNewView(options);
|
|
|
|
var newView = newViewInfo.elem;
|
2016-03-14 13:27:35 -07:00
|
|
|
|
2016-05-15 18:49:01 -07:00
|
|
|
var dependencies = typeof (newView) == 'string' ? null : newView.getAttribute('data-require');
|
|
|
|
dependencies = dependencies ? dependencies.split(',') : [];
|
2016-03-14 13:27:35 -07:00
|
|
|
|
2016-05-15 18:49:01 -07:00
|
|
|
var isPluginpage = options.url.toLowerCase().indexOf('/configurationpage?') != -1;
|
2016-03-14 13:27:35 -07:00
|
|
|
|
2016-05-15 18:49:01 -07:00
|
|
|
if (isPluginpage) {
|
|
|
|
dependencies.push('jqmpopup');
|
|
|
|
dependencies.push('jqmcollapsible');
|
|
|
|
dependencies.push('jqmcheckbox');
|
|
|
|
dependencies.push('legacy/dashboard');
|
|
|
|
dependencies.push('legacy/selectmenu');
|
|
|
|
dependencies.push('jqmcontrolgroup');
|
|
|
|
}
|
2016-03-15 21:13:57 -07:00
|
|
|
|
2016-05-15 18:49:01 -07:00
|
|
|
if (isPluginpage || (newView.classList && newView.classList.contains('type-interior'))) {
|
|
|
|
dependencies.push('jqmlistview');
|
|
|
|
dependencies.push('scripts/notifications');
|
|
|
|
}
|
2016-03-15 21:13:57 -07:00
|
|
|
|
2016-05-15 18:49:01 -07:00
|
|
|
return new Promise(function (resolve, reject) {
|
2016-03-15 21:13:57 -07:00
|
|
|
|
2016-03-15 11:03:25 -07:00
|
|
|
require(dependencies, function () {
|
2016-05-15 18:49:01 -07:00
|
|
|
|
2016-03-15 11:03:25 -07:00
|
|
|
var animatable = allPages[pageIndex];
|
2016-03-14 13:27:35 -07:00
|
|
|
|
2016-03-15 11:03:25 -07:00
|
|
|
var currentPage = animatable.querySelector('.page-view');
|
|
|
|
|
|
|
|
if (currentPage) {
|
|
|
|
triggerDestroy(currentPage);
|
|
|
|
}
|
|
|
|
|
2016-05-15 18:49:01 -07:00
|
|
|
var view;
|
2016-03-14 13:27:35 -07:00
|
|
|
|
2016-03-15 11:03:25 -07:00
|
|
|
if (typeof (newView) == 'string') {
|
|
|
|
animatable.innerHTML = newView;
|
2016-05-15 18:49:01 -07:00
|
|
|
view = animatable.querySelector('.page-view');
|
2016-03-14 22:25:47 -07:00
|
|
|
} else {
|
2016-03-15 22:33:31 -07:00
|
|
|
if (newViewInfo.hasScript) {
|
|
|
|
// TODO: figure this out without jQuery
|
2016-05-15 18:49:01 -07:00
|
|
|
animatable.innerHTML = '';
|
2016-03-15 22:33:31 -07:00
|
|
|
$(newView).appendTo(animatable);
|
|
|
|
} else {
|
2016-05-15 18:49:01 -07:00
|
|
|
if (currentPage) {
|
|
|
|
animatable.replaceChild(newView, currentPage);
|
|
|
|
} else {
|
|
|
|
animatable.appendChild(newView);
|
|
|
|
}
|
2016-03-15 22:33:31 -07:00
|
|
|
}
|
2016-03-15 21:13:57 -07:00
|
|
|
enhanceNewView(dependencies, newView);
|
2016-05-15 18:49:01 -07:00
|
|
|
view = newView;
|
2016-03-14 22:25:47 -07:00
|
|
|
}
|
2016-03-14 13:27:35 -07:00
|
|
|
|
2016-03-15 11:03:25 -07:00
|
|
|
if (onBeforeChange) {
|
|
|
|
onBeforeChange(view, false, options);
|
|
|
|
}
|
2016-03-14 13:27:35 -07:00
|
|
|
|
2016-05-15 18:49:01 -07:00
|
|
|
beforeAnimate(allPages, pageIndex, selected);
|
|
|
|
// animate here
|
|
|
|
animate(animatable, previousAnimatable, options.transition, options.isBack).then(function () {
|
2016-03-14 13:27:35 -07:00
|
|
|
|
2016-05-15 18:49:01 -07:00
|
|
|
selectedPageIndex = pageIndex;
|
2016-05-16 08:47:28 -07:00
|
|
|
currentUrls[pageIndex] = options.url;
|
2016-05-15 18:49:01 -07:00
|
|
|
if (!options.cancel && previousAnimatable) {
|
|
|
|
afterAnimate(allPages, pageIndex);
|
|
|
|
}
|
|
|
|
|
2016-05-16 11:20:08 -07:00
|
|
|
// 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', {}));
|
|
|
|
|
2016-05-15 18:49:01 -07:00
|
|
|
$.mobile = $.mobile || {};
|
|
|
|
$.mobile.activePage = view;
|
|
|
|
|
|
|
|
resolve(view);
|
|
|
|
});
|
2016-03-15 11:03:25 -07:00
|
|
|
});
|
|
|
|
});
|
2016-03-14 13:27:35 -07:00
|
|
|
}
|
|
|
|
|
2016-03-15 21:13:57 -07:00
|
|
|
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) {
|
|
|
|
$(newView).trigger('create');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-17 23:52:47 -07:00
|
|
|
function replaceAll(str, find, replace) {
|
|
|
|
|
|
|
|
return str.split(find).join(replace);
|
|
|
|
}
|
|
|
|
|
2016-03-18 09:23:55 -07:00
|
|
|
function parseHtml(html, hasScript) {
|
2016-03-15 22:33:31 -07:00
|
|
|
|
2016-03-18 09:23:55 -07:00
|
|
|
if (hasScript) {
|
|
|
|
html = replaceAll(html, '<!--<script', '<script');
|
|
|
|
html = replaceAll(html, '</script>-->', '</script>');
|
|
|
|
}
|
2016-03-17 23:52:47 -07:00
|
|
|
|
2016-03-15 22:33:31 -07:00
|
|
|
var wrapper = document.createElement('div');
|
|
|
|
wrapper.innerHTML = html;
|
|
|
|
return wrapper.querySelector('div[data-role="page"]');
|
|
|
|
}
|
|
|
|
|
2016-03-15 11:03:25 -07:00
|
|
|
function normalizeNewView(options) {
|
2016-03-14 13:27:35 -07:00
|
|
|
|
2016-03-15 11:03:25 -07:00
|
|
|
if (options.view.indexOf('data-role="page"') == -1) {
|
2016-05-16 08:47:28 -07:00
|
|
|
var html = '<div class="page-view" data-type="' + (options.type || '') + '">';
|
2016-03-15 11:03:25 -07:00
|
|
|
html += options.view;
|
|
|
|
html += '</div>';
|
|
|
|
return html;
|
|
|
|
}
|
2016-03-14 13:27:35 -07:00
|
|
|
|
2016-03-18 09:23:55 -07:00
|
|
|
var hasScript = options.view.indexOf('<script') != -1;
|
|
|
|
|
|
|
|
var elem = parseHtml(options.view, hasScript);
|
2016-03-15 11:03:25 -07:00
|
|
|
elem.classList.add('page-view');
|
|
|
|
elem.setAttribute('data-type', options.type || '');
|
2016-05-16 08:47:28 -07:00
|
|
|
|
2016-03-15 22:33:31 -07:00
|
|
|
return {
|
|
|
|
elem: elem,
|
2016-03-18 09:23:55 -07:00
|
|
|
hasScript: hasScript
|
2016-03-15 22:33:31 -07:00
|
|
|
};
|
2016-03-15 11:03:25 -07:00
|
|
|
}
|
2016-03-14 13:27:35 -07:00
|
|
|
|
2016-05-15 18:49:01 -07:00
|
|
|
function beforeAnimate(allPages, newPageIndex, oldPageIndex) {
|
|
|
|
for (var i = 0, length = allPages.length; i < length; i++) {
|
|
|
|
if (newPageIndex == i || oldPageIndex == i) {
|
|
|
|
//allPages[i].classList.remove('hide');
|
|
|
|
} else {
|
|
|
|
allPages[i].classList.add('hide');
|
|
|
|
}
|
|
|
|
}
|
2016-03-14 13:27:35 -07:00
|
|
|
}
|
|
|
|
|
2016-05-15 18:49:01 -07:00
|
|
|
function afterAnimate(allPages, newPageIndex) {
|
2016-03-14 22:25:47 -07:00
|
|
|
for (var i = 0, length = allPages.length; i < length; i++) {
|
2016-05-15 18:49:01 -07:00
|
|
|
if (newPageIndex == i) {
|
|
|
|
//allPages[i].classList.remove('hide');
|
|
|
|
} else {
|
|
|
|
allPages[i].classList.add('hide');
|
2016-03-14 22:25:47 -07:00
|
|
|
}
|
|
|
|
}
|
2016-05-15 18:49:01 -07:00
|
|
|
}
|
2016-03-14 22:25:47 -07:00
|
|
|
|
2016-05-15 18:49:01 -07:00
|
|
|
function animate(newAnimatedPage, oldAnimatedPage, transition, isBack) {
|
|
|
|
|
|
|
|
if (enableAnimation() && newAnimatedPage.animate) {
|
|
|
|
if (transition == 'slide') {
|
|
|
|
return slide(newAnimatedPage, oldAnimatedPage, transition, isBack);
|
|
|
|
} else if (transition == 'fade') {
|
|
|
|
return fade(newAnimatedPage, oldAnimatedPage, transition, isBack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullAnimation(newAnimatedPage, oldAnimatedPage, transition, isBack);
|
2016-03-14 22:25:47 -07:00
|
|
|
}
|
|
|
|
|
2016-05-15 18:49:01 -07:00
|
|
|
function nullAnimation(newAnimatedPage, oldAnimatedPage, transition, isBack) {
|
|
|
|
|
|
|
|
newAnimatedPage.classList.remove('hide');
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
2016-03-14 13:27:35 -07:00
|
|
|
|
2016-05-15 18:49:01 -07:00
|
|
|
function slide(newAnimatedPage, oldAnimatedPage, transition, isBack) {
|
|
|
|
|
|
|
|
var timings = {
|
|
|
|
duration: 450,
|
|
|
|
iterations: 1,
|
|
|
|
easing: 'ease-out',
|
|
|
|
fill: 'both'
|
2016-03-14 13:27:35 -07:00
|
|
|
}
|
2016-03-14 22:25:47 -07:00
|
|
|
|
2016-05-15 18:49:01 -07:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
newAnimatedPage.classList.remove('hide');
|
|
|
|
|
|
|
|
var start = isBack ? '-100%' : '100%';
|
|
|
|
|
|
|
|
animations.push(newAnimatedPage.animate([
|
|
|
|
|
|
|
|
{ transform: 'translate3d(' + start + ', 0, 0)', offset: 0 },
|
|
|
|
{ transform: 'none', offset: 1 }
|
|
|
|
|
|
|
|
], timings));
|
|
|
|
|
|
|
|
currentAnimations = animations;
|
|
|
|
|
|
|
|
return new Promise(function (resolve, reject) {
|
|
|
|
animations[animations.length - 1].onfinish = resolve;
|
|
|
|
});
|
2016-03-14 13:27:35 -07:00
|
|
|
}
|
|
|
|
|
2016-05-15 18:49:01 -07:00
|
|
|
function fade(newAnimatedPage, oldAnimatedPage, transition, isBack) {
|
|
|
|
|
|
|
|
var timings = {
|
2016-05-16 11:20:08 -07:00
|
|
|
duration: 140,
|
2016-05-15 18:49:01 -07:00
|
|
|
iterations: 1,
|
|
|
|
easing: 'ease-out',
|
|
|
|
fill: 'both'
|
|
|
|
}
|
|
|
|
|
|
|
|
var animations = [];
|
|
|
|
|
|
|
|
if (oldAnimatedPage) {
|
|
|
|
animations.push(oldAnimatedPage.animate([
|
|
|
|
|
|
|
|
{ opacity: 1, offset: 0 },
|
|
|
|
{ opacity: 0, offset: 1 }
|
|
|
|
|
|
|
|
], timings));
|
|
|
|
}
|
|
|
|
|
|
|
|
newAnimatedPage.classList.remove('hide');
|
|
|
|
|
|
|
|
animations.push(newAnimatedPage.animate([
|
|
|
|
|
|
|
|
{ opacity: 0, offset: 0 },
|
|
|
|
{ opacity: 1, offset: 1 }
|
|
|
|
|
|
|
|
], timings));
|
|
|
|
|
|
|
|
currentAnimations = animations;
|
|
|
|
|
2016-03-14 13:27:35 -07:00
|
|
|
return new Promise(function (resolve, reject) {
|
2016-05-15 18:49:01 -07:00
|
|
|
animations[animations.length - 1].onfinish = resolve;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
var currentAnimations = [];
|
|
|
|
function cancelActiveAnimations() {
|
2016-03-14 13:27:35 -07:00
|
|
|
|
2016-05-15 18:49:01 -07:00
|
|
|
var animations = currentAnimations;
|
|
|
|
for (var i = 0, length = animations.length; i < length; i++) {
|
|
|
|
cancelAnimation(animations[i]);
|
|
|
|
}
|
|
|
|
}
|
2016-03-14 13:27:35 -07:00
|
|
|
|
2016-05-15 18:49:01 -07:00
|
|
|
function cancelAnimation(animation) {
|
2016-03-14 13:27:35 -07:00
|
|
|
|
2016-05-15 18:49:01 -07:00
|
|
|
try {
|
|
|
|
animation.cancel();
|
|
|
|
} catch (err) {
|
|
|
|
console.log('Error canceling animation: ' + err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var onBeforeChange;
|
|
|
|
function setOnBeforeChange(fn) {
|
|
|
|
onBeforeChange = fn;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getSelectedIndex(allPages) {
|
|
|
|
|
|
|
|
return selectedPageIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
function tryRestoreView(options) {
|
|
|
|
|
|
|
|
var url = options.url;
|
2016-05-16 08:47:28 -07:00
|
|
|
var index = currentUrls.indexOf(url);
|
2016-05-15 18:49:01 -07:00
|
|
|
|
2016-05-16 08:47:28 -07:00
|
|
|
if (index != -1) {
|
|
|
|
var page = allPages[index];
|
|
|
|
var view = page.querySelector(".page-view");
|
2016-03-14 13:27:35 -07:00
|
|
|
|
2016-05-16 08:47:28 -07:00
|
|
|
if (view) {
|
2016-03-14 13:27:35 -07:00
|
|
|
|
2016-05-15 18:49:01 -07:00
|
|
|
if (options.cancel) {
|
|
|
|
return;
|
|
|
|
}
|
2016-03-14 13:27:35 -07:00
|
|
|
|
2016-05-15 18:49:01 -07:00
|
|
|
cancelActiveAnimations();
|
2016-03-14 13:27:35 -07:00
|
|
|
|
2016-05-15 18:49:01 -07:00
|
|
|
var animatable = allPages[index];
|
|
|
|
var selected = getSelectedIndex(allPages);
|
|
|
|
var previousAnimatable = selected == -1 ? null : allPages[selected];
|
|
|
|
|
|
|
|
if (onBeforeChange) {
|
|
|
|
onBeforeChange(view, true, options);
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeAnimate(allPages, index, selected);
|
|
|
|
|
|
|
|
return animate(animatable, previousAnimatable, options.transition, options.isBack).then(function () {
|
|
|
|
|
|
|
|
selectedPageIndex = index;
|
|
|
|
if (!options.cancel && previousAnimatable) {
|
|
|
|
afterAnimate(allPages, index);
|
2016-03-14 22:25:47 -07:00
|
|
|
}
|
|
|
|
|
2016-05-16 11:20:08 -07:00
|
|
|
// 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', {}));
|
|
|
|
|
2016-03-15 11:03:25 -07:00
|
|
|
$.mobile = $.mobile || {};
|
|
|
|
$.mobile.activePage = view;
|
|
|
|
|
2016-05-15 18:49:01 -07:00
|
|
|
return view;
|
|
|
|
});
|
2016-03-14 13:27:35 -07:00
|
|
|
}
|
2016-05-15 18:49:01 -07:00
|
|
|
}
|
2016-03-14 13:27:35 -07:00
|
|
|
|
2016-05-15 18:49:01 -07:00
|
|
|
return Promise.reject();
|
2016-03-14 13:27:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
function triggerDestroy(view) {
|
|
|
|
view.dispatchEvent(new CustomEvent("viewdestroy", {}));
|
|
|
|
}
|
|
|
|
|
|
|
|
function reset() {
|
|
|
|
|
2016-05-16 08:47:28 -07:00
|
|
|
currentUrls = [];
|
2016-03-14 13:27:35 -07:00
|
|
|
}
|
|
|
|
|
2016-05-16 22:45:06 -07:00
|
|
|
if (enableAnimation() && !document.documentElement.animate) {
|
2016-05-16 11:20:08 -07:00
|
|
|
require(['webAnimations']);
|
2016-05-15 18:49:01 -07:00
|
|
|
}
|
2016-03-14 13:27:35 -07:00
|
|
|
|
|
|
|
return {
|
|
|
|
loadView: loadView,
|
|
|
|
tryRestoreView: tryRestoreView,
|
|
|
|
reset: reset,
|
2016-05-16 11:20:08 -07:00
|
|
|
setOnBeforeChange: setOnBeforeChange
|
2016-03-14 13:27:35 -07:00
|
|
|
};
|
|
|
|
});
|