2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2018-02-08 09:22:31 -05:00
|
|
|
import autosize from 'autosize';
|
2017-05-16 14:42:06 -04:00
|
|
|
import GfmAutoComplete from './gfm_auto_complete';
|
2017-10-19 03:45:52 -04:00
|
|
|
import dropzoneInput from './dropzone_input';
|
2018-03-20 13:02:41 -04:00
|
|
|
import { addMarkdownListeners, removeMarkdownListeners } from './lib/utils/text_markdown';
|
2017-05-16 14:42:06 -04:00
|
|
|
|
2017-10-10 04:09:01 -04:00
|
|
|
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-02-20 17:20:48 -05:00
|
|
|
this.form.data('glForm', this);
|
2017-06-29 11:28:12 -04:00
|
|
|
}
|
2017-01-03 07:04:33 -05:00
|
|
|
|
2017-10-10 04:09:01 -04:00
|
|
|
destroy() {
|
|
|
|
// Clean form listeners
|
|
|
|
this.clearEventListeners();
|
|
|
|
if (this.autoComplete) {
|
|
|
|
this.autoComplete.destroy();
|
|
|
|
}
|
2018-02-20 17:20:48 -05:00
|
|
|
this.form.data('glForm', null);
|
2017-03-11 02:30:44 -05:00
|
|
|
}
|
2017-01-03 07:04:33 -05:00
|
|
|
|
2017-10-10 04:09:01 -04:00
|
|
|
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,
|
|
|
|
});
|
2017-10-19 03:45:52 -04:00
|
|
|
dropzoneInput(this.form);
|
2017-10-10 04:09:01 -04:00
|
|
|
autosize(this.textarea);
|
|
|
|
}
|
|
|
|
// form and textarea event listeners
|
|
|
|
this.addEventListeners();
|
2018-03-20 13:02:41 -04:00
|
|
|
addMarkdownListeners(this.form);
|
2017-10-10 04:09:01 -04:00
|
|
|
// hide discard button
|
|
|
|
this.form.find('.js-note-discard').hide();
|
|
|
|
this.form.show();
|
|
|
|
if (this.isAutosizeable) this.setupAutosize();
|
|
|
|
}
|
2017-01-03 07:04:33 -05:00
|
|
|
|
2017-10-10 04:09:01 -04:00
|
|
|
setupAutosize() {
|
|
|
|
this.textarea.off('autosize:resized')
|
|
|
|
.on('autosize:resized', this.setHeightData.bind(this));
|
2017-01-03 07:04:33 -05:00
|
|
|
|
2017-10-10 04:09:01 -04:00
|
|
|
this.textarea.off('mouseup.autosize')
|
|
|
|
.on('mouseup.autosize', this.destroyAutosize.bind(this));
|
2017-01-03 07:04:33 -05:00
|
|
|
|
2017-10-10 04:09:01 -04:00
|
|
|
setTimeout(() => {
|
|
|
|
autosize(this.textarea);
|
|
|
|
this.textarea.css('resize', 'vertical');
|
|
|
|
}, 0);
|
|
|
|
}
|
2017-01-03 07:04:33 -05:00
|
|
|
|
2017-10-10 04:09:01 -04:00
|
|
|
setHeightData() {
|
|
|
|
this.textarea.data('height', this.textarea.outerHeight());
|
|
|
|
}
|
2017-01-03 07:04:33 -05:00
|
|
|
|
2017-10-10 04:09:01 -04:00
|
|
|
destroyAutosize() {
|
|
|
|
const outerHeight = this.textarea.outerHeight();
|
2017-01-03 07:04:33 -05:00
|
|
|
|
2017-10-10 04:09:01 -04:00
|
|
|
if (this.textarea.data('height') === outerHeight) return;
|
2017-01-03 07:04:33 -05:00
|
|
|
|
2017-10-10 04:09:01 -04:00
|
|
|
autosize.destroy(this.textarea);
|
2017-01-03 07:04:33 -05:00
|
|
|
|
2017-10-10 04:09:01 -04:00
|
|
|
this.textarea.data('height', outerHeight);
|
|
|
|
this.textarea.outerHeight(outerHeight);
|
|
|
|
this.textarea.css('max-height', window.outerHeight);
|
|
|
|
}
|
2017-01-03 07:04:33 -05:00
|
|
|
|
2017-10-10 04:09:01 -04:00
|
|
|
clearEventListeners() {
|
|
|
|
this.textarea.off('focus');
|
|
|
|
this.textarea.off('blur');
|
2018-03-20 13:02:41 -04:00
|
|
|
removeMarkdownListeners(this.form);
|
2017-10-10 04:09:01 -04:00
|
|
|
}
|
2017-01-03 07:04:33 -05:00
|
|
|
|
2017-10-10 04:09:01 -04:00
|
|
|
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');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|