2017-08-17 22:00:37 +05:30
|
|
|
/* eslint-disable func-names, space-before-function-paren, wrap-iife, no-var, no-new, max-len */
|
|
|
|
/* global GitLab */
|
|
|
|
/* global DropzoneInput */
|
|
|
|
/* global autosize */
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
import GfmAutoComplete from './gfm_auto_complete';
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
window.gl = window.gl || {};
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
function GLForm(form, enableGFM = false) {
|
2017-08-17 22:00:37 +05:30
|
|
|
this.form = form;
|
|
|
|
this.textarea = this.form.find('textarea.js-gfm-input');
|
2017-09-10 17:25:29 +05:30
|
|
|
this.enableGFM = enableGFM;
|
2017-08-17 22:00:37 +05:30
|
|
|
// Before we start, we should clean up any previous data for this form
|
|
|
|
this.destroy();
|
|
|
|
// Setup the form
|
|
|
|
this.setupForm();
|
|
|
|
this.form.data('gl-form', this);
|
|
|
|
}
|
|
|
|
|
|
|
|
GLForm.prototype.destroy = function() {
|
|
|
|
// Clean form listeners
|
|
|
|
this.clearEventListeners();
|
2017-09-10 17:25:29 +05:30
|
|
|
if (this.autoComplete) {
|
|
|
|
this.autoComplete.destroy();
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
return this.form.data('gl-form', null);
|
|
|
|
};
|
|
|
|
|
|
|
|
GLForm.prototype.setupForm = function() {
|
|
|
|
var isNewForm;
|
|
|
|
isNewForm = this.form.is(':not(.gfm-form)');
|
|
|
|
this.form.removeClass('js-new-note-form');
|
|
|
|
if (isNewForm) {
|
|
|
|
this.form.find('.div-dropzone').remove();
|
|
|
|
this.form.addClass('gfm-form');
|
|
|
|
// remove notify commit author checkbox for non-commit notes
|
|
|
|
gl.utils.disableButtonIfEmptyField(this.form.find('.js-note-text'), this.form.find('.js-comment-button, .js-note-new-discussion'));
|
2017-09-10 17:25:29 +05:30
|
|
|
this.autoComplete = new GfmAutoComplete(gl.GfmAutoComplete && gl.GfmAutoComplete.dataSources);
|
|
|
|
this.autoComplete.setup(this.form.find('.js-gfm-input'), {
|
|
|
|
emojis: true,
|
|
|
|
members: this.enableGFM,
|
|
|
|
issues: this.enableGFM,
|
|
|
|
milestones: this.enableGFM,
|
|
|
|
mergeRequests: this.enableGFM,
|
|
|
|
labels: this.enableGFM,
|
|
|
|
});
|
2017-08-17 22:00:37 +05:30
|
|
|
new DropzoneInput(this.form);
|
|
|
|
autosize(this.textarea);
|
|
|
|
}
|
|
|
|
// form and textarea event listeners
|
|
|
|
this.addEventListeners();
|
|
|
|
gl.text.init(this.form);
|
|
|
|
// hide discard button
|
|
|
|
this.form.find('.js-note-discard').hide();
|
|
|
|
this.form.show();
|
|
|
|
if (this.isAutosizeable) this.setupAutosize();
|
|
|
|
};
|
|
|
|
|
|
|
|
GLForm.prototype.setupAutosize = function () {
|
|
|
|
this.textarea.off('autosize:resized')
|
|
|
|
.on('autosize:resized', this.setHeightData.bind(this));
|
|
|
|
|
|
|
|
this.textarea.off('mouseup.autosize')
|
|
|
|
.on('mouseup.autosize', this.destroyAutosize.bind(this));
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
autosize(this.textarea);
|
|
|
|
this.textarea.css('resize', 'vertical');
|
|
|
|
}, 0);
|
|
|
|
};
|
|
|
|
|
|
|
|
GLForm.prototype.setHeightData = function () {
|
|
|
|
this.textarea.data('height', this.textarea.outerHeight());
|
|
|
|
};
|
|
|
|
|
|
|
|
GLForm.prototype.destroyAutosize = function () {
|
|
|
|
const outerHeight = this.textarea.outerHeight();
|
|
|
|
|
|
|
|
if (this.textarea.data('height') === outerHeight) return;
|
|
|
|
|
|
|
|
autosize.destroy(this.textarea);
|
|
|
|
|
|
|
|
this.textarea.data('height', outerHeight);
|
|
|
|
this.textarea.outerHeight(outerHeight);
|
|
|
|
this.textarea.css('max-height', window.outerHeight);
|
|
|
|
};
|
|
|
|
|
|
|
|
GLForm.prototype.clearEventListeners = function() {
|
|
|
|
this.textarea.off('focus');
|
|
|
|
this.textarea.off('blur');
|
|
|
|
return gl.text.removeListeners(this.form);
|
|
|
|
};
|
|
|
|
|
|
|
|
GLForm.prototype.addEventListeners = function() {
|
|
|
|
this.textarea.on('focus', function() {
|
|
|
|
return $(this).closest('.md-area').addClass('is-focused');
|
|
|
|
});
|
|
|
|
return this.textarea.on('blur', function() {
|
|
|
|
return $(this).closest('.md-area').removeClass('is-focused');
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
window.gl.GLForm = GLForm;
|