debian-mirror-gitlab/app/assets/javascripts/projects/project_new.js

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

306 lines
10 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2021-11-18 22:05:49 +05:30
import { debounce } from 'lodash';
2022-07-16 23:28:13 +05:30
import DEFAULT_PROJECT_TEMPLATES from 'any_else_ce/projects/default_project_templates';
2022-04-04 11:22:00 +05:30
import { confirmAction } from '~/lib/utils/confirm_via_gl_modal/confirm_via_gl_modal';
import { DEFAULT_DEBOUNCE_AND_THROTTLE_MS } from '../lib/utils/constants';
2022-06-21 17:19:12 +05:30
import { ENTER_KEY } from '../lib/utils/keys';
2021-11-18 22:05:49 +05:30
import axios from '../lib/utils/axios_utils';
2020-11-24 15:15:51 +05:30
import {
convertToTitleCase,
humanize,
slugify,
convertUnicodeToAscii,
} from '../lib/utils/text_utility';
2018-03-27 19:54:05 +05:30
2017-09-10 17:25:29 +05:30
let hasUserDefinedProjectPath = false;
2020-03-13 15:44:24 +05:30
let hasUserDefinedProjectName = false;
2021-11-18 22:05:49 +05:30
const invalidInputClass = 'gl-field-error-outline';
2022-06-21 17:19:12 +05:30
const invalidDropdownClass = 'gl-inset-border-1-red-400!';
2021-11-18 22:05:49 +05:30
2022-04-04 11:22:00 +05:30
const cancelSource = axios.CancelToken.source();
const endpoint = `${gon.relative_url_root}/import/url/validate`;
let importCredentialsValidationPromise = null;
2021-11-18 22:05:49 +05:30
const validateImportCredentials = (url, user, password) => {
2022-04-04 11:22:00 +05:30
cancelSource.cancel();
importCredentialsValidationPromise = axios
.post(endpoint, { url, user, password }, { cancelToken: cancelSource.cancel() })
2021-11-18 22:05:49 +05:30
.then(({ data }) => data)
2022-04-04 11:22:00 +05:30
.catch((thrown) =>
axios.isCancel(thrown)
? {
cancelled: true,
}
: {
// intentionally reporting success in case of validation error
// we do not want to block users from trying import in case of validation exception
success: true,
},
);
return importCredentialsValidationPromise;
2021-11-18 22:05:49 +05:30
};
2020-03-13 15:44:24 +05:30
const onProjectNameChange = ($projectNameInput, $projectPathInput) => {
2020-11-24 15:15:51 +05:30
const slug = slugify(convertUnicodeToAscii($projectNameInput.val()));
2020-03-13 15:44:24 +05:30
$projectPathInput.val(slug);
};
const onProjectPathChange = ($projectNameInput, $projectPathInput, hasExistingProjectName) => {
const slug = $projectPathInput.val();
if (!hasExistingProjectName) {
$projectNameInput.val(convertToTitleCase(humanize(slug, '[-_]')));
}
};
2022-06-21 17:19:12 +05:30
const selectedNamespaceId = () => document.querySelector('[name="project[selected_namespace_id]"]');
const dropdownButton = () => document.querySelector('.js-group-namespace-dropdown > button');
const namespaceButton = () => document.querySelector('.js-group-namespace-button');
const namespaceError = () => document.querySelector('.js-group-namespace-error');
const validateGroupNamespaceDropdown = (e) => {
if (selectedNamespaceId() && !selectedNamespaceId().attributes.value) {
document.querySelector('input[data-qa-selector="project_name"]').reportValidity();
e.preventDefault();
dropdownButton().classList.add(invalidDropdownClass);
namespaceButton().classList.add(invalidDropdownClass);
namespaceError().classList.remove('gl-display-none');
} else {
dropdownButton().classList.remove(invalidDropdownClass);
namespaceButton().classList.remove(invalidDropdownClass);
namespaceError().classList.add('gl-display-none');
}
};
2020-03-13 15:44:24 +05:30
const setProjectNamePathHandlers = ($projectNameInput, $projectPathInput) => {
2021-12-11 22:18:48 +05:30
const specialRepo = document.querySelector('.js-user-readme-repo');
2021-02-22 17:27:13 +05:30
// eslint-disable-next-line @gitlab/no-global-event-off
2020-03-13 15:44:24 +05:30
$projectNameInput.off('keyup change').on('keyup change', () => {
onProjectNameChange($projectNameInput, $projectPathInput);
hasUserDefinedProjectName = $projectNameInput.val().trim().length > 0;
hasUserDefinedProjectPath = $projectPathInput.val().trim().length > 0;
});
2021-02-22 17:27:13 +05:30
// eslint-disable-next-line @gitlab/no-global-event-off
2020-03-13 15:44:24 +05:30
$projectPathInput.off('keyup change').on('keyup change', () => {
onProjectPathChange($projectNameInput, $projectPathInput, hasUserDefinedProjectName);
hasUserDefinedProjectPath = $projectPathInput.val().trim().length > 0;
2021-12-11 22:18:48 +05:30
specialRepo.classList.toggle(
'gl-display-none',
$projectPathInput.val() !== $projectPathInput.data('username'),
);
2020-03-13 15:44:24 +05:30
});
2022-06-21 17:19:12 +05:30
document.querySelector('.js-create-project-button').addEventListener('click', (e) => {
validateGroupNamespaceDropdown(e);
});
2020-03-13 15:44:24 +05:30
};
2017-09-10 17:25:29 +05:30
2021-03-08 18:12:59 +05:30
const deriveProjectPathFromUrl = ($projectImportUrl) => {
2020-03-13 15:44:24 +05:30
const $currentProjectName = $projectImportUrl
.parents('.toggle-import-form')
.find('#project_name');
2018-12-13 13:39:08 +05:30
const $currentProjectPath = $projectImportUrl
.parents('.toggle-import-form')
.find('#project_path');
2020-03-13 15:44:24 +05:30
2022-04-04 11:22:00 +05:30
if (hasUserDefinedProjectPath || $currentProjectPath.length === 0) {
2017-09-10 17:25:29 +05:30
return;
}
let importUrl = $projectImportUrl.val().trim();
if (importUrl.length === 0) {
return;
}
/*
\/?: remove trailing slash
(\.git\/?)?: remove trailing .git (with optional trailing slash)
(\?.*)?: remove query string
(#.*)?: remove fragment identifier
*/
importUrl = importUrl.replace(/\/?(\.git\/?)?(\?.*)?(#.*)?$/, '');
// extract everything after the last slash
const pathMatch = /\/([^/]+)$/.exec(importUrl);
if (pathMatch) {
2018-03-17 18:26:18 +05:30
$currentProjectPath.val(pathMatch[1]);
2020-03-13 15:44:24 +05:30
onProjectPathChange($currentProjectName, $currentProjectPath, false);
2017-09-10 17:25:29 +05:30
}
};
2021-11-11 11:23:49 +05:30
const bindHowToImport = () => {
2022-04-04 11:22:00 +05:30
const importLinks = document.querySelectorAll('.js-how-to-import-link');
importLinks.forEach((link) => {
const { modalTitle: title, modalMessage: modalHtmlMessage } = link.dataset;
link.addEventListener('click', (e) => {
e.preventDefault();
confirmAction('', {
modalHtmlMessage,
title,
hideCancel: true,
});
});
});
2021-11-11 11:23:49 +05:30
};
2017-09-10 17:25:29 +05:30
const bindEvents = () => {
const $newProjectForm = $('#new_project');
const $projectImportUrl = $('#project_import_url');
2021-11-18 22:05:49 +05:30
const $projectImportUrlUser = $('#project_import_url_user');
const $projectImportUrlPassword = $('#project_import_url_password');
const $projectImportUrlError = $('.js-import-url-error');
2022-04-04 11:22:00 +05:30
const $projectImportForm = $('form.js-project-import');
2018-11-20 20:47:30 +05:30
const $projectPath = $('.tab-pane.active #project_path');
2018-03-17 18:26:18 +05:30
const $useTemplateBtn = $('.template-button > input');
const $projectFieldsForm = $('.project-fields-form');
const $selectedTemplateText = $('.selected-template');
const $changeTemplateBtn = $('.change-template');
2018-11-18 11:00:15 +05:30
const $selectedIcon = $('.selected-icon');
const $projectTemplateButtons = $('.project-templates-buttons');
2018-11-20 20:47:30 +05:30
const $projectName = $('.tab-pane.active #project_name');
2017-09-10 17:25:29 +05:30
2022-04-04 11:22:00 +05:30
if ($newProjectForm.length !== 1 && $projectImportForm.length !== 1) {
2017-09-10 17:25:29 +05:30
return;
}
2021-11-11 11:23:49 +05:30
bindHowToImport();
2017-09-10 17:25:29 +05:30
2022-05-07 20:08:51 +05:30
$('.btn_import_gitlab_project').on('click contextmenu', () => {
const importHref = $('a.btn_import_gitlab_project').attr('data-href');
2018-12-13 13:39:08 +05:30
$('.btn_import_gitlab_project').attr(
'href',
`${importHref}?namespace_id=${$(
'#project_namespace_id',
).val()}&name=${$projectName.val()}&path=${$projectPath.val()}`,
);
2017-09-10 17:25:29 +05:30
});
2018-03-17 18:26:18 +05:30
function chooseTemplate() {
2018-11-18 11:00:15 +05:30
$projectTemplateButtons.addClass('hidden');
2018-03-17 18:26:18 +05:30
$projectFieldsForm.addClass('selected');
2018-11-18 11:00:15 +05:30
$selectedIcon.empty();
2022-06-21 17:19:12 +05:30
const $selectedTemplate = $(this);
$selectedTemplate.prop('checked', true);
const value = $selectedTemplate.val();
2020-04-22 19:07:51 +05:30
2021-02-22 17:27:13 +05:30
const selectedTemplate = DEFAULT_PROJECT_TEMPLATES[value];
2018-03-17 18:26:18 +05:30
$selectedTemplateText.text(selectedTemplate.text);
2021-03-08 18:12:59 +05:30
$(selectedTemplate.icon).clone().addClass('d-block').appendTo($selectedIcon);
2018-11-20 20:47:30 +05:30
const $activeTabProjectName = $('.tab-pane.active #project_name');
const $activeTabProjectPath = $('.tab-pane.active #project_path');
$activeTabProjectName.focus();
2020-03-13 15:44:24 +05:30
setProjectNamePathHandlers($activeTabProjectName, $activeTabProjectPath);
2018-03-17 18:26:18 +05:30
}
2022-06-21 17:19:12 +05:30
function toggleActiveClassOnLabel(event) {
const $label = $(event.target).parent();
$label.toggleClass('active');
}
function chooseTemplateOnEnter(event) {
if (event.code === ENTER_KEY) {
chooseTemplate.call(this);
}
}
$useTemplateBtn.on('click', chooseTemplate);
$useTemplateBtn.on('focus focusout', toggleActiveClassOnLabel);
$useTemplateBtn.on('keypress', chooseTemplateOnEnter);
2018-03-17 18:26:18 +05:30
$changeTemplateBtn.on('click', () => {
2018-11-18 11:00:15 +05:30
$projectTemplateButtons.removeClass('hidden');
2018-03-17 18:26:18 +05:30
$projectFieldsForm.removeClass('selected');
$useTemplateBtn.prop('checked', false);
});
2017-09-10 17:25:29 +05:30
$newProjectForm.on('submit', () => {
$projectPath.val($projectPath.val().trim());
});
2022-04-04 11:22:00 +05:30
const updateUrlPathWarningVisibility = async () => {
const { success: isUrlValid, cancelled } = await validateImportCredentials(
2021-11-18 22:05:49 +05:30
$projectImportUrl.val(),
$projectImportUrlUser.val(),
$projectImportUrlPassword.val(),
);
2022-04-04 11:22:00 +05:30
if (cancelled) {
return;
}
2021-11-18 22:05:49 +05:30
$projectImportUrl.toggleClass(invalidInputClass, !isUrlValid);
$projectImportUrlError.toggleClass('hide', isUrlValid);
2022-04-04 11:22:00 +05:30
};
const debouncedUpdateUrlPathWarningVisibility = debounce(
updateUrlPathWarningVisibility,
DEFAULT_DEBOUNCE_AND_THROTTLE_MS,
);
2021-09-30 23:02:18 +05:30
let isProjectImportUrlDirty = false;
$projectImportUrl.on('blur', () => {
isProjectImportUrlDirty = true;
2022-04-04 11:22:00 +05:30
debouncedUpdateUrlPathWarningVisibility();
2021-09-30 23:02:18 +05:30
});
$projectImportUrl.on('keyup', () => {
deriveProjectPathFromUrl($projectImportUrl);
2021-11-18 22:05:49 +05:30
});
[$projectImportUrl, $projectImportUrlUser, $projectImportUrlPassword].forEach(($f) => {
$f.on('input', () => {
if (isProjectImportUrlDirty) {
2022-04-04 11:22:00 +05:30
debouncedUpdateUrlPathWarningVisibility();
2021-11-18 22:05:49 +05:30
}
});
});
2022-04-04 11:22:00 +05:30
$projectImportForm.on('submit', async (e) => {
e.preventDefault();
if (importCredentialsValidationPromise === null) {
// we didn't validate credentials yet
debouncedUpdateUrlPathWarningVisibility.cancel();
updateUrlPathWarningVisibility();
}
const submitBtn = $projectImportForm.find('input[type="submit"]');
submitBtn.disable();
await importCredentialsValidationPromise;
submitBtn.enable();
2021-11-18 22:05:49 +05:30
const $invalidFields = $projectImportForm.find(`.${invalidInputClass}`);
if ($invalidFields.length > 0) {
$invalidFields[0].focus();
2022-04-04 11:22:00 +05:30
} else {
// calling .submit() on HTMLFormElement does not trigger 'submit' event
// We are using this behavior to bypass this handler and avoid infinite loop
$projectImportForm[0].submit();
2021-09-30 23:02:18 +05:30
}
});
2018-11-20 20:47:30 +05:30
2019-07-31 22:56:46 +05:30
$('.js-import-git-toggle-button').on('click', () => {
2020-03-13 15:44:24 +05:30
setProjectNamePathHandlers(
$('.tab-pane.active #project_name'),
$('.tab-pane.active #project_path'),
);
2019-07-31 22:56:46 +05:30
});
2020-03-13 15:44:24 +05:30
setProjectNamePathHandlers($projectName, $projectPath);
2017-09-10 17:25:29 +05:30
};
export default {
bindEvents,
deriveProjectPathFromUrl,
2018-11-20 20:47:30 +05:30
onProjectNameChange,
2020-03-13 15:44:24 +05:30
onProjectPathChange,
2017-09-10 17:25:29 +05:30
};
2021-11-11 11:23:49 +05:30
export { bindHowToImport };