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

115 lines
3.5 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2018-03-27 19:54:05 +05:30
import autosize from 'autosize';
2019-07-31 22:56:46 +05:30
import GfmAutoComplete, { defaultAutocompleteConfig } from 'ee_else_ce/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';
2020-07-28 23:09:34 +05:30
import { disableButtonIfEmptyField } from '~/lib/utils/common_utils';
2018-03-17 18:26:18 +05:30
export default class GLForm {
2018-11-08 19:23:39 +05:30
constructor(form, enableGFM = {}) {
2018-03-17 18:26:18 +05:30
this.form = form;
this.textarea = this.form.find('textarea.js-gfm-input');
2020-05-24 23:13:21 +05:30
this.enableGFM = { ...defaultAutocompleteConfig, ...enableGFM };
2020-07-28 23:09:34 +05:30
2018-11-08 19:23:39 +05:30
// Disable autocomplete for keywords which do not have dataSources available
const dataSources = (gl.GfmAutoComplete && gl.GfmAutoComplete.dataSources) || {};
Object.keys(this.enableGFM).forEach(item => {
2020-07-28 23:09:34 +05:30
if (item !== 'emojis' && !dataSources[item]) {
this.enableGFM[item] = false;
2018-11-08 19:23:39 +05:30
}
});
2020-07-28 23:09:34 +05:30
2018-03-17 18:26:18 +05:30
// Before we start, we should clean up any previous data for this form
this.destroy();
2018-12-05 23:21:45 +05:30
// Set up the form
2018-03-17 18:26:18 +05:30
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();
}
2020-05-24 23:13:21 +05:30
if (this.formDropzone) {
this.formDropzone.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
2020-07-28 23:09:34 +05:30
disableButtonIfEmptyField(
2018-12-13 13:39:08 +05:30
this.form.find('.js-note-text'),
this.form.find('.js-comment-button, .js-note-new-discussion'),
);
2018-03-17 18:26:18 +05:30
this.autoComplete = new GfmAutoComplete(gl.GfmAutoComplete && gl.GfmAutoComplete.dataSources);
2018-11-08 19:23:39 +05:30
this.autoComplete.setup(this.form.find('.js-gfm-input'), this.enableGFM);
2020-05-24 23:13:21 +05:30
this.formDropzone = dropzoneInput(this.form, { parallelUploads: 1 });
2018-03-17 18:26:18 +05:30
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
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() {
2018-12-13 13:39:08 +05:30
this.textarea.off('autosize:resized').on('autosize:resized', this.setHeightData.bind(this));
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +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() {
2018-12-13 13:39:08 +05:30
$(this)
.closest('.md-area')
.addClass('is-focused');
2018-03-17 18:26:18 +05:30
});
this.textarea.on('blur', function blurTextArea() {
2018-12-13 13:39:08 +05:30
$(this)
.closest('.md-area')
.removeClass('is-focused');
2018-03-17 18:26:18 +05:30
});
}
2020-07-28 23:09:34 +05:30
get supportsQuickActions() {
return Boolean(this.textarea.data('supports-quick-actions'));
}
2018-03-17 18:26:18 +05:30
}