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

85 lines
2.3 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-05-09 12:01:36 +05:30
import $ from 'jquery';
2018-03-17 18:26:18 +05:30
import axios from '~/lib/utils/axios_utils';
import { __ } from '~/locale';
import flash from '../flash';
2016-11-03 12:29:30 +05:30
2018-03-27 19:54:05 +05:30
export default class Profile {
constructor({ form } = {}) {
this.onSubmitForm = this.onSubmitForm.bind(this);
this.form = form || $('.edit-user');
this.setRepoRadio();
this.bindEvents();
this.initAvatarGlCrop();
}
2016-11-03 12:29:30 +05:30
2018-03-27 19:54:05 +05:30
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',
2018-05-09 12:01:36 +05:30
modalCropImg: '.modal-profile-crop-image',
2018-03-27 19:54:05 +05:30
};
2018-05-09 12:01:36 +05:30
this.avatarGlCrop = $('.js-user-avatar-input')
.glCrop(cropOpts)
.data('glcrop');
2018-03-27 19:54:05 +05:30
}
2016-11-03 12:29:30 +05:30
2018-03-27 19:54:05 +05:30
bindEvents() {
2018-05-09 12:01:36 +05:30
$('.js-preferences-form').on(
'change.preference',
'input[type=radio]',
this.submitForm,
);
2018-03-27 19:54:05 +05:30
$('#user_notification_email').on('change', this.submitForm);
$('#user_notified_of_own_activity').on('change', this.submitForm);
this.form.on('submit', this.onSubmitForm);
}
2016-11-03 12:29:30 +05:30
2018-03-27 19:54:05 +05:30
submitForm() {
2018-05-09 12:01:36 +05:30
return $(this)
.parents('form')
.submit();
2018-03-27 19:54:05 +05:30
}
2016-11-03 12:29:30 +05:30
2018-03-27 19:54:05 +05:30
onSubmitForm(e) {
e.preventDefault();
return this.saveForm();
}
2016-11-03 12:29:30 +05:30
2018-03-27 19:54:05 +05:30
saveForm() {
const self = this;
const formData = new FormData(this.form[0]);
const avatarBlob = this.avatarGlCrop.getBlob();
2016-11-03 12:29:30 +05:30
2018-03-27 19:54:05 +05:30
if (avatarBlob != null) {
formData.append('user[avatar]', avatarBlob, 'avatar.png');
2018-03-17 18:26:18 +05:30
}
2018-03-27 19:54:05 +05:30
axios({
method: this.form.attr('method'),
url: this.form.attr('action'),
data: formData,
})
2018-05-09 12:01:36 +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));
2016-11-03 12:29:30 +05:30
}
2018-03-27 19:54:05 +05:30
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
}
2018-03-27 19:54:05 +05:30
}
}