szurubooru/public_html/js/Presenters/UserPresenter.js

108 lines
3.1 KiB
JavaScript
Raw Normal View History

var App = App || {};
App.Presenters = App.Presenters || {};
2014-09-04 19:07:57 +02:00
App.Presenters.UserPresenter = function(
2014-09-08 22:02:28 +02:00
_,
2014-09-04 19:07:57 +02:00
jQuery,
util,
promise,
api,
auth,
2014-09-04 19:07:57 +02:00
topNavigationPresenter,
2014-09-17 22:37:49 +02:00
presenterManager,
userBrowsingSettingsPresenter,
userAccountSettingsPresenter,
userAccountRemovalPresenter,
2014-09-04 19:07:57 +02:00
messagePresenter) {
var $el = jQuery('#content');
2014-09-04 19:07:57 +02:00
var $messages = $el;
2014-10-05 10:41:12 +02:00
var templates = {};
2014-09-04 19:07:57 +02:00
var user;
var userName = null;
2014-09-07 09:57:01 +02:00
var activeTab;
function init(params, loaded) {
promise.wait(util.promiseTemplate('user'))
2014-10-06 21:16:59 +02:00
.then(function(template) {
2014-10-05 10:41:12 +02:00
templates.user = template;
reinit(params, loaded);
}).fail(function() {
console.log(arguments);
loaded();
2014-09-08 22:02:28 +02:00
});
2014-09-17 22:37:49 +02:00
}
function reinit(params, loaded) {
if (params.userName !== userName) {
userName = params.userName;
topNavigationPresenter.select(auth.isLoggedIn(userName) ? 'my-account' : 'users');
topNavigationPresenter.changeTitle(userName);
promise.wait(api.get('/users/' + userName))
.then(function(response) {
user = response.json;
var extendedContext = _.extend(params, {user: user});
presenterManager.initPresenters([
[userBrowsingSettingsPresenter, _.extend({}, extendedContext, {target: '#browsing-settings-target'})],
[userAccountSettingsPresenter, _.extend({}, extendedContext, {target: '#account-settings-target'})],
[userAccountRemovalPresenter, _.extend({}, extendedContext, {target: '#account-removal-target'})]],
function() {
initTabs(params);
loaded();
});
}).fail(function(response) {
$el.empty();
messagePresenter.showError($messages, response.json && response.json.error || response);
loaded();
});
} else {
initTabs(params);
loaded();
}
}
function initTabs(params) {
activeTab = params.tab || 'basic-info';
2014-09-07 09:57:01 +02:00
render();
}
function render() {
2014-10-05 10:41:12 +02:00
$el.html(templates.user({
2014-09-05 13:50:51 +02:00
user: user,
2014-10-04 20:43:37 +02:00
isLoggedIn: auth.isLoggedIn(user.name),
formatRelativeTime: util.formatRelativeTime,
canChangeBrowsingSettings: userBrowsingSettingsPresenter.getPrivileges().canChangeBrowsingSettings,
canChangeAccountSettings: _.any(userAccountSettingsPresenter.getPrivileges()),
canDeleteAccount: userAccountRemovalPresenter.getPrivileges().canDeleteAccount}));
$messages = $el.find('.messages');
2014-10-05 11:05:34 +02:00
util.loadImagesNicely($el.find('img'));
userBrowsingSettingsPresenter.render();
userAccountSettingsPresenter.render();
userAccountRemovalPresenter.render();
2014-09-07 09:57:01 +02:00
changeTab(activeTab);
}
function changeTab(targetTab) {
var $link = $el.find('a[data-tab=' + targetTab + ']');
var $links = $link.closest('ul').find('a[data-tab]');
var $tabs = $el.find('.tab-wrapper').find('.tab');
2014-09-07 09:57:01 +02:00
$links.removeClass('active');
$link.addClass('active');
$tabs.removeClass('active');
$tabs.filter('[data-tab=' + targetTab + ']').addClass('active');
2014-09-07 09:57:01 +02:00
}
return {
init: init,
2014-09-17 22:37:49 +02:00
reinit: reinit,
render: render
};
};
2014-09-17 22:37:49 +02:00
App.DI.register('userPresenter', ['_', 'jQuery', 'util', 'promise', 'api', 'auth', 'topNavigationPresenter', 'presenterManager', 'userBrowsingSettingsPresenter', 'userAccountSettingsPresenter', 'userAccountRemovalPresenter', 'messagePresenter'], App.Presenters.UserPresenter);