(WIP) Added spinner (#39)

This commit is contained in:
Marcin Kurczewski 2014-09-17 22:37:49 +02:00
parent a16a2d3235
commit eb7c507359
21 changed files with 310 additions and 221 deletions

View file

@ -52,3 +52,22 @@ hr {
background: #f3f7f8; background: #f3f7f8;
color: #7a9 !important; color: #7a9 !important;
} }
#wait-cover {
display: flex;
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
width: 100%;
height: 100%;
background: rgba(255, 255, 255, 0.9);
z-index: 100;
}
#wait .spinner {
margin: auto;
}
#wait .spinner .fa {
font-size: 40px;
}

View file

@ -42,6 +42,15 @@
</div> </div>
</div> </div>
<div id="wait">
<div id="wait-cover">
<div class="spinner">
<i class="fa fa-spin fa-circle-o-notch"></i>
<p>Loading</p>
</div>
</div>
</div>
<!-- build:template <!-- build:template
<% _.each(templates, function(templateString, templateName) { %> <% _.each(templates, function(templateString, templateName) { %>
<script type="text/template" id="<%= templateName %>-template"> <script type="text/template" id="<%= templateName %>-template">

View file

@ -1,36 +1,60 @@
var App = App || {}; var App = App || {};
App.PresenterManager = function(topNavigationPresenter) { App.PresenterManager = function(jQuery, topNavigationPresenter) {
var lastContentPresenterName; var lastContentPresenter = null;
var lastContentPresenter; var $spinner;
var spinnerTimeout;
function init() { function init() {
initPresenter('topNavigationPresenter'); initPresenter(topNavigationPresenter, [], function() {});
$spinner = jQuery('body').find('#wait');
} }
function initPresenter(presenterName, args) { function initPresenter(presenter, args, loaded) {
var presenter = App.DI.get(presenterName); presenter.init.call(presenter, args, loaded);
presenter.init.call(presenter, args);
} }
function switchContentPresenter(presenterName, args) { function switchContentPresenter(presenter, args) {
if (lastContentPresenterName !== presenterName) { var contentPresenterLoaded = function() {
window.clearTimeout(spinnerTimeout);
$spinner.hide();
};
spinnerTimeout = window.setTimeout(function() {
$spinner.stop().fadeIn('slow');
}, 300);
if (lastContentPresenter === null || lastContentPresenter.name !== presenter.name) {
topNavigationPresenter.changeTitle(null); topNavigationPresenter.changeTitle(null);
var presenter = App.DI.get(presenterName); presenter.init.call(presenter, args, contentPresenterLoaded);
presenter.init.call(presenter, args);
lastContentPresenterName = presenterName;
lastContentPresenter = presenter; lastContentPresenter = presenter;
} else if (lastContentPresenter.reinit) { } else if (lastContentPresenter.reinit) {
lastContentPresenter.reinit.call(lastContentPresenter, args); 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 { return {
init: init, init: init,
initPresenter: initPresenter,
initPresenters: initPresenters,
switchContentPresenter: switchContentPresenter, switchContentPresenter: switchContentPresenter,
}; };
}; };
App.DI.registerSingleton('presenterManager', ['topNavigationPresenter'], App.PresenterManager); App.DI.registerSingleton('presenterManager', ['jQuery', 'topNavigationPresenter'], App.PresenterManager);

View file

@ -7,10 +7,11 @@ App.Presenters.CommentListPresenter = function(
var $el = jQuery('#content'); var $el = jQuery('#content');
function init(args) { function init(args, loaded) {
topNavigationPresenter.select('comments'); topNavigationPresenter.select('comments');
topNavigationPresenter.changeTitle('Comments'); topNavigationPresenter.changeTitle('Comments');
render(); render();
loaded();
} }
function render() { function render() {

View file

@ -7,10 +7,11 @@ App.Presenters.HelpPresenter = function(
var $el = jQuery('#content'); var $el = jQuery('#content');
function init(args) { function init(args, loaded) {
topNavigationPresenter.select('help'); topNavigationPresenter.select('help');
topNavigationPresenter.changeTitle('Help'); topNavigationPresenter.changeTitle('Help');
render(); render();
loaded();
} }
function render() { function render() {

View file

@ -7,10 +7,11 @@ App.Presenters.HomePresenter = function(
var $el = jQuery('#content'); var $el = jQuery('#content');
function init(args) { function init(args, loaded) {
topNavigationPresenter.select('home'); topNavigationPresenter.select('home');
topNavigationPresenter.changeTitle('Home'); topNavigationPresenter.changeTitle('Home');
render(); render();
loaded();
} }
function render() { function render() {

View file

@ -16,18 +16,20 @@ App.Presenters.LoginPresenter = function(
var template; var template;
var previousRoute; var previousRoute;
function init(args) { function init(args, loaded) {
topNavigationPresenter.select('login'); topNavigationPresenter.select('login');
topNavigationPresenter.changeTitle('Login'); topNavigationPresenter.changeTitle('Login');
previousRoute = args.previousRoute; previousRoute = args.previousRoute;
promise.wait(util.promiseTemplate('login-form')).then(function(html) { promise.wait(util.promiseTemplate('login-form'))
template = _.template(html); .then(function(html) {
if (auth.isLoggedIn()) { template = _.template(html);
finishLogin(); if (auth.isLoggedIn()) {
} else { finishLogin();
render(); } else {
} render();
}); }
loaded();
});
} }
function render() { function render() {

View file

@ -11,16 +11,19 @@ App.Presenters.LogoutPresenter = function(
var $messages = jQuery('#content'); var $messages = jQuery('#content');
function init() { function init(args, loaded) {
topNavigationPresenter.select('logout'); topNavigationPresenter.select('logout');
topNavigationPresenter.changeTitle('Logout'); topNavigationPresenter.changeTitle('Logout');
promise.wait(auth.logout()).then(function() { promise.wait(auth.logout())
$messages.empty(); .then(function() {
var $messageDiv = messagePresenter.showInfo($messages, 'Logged out. <a href="">Back to main page</a>'); loaded();
$messageDiv.find('a').click(mainPageLinkClicked); $messages.empty();
}).fail(function(response) { var $messageDiv = messagePresenter.showInfo($messages, 'Logged out. <a href="">Back to main page</a>');
messagePresenter.showError(($messages, response.json && response.json.error || response) + '<br/>Reload the page to continue.'); $messageDiv.find('a').click(mainPageLinkClicked);
}); }).fail(function(response) {
loaded();
messagePresenter.showError(($messages, response.json && response.json.error || response) + '<br/>Reload the page to continue.');
});
} }
function mainPageLinkClicked(e) { function mainPageLinkClicked(e) {

View file

@ -24,7 +24,7 @@ App.Presenters.PagedCollectionPresenter = function(
var updateCallback; var updateCallback;
var failCallback; var failCallback;
function init(args) { function init(args, loaded) {
forceClear = !_.isEqual(args.searchParams, searchParams) || parseInt(args.page) !== pageNumber + 1; forceClear = !_.isEqual(args.searchParams, searchParams) || parseInt(args.page) !== pageNumber + 1;
searchParams = args.searchParams; searchParams = args.searchParams;
pageNumber = parseInt(args.page) || 1; pageNumber = parseInt(args.page) || 1;
@ -34,23 +34,26 @@ App.Presenters.PagedCollectionPresenter = function(
updateCallback = args.updateCallback; updateCallback = args.updateCallback;
failCallback = args.failCallback; failCallback = args.failCallback;
promise.wait(util.promiseTemplate('pager')).then(function(html) { promise.wait(util.promiseTemplate('pager'))
template = _.template(html); .then(function(html) {
softChangePage(pageNumber); template = _.template(html);
softChangePage(pageNumber)
.then(loaded)
.fail(loaded);
if (!endlessScroll) { if (!endlessScroll) {
mousetrap.bind('a', function(e) { mousetrap.bind('a', function(e) {
if (!e.altKey && !e.ctrlKey) { if (!e.altKey && !e.ctrlKey) {
prevPage(); prevPage();
} }
}); });
mousetrap.bind('d', function(e) { mousetrap.bind('d', function(e) {
if (!e.altKey && !e.ctrlKey) { if (!e.altKey && !e.ctrlKey) {
nextPage(); nextPage();
} }
}); });
} }
}); });
} }
function prevPage() { function prevPage() {
@ -82,27 +85,31 @@ App.Presenters.PagedCollectionPresenter = function(
function softChangePage(newPageNumber) { function softChangePage(newPageNumber) {
pageNumber = newPageNumber; pageNumber = newPageNumber;
promise.wait( return promise.make(function(resolve, reject) {
api.get(backendUri, _.extend({}, searchParams, {page: pageNumber}))) promise.wait(
.then(function(response) { api.get(backendUri, _.extend({}, searchParams, {page: pageNumber})))
var pageSize = response.json.pageSize; .then(function(response) {
var totalRecords = response.json.totalRecords; resolve(response);
totalPages = Math.ceil(totalRecords / pageSize); var pageSize = response.json.pageSize;
var totalRecords = response.json.totalRecords;
totalPages = Math.ceil(totalRecords / pageSize);
var $target = updateCallback({ var $target = updateCallback({
entities: response.json.data, entities: response.json.data,
totalRecords: totalRecords}, totalRecords: totalRecords},
forceClear || !endlessScroll); forceClear || !endlessScroll);
forceClear = false; forceClear = false;
render($target); render($target);
}).fail(function(response) { }).fail(function(response) {
if (typeof(failCallback) !== 'undefined') { reject(response);
failCallback(response); if (typeof(failCallback) !== 'undefined') {
} else { failCallback(response);
console.log(new Error(response.json && response.json.error || response)); } else {
} console.log(new Error(response.json && response.json.error || response));
}); }
});
});
} }
function render($target) { function render($target) {

View file

@ -16,30 +16,31 @@ App.Presenters.PostListPresenter = function(
var listTemplate; var listTemplate;
var itemTemplate; var itemTemplate;
function init(args) { function init(args, loaded) {
topNavigationPresenter.select('posts'); topNavigationPresenter.select('posts');
topNavigationPresenter.changeTitle('Posts'); topNavigationPresenter.changeTitle('Posts');
promise.waitAll( promise.waitAll(
util.promiseTemplate('post-list'), util.promiseTemplate('post-list'),
util.promiseTemplate('post-list-item')).then(function(listHtml, itemHtml) { util.promiseTemplate('post-list-item'))
listTemplate = _.template(listHtml); .then(function(listHtml, itemHtml) {
itemTemplate = _.template(itemHtml); listTemplate = _.template(listHtml);
itemTemplate = _.template(itemHtml);
render(); render();
reinit(args); reinit(args, loaded);
}); });
} }
function reinit(args) { function reinit(args, loaded) {
var searchArgs = util.parseComplexRouteArgs(args.searchArgs); var searchArgs = util.parseComplexRouteArgs(args.searchArgs);
searchArgs.order = searchArgs.order; searchArgs.order = searchArgs.order;
updateActiveOrder(searchArgs.order); updateActiveOrder(searchArgs.order);
initPaginator(searchArgs); initPaginator(searchArgs, loaded);
} }
function initPaginator(searchArgs) { function initPaginator(searchArgs, onLoad) {
pagedCollectionPresenter.init({ pagedCollectionPresenter.init({
page: searchArgs.page, page: searchArgs.page,
searchParams: {order: searchArgs.order}, searchParams: {order: searchArgs.order},
@ -52,7 +53,7 @@ App.Presenters.PostListPresenter = function(
failCallback: function(response) { failCallback: function(response) {
$el.empty(); $el.empty();
messagePresenter.showError($el, response.json && response.json.error || response); messagePresenter.showError($el, response.json && response.json.error || response);
}}); }}, onLoad);
} }
function render() { function render() {

View file

@ -20,14 +20,16 @@ App.Presenters.PostUploadPresenter = function(
var tagInput; var tagInput;
var interactionEnabled = true; var interactionEnabled = true;
function init(args) { function init(args, loaded) {
topNavigationPresenter.select('upload'); topNavigationPresenter.select('upload');
topNavigationPresenter.changeTitle('Upload'); topNavigationPresenter.changeTitle('Upload');
promise.wait(util.promiseTemplate('post-upload')).then(function(html) { promise.wait(util.promiseTemplate('post-upload'))
template = _.template(html); .then(function(html) {
render(); template = _.template(html);
}); render();
loaded();
});
} }
function render() { function render() {
@ -524,18 +526,19 @@ App.Presenters.PostUploadPresenter = function(
return; return;
} }
promise.wait(api.post('/posts', formData)).then(function(response) { promise.wait(api.post('/posts', formData))
$row.slideUp(function(response) { .then(function(response) {
$row.remove(); $row.slideUp(function(response) {
posts.shift(); $row.remove();
setAllPosts(posts); posts.shift();
uploadNextPost(); setAllPosts(posts);
uploadNextPost();
});
}).fail(function(response) {
messagePresenter.hideMessages($messages);
messagePresenter.showError($messages, response.json && response.json.error || response);
interactionEnabled = true;
}); });
}).fail(function(response) {
messagePresenter.hideMessages($messages);
messagePresenter.showError($messages, response.json && response.json.error || response);
interactionEnabled = true;
});
} }
function submitButtonClicked(e) { function submitButtonClicked(e) {

View file

@ -14,13 +14,15 @@ App.Presenters.RegistrationPresenter = function(
var template; var template;
var $messages; var $messages;
function init() { function init(args, loaded) {
topNavigationPresenter.select('register'); topNavigationPresenter.select('register');
topNavigationPresenter.changeTitle('Registration'); topNavigationPresenter.changeTitle('Registration');
promise.wait(util.promiseTemplate('registration-form')).then(function(html) { promise.wait(util.promiseTemplate('registration-form'))
template = _.template(html); .then(function(html) {
render(); template = _.template(html);
}); render();
loaded();
});
} }
function render() { function render() {

View file

@ -7,10 +7,11 @@ App.Presenters.TagListPresenter = function(
var $el = jQuery('#content'); var $el = jQuery('#content');
function init(args) { function init(args, loaded) {
topNavigationPresenter.select('tags'); topNavigationPresenter.select('tags');
topNavigationPresenter.changeTitle('Tags'); topNavigationPresenter.changeTitle('Tags');
render(); render();
loaded();
} }
function render() { function render() {

View file

@ -13,12 +13,14 @@ App.Presenters.TopNavigationPresenter = function(
var template; var template;
var baseTitle = document.title; var baseTitle = document.title;
function init() { function init(args, loaded) {
promise.wait(util.promiseTemplate('top-navigation')).then(function(html) { promise.wait(util.promiseTemplate('top-navigation'))
template = _.template(html); .then(function(html) {
render(); template = _.template(html);
auth.startObservingLoginChanges('top-navigation', loginStateChanged); render();
}); loaded();
auth.startObservingLoginChanges('top-navigation', loginStateChanged);
});
} }
function select(newSelectedElement) { function select(newSelectedElement) {

View file

@ -16,21 +16,20 @@ App.Presenters.UserAccountRemovalPresenter = function(
var user; var user;
var privileges = {}; var privileges = {};
function init(args) { function init(args, loaded) {
return promise.make(function(resolve, reject) { user = args.user;
user = args.user; target = args.target;
target = args.target;
privileges.canDeleteAccount = privileges.canDeleteAccount =
auth.hasPrivilege(auth.privileges.deleteAllAccounts) || auth.hasPrivilege(auth.privileges.deleteAllAccounts) ||
(auth.hasPrivilege(auth.privileges.deleteOwnAccount) && auth.isLoggedIn(user.name)); (auth.hasPrivilege(auth.privileges.deleteOwnAccount) && auth.isLoggedIn(user.name));
promise.wait(util.promiseTemplate('account-removal')).then(function(html) { promise.wait(util.promiseTemplate('account-removal'))
.then(function(html) {
template = _.template(html); template = _.template(html);
render(); render();
resolve(); loaded();
}); });
});
} }
function render() { function render() {

View file

@ -16,34 +16,33 @@ App.Presenters.UserAccountSettingsPresenter = function(
var privileges; var privileges;
var avatarContent; var avatarContent;
function init(args) { function init(args, loaded) {
return promise.make(function(resolve, reject) { user = args.user;
user = args.user; target = args.target;
target = args.target;
privileges = { privileges = {
canChangeAccessRank: canChangeAccessRank:
auth.hasPrivilege(auth.privileges.changeAccessRank), auth.hasPrivilege(auth.privileges.changeAccessRank),
canChangeAvatarStyle: canChangeAvatarStyle:
auth.hasPrivilege(auth.privileges.changeAllAvatarStyles) || auth.hasPrivilege(auth.privileges.changeAllAvatarStyles) ||
(auth.hasPrivilege(auth.privileges.changeOwnAvatarStyle) && auth.isLoggedIn(user.name)), (auth.hasPrivilege(auth.privileges.changeOwnAvatarStyle) && auth.isLoggedIn(user.name)),
canChangeName: canChangeName:
auth.hasPrivilege(auth.privileges.changeAllNames) || auth.hasPrivilege(auth.privileges.changeAllNames) ||
(auth.hasPrivilege(auth.privileges.changeOwnName) && auth.isLoggedIn(user.name)), (auth.hasPrivilege(auth.privileges.changeOwnName) && auth.isLoggedIn(user.name)),
canChangeEmailAddress: canChangeEmailAddress:
auth.hasPrivilege(auth.privileges.changeAllEmailAddresses) || auth.hasPrivilege(auth.privileges.changeAllEmailAddresses) ||
(auth.hasPrivilege(auth.privileges.changeOwnEmailAddress) && auth.isLoggedIn(user.name)), (auth.hasPrivilege(auth.privileges.changeOwnEmailAddress) && auth.isLoggedIn(user.name)),
canChangePassword: canChangePassword:
auth.hasPrivilege(auth.privileges.changeAllPasswords) || auth.hasPrivilege(auth.privileges.changeAllPasswords) ||
(auth.hasPrivilege(auth.privileges.changeOwnPassword) && auth.isLoggedIn(user.name)), (auth.hasPrivilege(auth.privileges.changeOwnPassword) && auth.isLoggedIn(user.name)),
}; };
promise.wait(util.promiseTemplate('account-settings')).then(function(html) { promise.wait(util.promiseTemplate('account-settings'))
.then(function(html) {
template = _.template(html); template = _.template(html);
render(); render();
resolve(); loaded();
}); });
});
} }
function render() { function render() {

View file

@ -18,24 +18,26 @@ App.Presenters.UserActivationPresenter = function(
var formHidden = false; var formHidden = false;
var operation; var operation;
function init(args) { function init(args, loaded) {
topNavigationPresenter.select('login'); topNavigationPresenter.select('login');
topNavigationPresenter.changeTitle('Account recovery'); topNavigationPresenter.changeTitle('Account recovery');
reinit(args); reinit(args, loaded);
} }
function reinit(args) { function reinit(args, loaded) {
operation = args.operation; operation = args.operation;
promise.wait(util.promiseTemplate('user-query-form')).then(function(html) { promise.wait(util.promiseTemplate('user-query-form'))
template = _.template(html); .then(function(html) {
if (args.token) { template = _.template(html);
hideForm(); if (args.token) {
confirmToken(args.token); hideForm();
} else { confirmToken(args.token);
showForm(); } else {
} showForm();
render(); }
}); render();
loaded();
});
} }
function render() { function render() {
@ -68,18 +70,19 @@ App.Presenters.UserActivationPresenter = function(
'/password-reset/' + userNameOrEmail : '/password-reset/' + userNameOrEmail :
'/activation/' + userNameOrEmail; '/activation/' + userNameOrEmail;
api.post(url).then(function(response) { api.post(url)
var message = operation === 'passwordReset' ? .then(function(response) {
'Password reset request sent.' : var message = operation === 'passwordReset' ?
'Activation e-mail resent.'; 'Password reset request sent.' :
message += ' Check your inbox.<br/>If e-mail doesn\'t show up, check your spam folder.'; 'Activation e-mail resent.';
message += ' Check your inbox.<br/>If e-mail doesn\'t show up, check your spam folder.';
$el.find('#user-query-form').slideUp(function() { $el.find('#user-query-form').slideUp(function() {
messagePresenter.showInfo($messages, message); messagePresenter.showInfo($messages, message);
});
}).fail(function(response) {
messagePresenter.showError($messages, response.json && response.json.error || response);
}); });
}).fail(function(response) {
messagePresenter.showError($messages, response.json && response.json.error || response);
});
} }
function confirmToken(token) { function confirmToken(token) {
@ -89,17 +92,18 @@ App.Presenters.UserActivationPresenter = function(
'/finish-password-reset/' + token : '/finish-password-reset/' + token :
'/finish-activation/' + token; '/finish-activation/' + token;
api.post(url).then(function(response) { api.post(url)
var message = operation === 'passwordReset' ? .then(function(response) {
'Your new password is <strong>' + response.json.newPassword + '</strong>.' : var message = operation === 'passwordReset' ?
'E-mail activation successful.'; 'Your new password is <strong>' + response.json.newPassword + '</strong>.' :
'E-mail activation successful.';
$el.find('#user-query-form').slideUp(function() { $el.find('#user-query-form').slideUp(function() {
messagePresenter.showInfo($messages, message); messagePresenter.showInfo($messages, message);
});
}).fail(function(response) {
messagePresenter.showError($messages, response.json && response.json.error || response);
}); });
}).fail(function(response) {
messagePresenter.showError($messages, response.json && response.json.error || response);
});
} }
return { return {

View file

@ -15,19 +15,18 @@ App.Presenters.UserBrowsingSettingsPresenter = function(
var user; var user;
var privileges = {}; var privileges = {};
function init(args) { function init(args, loaded) {
return promise.make(function(resolve, reject) { user = args.user;
user = args.user; target = args.target;
target = args.target;
privileges.canChangeBrowsingSettings = auth.isLoggedIn(user.name) && user.name === auth.getCurrentUser().name; privileges.canChangeBrowsingSettings = auth.isLoggedIn(user.name) && user.name === auth.getCurrentUser().name;
promise.wait(util.promiseTemplate('browsing-settings')).then(function(html) { promise.wait(util.promiseTemplate('browsing-settings'))
.then(function(html) {
template = _.template(html); template = _.template(html);
render(); render();
resolve(); loaded();
}); });
});
} }
function render() { function render() {
@ -41,6 +40,7 @@ App.Presenters.UserBrowsingSettingsPresenter = function(
var $el = jQuery(target); var $el = jQuery(target);
var $messages = $el.find('.messages'); var $messages = $el.find('.messages');
messagePresenter.hideMessages($messages); messagePresenter.hideMessages($messages);
var newSettings = { var newSettings = {
endlessScroll: $el.find('[name=endless-scroll]').is(':checked'), endlessScroll: $el.find('[name=endless-scroll]').is(':checked'),
hideDownvoted: $el.find('[name=hide-downvoted]').is(':checked'), hideDownvoted: $el.find('[name=hide-downvoted]').is(':checked'),
@ -50,9 +50,11 @@ App.Presenters.UserBrowsingSettingsPresenter = function(
unsafe: $el.find('[name=listUnsafePosts]').is(':checked'), unsafe: $el.find('[name=listUnsafePosts]').is(':checked'),
}, },
}; };
browsingSettings.setSettings(newSettings).then(function() {
messagePresenter.showInfo($messages, 'Browsing settings updated!'); browsingSettings.setSettings(newSettings)
}); .then(function() {
messagePresenter.showInfo($messages, 'Browsing settings updated!');
});
} }
function getPrivileges() { function getPrivileges() {

View file

@ -16,30 +16,31 @@ App.Presenters.UserListPresenter = function(
var listTemplate; var listTemplate;
var itemTemplate; var itemTemplate;
function init(args) { function init(args, loaded) {
topNavigationPresenter.select('users'); topNavigationPresenter.select('users');
topNavigationPresenter.changeTitle('Users'); topNavigationPresenter.changeTitle('Users');
promise.waitAll( promise.waitAll(
util.promiseTemplate('user-list'), util.promiseTemplate('user-list'),
util.promiseTemplate('user-list-item')).then(function(listHtml, itemHtml) { util.promiseTemplate('user-list-item'))
listTemplate = _.template(listHtml); .then(function(listHtml, itemHtml) {
itemTemplate = _.template(itemHtml); listTemplate = _.template(listHtml);
itemTemplate = _.template(itemHtml);
render(); render();
reinit(args); reinit(args, loaded);
}); });
} }
function reinit(args) { function reinit(args, loaded) {
var searchArgs = util.parseComplexRouteArgs(args.searchArgs); var searchArgs = util.parseComplexRouteArgs(args.searchArgs);
searchArgs.order = searchArgs.order || 'name'; searchArgs.order = searchArgs.order || 'name';
updateActiveOrder(searchArgs.order); updateActiveOrder(searchArgs.order);
initPaginator(searchArgs); initPaginator(searchArgs, loaded);
} }
function initPaginator(searchArgs) { function initPaginator(searchArgs, onLoad) {
pagedCollectionPresenter.init({ pagedCollectionPresenter.init({
page: searchArgs.page, page: searchArgs.page,
searchParams: {order: searchArgs.order}, searchParams: {order: searchArgs.order},
@ -52,7 +53,7 @@ App.Presenters.UserListPresenter = function(
failCallback: function(response) { failCallback: function(response) {
$el.empty(); $el.empty();
messagePresenter.showError($el, response.json && response.json.error || response); messagePresenter.showError($el, response.json && response.json.error || response);
}}); }}, onLoad);
} }
function render() { function render() {

View file

@ -9,6 +9,7 @@ App.Presenters.UserPresenter = function(
api, api,
auth, auth,
topNavigationPresenter, topNavigationPresenter,
presenterManager,
userBrowsingSettingsPresenter, userBrowsingSettingsPresenter,
userAccountSettingsPresenter, userAccountSettingsPresenter,
userAccountRemovalPresenter, userAccountRemovalPresenter,
@ -21,35 +22,40 @@ App.Presenters.UserPresenter = function(
var userName; var userName;
var activeTab; var activeTab;
function init(args) { function init(args, loaded) {
userName = args.userName; userName = args.userName;
topNavigationPresenter.select(auth.isLoggedIn(userName) ? 'my-account' : 'users'); topNavigationPresenter.select(auth.isLoggedIn(userName) ? 'my-account' : 'users');
topNavigationPresenter.changeTitle(userName); topNavigationPresenter.changeTitle(userName);
promise.waitAll( promise.waitAll(
util.promiseTemplate('user'), util.promiseTemplate('user'),
api.get('/users/' + userName)) api.get('/users/' + userName))
.then(function( .then(function(
userHtml, userHtml,
response) { response) {
$messages = $el.find('.messages'); $messages = $el.find('.messages');
template = _.template(userHtml); template = _.template(userHtml);
user = response.json; user = response.json;
var extendedContext = _.extend(args, {user: user}); var extendedContext = _.extend(args, {user: user});
promise.waitAll( presenterManager.initPresenters([
userBrowsingSettingsPresenter.init(_.extend(extendedContext, {target: '#browsing-settings-target'})), [userBrowsingSettingsPresenter, _.extend({}, extendedContext, {target: '#browsing-settings-target'})],
userAccountSettingsPresenter.init(_.extend(extendedContext, {target: '#account-settings-target'})), [userAccountSettingsPresenter, _.extend({}, extendedContext, {target: '#account-settings-target'})],
userAccountRemovalPresenter.init(_.extend(extendedContext, {target: '#account-removal-target'}))) [userAccountRemovalPresenter, _.extend({}, extendedContext, {target: '#account-removal-target'})]],
.then(function() { function() {
initTabs(args); reinit(args, loaded);
});
}).fail(function(response) {
$el.empty();
messagePresenter.showError($messages, response.json && response.json.error || response);
}); });
}
}).fail(function(response) { function reinit(args, loaded) {
$el.empty(); initTabs(args);
messagePresenter.showError($messages, response.json && response.json.error || response); loaded();
});
} }
function initTabs(args) { function initTabs(args) {
@ -82,10 +88,10 @@ App.Presenters.UserPresenter = function(
return { return {
init: init, init: init,
reinit: initTabs, reinit: reinit,
render: render render: render
}; };
}; };
App.DI.register('userPresenter', ['_', 'jQuery', 'util', 'promise', 'api', 'auth', 'topNavigationPresenter', 'userBrowsingSettingsPresenter', 'userAccountSettingsPresenter', 'userAccountRemovalPresenter', 'messagePresenter'], App.Presenters.UserPresenter); App.DI.register('userPresenter', ['_', 'jQuery', 'util', 'promise', 'api', 'auth', 'topNavigationPresenter', 'presenterManager', 'userBrowsingSettingsPresenter', 'userAccountSettingsPresenter', 'userAccountRemovalPresenter', 'messagePresenter'], App.Presenters.UserPresenter);

View file

@ -57,7 +57,9 @@ App.Router = function(pathJs, _, jQuery, util, appState, presenterManager) {
additionalParams, additionalParams,
{previousRoute: pathJs.routes.previous}); {previousRoute: pathJs.routes.previous});
presenterManager.switchContentPresenter( presenterName, finalParams); var presenter = App.DI.get(presenterName);
presenter.name = presenterName;
presenterManager.switchContentPresenter(presenter, finalParams);
}).enter(function(e) { }).enter(function(e) {
if (util.isExitConfirmationEnabled()) { if (util.isExitConfirmationEnabled()) {
if (window.location.href !== previousLocation) { if (window.location.href !== previousLocation) {