debian-mirror-gitlab/app/assets/javascripts/templates/issuable_template_selector.js

117 lines
3.5 KiB
JavaScript
Raw Normal View History

2018-12-13 13:39:08 +05:30
/* eslint-disable no-useless-return */
2017-08-17 22:00:37 +05:30
2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2018-03-17 18:26:18 +05:30
import Api from '../api';
2017-08-17 22:00:37 +05:30
import TemplateSelector from '../blob/template_selector';
2019-07-31 22:56:46 +05:30
import { __ } from '~/locale';
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
export default class IssuableTemplateSelector extends TemplateSelector {
constructor(...args) {
super(...args);
2019-12-21 20:55:43 +05:30
2018-03-27 19:54:05 +05:30
this.projectPath = this.dropdown.data('projectPath');
this.namespacePath = this.dropdown.data('namespacePath');
this.issuableType = this.$dropdownContainer.data('issuableType');
2018-03-17 18:26:18 +05:30
this.titleInput = $(`#${this.issuableType}_title`);
2019-12-21 20:55:43 +05:30
this.templateWarningEl = $('.js-issuable-template-warning');
this.warnTemplateOverride = args[0].warnTemplateOverride;
2018-03-17 18:26:18 +05:30
const initialQuery = {
name: this.dropdown.data('selected'),
};
if (initialQuery.name) this.requestFile(initialQuery);
$('.reset-template', this.dropdown.parent()).on('click', () => {
this.setInputValueToTemplateContent();
});
$('.no-template', this.dropdown.parent()).on('click', () => {
2019-12-21 20:55:43 +05:30
this.reset();
});
this.templateWarningEl.find('.js-close-btn').on('click', () => {
// Explicitly check against 0 value
if (this.previousSelectedIndex !== undefined) {
2020-11-24 15:15:51 +05:30
this.dropdown.data('deprecatedJQueryDropdown').selectRowAtIndex(this.previousSelectedIndex);
2019-12-21 20:55:43 +05:30
} else {
this.reset();
}
this.templateWarningEl.addClass('hidden');
2018-03-17 18:26:18 +05:30
});
2019-12-21 20:55:43 +05:30
this.templateWarningEl.find('.js-override-template').on('click', () => {
this.requestFile(this.overridingTemplate);
this.setSelectedIndex();
this.templateWarningEl.addClass('hidden');
this.overridingTemplate = null;
});
}
reset() {
if (this.currentTemplate) {
this.currentTemplate.content = '';
}
this.setInputValueToTemplateContent();
$('.dropdown-toggle-text', this.dropdown).text(__('Choose a template'));
this.previousSelectedIndex = null;
}
setSelectedIndex() {
2020-11-24 15:15:51 +05:30
this.previousSelectedIndex = this.dropdown.data('deprecatedJQueryDropdown').selectedIndex;
2019-12-21 20:55:43 +05:30
}
onDropdownClicked(query) {
const content = this.getEditorContent();
const isContentUnchanged =
content === '' || (this.currentTemplate && content === this.currentTemplate.content);
if (!this.warnTemplateOverride || isContentUnchanged) {
super.onDropdownClicked(query);
this.setSelectedIndex();
return;
}
this.overridingTemplate = query.selectedObj;
this.templateWarningEl.removeClass('hidden');
2018-03-17 18:26:18 +05:30
}
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
requestFile(query) {
this.startLoadingSpinner();
2019-12-21 20:55:43 +05:30
2018-12-13 13:39:08 +05:30
Api.issueTemplate(
this.namespacePath,
this.projectPath,
query.name,
this.issuableType,
(err, currentTemplate) => {
this.currentTemplate = currentTemplate;
this.stopLoadingSpinner();
if (err) return; // Error handled by global AJAX error handler
this.setInputValueToTemplateContent();
},
);
2018-03-17 18:26:18 +05:30
return;
}
2016-09-13 17:45:13 +05:30
2018-03-17 18:26:18 +05:30
setInputValueToTemplateContent() {
// `this.setEditorContent` sets the value of the description input field
// to the content of the template selected.
if (this.titleInput.val() === '') {
// If the title has not yet been set, focus the title input and
// skip focusing the description input by setting `true` as the
// `skipFocus` option to `setEditorContent`.
this.setEditorContent(this.currentTemplate, { skipFocus: true });
this.titleInput.focus();
} else {
this.setEditorContent(this.currentTemplate, { skipFocus: false });
2016-09-13 17:45:13 +05:30
}
2019-12-21 20:55:43 +05:30
2018-03-17 18:26:18 +05:30
return;
2016-09-13 17:45:13 +05:30
}
2018-03-17 18:26:18 +05:30
}