2017-01-10 18:02:20 -05:00
|
|
|
/* eslint-disable comma-dangle, no-unused-vars, class-methods-use-this, quotes, consistent-return, func-names, prefer-arrow-callback, space-before-function-paren, max-len */
|
2016-12-13 22:01:05 -05:00
|
|
|
/* global Flash */
|
|
|
|
|
2016-09-08 11:22:14 -04:00
|
|
|
((global) => {
|
2016-09-08 11:18:35 -04:00
|
|
|
class Profile {
|
2016-09-27 07:37:26 -04:00
|
|
|
constructor({ form } = {}) {
|
2016-09-08 11:18:35 -04:00
|
|
|
this.onSubmitForm = this.onSubmitForm.bind(this);
|
2016-09-09 12:31:26 -04:00
|
|
|
this.form = form || $('.edit-user');
|
2016-07-24 16:45:11 -04:00
|
|
|
this.bindEvents();
|
2016-09-08 11:18:35 -04:00
|
|
|
this.initAvatarGlCrop();
|
|
|
|
}
|
|
|
|
|
|
|
|
initAvatarGlCrop() {
|
|
|
|
const cropOpts = {
|
2016-07-24 16:45:11 -04:00
|
|
|
filename: '.js-avatar-filename',
|
|
|
|
previewImage: '.avatar-image .avatar',
|
|
|
|
modalCrop: '.modal-profile-crop',
|
|
|
|
pickImageEl: '.js-choose-user-avatar-button',
|
|
|
|
uploadImageBtn: '.js-upload-user-avatar',
|
|
|
|
modalCropImg: '.modal-profile-crop-image'
|
|
|
|
};
|
|
|
|
this.avatarGlCrop = $('.js-user-avatar-input').glCrop(cropOpts).data('glcrop');
|
|
|
|
}
|
|
|
|
|
2016-09-08 11:18:35 -04:00
|
|
|
bindEvents() {
|
|
|
|
$('.js-preferences-form').on('change.preference', 'input[type=radio]', this.submitForm);
|
|
|
|
$('#user_notification_email').on('change', this.submitForm);
|
2017-01-27 04:32:00 -05:00
|
|
|
$('#user_notified_of_own_activity').on('change', this.submitForm);
|
2016-09-08 11:18:35 -04:00
|
|
|
$('.update-username').on('ajax:before', this.beforeUpdateUsername);
|
|
|
|
$('.update-username').on('ajax:complete', this.afterUpdateUsername);
|
|
|
|
$('.update-notifications').on('ajax:success', this.onUpdateNotifs);
|
|
|
|
this.form.on('submit', this.onSubmitForm);
|
|
|
|
}
|
|
|
|
|
|
|
|
submitForm() {
|
|
|
|
return $(this).parents('form').submit();
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
2016-09-08 11:18:35 -04:00
|
|
|
onSubmitForm(e) {
|
2017-01-26 14:05:27 -05:00
|
|
|
e.preventDefault();
|
2016-07-24 16:45:11 -04:00
|
|
|
return this.saveForm();
|
2016-09-08 11:18:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
beforeUpdateUsername() {
|
2016-12-27 16:43:54 -05:00
|
|
|
$('.loading-username', this).removeClass('hidden');
|
2016-09-08 11:18:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
afterUpdateUsername() {
|
2016-12-27 16:43:54 -05:00
|
|
|
$('.loading-username', this).addClass('hidden');
|
|
|
|
$('button[type=submit]', this).enable();
|
2016-09-08 11:18:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
onUpdateNotifs(e, data) {
|
|
|
|
return data.saved ?
|
|
|
|
new Flash("Notification settings saved", "notice") :
|
|
|
|
new Flash("Failed to save new settings", "alert");
|
|
|
|
}
|
|
|
|
|
|
|
|
saveForm() {
|
|
|
|
const self = this;
|
|
|
|
const formData = new FormData(this.form[0]);
|
|
|
|
const avatarBlob = this.avatarGlCrop.getBlob();
|
2016-07-24 16:45:11 -04:00
|
|
|
|
|
|
|
if (avatarBlob != null) {
|
|
|
|
formData.append('user[avatar]', avatarBlob, 'avatar.png');
|
|
|
|
}
|
2016-09-08 11:18:35 -04:00
|
|
|
|
2016-07-24 16:45:11 -04:00
|
|
|
return $.ajax({
|
|
|
|
url: this.form.attr('action'),
|
|
|
|
type: this.form.attr('method'),
|
|
|
|
data: formData,
|
|
|
|
dataType: "json",
|
|
|
|
processData: false,
|
|
|
|
contentType: false,
|
2016-09-09 10:57:13 -04:00
|
|
|
success: response => new Flash(response.message, 'notice'),
|
|
|
|
error: jqXHR => new Flash(jqXHR.responseJSON.message, 'alert'),
|
2016-09-08 11:18:35 -04:00
|
|
|
complete: () => {
|
2016-07-24 16:45:11 -04:00
|
|
|
window.scrollTo(0, 0);
|
2016-07-26 23:32:10 -04:00
|
|
|
// Enable submit button after requests ends
|
2016-07-24 16:45:11 -04:00
|
|
|
return self.form.find(':input[disabled]').enable();
|
|
|
|
}
|
|
|
|
});
|
2016-09-08 11:18:35 -04:00
|
|
|
}
|
|
|
|
}
|
2016-07-24 16:45:11 -04:00
|
|
|
|
|
|
|
$(function() {
|
|
|
|
$(document).on('focusout.ssh_key', '#key_key', function() {
|
2016-09-08 11:18:35 -04:00
|
|
|
const $title = $('#key_title');
|
|
|
|
const comment = $(this).val().match(/^\S+ \S+ (.+)\n?$/);
|
2016-07-24 16:45:11 -04:00
|
|
|
if (comment && comment.length > 1 && $title.val() === '') {
|
|
|
|
return $title.val(comment[1]).change();
|
|
|
|
}
|
2016-07-26 23:32:10 -04:00
|
|
|
// Extract the SSH Key title from its comment
|
2016-07-24 16:45:11 -04:00
|
|
|
});
|
2016-09-08 11:18:35 -04:00
|
|
|
if (global.utils.getPagePath() === 'profiles') {
|
2016-07-24 16:45:11 -04:00
|
|
|
return new Profile();
|
|
|
|
}
|
|
|
|
});
|
2016-09-08 11:18:35 -04:00
|
|
|
})(window.gl || (window.gl = {}));
|