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

190 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';
2018-03-17 18:26:18 +05:30
import Cookies from 'js-cookie';
import { __ } from '~/locale';
2019-12-21 20:55:43 +05:30
import { mergeUrlParams } from '~/lib/utils/url_utility';
2019-10-12 21:52:04 +05:30
import { serializeForm } from '~/lib/utils/forms';
2018-03-17 18:26:18 +05:30
import axios from '~/lib/utils/axios_utils';
2020-10-24 23:57:45 +05:30
import { deprecatedCreateFlash as flash } from '~/flash';
2018-03-17 18:26:18 +05:30
import projectSelect from '../../project_select';
2020-11-24 15:15:51 +05:30
import initDeprecatedJQueryDropdown from '~/deprecated_jquery_dropdown';
2018-03-17 18:26:18 +05:30
export default class Project {
constructor() {
const $cloneOptions = $('ul.clone-options-dropdown');
2020-11-24 15:15:51 +05:30
if ($cloneOptions.length) {
const $projectCloneField = $('#project_clone');
const $cloneBtnLabel = $('.js-git-clone-holder .js-clone-dropdown-label');
const mobileCloneField = document.querySelector(
'.js-mobile-git-clone .js-clone-dropdown-label',
);
const selectedCloneOption = $cloneBtnLabel.text().trim();
if (selectedCloneOption.length > 0) {
$(`a:contains('${selectedCloneOption}')`, $cloneOptions).addClass('is-active');
}
$('a', $cloneOptions).on('click', e => {
e.preventDefault();
const $this = $(e.currentTarget);
const url = $this.attr('href');
const cloneType = $this.data('cloneType');
2018-03-17 18:26:18 +05:30
2020-11-24 15:15:51 +05:30
$('.is-active', $cloneOptions).removeClass('is-active');
$(`a[data-clone-type="${cloneType}"]`).each(function() {
const $el = $(this);
const activeText = $el.find('.dropdown-menu-inner-title').text();
const $container = $el.closest('.project-clone-holder');
const $label = $container.find('.js-clone-dropdown-label');
$el.toggleClass('is-active');
$label.text(activeText);
});
if (mobileCloneField) {
mobileCloneField.dataset.clipboardText = url;
} else {
$projectCloneField.val(url);
}
$('.js-git-empty .js-clone').text(url);
2018-11-20 20:47:30 +05:30
});
2020-11-24 15:15:51 +05:30
}
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();
$('.project-refs-select').on('change', function() {
return $(this)
.parents('form')
2021-01-29 00:20:46 +05:30
.trigger('submit');
2020-11-24 15:15:51 +05:30
});
}
2018-03-17 18:26:18 +05:30
$('.hide-no-ssh-message').on('click', function(e) {
Cookies.set('hide_no_ssh_message', 'false');
2018-11-20 20:47:30 +05:30
$(this)
.parents('.no-ssh-key-message')
.remove();
2018-03-17 18:26:18 +05:30
return e.preventDefault();
});
$('.hide-no-password-message').on('click', function(e) {
Cookies.set('hide_no_password_message', 'false');
2018-11-20 20:47:30 +05:30
$(this)
.parents('.no-password-message')
.remove();
return e.preventDefault();
});
$('.hide-auto-devops-implicitly-enabled-banner').on('click', function(e) {
const projectId = $(this).data('project-id');
const cookieKey = `hide_auto_devops_implicitly_enabled_banner_${projectId}`;
Cookies.set(cookieKey, 'false');
2018-12-13 13:39:08 +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();
$('.project-item-select').on('click', e => Project.changeProject($(e.currentTarget).val()));
}
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 = '#';
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))
.catch(() => flash(__('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('/-/')) {
const refs = this.fullData.Branches.concat(this.fullData.Tags);
const currentRef = refs.find(ref => loc.indexOf(ref) > -1);
if (currentRef) {
const targetPath = loc.split(currentRef)[1].slice(1);
selectedUrl.searchParams.set('path', targetPath);
}
}
// 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
}
},
});
});
}
}