debian-mirror-gitlab/app/assets/javascripts/pages/projects/project.js

181 lines
6.2 KiB
JavaScript
Raw Normal View History

2020-01-01 13:55:28 +05:30
/* eslint-disable func-names, no-return-assign */
2018-03-17 18:26:18 +05:30
2018-05-09 12:01:36 +05:30
import $ from 'jquery';
2022-04-04 11:22:00 +05:30
import { setCookie } from '~/lib/utils/common_utils';
2021-03-11 19:13:27 +05:30
import initClonePanel from '~/clone_panel';
import initDeprecatedJQueryDropdown from '~/deprecated_jquery_dropdown';
2022-11-25 23:54:43 +05:30
import { createAlert } from '~/flash';
2021-03-11 19:13:27 +05:30
import axios from '~/lib/utils/axios_utils';
import { serializeForm } from '~/lib/utils/forms';
import { mergeUrlParams } from '~/lib/utils/url_utility';
import { __ } from '~/locale';
2022-06-21 17:19:12 +05:30
import projectSelect from '~/project_select';
2018-03-17 18:26:18 +05:30
2023-03-04 22:38:38 +05:30
const BRANCH_REF_TYPE = 'heads';
const TAG_REF_TYPE = 'tags';
const BRANCH_GROUP_NAME = __('Branches');
const TAG_GROUP_NAME = __('Tags');
2018-03-17 18:26:18 +05:30
export default class Project {
constructor() {
2021-02-22 17:27:13 +05:30
initClonePanel();
2018-03-17 18:26:18 +05:30
// Ref switcher
2020-11-24 15:15:51 +05:30
if (document.querySelector('.js-project-refs-dropdown')) {
Project.initRefSwitcher();
2021-03-08 18:12:59 +05:30
$('.project-refs-select').on('change', function () {
return $(this).parents('form').trigger('submit');
2020-11-24 15:15:51 +05:30
});
}
2021-12-11 22:18:48 +05:30
$('.js-hide-no-ssh-message').on('click', function (e) {
2022-04-04 11:22:00 +05:30
setCookie('hide_no_ssh_message', 'false');
2021-12-11 22:18:48 +05:30
$(this).parents('.js-no-ssh-key-message').remove();
2018-03-17 18:26:18 +05:30
return e.preventDefault();
});
2021-12-11 22:18:48 +05:30
$('.js-hide-no-password-message').on('click', function (e) {
2022-04-04 11:22:00 +05:30
setCookie('hide_no_password_message', 'false');
2021-12-11 22:18:48 +05:30
$(this).parents('.js-no-password-message').remove();
2018-11-20 20:47:30 +05:30
return e.preventDefault();
});
2021-03-08 18:12:59 +05:30
$('.hide-auto-devops-implicitly-enabled-banner').on('click', function (e) {
2018-11-20 20:47:30 +05:30
const projectId = $(this).data('project-id');
const cookieKey = `hide_auto_devops_implicitly_enabled_banner_${projectId}`;
2022-04-04 11:22:00 +05:30
setCookie(cookieKey, 'false');
2021-03-08 18:12:59 +05:30
$(this).parents('.auto-devops-implicitly-enabled-banner').remove();
2018-03-17 18:26:18 +05:30
return e.preventDefault();
});
2020-11-24 15:15:51 +05:30
2018-03-17 18:26:18 +05:30
Project.projectSelectDropdown();
}
2018-03-27 19:54:05 +05:30
static projectSelectDropdown() {
2018-03-17 18:26:18 +05:30
projectSelect();
2021-03-08 18:12:59 +05:30
$('.project-item-select').on('click', (e) => Project.changeProject($(e.currentTarget).val()));
2018-03-17 18:26:18 +05:30
}
static changeProject(url) {
2018-11-20 20:47:30 +05:30
return (window.location = url);
2018-03-17 18:26:18 +05:30
}
static initRefSwitcher() {
2020-01-01 13:55:28 +05:30
const refListItem = document.createElement('li');
const refLink = document.createElement('a');
2018-03-17 18:26:18 +05:30
refLink.href = '#';
2021-03-08 18:12:59 +05:30
return $('.js-project-refs-dropdown').each(function () {
2020-01-01 13:55:28 +05:30
const $dropdown = $(this);
const selected = $dropdown.data('selected');
2023-03-04 22:38:38 +05:30
const refType = $dropdown.data('refType');
2020-01-01 13:55:28 +05:30
const fieldName = $dropdown.data('fieldName');
const shouldVisit = Boolean($dropdown.data('visit'));
const $form = $dropdown.closest('form');
2022-07-16 23:28:13 +05:30
const path = $form.find('#path').val();
2020-01-01 13:55:28 +05:30
const action = $form.attr('action');
const linkTarget = mergeUrlParams(serializeForm($form[0]), action);
2019-12-21 20:55:43 +05:30
2020-11-24 15:15:51 +05:30
return initDeprecatedJQueryDropdown($dropdown, {
2018-03-17 18:26:18 +05:30
data(term, callback) {
2018-11-20 20:47:30 +05:30
axios
.get($dropdown.data('refsUrl'), {
params: {
ref: $dropdown.data('ref'),
search: term,
},
})
.then(({ data }) => callback(data))
2021-09-30 23:02:18 +05:30
.catch(() =>
2022-11-25 23:54:43 +05:30
createAlert({
2021-09-30 23:02:18 +05:30
message: __('An error occurred while getting projects'),
}),
);
2018-03-17 18:26:18 +05:30
},
selectable: true,
filterable: true,
filterRemote: true,
filterByText: true,
2018-03-27 19:54:05 +05:30
inputFieldName: $dropdown.data('inputFieldName'),
2019-10-12 21:52:04 +05:30
fieldName,
2023-03-04 22:38:38 +05:30
renderRow(ref, _, params) {
2020-01-01 13:55:28 +05:30
const li = refListItem.cloneNode(false);
2018-03-17 18:26:18 +05:30
2020-01-01 13:55:28 +05:30
const link = refLink.cloneNode(false);
2018-03-17 18:26:18 +05:30
2019-12-21 20:55:43 +05:30
if (ref === selected) {
2023-03-04 22:38:38 +05:30
// Check group and current ref type to avoid adding a class when tags and branches share the same name
if (
(refType === BRANCH_REF_TYPE && params.group === BRANCH_GROUP_NAME) ||
(refType === TAG_REF_TYPE && params.group === TAG_GROUP_NAME) ||
!refType
) {
link.className = 'is-active';
}
2018-03-17 18:26:18 +05:30
}
2023-03-04 22:38:38 +05:30
2019-12-21 20:55:43 +05:30
link.textContent = ref;
link.dataset.ref = ref;
if (ref.length > 0 && shouldVisit) {
2023-03-04 22:38:38 +05:30
const urlParams = { [fieldName]: ref };
if (params.group === BRANCH_GROUP_NAME) {
urlParams.ref_type = BRANCH_REF_TYPE;
} else {
urlParams.ref_type = TAG_REF_TYPE;
}
link.href = mergeUrlParams(urlParams, linkTarget);
2019-12-21 20:55:43 +05:30
}
li.appendChild(link);
2018-03-17 18:26:18 +05:30
return li;
},
2019-12-04 20:38:33 +05:30
id(obj, $el) {
2018-03-17 18:26:18 +05:30
return $el.attr('data-ref');
},
2019-12-04 20:38:33 +05:30
toggleLabel(obj, $el) {
2018-03-17 18:26:18 +05:30
return $el.text().trim();
},
2019-12-04 20:38:33 +05:30
clicked(options) {
2018-03-17 18:26:18 +05:30
const { e } = options;
2021-01-29 00:20:46 +05:30
2022-07-16 23:28:13 +05:30
if (!shouldVisit) {
e.preventDefault();
}
// Some pages need to dynamically get the current path
// so they can opt-in to JS getting the path from the
// current URL by not setting a path in the dropdown form
if (shouldVisit && path === undefined) {
e.preventDefault();
2021-01-29 00:20:46 +05:30
const selectedUrl = new URL(e.target.href);
const loc = window.location.href;
if (loc.includes('/-/')) {
2021-04-29 21:17:54 +05:30
const currentRef = $dropdown.data('ref');
// The split and startWith is to ensure an exact word match
// and avoid partial match ie. currentRef is "dev" and loc is "development"
2021-09-04 01:27:46 +05:30
const splitPathAfterRefPortion = loc.split('/-/')[1].split(currentRef)[1];
2021-04-29 21:17:54 +05:30
const doesPathContainRef = splitPathAfterRefPortion?.startsWith('/');
if (doesPathContainRef) {
// We are ignoring the url containing the ref portion
// and plucking the thereafter portion to reconstructure the url that is correct
2022-10-11 01:57:18 +05:30
const targetPath = splitPathAfterRefPortion?.slice(1).split('#')[0].split('?')[0];
2021-01-29 00:20:46 +05:30
selectedUrl.searchParams.set('path', targetPath);
2021-03-11 19:13:27 +05:30
selectedUrl.hash = window.location.hash;
2021-01-29 00:20:46 +05:30
}
}
// Open in new window if "meta" key is pressed
if (e.metaKey) {
window.open(selectedUrl.href, '_blank');
} else {
window.location.href = selectedUrl.href;
}
2018-03-17 18:26:18 +05:30
}
},
});
});
}
}