szurubooru/public_html/js/Presenters/UserPresenter.js

91 lines
2.6 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,
messagePresenter) {
var $el = jQuery('#content');
2014-09-04 19:07:57 +02:00
var $messages = $el;
var template;
var accountSettingsTemplate;
2014-09-05 13:50:51 +02:00
var accountRemovalTemplate;
2014-09-04 19:07:57 +02:00
var browsingSettingsTemplate;
var user;
var userName;
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'),
util.promiseTemplate('account-settings'),
2014-09-05 13:50:51 +02:00
util.promiseTemplate('account-removal'),
2014-09-04 19:07:57 +02:00
util.promiseTemplate('browsing-settings'),
api.get('/users/' + userName))
2014-09-05 13:50:51 +02:00
.then(function(
userHtml,
accountSettingsHtml,
accountRemovalHtml,
browsingSettingsHtml,
response) {
2014-09-04 19:07:57 +02:00
template = _.template(userHtml);
accountSettingsTemplate = _.template(accountSettingsHtml);
2014-09-05 13:50:51 +02:00
accountRemovalTemplate = _.template(accountRemovalHtml);
2014-09-04 19:07:57 +02:00
browsingSettingsTemplate = _.template(browsingSettingsHtml);
user = response.json;
render();
}).fail(function(response) {
$el.empty();
messagePresenter.showError($messages, response.json && response.json.error || response);
});
}
function render() {
2014-09-05 13:50:51 +02:00
var context = {
user: user,
2014-09-06 10:00:26 +02:00
canDeleteAccount: auth.hasPrivilege(auth.privileges.deleteAllAccounts) ||
(auth.isLoggedIn(userName) && auth.hasPrivilege(auth.privileges.deleteOwnAccount)),
2014-09-05 13:50:51 +02:00
};
$el.html(template(context));
$el.find('.browsing-settings').html(browsingSettingsTemplate(context));
$el.find('.account-settings').html(accountSettingsTemplate(context));
$el.find('.account-removal').html(accountRemovalTemplate(context));
$el.find('.account-removal form').submit(accountRemovalFormSubmitted);
2014-09-04 19:07:57 +02:00
$messages = $el.find('.messages');
};
2014-09-05 13:50:51 +02:00
function accountRemovalFormSubmitted(e) {
e.preventDefault();
$messages = $el.find('.account-removal .messages');
messagePresenter.hideMessages($messages);
if (!$el.find('.account-removal input[name=confirmation]:visible').prop('checked')) {
messagePresenter.showError($messages, 'Must confirm to proceed.');
return;
}
api.delete('/users/' + user.name)
.then(function() {
auth.logout();
var $messageDiv = messagePresenter.showInfo($messages, 'Account deleted. <a href="">Back to main page</a>');
$messageDiv.find('a').click(mainPageLinkClicked);
}).fail(function(response) {
messagePresenter.showError($messages, response.json && response.json.error || response);
});
}
return {
init: init,
render: render
};
};
App.DI.register('userPresenter', App.Presenters.UserPresenter);