BigW Consortium Gitlab

project_new.js 1.76 KB
Newer Older
1 2 3
let hasUserDefinedProjectPath = false;

const deriveProjectPathFromUrl = ($projectImportUrl, $projectPath) => {
4
  if (hasUserDefinedProjectPath) {
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
    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');
  const $projectImportUrl = $('#project_import_url');
  const $projectPath = $('#project_path');

  if ($newProjectForm.length !== 1) {
    return;
  }
36

37
  $('.how_to_import_link').on('click', (e) => {
38
    e.preventDefault();
39
    $(e.currentTarget).next('.modal').show();
40 41
  });

42
  $('.modal-header .close').on('click', () => {
43 44 45
    $('.modal').hide();
  });

46
  $('.btn_import_gitlab_project').on('click', () => {
47
    const importHref = $('a.btn_import_gitlab_project').attr('href');
48
    $('.btn_import_gitlab_project').attr('href', `${importHref}?namespace_id=${$('#project_namespace_id').val()}&path=${$projectPath.val()}`);
49 50
  });

51 52
  $newProjectForm.on('submit', () => {
    $projectPath.val($projectPath.val().trim());
53 54
  });

55 56
  $projectPath.on('keyup', () => {
    hasUserDefinedProjectPath = $projectPath.val().trim().length > 0;
57 58
  });

59 60 61 62 63 64 65 66 67
  $projectImportUrl.keyup(() => deriveProjectPathFromUrl($projectImportUrl, $projectPath));
};

document.addEventListener('DOMContentLoaded', bindEvents);

export default {
  bindEvents,
  deriveProjectPathFromUrl,
};