2014-09-07 00:33:46 +02:00
|
|
|
var App = App || {};
|
|
|
|
App.Presenters = App.Presenters || {};
|
|
|
|
|
|
|
|
App.Presenters.UserAccountSettingsPresenter = function(
|
2014-09-08 22:02:28 +02:00
|
|
|
_,
|
2014-09-07 00:33:46 +02:00
|
|
|
jQuery,
|
|
|
|
util,
|
|
|
|
promise,
|
|
|
|
api,
|
|
|
|
auth,
|
|
|
|
messagePresenter) {
|
|
|
|
|
|
|
|
var target;
|
|
|
|
var template;
|
|
|
|
var user;
|
|
|
|
var privileges;
|
|
|
|
var avatarContent;
|
|
|
|
|
2014-09-17 22:37:49 +02:00
|
|
|
function init(args, loaded) {
|
|
|
|
user = args.user;
|
|
|
|
target = args.target;
|
2014-09-07 00:33:46 +02:00
|
|
|
|
2014-09-17 22:37:49 +02:00
|
|
|
privileges = {
|
|
|
|
canChangeAccessRank:
|
|
|
|
auth.hasPrivilege(auth.privileges.changeAccessRank),
|
|
|
|
canChangeAvatarStyle:
|
|
|
|
auth.hasPrivilege(auth.privileges.changeAllAvatarStyles) ||
|
|
|
|
(auth.hasPrivilege(auth.privileges.changeOwnAvatarStyle) && auth.isLoggedIn(user.name)),
|
|
|
|
canChangeName:
|
|
|
|
auth.hasPrivilege(auth.privileges.changeAllNames) ||
|
|
|
|
(auth.hasPrivilege(auth.privileges.changeOwnName) && auth.isLoggedIn(user.name)),
|
|
|
|
canChangeEmailAddress:
|
|
|
|
auth.hasPrivilege(auth.privileges.changeAllEmailAddresses) ||
|
|
|
|
(auth.hasPrivilege(auth.privileges.changeOwnEmailAddress) && auth.isLoggedIn(user.name)),
|
|
|
|
canChangePassword:
|
|
|
|
auth.hasPrivilege(auth.privileges.changeAllPasswords) ||
|
|
|
|
(auth.hasPrivilege(auth.privileges.changeOwnPassword) && auth.isLoggedIn(user.name)),
|
|
|
|
};
|
2014-09-07 00:33:46 +02:00
|
|
|
|
2014-09-17 22:37:49 +02:00
|
|
|
promise.wait(util.promiseTemplate('account-settings'))
|
|
|
|
.then(function(html) {
|
2014-09-07 00:33:46 +02:00
|
|
|
template = _.template(html);
|
|
|
|
render();
|
2014-09-17 22:37:49 +02:00
|
|
|
loaded();
|
2014-09-07 00:33:46 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function render() {
|
|
|
|
var $el = jQuery(target);
|
|
|
|
$el.html(template(_.extend({user: user}, privileges)));
|
|
|
|
$el.find('form').submit(accountSettingsFormSubmitted);
|
|
|
|
$el.find('form [name=avatar-style]').change(avatarStyleChanged);
|
|
|
|
avatarStyleChanged();
|
2014-09-13 16:09:23 +02:00
|
|
|
new App.Controls.FileDropper($el.find('[name=avatar-content]'), avatarContentChanged, jQuery);
|
2014-09-07 00:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function getPrivileges() {
|
|
|
|
return privileges;
|
|
|
|
}
|
|
|
|
|
|
|
|
function avatarStyleChanged(e) {
|
|
|
|
var $el = jQuery(target);
|
|
|
|
var $target = $el.find('.avatar-content .file-handler');
|
2014-09-08 22:02:28 +02:00
|
|
|
if ($el.find('[name=avatar-style]:checked').val() === 'manual') {
|
2014-09-07 00:33:46 +02:00
|
|
|
$target.show();
|
|
|
|
} else {
|
|
|
|
$target.hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function avatarContentChanged(files) {
|
2014-09-08 22:02:28 +02:00
|
|
|
if (files.length === 1) {
|
2014-09-07 00:33:46 +02:00
|
|
|
var reader = new FileReader();
|
|
|
|
reader.onloadend = function() {
|
|
|
|
avatarContent = reader.result;
|
|
|
|
var $el = jQuery(target);
|
|
|
|
var $target = $el.find('.avatar-content .file-handler');
|
|
|
|
$target.html(files[0].name);
|
2014-09-08 22:02:28 +02:00
|
|
|
};
|
2014-09-07 00:33:46 +02:00
|
|
|
reader.readAsDataURL(files[0]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function accountSettingsFormSubmitted(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
var $el = jQuery(target);
|
|
|
|
var $messages = jQuery(target).find('.messages');
|
|
|
|
messagePresenter.hideMessages($messages);
|
|
|
|
var formData = {};
|
|
|
|
|
|
|
|
if (privileges.canChangeAvatarStyle) {
|
|
|
|
formData.avatarStyle = $el.find('[name=avatar-style]:checked').val();
|
2014-09-08 22:02:28 +02:00
|
|
|
if (avatarContent) {
|
2014-09-07 00:33:46 +02:00
|
|
|
formData.avatarContent = avatarContent;
|
2014-09-08 22:02:28 +02:00
|
|
|
}
|
2014-09-07 00:33:46 +02:00
|
|
|
}
|
|
|
|
if (privileges.canChangeName) {
|
|
|
|
formData.userName = $el.find('[name=userName]').val();
|
|
|
|
}
|
|
|
|
if (privileges.canChangeEmailAddress) {
|
|
|
|
formData.email = $el.find('[name=email]').val();
|
|
|
|
}
|
|
|
|
if (privileges.canChangePassword) {
|
|
|
|
formData.password = $el.find('[name=password]').val();
|
|
|
|
formData.passwordConfirmation = $el.find('[name=passwordConfirmation]').val();
|
|
|
|
}
|
|
|
|
if (privileges.canChangeAccessRank) {
|
2014-09-17 10:40:09 +02:00
|
|
|
formData.accessRank = $el.find('[name=access-rank]:checked').val();
|
2014-09-07 00:33:46 +02:00
|
|
|
}
|
|
|
|
|
2014-09-10 18:06:49 +02:00
|
|
|
if (!validateAccountSettingsFormData(formData)) {
|
2014-09-07 00:33:46 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!formData.password) {
|
|
|
|
delete formData.password;
|
|
|
|
delete formData.passwordConfirmation;
|
|
|
|
}
|
|
|
|
|
|
|
|
api.put('/users/' + user.name, formData)
|
|
|
|
.then(function(response) {
|
2014-09-10 18:06:49 +02:00
|
|
|
editSuccess(response);
|
2014-09-07 00:33:46 +02:00
|
|
|
}).fail(function(response) {
|
2014-09-10 18:06:49 +02:00
|
|
|
editFailure(response);
|
2014-09-07 00:33:46 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-09-10 18:06:49 +02:00
|
|
|
function editSuccess(apiResponse) {
|
2014-09-10 19:19:30 +02:00
|
|
|
var wasLoggedIn = auth.isLoggedIn(user.name);
|
2014-09-10 18:06:49 +02:00
|
|
|
user = apiResponse.json;
|
2014-09-10 19:19:30 +02:00
|
|
|
if (wasLoggedIn) {
|
|
|
|
auth.updateCurrentUser(user);
|
|
|
|
}
|
|
|
|
|
2014-09-10 18:06:49 +02:00
|
|
|
render();
|
2014-09-10 19:19:30 +02:00
|
|
|
|
2014-09-10 18:06:49 +02:00
|
|
|
var $messages = jQuery(target).find('.messages');
|
2014-09-07 00:33:46 +02:00
|
|
|
var message = 'Account settings updated!';
|
2014-09-10 18:06:49 +02:00
|
|
|
if (!apiResponse.json.confirmed) {
|
|
|
|
message += '<br/>Check your inbox for activation e-mail.<br/>If e-mail doesn\'t show up, check your spam folder.';
|
|
|
|
}
|
2014-09-07 00:33:46 +02:00
|
|
|
messagePresenter.showInfo($messages, message);
|
|
|
|
}
|
|
|
|
|
2014-09-10 18:06:49 +02:00
|
|
|
function editFailure(apiResponse) {
|
|
|
|
var $messages = jQuery(target).find('.messages');
|
2014-09-07 00:33:46 +02:00
|
|
|
messagePresenter.showError($messages, apiResponse.json && apiResponse.json.error || apiResponse);
|
|
|
|
}
|
|
|
|
|
2014-09-10 18:06:49 +02:00
|
|
|
function validateAccountSettingsFormData(formData) {
|
|
|
|
var $messages = jQuery(target).find('.messages');
|
2014-09-08 22:02:28 +02:00
|
|
|
if (formData.password !== formData.passwordConfirmation) {
|
2014-09-07 00:33:46 +02:00
|
|
|
messagePresenter.showError($messages, 'Passwords must be the same.');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
init: init,
|
|
|
|
render: render,
|
|
|
|
getPrivileges: getPrivileges,
|
|
|
|
};
|
2014-09-08 22:02:28 +02:00
|
|
|
|
|
|
|
};
|
2014-09-07 00:33:46 +02:00
|
|
|
|
2014-09-11 12:33:44 +02:00
|
|
|
App.DI.register('userAccountSettingsPresenter', ['_', 'jQuery', 'util', 'promise', 'api', 'auth', 'messagePresenter'], App.Presenters.UserAccountSettingsPresenter);
|