2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2018-02-08 09:22:31 -05:00
|
|
|
import autosize from 'autosize';
|
2019-04-23 15:18:07 -04:00
|
|
|
import GfmAutoComplete, { defaultAutocompleteConfig } from 'ee_else_ce/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 {
|
2018-06-21 07:52:43 -04:00
|
|
|
constructor(form, enableGFM = {}) {
|
2017-10-10 04:09:01 -04:00
|
|
|
this.form = form;
|
|
|
|
this.textarea = this.form.find('textarea.js-gfm-input');
|
2019-04-23 15:18:07 -04:00
|
|
|
this.enableGFM = Object.assign({}, defaultAutocompleteConfig, enableGFM);
|
2018-06-28 02:41:56 -04:00
|
|
|
// Disable autocomplete for keywords which do not have dataSources available
|
|
|
|
const dataSources = (gl.GfmAutoComplete && gl.GfmAutoComplete.dataSources) || {};
|
|
|
|
Object.keys(this.enableGFM).forEach(item => {
|
|
|
|
if (item !== 'emojis') {
|
2019-06-03 18:51:02 -04:00
|
|
|
this.enableGFM[item] = Boolean(dataSources[item]);
|
2018-06-28 02:41:56 -04:00
|
|
|
}
|
|
|
|
});
|
2017-10-10 04:09:01 -04:00
|
|
|
// Before we start, we should clean up any previous data for this form
|
|
|
|
this.destroy();
|
2018-09-17 05:17:33 -04:00
|
|
|
// Set up the form
|
2017-10-10 04:09:01 -04:00
|
|
|
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
|
2018-10-24 15:17:03 -04:00
|
|
|
gl.utils.disableButtonIfEmptyField(
|
|
|
|
this.form.find('.js-note-text'),
|
|
|
|
this.form.find('.js-comment-button, .js-note-new-discussion'),
|
|
|
|
);
|
2017-10-10 04:09:01 -04:00
|
|
|
this.autoComplete = new GfmAutoComplete(gl.GfmAutoComplete && gl.GfmAutoComplete.dataSources);
|
2018-06-21 07:52:43 -04:00
|
|
|
this.autoComplete.setup(this.form.find('.js-gfm-input'), 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
|
|
|
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() {
|
2018-10-24 15:17:03 -04:00
|
|
|
this.textarea.off('autosize:resized').on('autosize:resized', this.setHeightData.bind(this));
|
2017-01-03 07:04:33 -05:00
|
|
|
|
2018-10-24 15:17:03 -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() {
|
2018-10-24 15:17:03 -04:00
|
|
|
$(this)
|
|
|
|
.closest('.md-area')
|
|
|
|
.addClass('is-focused');
|
2017-10-10 04:09:01 -04:00
|
|
|
});
|
|
|
|
this.textarea.on('blur', function blurTextArea() {
|
2018-10-24 15:17:03 -04:00
|
|
|
$(this)
|
|
|
|
.closest('.md-area')
|
|
|
|
.removeClass('is-focused');
|
2017-10-10 04:09:01 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|