2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2020-08-17 17:09:56 -04:00
|
|
|
import DEFAULT_PROJECT_TEMPLATES from 'ee_else_ce/projects/default_project_templates';
|
2020-08-25 05:52:44 -04:00
|
|
|
import {
|
|
|
|
convertToTitleCase,
|
|
|
|
humanize,
|
|
|
|
slugify,
|
|
|
|
convertUnicodeToAscii,
|
|
|
|
} from '../lib/utils/text_utility';
|
2018-02-23 04:00:19 -05:00
|
|
|
|
2018-01-16 11:20:59 -05:00
|
|
|
let hasUserDefinedProjectPath = false;
|
2020-01-17 16:08:29 -05:00
|
|
|
let hasUserDefinedProjectName = false;
|
|
|
|
|
|
|
|
const onProjectNameChange = ($projectNameInput, $projectPathInput) => {
|
2020-08-25 05:52:44 -04:00
|
|
|
const slug = slugify(convertUnicodeToAscii($projectNameInput.val()));
|
2020-01-17 16:08:29 -05:00
|
|
|
$projectPathInput.val(slug);
|
|
|
|
};
|
|
|
|
|
|
|
|
const onProjectPathChange = ($projectNameInput, $projectPathInput, hasExistingProjectName) => {
|
|
|
|
const slug = $projectPathInput.val();
|
|
|
|
|
|
|
|
if (!hasExistingProjectName) {
|
|
|
|
$projectNameInput.val(convertToTitleCase(humanize(slug, '[-_]')));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const setProjectNamePathHandlers = ($projectNameInput, $projectPathInput) => {
|
2020-12-08 16:10:06 -05:00
|
|
|
// eslint-disable-next-line @gitlab/no-global-event-off
|
2020-01-17 16:08:29 -05:00
|
|
|
$projectNameInput.off('keyup change').on('keyup change', () => {
|
|
|
|
onProjectNameChange($projectNameInput, $projectPathInput);
|
|
|
|
hasUserDefinedProjectName = $projectNameInput.val().trim().length > 0;
|
|
|
|
hasUserDefinedProjectPath = $projectPathInput.val().trim().length > 0;
|
|
|
|
});
|
|
|
|
|
2020-12-08 16:10:06 -05:00
|
|
|
// eslint-disable-next-line @gitlab/no-global-event-off
|
2020-01-17 16:08:29 -05:00
|
|
|
$projectPathInput.off('keyup change').on('keyup change', () => {
|
|
|
|
onProjectPathChange($projectNameInput, $projectPathInput, hasUserDefinedProjectName);
|
|
|
|
hasUserDefinedProjectPath = $projectPathInput.val().trim().length > 0;
|
|
|
|
});
|
|
|
|
};
|
2018-01-16 11:20:59 -05:00
|
|
|
|
2020-12-23 19:10:25 -05:00
|
|
|
const deriveProjectPathFromUrl = ($projectImportUrl) => {
|
2020-01-17 16:08:29 -05:00
|
|
|
const $currentProjectName = $projectImportUrl
|
|
|
|
.parents('.toggle-import-form')
|
|
|
|
.find('#project_name');
|
2018-10-30 16:28:31 -04:00
|
|
|
const $currentProjectPath = $projectImportUrl
|
|
|
|
.parents('.toggle-import-form')
|
|
|
|
.find('#project_path');
|
2020-01-17 16:08:29 -05:00
|
|
|
|
2018-01-16 11:20:59 -05:00
|
|
|
if (hasUserDefinedProjectPath) {
|
|
|
|
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) {
|
|
|
|
$currentProjectPath.val(pathMatch[1]);
|
2020-01-17 16:08:29 -05:00
|
|
|
onProjectPathChange($currentProjectName, $currentProjectPath, false);
|
2018-01-16 11:20:59 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const bindEvents = () => {
|
|
|
|
const $newProjectForm = $('#new_project');
|
|
|
|
const $projectImportUrl = $('#project_import_url');
|
2018-09-08 02:03:00 -04:00
|
|
|
const $projectPath = $('.tab-pane.active #project_path');
|
2018-01-16 11:20:59 -05:00
|
|
|
const $useTemplateBtn = $('.template-button > input');
|
|
|
|
const $projectFieldsForm = $('.project-fields-form');
|
|
|
|
const $selectedTemplateText = $('.selected-template');
|
|
|
|
const $changeTemplateBtn = $('.change-template');
|
2018-08-03 11:36:53 -04:00
|
|
|
const $selectedIcon = $('.selected-icon');
|
|
|
|
const $projectTemplateButtons = $('.project-templates-buttons');
|
2018-09-08 02:03:00 -04:00
|
|
|
const $projectName = $('.tab-pane.active #project_name');
|
2018-01-16 11:20:59 -05:00
|
|
|
|
|
|
|
if ($newProjectForm.length !== 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-23 19:10:25 -05:00
|
|
|
$('.how_to_import_link').on('click', (e) => {
|
2018-01-16 11:20:59 -05:00
|
|
|
e.preventDefault();
|
2020-12-23 07:10:26 -05:00
|
|
|
$(e.currentTarget).next('.modal').show();
|
2018-01-16 11:20:59 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
$('.modal-header .close').on('click', () => {
|
|
|
|
$('.modal').hide();
|
|
|
|
});
|
|
|
|
|
|
|
|
$('.btn_import_gitlab_project').on('click', () => {
|
|
|
|
const importHref = $('a.btn_import_gitlab_project').attr('href');
|
2018-10-30 16:28:31 -04:00
|
|
|
$('.btn_import_gitlab_project').attr(
|
|
|
|
'href',
|
|
|
|
`${importHref}?namespace_id=${$(
|
|
|
|
'#project_namespace_id',
|
|
|
|
).val()}&name=${$projectName.val()}&path=${$projectPath.val()}`,
|
|
|
|
);
|
2018-01-16 11:20:59 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
function chooseTemplate() {
|
2018-08-03 11:36:53 -04:00
|
|
|
$projectTemplateButtons.addClass('hidden');
|
2018-01-16 11:20:59 -05:00
|
|
|
$projectFieldsForm.addClass('selected');
|
2018-08-03 11:36:53 -04:00
|
|
|
$selectedIcon.empty();
|
2018-01-16 11:20:59 -05:00
|
|
|
const value = $(this).val();
|
2020-04-07 08:09:34 -04:00
|
|
|
|
2020-12-11 13:09:57 -05:00
|
|
|
const selectedTemplate = DEFAULT_PROJECT_TEMPLATES[value];
|
2018-01-16 11:20:59 -05:00
|
|
|
$selectedTemplateText.text(selectedTemplate.text);
|
2020-12-23 07:10:26 -05:00
|
|
|
$(selectedTemplate.icon).clone().addClass('d-block').appendTo($selectedIcon);
|
2018-09-08 02:03:00 -04:00
|
|
|
|
|
|
|
const $activeTabProjectName = $('.tab-pane.active #project_name');
|
|
|
|
const $activeTabProjectPath = $('.tab-pane.active #project_path');
|
|
|
|
$activeTabProjectName.focus();
|
2020-01-17 16:08:29 -05:00
|
|
|
setProjectNamePathHandlers($activeTabProjectName, $activeTabProjectPath);
|
2018-01-16 11:20:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
$useTemplateBtn.on('change', chooseTemplate);
|
|
|
|
|
|
|
|
$changeTemplateBtn.on('click', () => {
|
2018-08-03 11:36:53 -04:00
|
|
|
$projectTemplateButtons.removeClass('hidden');
|
2018-01-16 11:20:59 -05:00
|
|
|
$projectFieldsForm.removeClass('selected');
|
|
|
|
$useTemplateBtn.prop('checked', false);
|
|
|
|
});
|
|
|
|
|
|
|
|
$newProjectForm.on('submit', () => {
|
|
|
|
$projectPath.val($projectPath.val().trim());
|
|
|
|
});
|
|
|
|
|
|
|
|
$projectImportUrl.keyup(() => deriveProjectPathFromUrl($projectImportUrl));
|
2018-09-08 02:03:00 -04:00
|
|
|
|
2019-04-17 05:00:50 -04:00
|
|
|
$('.js-import-git-toggle-button').on('click', () => {
|
|
|
|
const $projectMirror = $('#project_mirror');
|
|
|
|
|
|
|
|
$projectMirror.attr('disabled', !$projectMirror.attr('disabled'));
|
2020-01-17 16:08:29 -05:00
|
|
|
setProjectNamePathHandlers(
|
|
|
|
$('.tab-pane.active #project_name'),
|
|
|
|
$('.tab-pane.active #project_path'),
|
|
|
|
);
|
2019-04-17 05:00:50 -04:00
|
|
|
});
|
|
|
|
|
2020-01-17 16:08:29 -05:00
|
|
|
setProjectNamePathHandlers($projectName, $projectPath);
|
2018-01-16 11:20:59 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
export default {
|
|
|
|
bindEvents,
|
|
|
|
deriveProjectPathFromUrl,
|
2018-09-08 02:03:00 -04:00
|
|
|
onProjectNameChange,
|
2020-01-17 16:08:29 -05:00
|
|
|
onProjectPathChange,
|
2018-01-16 11:20:59 -05:00
|
|
|
};
|