2020-01-01 13:55:28 +05:30
|
|
|
import $ from 'jquery';
|
2020-11-24 15:15:51 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
import Api from '~/api';
|
2020-11-24 15:15:51 +05:30
|
|
|
import initPopover from '~/blob/suggest_gitlab_ci_yml';
|
2023-05-27 22:25:52 +05:30
|
|
|
import { createAlert } from '~/alert';
|
2021-03-11 19:13:27 +05:30
|
|
|
import { __ } from '~/locale';
|
|
|
|
import toast from '~/vue_shared/plugins/global_toast';
|
2018-05-09 12:01:36 +05:30
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
import BlobCiYamlSelector from './template_selectors/ci_yaml_selector';
|
2017-08-17 22:00:37 +05:30
|
|
|
import DockerfileSelector from './template_selectors/dockerfile_selector';
|
|
|
|
import GitignoreSelector from './template_selectors/gitignore_selector';
|
|
|
|
import LicenseSelector from './template_selectors/license_selector';
|
2020-10-24 23:57:45 +05:30
|
|
|
import MetricsDashboardSelector from './template_selectors/metrics_dashboard_selector';
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
export default class FileTemplateMediator {
|
2018-12-05 23:21:45 +05:30
|
|
|
constructor({ editor, currentAction, projectId }) {
|
2017-08-17 22:00:37 +05:30
|
|
|
this.editor = editor;
|
|
|
|
this.currentAction = currentAction;
|
2018-12-05 23:21:45 +05:30
|
|
|
this.projectId = projectId;
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
this.initTemplateSelectors();
|
|
|
|
this.initDomElements();
|
|
|
|
this.initDropdowns();
|
|
|
|
this.initPageEvents();
|
2019-12-21 20:55:43 +05:30
|
|
|
this.cacheFileContents();
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
initTemplateSelectors() {
|
|
|
|
// Order dictates template type dropdown item order
|
|
|
|
this.templateSelectors = [
|
|
|
|
GitignoreSelector,
|
|
|
|
BlobCiYamlSelector,
|
2020-10-24 23:57:45 +05:30
|
|
|
MetricsDashboardSelector,
|
2017-08-17 22:00:37 +05:30
|
|
|
DockerfileSelector,
|
|
|
|
LicenseSelector,
|
2021-03-08 18:12:59 +05:30
|
|
|
].map((TemplateSelectorClass) => new TemplateSelectorClass({ mediator: this }));
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
initDomElements() {
|
|
|
|
const $templatesMenu = $('.template-selectors-menu');
|
|
|
|
const $undoMenu = $templatesMenu.find('.template-selectors-undo-menu');
|
|
|
|
const $fileEditor = $('.file-editor');
|
|
|
|
|
|
|
|
this.$templatesMenu = $templatesMenu;
|
|
|
|
this.$undoMenu = $undoMenu;
|
|
|
|
this.$undoBtn = $undoMenu.find('button');
|
|
|
|
this.$templateSelectors = $templatesMenu.find('.template-selector-dropdowns-wrap');
|
|
|
|
this.$filenameInput = $fileEditor.find('.js-file-path-name-input');
|
|
|
|
this.$fileContent = $fileEditor.find('#file-content');
|
|
|
|
this.$commitForm = $fileEditor.find('form');
|
|
|
|
this.$navLinks = $fileEditor.find('.nav-links');
|
|
|
|
}
|
|
|
|
|
|
|
|
initDropdowns() {
|
2023-03-17 16:20:25 +05:30
|
|
|
if (this.currentAction !== 'create') {
|
2017-08-17 22:00:37 +05:30
|
|
|
this.hideTemplateSelectorMenu();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.displayMatchedTemplateSelector();
|
|
|
|
}
|
|
|
|
|
|
|
|
initPageEvents() {
|
|
|
|
this.listenForFilenameInput();
|
|
|
|
this.listenForPreviewMode();
|
|
|
|
}
|
|
|
|
|
|
|
|
listenForFilenameInput() {
|
|
|
|
this.$filenameInput.on('keyup blur', () => {
|
|
|
|
this.displayMatchedTemplateSelector();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
listenForPreviewMode() {
|
2021-03-08 18:12:59 +05:30
|
|
|
this.$navLinks.on('click', 'a', (e) => {
|
2017-08-17 22:00:37 +05:30
|
|
|
const urlPieces = e.target.href.split('#');
|
|
|
|
const hash = urlPieces[1];
|
|
|
|
if (hash === 'preview') {
|
|
|
|
this.hideTemplateSelectorMenu();
|
2023-03-17 16:20:25 +05:30
|
|
|
} else if (hash === 'editor' && this.templateSelectors.find((sel) => sel.dropdown !== null)) {
|
2017-08-17 22:00:37 +05:30
|
|
|
this.showTemplateSelectorMenu();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
selectTemplateFile(selector, query, data) {
|
2019-12-21 20:55:43 +05:30
|
|
|
const self = this;
|
2019-12-26 22:10:19 +05:30
|
|
|
const { name } = selector.config;
|
2020-04-08 14:13:33 +05:30
|
|
|
const suggestCommitChanges = document.querySelector('.js-suggest-gitlab-ci-yml-commit-changes');
|
2019-12-21 20:55:43 +05:30
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
selector.renderLoading();
|
2019-12-21 20:55:43 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
this.fetchFileTemplate(selector.config.type, query, data)
|
2021-03-08 18:12:59 +05:30
|
|
|
.then((file) => {
|
2017-08-17 22:00:37 +05:30
|
|
|
this.setEditorContent(file);
|
2019-12-26 22:10:19 +05:30
|
|
|
this.setFilename(name);
|
2017-08-17 22:00:37 +05:30
|
|
|
selector.renderLoaded();
|
2023-03-17 16:20:25 +05:30
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
toast(__(`${query} template applied`), {
|
|
|
|
action: {
|
|
|
|
text: __('Undo'),
|
|
|
|
onClick: (e, toastObj) => {
|
|
|
|
self.restoreFromCache();
|
2021-09-30 23:02:18 +05:30
|
|
|
toastObj.hide();
|
2019-12-21 20:55:43 +05:30
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2020-04-08 14:13:33 +05:30
|
|
|
|
|
|
|
if (suggestCommitChanges) {
|
|
|
|
initPopover(suggestCommitChanges);
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
})
|
2021-09-30 23:02:18 +05:30
|
|
|
.catch((err) =>
|
2022-11-25 23:54:43 +05:30
|
|
|
createAlert({
|
2021-09-30 23:02:18 +05:30
|
|
|
message: __(`An error occurred while fetching the template: ${err}`),
|
|
|
|
}),
|
|
|
|
);
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
displayMatchedTemplateSelector() {
|
|
|
|
const currentInput = this.getFilename();
|
2023-03-17 16:20:25 +05:30
|
|
|
const matchedSelector = this.templateSelectors.find((sel) =>
|
|
|
|
sel.config.pattern.test(currentInput),
|
|
|
|
);
|
|
|
|
const currentSelector = this.templateSelectors.find((sel) => !sel.isHidden());
|
|
|
|
|
|
|
|
if (matchedSelector) {
|
|
|
|
if (currentSelector) {
|
|
|
|
currentSelector.hide();
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
2023-03-17 16:20:25 +05:30
|
|
|
matchedSelector.show();
|
|
|
|
this.showTemplateSelectorMenu();
|
|
|
|
} else {
|
|
|
|
this.hideTemplateSelectorMenu();
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
fetchFileTemplate(type, query, data = {}) {
|
2021-03-08 18:12:59 +05:30
|
|
|
return new Promise((resolve) => {
|
|
|
|
const resolveFile = (file) => resolve(file);
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
Api.projectTemplate(this.projectId, type, query, data, resolveFile);
|
2017-08-17 22:00:37 +05:30
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
setEditorContent(file) {
|
|
|
|
if (!file && file !== '') return;
|
|
|
|
|
|
|
|
const newValue = file.content || file;
|
|
|
|
|
|
|
|
this.editor.setValue(newValue, 1);
|
|
|
|
|
|
|
|
this.editor.focus();
|
|
|
|
|
|
|
|
this.editor.navigateFileStart();
|
|
|
|
}
|
|
|
|
|
|
|
|
hideTemplateSelectorMenu() {
|
|
|
|
this.$templatesMenu.hide();
|
|
|
|
}
|
|
|
|
|
|
|
|
showTemplateSelectorMenu() {
|
|
|
|
this.$templatesMenu.show();
|
2023-03-17 16:20:25 +05:30
|
|
|
this.cacheToggleText();
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
cacheToggleText() {
|
|
|
|
this.cachedToggleText = this.getTemplateSelectorToggleText();
|
|
|
|
}
|
|
|
|
|
|
|
|
cacheFileContents() {
|
|
|
|
this.cachedContent = this.editor.getValue();
|
|
|
|
this.cachedFilename = this.getFilename();
|
|
|
|
}
|
|
|
|
|
|
|
|
restoreFromCache() {
|
|
|
|
this.setEditorContent(this.cachedContent);
|
|
|
|
this.setFilename(this.cachedFilename);
|
|
|
|
this.setTemplateSelectorToggleText();
|
|
|
|
}
|
|
|
|
|
|
|
|
getTemplateSelectorToggleText() {
|
|
|
|
return this.$templateSelectors
|
|
|
|
.find('.js-template-selector-wrap:visible .dropdown-toggle-text')
|
|
|
|
.text();
|
|
|
|
}
|
|
|
|
|
|
|
|
setTemplateSelectorToggleText() {
|
|
|
|
return this.$templateSelectors
|
|
|
|
.find('.js-template-selector-wrap:visible .dropdown-toggle-text')
|
|
|
|
.text(this.cachedToggleText);
|
|
|
|
}
|
|
|
|
|
|
|
|
getFilename() {
|
|
|
|
return this.$filenameInput.val();
|
|
|
|
}
|
|
|
|
|
|
|
|
setFilename(name) {
|
2021-11-18 22:05:49 +05:30
|
|
|
const input = this.$filenameInput.get(0);
|
|
|
|
if (name !== undefined && input.value !== name) {
|
|
|
|
input.value = name;
|
|
|
|
input.dispatchEvent(new Event('change'));
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
}
|