szurubooru/public_html/js/Presenters/UserPresenter.js

99 lines
2.8 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;
var template;
var user;
var userName;
2014-09-07 09:57:01 +02:00
var activeTab;
2014-09-17 22:37:49 +02:00
function init(args, loaded) {
userName = args.userName;
2014-09-06 10:00:26 +02:00
topNavigationPresenter.select(auth.isLoggedIn(userName) ? 'my-account' : 'users');
topNavigationPresenter.changeTitle(userName);
2014-09-04 19:07:57 +02:00
2014-10-02 00:30:25 +02:00
promise.wait(
2014-09-17 22:37:49 +02:00
util.promiseTemplate('user'),
api.get('/users/' + userName))
.then(function(
userHtml,
response) {
$messages = $el.find('.messages');
template = _.template(userHtml);
2014-09-04 19:07:57 +02:00
2014-09-17 22:37:49 +02:00
user = response.json;
var extendedContext = _.extend(args, {user: user});
2014-09-17 22:37:49 +02:00
presenterManager.initPresenters([
[userBrowsingSettingsPresenter, _.extend({}, extendedContext, {target: '#browsing-settings-target'})],
[userAccountSettingsPresenter, _.extend({}, extendedContext, {target: '#account-settings-target'})],
[userAccountRemovalPresenter, _.extend({}, extendedContext, {target: '#account-removal-target'})]],
function() {
reinit(args, loaded);
});
}).fail(function(response) {
$el.empty();
messagePresenter.showError($messages, response.json && response.json.error || response);
loaded();
2014-09-08 22:02:28 +02:00
});
2014-09-17 22:37:49 +02:00
}
2014-09-17 22:37:49 +02:00
function reinit(args, loaded) {
initTabs(args);
loaded();
}
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,
formatRelativeTime: util.formatRelativeTime,
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 $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);