2018-10-09 11:25:53 -04:00
|
|
|
/* eslint-disable no-useless-return */
|
2016-12-13 22:01:05 -05:00
|
|
|
|
2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2017-12-06 04:29:59 -05:00
|
|
|
import Api from '../api';
|
2017-04-03 13:54:40 -04:00
|
|
|
import TemplateSelector from '../blob/template_selector';
|
2019-05-02 09:05:48 -04:00
|
|
|
import { __ } from '~/locale';
|
2016-06-24 15:43:46 -04:00
|
|
|
|
2017-12-06 04:29:59 -05:00
|
|
|
export default class IssuableTemplateSelector extends TemplateSelector {
|
|
|
|
constructor(...args) {
|
|
|
|
super(...args);
|
2018-02-20 17:20:48 -05:00
|
|
|
this.projectPath = this.dropdown.data('projectPath');
|
|
|
|
this.namespacePath = this.dropdown.data('namespacePath');
|
|
|
|
this.issuableType = this.$dropdownContainer.data('issuableType');
|
2017-12-06 04:29:59 -05:00
|
|
|
this.titleInput = $(`#${this.issuableType}_title`);
|
|
|
|
|
|
|
|
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', () => {
|
|
|
|
this.currentTemplate.content = '';
|
|
|
|
this.setInputValueToTemplateContent();
|
2019-05-02 09:05:48 -04:00
|
|
|
$('.dropdown-toggle-text', this.dropdown).text(__('Choose a template'));
|
2017-12-06 04:29:59 -05:00
|
|
|
});
|
|
|
|
}
|
2016-06-24 15:43:46 -04:00
|
|
|
|
2017-12-06 04:29:59 -05:00
|
|
|
requestFile(query) {
|
|
|
|
this.startLoadingSpinner();
|
2018-10-10 03:13:34 -04:00
|
|
|
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();
|
|
|
|
},
|
|
|
|
);
|
2017-12-06 04:29:59 -05:00
|
|
|
return;
|
|
|
|
}
|
2016-06-24 15:43:46 -04:00
|
|
|
|
2017-12-06 04:29:59 -05:00
|
|
|
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-06-24 15:43:46 -04:00
|
|
|
}
|
2017-12-06 04:29:59 -05:00
|
|
|
return;
|
2016-06-24 15:43:46 -04:00
|
|
|
}
|
2017-12-06 04:29:59 -05:00
|
|
|
}
|