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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

112 lines
3.3 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2022-01-26 12:08:38 +05:30
import TemplateSelector from '~/blob/template_selector';
2021-03-11 19:13:27 +05:30
import { __ } from '~/locale';
2018-03-17 18:26:18 +05:30
import Api from '../api';
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
2021-03-11 19:13:27 +05:30
this.projectId = this.dropdown.data('projectId');
2018-03-27 19:54:05 +05:30
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) {
2021-03-11 19:13:27 +05:30
const callback = (currentTemplate) => {
this.currentTemplate = currentTemplate;
this.stopLoadingSpinner();
this.setInputValueToTemplateContent();
};
2018-03-17 18:26:18 +05:30
this.startLoadingSpinner();
2019-12-21 20:55:43 +05:30
2021-03-11 19:13:27 +05:30
Api.projectTemplate(
this.projectId,
2018-12-13 13:39:08 +05:30
this.issuableType,
2021-03-11 19:13:27 +05:30
query.name,
{ source_template_project_id: query.project_id },
callback,
2018-12-13 13:39:08 +05:30
);
2018-03-17 18:26:18 +05:30
}
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
}
}
2018-03-17 18:26:18 +05:30
}