gitlab-org--gitlab-foss/app/assets/javascripts/projects/project_new.js

86 lines
2.5 KiB
JavaScript
Raw Normal View History

2017-08-01 18:06:06 +00:00
let hasUserDefinedProjectPath = false;
const deriveProjectPathFromUrl = ($projectImportUrl, $projectPath) => {
if ($projectImportUrl.attr('disabled') || 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) {
$projectPath.val(pathMatch[1]);
}
};
const bindEvents = () => {
const $newProjectForm = $('#new_project');
2017-07-25 08:41:43 +00:00
const importBtnTooltip = 'Please enter a valid project name.';
const $importBtnWrapper = $('.import_gitlab_project');
2017-08-01 18:06:06 +00:00
const $projectImportUrl = $('#project_import_url');
const $projectPath = $('#project_path');
if ($newProjectForm.length !== 1) {
return;
}
2017-07-25 08:41:43 +00:00
$('.how_to_import_link').on('click', (e) => {
2017-07-25 08:41:43 +00:00
e.preventDefault();
2017-07-25 13:02:24 +00:00
$('.how_to_import_link').next('.modal').show();
2017-07-25 08:41:43 +00:00
});
$('.modal-header .close').on('click', () => {
2017-07-25 08:41:43 +00:00
$('.modal').hide();
});
$('.btn_import_gitlab_project').on('click', () => {
2017-07-25 08:41:43 +00:00
const importHref = $('a.btn_import_gitlab_project').attr('href');
2017-08-01 18:06:06 +00:00
$('.btn_import_gitlab_project').attr('href', `${importHref}?namespace_id=${$('#project_namespace_id').val()}&path=${$projectPath.val()}`);
2017-07-25 08:41:43 +00:00
});
2017-08-01 18:06:06 +00:00
$('.btn_import_gitlab_project').attr('disabled', !$projectPath.val().trim().length);
2017-07-25 08:41:43 +00:00
$importBtnWrapper.attr('title', importBtnTooltip);
2017-08-01 18:06:06 +00:00
$newProjectForm.on('submit', () => {
$projectPath.val($projectPath.val().trim());
2017-07-25 08:41:43 +00:00
});
2017-08-01 18:06:06 +00:00
$projectPath.on('keyup', () => {
hasUserDefinedProjectPath = $projectPath.val().trim().length > 0;
if (hasUserDefinedProjectPath) {
2017-07-25 08:41:43 +00:00
$('.btn_import_gitlab_project').attr('disabled', false);
$importBtnWrapper.attr('title', '');
$importBtnWrapper.removeClass('has-tooltip');
} else {
$('.btn_import_gitlab_project').attr('disabled', true);
$importBtnWrapper.addClass('has-tooltip');
}
});
2017-08-01 18:06:06 +00:00
$projectImportUrl.disable();
$projectImportUrl.keyup(() => deriveProjectPathFromUrl($projectImportUrl, $projectPath));
$('.import_git').on('click', () => {
2017-07-25 08:41:43 +00:00
$projectImportUrl.attr('disabled', !$projectImportUrl.attr('disabled'));
});
2017-08-01 18:06:06 +00:00
};
document.addEventListener('DOMContentLoaded', bindEvents);
export default {
bindEvents,
deriveProjectPathFromUrl,
};