szurubooru/public_html/js/Presenters/UserPresenter.js

90 lines
2.4 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(
jQuery,
util,
promise,
api,
auth,
2014-09-04 19:07:57 +02:00
topNavigationPresenter,
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;
var template;
var user;
var userName;
2014-09-07 09:57:01 +02:00
var activeTab;
function init(args) {
userName = args.userName;
2014-09-06 10:00:26 +02:00
topNavigationPresenter.select(auth.isLoggedIn(userName) ? 'my-account' : 'users');
2014-09-04 19:07:57 +02:00
promise.waitAll(
util.promiseTemplate('user'),
api.get('/users/' + userName))
2014-09-05 13:50:51 +02:00
.then(function(
userHtml,
response) {
$messages = $el.find('.messages');
2014-09-04 19:07:57 +02:00
template = _.template(userHtml);
user = response.json;
var extendedContext = _.extend(args, {user: user});
promise.waitAll(
userBrowsingSettingsPresenter.init(_.extend(extendedContext, {target: '#browsing-settings-target'})),
userAccountSettingsPresenter.init(_.extend(extendedContext, {target: '#account-settings-target'})),
userAccountRemovalPresenter.init(_.extend(extendedContext, {target: '#account-removal-target'})))
2014-09-07 09:57:01 +02:00
.then(function() {
initTabs(args);
})
2014-09-04 19:07:57 +02:00
}).fail(function(response) {
$el.empty();
messagePresenter.showError($messages, response.json && response.json.error || response);
});
}
2014-09-07 09:57:01 +02:00
function initTabs(args) {
activeTab = args.tab || 'basic-info';
render();
}
function render() {
$el.html(template({
2014-09-05 13:50:51 +02:00
user: user,
canChangeBrowsingSettings: userBrowsingSettingsPresenter.getPrivileges().canChangeBrowsingSettings,
canChangeAccountSettings: _.any(userAccountSettingsPresenter.getPrivileges()),
canDeleteAccount: userAccountRemovalPresenter.getPrivileges().canDeleteAccount}));
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 tab = $link.attr('data-tab');
var $tabs = $link.closest('.tab-wrapper').find('.tab');
$links.removeClass('active');
$link.addClass('active');
$tabs.removeClass('active');
$tabs.filter('[data-tab=' + tab + ']').addClass('active');
}
return {
init: init,
2014-09-07 09:57:01 +02:00
reinit: initTabs,
render: render
};
};
App.DI.register('userPresenter', App.Presenters.UserPresenter);