debian-mirror-gitlab/app/assets/javascripts/profile/profile.js

122 lines
3.8 KiB
JavaScript
Raw Normal View History

2017-08-17 22:00:37 +05:30
/* 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 */
2018-03-17 18:26:18 +05:30
import Cookies from 'js-cookie';
import { getPagePath } from '~/lib/utils/common_utils';
import axios from '~/lib/utils/axios_utils';
import { __ } from '~/locale';
import flash from '../flash';
2016-11-03 12:29:30 +05:30
2017-08-17 22:00:37 +05:30
((global) => {
2016-11-03 12:29:30 +05:30
class Profile {
constructor({ form } = {}) {
this.onSubmitForm = this.onSubmitForm.bind(this);
this.form = form || $('.edit-user');
2018-03-17 18:26:18 +05:30
this.newRepoActivated = Cookies.get('new_repo');
this.setRepoRadio();
2016-11-03 12:29:30 +05:30
this.bindEvents();
this.initAvatarGlCrop();
}
initAvatarGlCrop() {
const cropOpts = {
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');
}
bindEvents() {
$('.js-preferences-form').on('change.preference', 'input[type=radio]', this.submitForm);
2018-03-17 18:26:18 +05:30
$('input[name="user[multi_file]"]').on('change', this.setNewRepoCookie);
2016-11-03 12:29:30 +05:30
$('#user_notification_email').on('change', this.submitForm);
2017-08-17 22:00:37 +05:30
$('#user_notified_of_own_activity').on('change', this.submitForm);
2016-11-03 12:29:30 +05:30
$('.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();
}
onSubmitForm(e) {
e.preventDefault();
return this.saveForm();
}
beforeUpdateUsername() {
2017-08-17 22:00:37 +05:30
$('.loading-username', this).removeClass('hidden');
2016-11-03 12:29:30 +05:30
}
afterUpdateUsername() {
2017-08-17 22:00:37 +05:30
$('.loading-username', this).addClass('hidden');
$('button[type=submit]', this).enable();
2016-11-03 12:29:30 +05:30
}
onUpdateNotifs(e, data) {
return data.saved ?
2018-03-17 18:26:18 +05:30
flash(__('Notification settings saved'), 'notice') :
flash(__('Failed to save new settings'));
2016-11-03 12:29:30 +05:30
}
saveForm() {
const self = this;
const formData = new FormData(this.form[0]);
const avatarBlob = this.avatarGlCrop.getBlob();
if (avatarBlob != null) {
formData.append('user[avatar]', avatarBlob, 'avatar.png');
}
2018-03-17 18:26:18 +05:30
axios({
method: this.form.attr('method'),
2016-11-03 12:29:30 +05:30
url: this.form.attr('action'),
data: formData,
2018-03-17 18:26:18 +05:30
})
.then(({ data }) => flash(data.message, 'notice'))
.then(() => {
window.scrollTo(0, 0);
// Enable submit button after requests ends
self.form.find(':input[disabled]').enable();
})
.catch(error => flash(error.message));
}
setNewRepoCookie() {
if (this.value === 'off') {
Cookies.remove('new_repo');
} else {
Cookies.set('new_repo', true, { expires_in: 365 });
}
}
setRepoRadio() {
const multiEditRadios = $('input[name="user[multi_file]"]');
if (this.newRepoActivated || this.newRepoActivated === 'true') {
multiEditRadios.filter('[value=on]').prop('checked', true);
} else {
multiEditRadios.filter('[value=off]').prop('checked', true);
}
2016-11-03 12:29:30 +05:30
}
}
$(function() {
2017-08-17 22:00:37 +05:30
$(document).on('input.ssh_key', '#key_key', function() {
2016-11-03 12:29:30 +05:30
const $title = $('#key_title');
const comment = $(this).val().match(/^\S+ \S+ (.+)\n?$/);
2017-08-17 22:00:37 +05:30
// Extract the SSH Key title from its comment
if (comment && comment.length > 1) {
2016-11-03 12:29:30 +05:30
return $title.val(comment[1]).change();
}
});
2018-03-17 18:26:18 +05:30
if (getPagePath() === 'profiles') {
2016-11-03 12:29:30 +05:30
return new Profile();
}
});
})(window.gl || (window.gl = {}));