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

169 lines
5.2 KiB
JavaScript
Raw Normal View History

2019-12-04 20:38:33 +05:30
/* eslint-disable func-names, no-var, no-return-assign, vars-on-top */
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-10-12 21:52:04 +05:30
import { visitUrl, mergeUrlParams } from '~/lib/utils/url_utility';
import { serializeForm } from '~/lib/utils/forms';
2018-03-17 18:26:18 +05:30
import axios from '~/lib/utils/axios_utils';
import flash from '~/flash';
import projectSelect from '../../project_select';
export default class Project {
constructor() {
const $cloneOptions = $('ul.clone-options-dropdown');
const $projectCloneField = $('#project_clone');
2018-11-20 20:47:30 +05:30
const $cloneBtnLabel = $('.js-git-clone-holder .js-clone-dropdown-label');
2019-02-15 15:39:39 +05:30
const mobileCloneField = document.querySelector(
'.js-mobile-git-clone .js-clone-dropdown-label',
);
2018-03-17 18:26:18 +05:30
2018-11-20 20:47:30 +05:30
const selectedCloneOption = $cloneBtnLabel.text().trim();
2018-03-17 18:26:18 +05:30
if (selectedCloneOption.length > 0) {
$(`a:contains('${selectedCloneOption}')`, $cloneOptions).addClass('is-active');
}
2018-11-20 20:47:30 +05:30
$('a', $cloneOptions).on('click', e => {
e.preventDefault();
2018-03-17 18:26:18 +05:30
const $this = $(e.currentTarget);
const url = $this.attr('href');
2018-11-20 20:47:30 +05:30
const cloneType = $this.data('cloneType');
2018-03-17 18:26:18 +05:30
2018-11-20 20:47:30 +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');
2018-03-17 18:26:18 +05:30
2018-11-20 20:47:30 +05:30
$el.toggleClass('is-active');
$label.text(activeText);
});
2018-03-17 18:26:18 +05:30
2019-09-04 21:01:54 +05:30
$('#modal-geo-info').data({
cloneUrlSecondary: $this.attr('href'),
cloneUrlPrimary: $this.data('primaryUrl') || '',
});
2019-02-15 15:39:39 +05:30
if (mobileCloneField) {
mobileCloneField.dataset.clipboardText = url;
} else {
$projectCloneField.val(url);
}
2018-11-20 20:47:30 +05:30
$('.js-git-empty .js-clone').text(url);
2018-03-17 18:26:18 +05:30
});
// Ref switcher
Project.initRefSwitcher();
$('.project-refs-select').on('change', function() {
2018-11-20 20:47:30 +05:30
return $(this)
.parents('form')
.submit();
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();
});
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() {
var refListItem = document.createElement('li');
var refLink = document.createElement('a');
refLink.href = '#';
return $('.js-project-refs-dropdown').each(function() {
2019-10-12 21:52:04 +05:30
var $dropdown = $(this);
var selected = $dropdown.data('selected');
var fieldName = $dropdown.data('fieldName');
var shouldVisit = Boolean($dropdown.data('visit'));
2018-03-17 18:26:18 +05:30
return $dropdown.glDropdown({
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) {
2018-03-17 18:26:18 +05:30
var li = refListItem.cloneNode(false);
if (ref.header != null) {
li.className = 'dropdown-header';
li.textContent = ref.header;
} else {
var link = refLink.cloneNode(false);
if (ref === selected) {
link.className = 'is-active';
}
link.textContent = ref;
link.dataset.ref = ref;
li.appendChild(link);
}
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;
e.preventDefault();
2019-10-12 21:52:04 +05:30
if ($(`input[name="${fieldName}"]`).length) {
2018-03-17 18:26:18 +05:30
var $form = $dropdown.closest('form');
var action = $form.attr('action');
2019-10-12 21:52:04 +05:30
2018-03-17 18:26:18 +05:30
if (shouldVisit) {
2019-10-12 21:52:04 +05:30
visitUrl(mergeUrlParams(serializeForm($form[0]), action));
2018-03-17 18:26:18 +05:30
}
}
},
});
});
}
}