Files
NodeBB/public/src/forum/accountedit.js

290 lines
7.2 KiB
JavaScript
Raw Normal View History

var gravatarPicture = templates.get('gravatarpicture');
var uploadedPicture = templates.get('uploadedpicture');
$(document).ready(function() {
2013-08-28 04:36:14 +08:00
2013-06-20 16:48:17 -04:00
$('#uploadForm').submit(function() {
status('uploading the file ...');
2013-08-28 04:36:14 +08:00
$('#upload-progress-bar').css('width', '0%');
$('#upload-progress-box').show();
2013-08-28 04:36:14 +08:00
$('#upload-progress-box').removeClass('hide');
2013-09-17 13:05:54 -04:00
if (!$('#userPhotoInput').val()) {
error('select an image to upload!');
2013-08-28 04:36:14 +08:00
return false;
}
2013-08-28 04:36:14 +08:00
2013-06-21 15:51:03 -04:00
$(this).find('#imageUploadCsrf').val($('#csrf_token').val());
2013-08-28 04:36:14 +08:00
2013-06-20 16:48:17 -04:00
$(this).ajaxSubmit({
2013-08-28 04:36:14 +08:00
error: function(xhr) {
error('Error: ' + xhr.status);
},
2013-08-28 04:36:14 +08:00
2013-09-17 13:05:54 -04:00
uploadProgress: function(event, position, total, percent) {
$('#upload-progress-bar').css('width', percent + '%');
2013-06-20 16:48:17 -04:00
},
2013-08-28 04:36:14 +08:00
success: function(response) {
2013-09-17 13:05:54 -04:00
if (response.error) {
error(response.error);
return;
}
2013-08-28 04:36:14 +08:00
var imageUrlOnServer = response.path;
2013-08-28 04:36:14 +08:00
2013-06-20 16:48:17 -04:00
$('#user-current-picture').attr('src', imageUrlOnServer);
$('#user-uploaded-picture').attr('src', imageUrlOnServer);
2013-08-28 04:36:14 +08:00
uploadedPicture = imageUrlOnServer;
setTimeout(function() {
hideAlerts();
2013-06-20 16:48:17 -04:00
$('#upload-picture-modal').modal('hide');
}, 750);
2013-08-28 04:36:14 +08:00
2013-09-17 13:05:54 -04:00
socket.emit('api:updateHeader', {
fields: ['username', 'picture', 'userslug']
});
success('File uploaded successfully!');
2013-06-20 16:48:17 -04:00
}
});
return false;
2013-06-20 16:48:17 -04:00
});
2013-08-28 04:36:14 +08:00
2013-06-20 16:48:17 -04:00
function hideAlerts() {
$('#alert-status').hide();
$('#alert-success').hide();
$('#alert-error').hide();
$('#upload-progress-box').hide();
2013-06-20 16:48:17 -04:00
}
2013-08-28 04:36:14 +08:00
2013-06-20 16:48:17 -04:00
function status(message) {
hideAlerts();
$('#alert-status').text(message).show();
2013-06-20 16:48:17 -04:00
}
2013-08-28 04:36:14 +08:00
2013-06-20 16:48:17 -04:00
function success(message) {
hideAlerts();
$('#alert-success').text(message).show();
2013-06-20 16:48:17 -04:00
}
2013-08-28 04:36:14 +08:00
2013-06-20 16:48:17 -04:00
function error(message) {
hideAlerts();
$('#alert-error').text(message).show();
2013-06-20 16:48:17 -04:00
}
2013-08-28 04:36:14 +08:00
function changeUserPicture(type) {
var userData = {
type: type
2013-06-20 16:48:17 -04:00
};
2013-08-28 04:36:14 +08:00
socket.emit('api:user.changePicture', userData, function(success) {
2013-09-17 13:05:54 -04:00
if (!success) {
2013-07-30 18:34:51 -04:00
app.alertError('There was an error changing picture!');
}
});
}
2013-08-28 04:36:14 +08:00
var selectedImageType = '';
2013-08-28 04:36:14 +08:00
2013-09-17 13:05:54 -04:00
$('#submitBtn').on('click', function() {
2013-08-28 04:36:14 +08:00
2013-09-17 13:05:54 -04:00
var userData = {
uid: $('#inputUID').val(),
email: $('#inputEmail').val(),
fullname: $('#inputFullname').val(),
website: $('#inputWebsite').val(),
birthday: $('#inputBirthday').val(),
location: $('#inputLocation').val(),
signature: $('#inputSignature').val()
2013-06-20 16:48:17 -04:00
};
2013-07-08 14:14:18 -04:00
2013-09-11 13:53:42 -04:00
socket.emit('api:user.updateProfile', userData, function(err, data) {
2013-09-17 13:05:54 -04:00
if (data.success) {
app.alertSuccess('Your profile has been updated successfully!');
2013-09-17 13:05:54 -04:00
if (data.picture) {
$('#user-current-picture').attr('src', data.picture);
$('#user_label img').attr('src', data.picture);
}
2013-09-17 13:05:54 -04:00
if (data.gravatarpicture) {
$('#user-gravatar-picture').attr('src', data.gravatarpicture);
gravatarPicture = data.gravatarpicture;
2013-08-28 04:36:14 +08:00
}
} else {
2013-09-11 15:24:01 -04:00
app.alertError('There was an error updating your profile! ' + err.error);
}
});
return false;
2013-06-20 16:48:17 -04:00
});
2013-08-28 04:36:14 +08:00
2013-06-20 16:48:17 -04:00
function updateImages() {
var currentPicture = $('#user-current-picture').attr('src');
2013-09-17 13:05:54 -04:00
if (gravatarPicture) {
2013-06-20 16:48:17 -04:00
$('#user-gravatar-picture').attr('src', gravatarPicture);
$('#gravatar-box').show();
2013-09-17 13:05:54 -04:00
} else
2013-06-20 16:48:17 -04:00
$('#gravatar-box').hide();
2013-09-17 13:05:54 -04:00
if (uploadedPicture) {
2013-06-20 16:48:17 -04:00
$('#user-uploaded-picture').attr('src', uploadedPicture);
$('#uploaded-box').show();
2013-09-17 13:05:54 -04:00
} else
2013-06-20 16:48:17 -04:00
$('#uploaded-box').hide();
2013-08-28 04:36:14 +08:00
2013-09-17 13:05:54 -04:00
if (currentPicture == gravatarPicture)
2013-06-20 16:48:17 -04:00
$('#gravatar-box .icon-ok').show();
else
2013-06-20 16:48:17 -04:00
$('#gravatar-box .icon-ok').hide();
2013-08-28 04:36:14 +08:00
2013-09-17 13:05:54 -04:00
if (currentPicture == uploadedPicture)
2013-06-20 16:48:17 -04:00
$('#uploaded-box .icon-ok').show();
else
2013-06-20 16:48:17 -04:00
$('#uploaded-box .icon-ok').hide();
}
2013-08-28 04:36:14 +08:00
2013-06-20 16:48:17 -04:00
$('#changePictureBtn').on('click', function() {
selectedImageType = '';
updateImages();
2013-08-28 04:36:14 +08:00
2013-06-20 16:48:17 -04:00
$('#change-picture-modal').modal('show');
2013-08-28 04:36:14 +08:00
$('#change-picture-modal').removeClass('hide');
2013-06-20 16:48:17 -04:00
return false;
});
2013-08-28 04:36:14 +08:00
2013-09-17 13:05:54 -04:00
$('#gravatar-box').on('click', function() {
$('#gravatar-box .icon-ok').show();
2013-06-20 16:48:17 -04:00
$('#uploaded-box .icon-ok').hide();
selectedImageType = 'gravatar';
});
2013-08-28 04:36:14 +08:00
2013-09-17 13:05:54 -04:00
$('#uploaded-box').on('click', function() {
$('#gravatar-box .icon-ok').hide();
2013-06-20 16:48:17 -04:00
$('#uploaded-box .icon-ok').show();
selectedImageType = 'uploaded';
});
2013-08-28 04:36:14 +08:00
$('#savePictureChangesBtn').on('click', function() {
2013-06-20 16:48:17 -04:00
$('#change-picture-modal').modal('hide');
2013-09-17 13:05:54 -04:00
if (selectedImageType) {
2013-06-20 16:48:17 -04:00
changeUserPicture(selectedImageType);
2013-08-28 04:36:14 +08:00
2013-09-17 13:05:54 -04:00
if (selectedImageType == 'gravatar')
2013-08-28 04:36:14 +08:00
$('#user-current-picture').attr('src', gravatarPicture);
2013-09-17 13:05:54 -04:00
else if (selectedImageType == 'uploaded')
2013-08-28 04:36:14 +08:00
$('#user-current-picture').attr('src', uploadedPicture);
2013-06-20 16:48:17 -04:00
}
2013-08-28 04:36:14 +08:00
});
2013-08-28 04:36:14 +08:00
$('#upload-picture-modal').on('hide', function() {
$('#userPhotoInput').val('');
});
2013-08-28 04:36:14 +08:00
2013-09-17 13:05:54 -04:00
$('#uploadPictureBtn').on('click', function() {
2013-08-28 04:36:14 +08:00
2013-06-20 16:48:17 -04:00
$('#change-picture-modal').modal('hide');
$('#upload-picture-modal').modal('show');
2013-08-28 04:36:14 +08:00
$('#upload-picture-modal').removeClass('hide');
2013-06-20 16:48:17 -04:00
hideAlerts();
2013-08-28 04:36:14 +08:00
2013-06-20 16:48:17 -04:00
return false;
});
2013-08-28 04:36:14 +08:00
$('#pictureUploadSubmitBtn').on('click', function() {
$('#uploadForm').submit();
});
2013-08-28 04:36:14 +08:00
2013-07-08 12:10:21 -04:00
(function handlePasswordChange() {
var currentPassword = $('#inputCurrentPassword');
var password_notify = $('#password-notify');
var password_confirm_notify = $('#password-confirm-notify');
var password = $('#inputNewPassword');
var password_confirm = $('#inputNewPasswordAgain');
var passwordvalid = false;
var passwordsmatch = false;
function onPasswordChanged() {
passwordvalid = utils.isPasswordValid(password.val());
2013-09-11 15:24:01 -04:00
if (password.val().length < config.minimumPasswordLength) {
2013-07-08 12:10:21 -04:00
password_notify.html('Password too short');
2013-09-11 15:24:01 -04:00
password_notify.attr('class', 'alert alert-danger');
password_notify.removeClass('hide');
2013-09-17 13:05:54 -04:00
} else if (!passwordvalid) {
2013-07-08 12:10:21 -04:00
password_notify.html('Invalid password');
2013-09-11 15:24:01 -04:00
password_notify.attr('class', 'alert alert-danger');
password_notify.removeClass('hide');
2013-07-08 12:10:21 -04:00
} else {
password_notify.html('OK!');
2013-09-11 15:24:01 -04:00
password_notify.attr('class', 'alert alert-success');
password_notify.removeClass('hide');
2013-07-08 12:10:21 -04:00
}
2013-08-28 04:36:14 +08:00
2013-07-08 12:10:21 -04:00
onPasswordConfirmChanged();
}
function onPasswordConfirmChanged() {
2013-09-17 13:05:54 -04:00
if (password_notify.hasClass('alert-danger') || !password_confirm.val()) {
2013-09-11 15:24:01 -04:00
password_confirm_notify.addClass('hide');
return;
}
2013-09-17 13:05:54 -04:00
if (password.val() !== password_confirm.val()) {
2013-07-08 12:10:21 -04:00
password_confirm_notify.html('Passwords must match!');
2013-09-11 15:24:01 -04:00
password_confirm_notify.attr('class', 'alert alert-danger');
password_confirm_notify.removeClass('hide');
2013-07-08 12:10:21 -04:00
passwordsmatch = false;
} else {
password_confirm_notify.html('OK!');
2013-09-11 15:24:01 -04:00
password_confirm_notify.attr('class', 'alert alert-success');
password_confirm_notify.removeClass('hide');
2013-07-08 12:10:21 -04:00
passwordsmatch = true;
}
}
2013-09-11 15:24:01 -04:00
password.on('blur', onPasswordChanged);
password_confirm.on('blur', onPasswordConfirmChanged);
2013-07-08 12:10:21 -04:00
$('#changePasswordBtn').on('click', function() {
2013-08-28 04:36:14 +08:00
2013-09-17 13:05:54 -04:00
if (passwordvalid && passwordsmatch && currentPassword.val()) {
socket.emit('api:user.changePassword', {
'currentPassword': currentPassword.val(),
'newPassword': password.val()
}, function(err) {
2013-08-28 04:36:14 +08:00
currentPassword.val('');
password.val('');
password_confirm.val('');
2013-09-11 15:24:01 -04:00
password_notify.addClass('hide');
password_confirm_notify.addClass('hide');
passwordsmatch = false;
passwordvalid = false;
2013-08-28 04:36:14 +08:00
2013-09-17 13:05:54 -04:00
if (err) {
2013-09-11 15:24:01 -04:00
app.alertError(err.error);
return;
}
2013-08-28 04:36:14 +08:00
app.alertSuccess('Your password is updated!');
2013-08-28 04:36:14 +08:00
2013-07-08 12:10:21 -04:00
});
}
return false;
});
2013-08-28 04:36:14 +08:00
2013-07-08 12:10:21 -04:00
}());
});