szurubooru/public_html/js/PresenterManager.js
Marcin Kurczewski 6e09f90251 Fixed top navigation loading glitch
A race condition occurred - router could execute promise.abortAll()
even when top navigation didn't fully load. This caused top navigation
presenter to throw "broken promise" error, which is reserved for
promises made after core modules have been loaded.
2014-10-18 18:48:35 +02:00

77 lines
2 KiB
JavaScript

var App = App || {};
App.PresenterManager = function(jQuery, promise, topNavigationPresenter, keyboard) {
var lastContentPresenter = null;
var $spinner;
var spinnerTimeout;
function init() {
$spinner = jQuery('body').find('#wait');
return promise.make(function(resolve, reject) {
initPresenter(topNavigationPresenter, [], resolve);
});
}
function initPresenter(presenter, args, loaded) {
presenter.init.call(presenter, args, loaded);
}
function showContentSpinner() {
$spinner.show();
}
function hideContentSpinner() {
$spinner.hide();
}
function switchContentPresenter(presenter, args) {
var contentPresenterLoaded = function() {
window.clearTimeout(spinnerTimeout);
hideContentSpinner();
};
spinnerTimeout = window.setTimeout(function() {
showContentSpinner();
}, 100);
if (lastContentPresenter === null || lastContentPresenter.name !== presenter.name) {
if (lastContentPresenter !== null && lastContentPresenter.deinit) {
lastContentPresenter.deinit();
}
keyboard.reset();
topNavigationPresenter.changeTitle(null);
topNavigationPresenter.focus();
presenter.init.call(presenter, args, contentPresenterLoaded);
lastContentPresenter = presenter;
} else if (lastContentPresenter.reinit) {
lastContentPresenter.reinit.call(lastContentPresenter, args, contentPresenterLoaded);
}
}
function initPresenters(options, loaded) {
var count = 0;
var subPresenterLoaded = function() {
count ++;
if (count === options.length) {
loaded();
}
};
for (var i = 0; i < options.length; i ++) {
initPresenter(options[i][0], options[i][1], subPresenterLoaded);
}
}
return {
init: init,
initPresenter: initPresenter,
initPresenters: initPresenters,
switchContentPresenter: switchContentPresenter,
showContentSpinner: showContentSpinner,
hideContentSpinner: hideContentSpinner,
};
};
App.DI.registerSingleton('presenterManager', ['jQuery', 'promise', 'topNavigationPresenter', 'keyboard'], App.PresenterManager);