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

160 lines
5.6 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';
2021-09-30 23:02:18 +05:30
import createFlash 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';
2018-03-17 18:26:18 +05:30
import projectSelect from '../../project_select';
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');
const fieldName = $dropdown.data('fieldName');
const shouldVisit = Boolean($dropdown.data('visit'));
const $form = $dropdown.closest('form');
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(() =>
createFlash({
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,
2019-12-04 20:38:33 +05:30
renderRow(ref) {
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) {
link.className = 'is-active';
2018-03-17 18:26:18 +05:30
}
2019-12-21 20:55:43 +05:30
link.textContent = ref;
link.dataset.ref = ref;
if (ref.length > 0 && shouldVisit) {
link.href = mergeUrlParams({ [fieldName]: ref }, linkTarget);
}
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
e.preventDefault();
// Since this page does not reload when changing directories in a repo
// the rendered links do not have the path to the current directory.
// This updates the path based on the current url and then opens
// the the url with the updated path parameter.
if (shouldVisit) {
const selectedUrl = new URL(e.target.href);
const loc = window.location.href;
if (loc.includes('/-/')) {
2021-04-29 21:17:54 +05:30
// Since the current ref in renderRow is outdated on page changes
// (To be addressed in: https://gitlab.com/gitlab-org/gitlab/-/issues/327085)
// We are deciphering the current ref from the dropdown data instead
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
const targetPath = splitPathAfterRefPortion?.slice(1).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
}
},
});
});
}
}