2018-05-09 12:01:36 +05:30
|
|
|
import $ from 'jquery';
|
2018-03-27 19:54:05 +05:30
|
|
|
import autosize from 'autosize';
|
2017-09-10 17:25:29 +05:30
|
|
|
import GfmAutoComplete from './gfm_auto_complete';
|
2018-03-17 18:26:18 +05:30
|
|
|
import dropzoneInput from './dropzone_input';
|
2018-05-09 12:01:36 +05:30
|
|
|
import { addMarkdownListeners, removeMarkdownListeners } from './lib/utils/text_markdown';
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
export default class GLForm {
|
|
|
|
constructor(form, enableGFM = false) {
|
|
|
|
this.form = form;
|
|
|
|
this.textarea = this.form.find('textarea.js-gfm-input');
|
|
|
|
this.enableGFM = enableGFM;
|
|
|
|
// Before we start, we should clean up any previous data for this form
|
|
|
|
this.destroy();
|
|
|
|
// Setup the form
|
|
|
|
this.setupForm();
|
2018-03-27 19:54:05 +05:30
|
|
|
this.form.data('glForm', this);
|
2017-09-10 17:25:29 +05:30
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
destroy() {
|
|
|
|
// Clean form listeners
|
|
|
|
this.clearEventListeners();
|
|
|
|
if (this.autoComplete) {
|
|
|
|
this.autoComplete.destroy();
|
|
|
|
}
|
2018-03-27 19:54:05 +05:30
|
|
|
this.form.data('glForm', null);
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
setupForm() {
|
|
|
|
const 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'));
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
dropzoneInput(this.form);
|
|
|
|
autosize(this.textarea);
|
|
|
|
}
|
|
|
|
// form and textarea event listeners
|
|
|
|
this.addEventListeners();
|
2018-05-09 12:01:36 +05:30
|
|
|
addMarkdownListeners(this.form);
|
2018-03-17 18:26:18 +05:30
|
|
|
// hide discard button
|
|
|
|
this.form.find('.js-note-discard').hide();
|
|
|
|
this.form.show();
|
|
|
|
if (this.isAutosizeable) this.setupAutosize();
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
setupAutosize() {
|
|
|
|
this.textarea.off('autosize:resized')
|
|
|
|
.on('autosize:resized', this.setHeightData.bind(this));
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
this.textarea.off('mouseup.autosize')
|
|
|
|
.on('mouseup.autosize', this.destroyAutosize.bind(this));
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
setTimeout(() => {
|
|
|
|
autosize(this.textarea);
|
|
|
|
this.textarea.css('resize', 'vertical');
|
|
|
|
}, 0);
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
setHeightData() {
|
|
|
|
this.textarea.data('height', this.textarea.outerHeight());
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
destroyAutosize() {
|
|
|
|
const outerHeight = this.textarea.outerHeight();
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
if (this.textarea.data('height') === outerHeight) return;
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
autosize.destroy(this.textarea);
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
this.textarea.data('height', outerHeight);
|
|
|
|
this.textarea.outerHeight(outerHeight);
|
|
|
|
this.textarea.css('max-height', window.outerHeight);
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
clearEventListeners() {
|
|
|
|
this.textarea.off('focus');
|
|
|
|
this.textarea.off('blur');
|
2018-05-09 12:01:36 +05:30
|
|
|
removeMarkdownListeners(this.form);
|
2018-03-17 18:26:18 +05:30
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
addEventListeners() {
|
|
|
|
this.textarea.on('focus', function focusTextArea() {
|
|
|
|
$(this).closest('.md-area').addClass('is-focused');
|
|
|
|
});
|
|
|
|
this.textarea.on('blur', function blurTextArea() {
|
|
|
|
$(this).closest('.md-area').removeClass('is-focused');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|