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

177 lines
5.6 KiB
JavaScript
Raw Normal View History

2018-12-13 13:39:08 +05:30
/* eslint-disable func-names, no-var, no-return-assign, one-var, object-shorthand, 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';
import { visitUrl } from '~/lib/utils/url_utility';
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();
});
2019-09-04 21:01:54 +05:30
$('.hide-shared-runner-limit-message').on('click', function(e) {
var $alert = $(this).parents('.shared-runner-quota-message');
var scope = $alert.data('scope');
Cookies.set('hide_shared_runner_quota_message', 'false', { path: scope });
$alert.remove();
e.preventDefault();
});
2018-11-20 20:47:30 +05:30
$('.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() {
var $dropdown, selected;
$dropdown = $(this);
selected = $dropdown.data('selected');
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'),
fieldName: $dropdown.data('fieldName'),
2018-03-17 18:26:18 +05:30
renderRow: function(ref) {
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;
},
id: function(obj, $el) {
return $el.attr('data-ref');
},
toggleLabel: function(obj, $el) {
return $el.text().trim();
},
clicked: function(options) {
const { e } = options;
e.preventDefault();
if ($('input[name="ref"]').length) {
var $form = $dropdown.closest('form');
var $visit = $dropdown.data('visit');
var shouldVisit = $visit ? true : $visit;
var action = $form.attr('action');
var divider = action.indexOf('?') === -1 ? '?' : '&';
if (shouldVisit) {
visitUrl(`${action}${divider}${$form.serialize()}`);
}
}
},
});
});
}
}