jshint and refactor accountedit

v1.18.x
barisusakli 11 years ago
parent 1e3b88a7f0
commit 7545d55b22

@ -23,6 +23,7 @@
"follow": "Follow", "follow": "Follow",
"unfollow": "Unfollow", "unfollow": "Unfollow",
"profile_update_success": "Profile has been updated successfully!",
"change_picture": "Change Picture", "change_picture": "Change Picture",
"edit": "Edit", "edit": "Edit",
"uploaded_picture": "Uploaded Picture", "uploaded_picture": "Uploaded Picture",

@ -1,18 +1,28 @@
'use strict';
/* globals define, ajaxify, socket, app, config, utils, translator */
define(['forum/accountheader', 'uploader'], function(header, uploader) { define(['forum/accountheader', 'uploader'], function(header, uploader) {
var AccountEdit = {}, var AccountEdit = {},
gravatarPicture = '', gravatarPicture = '',
uploadedPicture = ''; uploadedPicture = '',
selectedImageType = '';
AccountEdit.init = function() { AccountEdit.init = function() {
header.init();
gravatarPicture = ajaxify.variables.get('gravatarpicture'); gravatarPicture = ajaxify.variables.get('gravatarpicture');
uploadedPicture = ajaxify.variables.get('uploadedpicture'); uploadedPicture = ajaxify.variables.get('uploadedpicture');
var selectedImageType = ''; header.init();
$('#submitBtn').on('click', function() { $('#submitBtn').on('click', updateProfile);
handleImageChange();
handleImageUpload();
handlePasswordChange();
updateSignature();
};
function updateProfile() {
var userData = { var userData = {
uid: $('#inputUID').val(), uid: $('#inputUID').val(),
username: $('#inputUsername').val(), username: $('#inputUsername').val(),
@ -29,11 +39,8 @@ define(['forum/accountheader', 'uploader'], function(header, uploader) {
return app.alertError(err.message); return app.alertError(err.message);
} }
if (!data) { app.alertSuccess('[[user:profile_update_success]]');
return app.alertError('There was an error updating the profile!');
}
app.alertSuccess('Profile has been updated successfully!');
if (data.picture) { if (data.picture) {
$('#user-current-picture').attr('src', data.picture); $('#user-current-picture').attr('src', data.picture);
$('#user_label img').attr('src', data.picture); $('#user_label img').attr('src', data.picture);
@ -56,27 +63,20 @@ define(['forum/accountheader', 'uploader'], function(header, uploader) {
$('#user-profile-link span').html(' ' + userData.username); $('#user-profile-link span').html(' ' + userData.username);
} }
}); });
return false;
});
return false;
function getSignatureCharsLeft() {
if($('#inputSignature').length) {
return '(' + $('#inputSignature').val().length + '/' + config.maximumSignatureLength + ')';
} else {
return '';
}
} }
$('#signatureCharCountLeft').html(getSignatureCharsLeft()); function handleImageChange() {
function selectImageType(type) {
$('#inputSignature').on('keyup change', function(ev) { $('#gravatar-box .fa-check').toggle(type === 'gravatar');
$('#signatureCharCountLeft').html(getSignatureCharsLeft()); $('#uploaded-box .fa-check').toggle(type === 'uploaded');
}); selectedImageType = type;
}
$('#changePictureBtn').on('click', function() { $('#changePictureBtn').on('click', function() {
selectedImageType = ''; selectedImageType = '';
AccountEdit.updateImages(); updateImages();
$('#change-picture-modal').modal('show'); $('#change-picture-modal').modal('show');
$('#change-picture-modal').removeClass('hide'); $('#change-picture-modal').removeClass('hide');
@ -85,31 +85,29 @@ define(['forum/accountheader', 'uploader'], function(header, uploader) {
}); });
$('#gravatar-box').on('click', function() { $('#gravatar-box').on('click', function() {
$('#gravatar-box .fa-check').show(); selectImageType('gravatar');
$('#uploaded-box .fa-check').hide();
selectedImageType = 'gravatar';
}); });
$('#uploaded-box').on('click', function() { $('#uploaded-box').on('click', function() {
$('#gravatar-box .fa-check').hide(); selectImageType('uploaded');
$('#uploaded-box .fa-check').show();
selectedImageType = 'uploaded';
}); });
$('#savePictureChangesBtn').on('click', function() { $('#savePictureChangesBtn').on('click', function() {
$('#change-picture-modal').modal('hide'); $('#change-picture-modal').modal('hide');
if (selectedImageType) { if (selectedImageType) {
AccountEdit.changeUserPicture(selectedImageType); changeUserPicture(selectedImageType);
if (selectedImageType == 'gravatar') if (selectedImageType === 'gravatar') {
$('#user-current-picture').attr('src', gravatarPicture); $('#user-current-picture').attr('src', gravatarPicture);
else if (selectedImageType == 'uploaded') } else if (selectedImageType === 'uploaded') {
$('#user-current-picture').attr('src', uploadedPicture); $('#user-current-picture').attr('src', uploadedPicture);
} }
}
}); });
}
function handleImageUpload() {
$('#upload-picture-modal').on('hide', function() { $('#upload-picture-modal').on('hide', function() {
$('#userPhotoInput').val(''); $('#userPhotoInput').val('');
}); });
@ -117,7 +115,7 @@ define(['forum/accountheader', 'uploader'], function(header, uploader) {
$('#uploadPictureBtn').on('click', function() { $('#uploadPictureBtn').on('click', function() {
$('#change-picture-modal').modal('hide'); $('#change-picture-modal').modal('hide');
uploader.open(RELATIVE_PATH + '/user/uploadpicture', {uid: ajaxify.variables.get('theirid')}, config.maximumProfileImageSize, function(imageUrlOnServer) { uploader.open(config.relative_path + '/user/uploadpicture', {uid: ajaxify.variables.get('theirid')}, config.maximumProfileImageSize, function(imageUrlOnServer) {
imageUrlOnServer = imageUrlOnServer + '?' + new Date().getTime(); imageUrlOnServer = imageUrlOnServer + '?' + new Date().getTime();
$('#user-current-picture').attr('src', imageUrlOnServer); $('#user-current-picture').attr('src', imageUrlOnServer);
@ -130,32 +128,11 @@ define(['forum/accountheader', 'uploader'], function(header, uploader) {
}, app.updateHeader); }, app.updateHeader);
}); });
return false; return false;
}); });
function showError(element, msg) {
translator.translate(msg, function(msg) {
element.html(msg);
element.parent()
.removeClass('alert-success')
.addClass('alert-danger');
element.show();
validationError = true;
});
} }
function showSuccess(element, msg) { function handlePasswordChange() {
translator.translate(msg, function(msg) {
element.html(msg);
element.parent()
.removeClass('alert-danger')
.addClass('alert-success');
element.show();
});
}
(function handlePasswordChange() {
var currentPassword = $('#inputCurrentPassword'); var currentPassword = $('#inputCurrentPassword');
var password_notify = $('#password-notify'); var password_notify = $('#password-notify');
var password_confirm_notify = $('#password-confirm-notify'); var password_confirm_notify = $('#password-confirm-notify');
@ -165,7 +142,6 @@ define(['forum/accountheader', 'uploader'], function(header, uploader) {
var passwordsmatch = false; var passwordsmatch = false;
var successIcon = '<i class="fa fa-check"></i>'; var successIcon = '<i class="fa fa-check"></i>';
function onPasswordChanged() { function onPasswordChanged() {
passwordvalid = utils.isPasswordValid(password.val()); passwordvalid = utils.isPasswordValid(password.val());
if (password.val().length < config.minimumPasswordLength) { if (password.val().length < config.minimumPasswordLength) {
@ -180,7 +156,7 @@ define(['forum/accountheader', 'uploader'], function(header, uploader) {
function onPasswordConfirmChanged() { function onPasswordConfirmChanged() {
if(password.val()) { if(password.val()) {
if (password.val() !== password_confirm.val()) { if (password.val() !== password_confirm.val()) {
showError(password_confirm_notify, '[[user:change_password_error_match]]') showError(password_confirm_notify, '[[user:change_password_error_match]]');
passwordsmatch = false; passwordsmatch = false;
} else { } else {
showSuccess(password_confirm_notify, successIcon); showSuccess(password_confirm_notify, successIcon);
@ -214,53 +190,67 @@ define(['forum/accountheader', 'uploader'], function(header, uploader) {
} }
return false; return false;
}); });
}
}()); function changeUserPicture(type) {
}; socket.emit('user.changePicture', {
AccountEdit.changeUserPicture = function(type) {
var userData = {
type: type, type: type,
uid: ajaxify.variables.get('theirid') uid: ajaxify.variables.get('theirid')
}; }, function(err) {
socket.emit('user.changePicture', userData, function(err) {
if(err) { if(err) {
app.alertError(err.message); app.alertError(err.message);
} }
}); });
} }
AccountEdit.updateImages = function() { function updateImages() {
var currentPicture = $('#user-current-picture').attr('src'); var currentPicture = $('#user-current-picture').attr('src');
if (gravatarPicture) { if (gravatarPicture) {
$('#user-gravatar-picture').attr('src', gravatarPicture); $('#user-gravatar-picture').attr('src', gravatarPicture);
$('#gravatar-box').show();
} else {
$('#gravatar-box').hide();
} }
if (uploadedPicture) { if (uploadedPicture) {
$('#user-uploaded-picture').attr('src', uploadedPicture); $('#user-uploaded-picture').attr('src', uploadedPicture);
$('#uploaded-box').show();
} else {
$('#uploaded-box').hide();
} }
$('#gravatar-box').toggle(!!gravatarPicture);
$('#uploaded-box').toggle(!!uploadedPicture);
if (currentPicture == gravatarPicture) { $('#gravatar-box .fa-check').toggle(currentPicture === gravatarPicture);
$('#gravatar-box .fa-check').show(); $('#uploaded-box .fa-check').toggle(currentPicture === uploadedPicture);
} else {
$('#gravatar-box .fa-check').hide();
} }
if (currentPicture == uploadedPicture) { function updateSignature() {
$('#uploaded-box .fa-check').show(); function getSignatureCharsLeft() {
} else { return $('#inputSignature').length ? '(' + $('#inputSignature').val().length + '/' + config.maximumSignatureLength + ')' : '';
$('#uploaded-box .fa-check').hide(); }
$('#signatureCharCountLeft').html(getSignatureCharsLeft());
$('#inputSignature').on('keyup change', function(ev) {
$('#signatureCharCountLeft').html(getSignatureCharsLeft());
});
} }
function showError(element, msg) {
translator.translate(msg, function(msg) {
element.html(msg);
element.parent()
.removeClass('alert-success')
.addClass('alert-danger');
element.show();
});
}
function showSuccess(element, msg) {
translator.translate(msg, function(msg) {
element.html(msg);
element.parent()
.removeClass('alert-danger')
.addClass('alert-success');
element.show();
});
} }
return AccountEdit; return AccountEdit;

Loading…
Cancel
Save