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

144 lines
4.7 KiB
JavaScript
Raw Normal View History

2017-08-17 22:00:37 +05:30
/* eslint-disable func-names, space-before-function-paren, wrap-iife, no-var, quotes, consistent-return, no-new, prefer-arrow-callback, no-return-assign, one-var, one-var-declaration-per-line, object-shorthand, comma-dangle, no-else-return, newline-per-chained-call, no-shadow, vars-on-top, prefer-template, max-len */
/* global ProjectSelect */
import Cookies from 'js-cookie';
2016-09-13 17:45:13 +05:30
(function() {
this.Project = (function() {
function Project() {
2017-09-10 17:25:29 +05:30
const $cloneOptions = $('ul.clone-options-dropdown');
const $projectCloneField = $('#project_clone');
const $cloneBtnText = $('a.clone-dropdown-btn span');
const selectedCloneOption = $cloneBtnText.text().trim();
if (selectedCloneOption.length > 0) {
$(`a:contains('${selectedCloneOption}')`, $cloneOptions).addClass('is-active');
}
$('a', $cloneOptions).on('click', (e) => {
const $this = $(e.currentTarget);
const url = $this.attr('href');
e.preventDefault();
$('.is-active', $cloneOptions).not($this).removeClass('is-active');
$this.toggleClass('is-active');
$projectCloneField.val(url);
$cloneBtnText.text($this.text());
2016-09-13 17:45:13 +05:30
return $('.clone').text(url);
});
2016-09-29 09:46:39 +05:30
// Ref switcher
2016-09-13 17:45:13 +05:30
this.initRefSwitcher();
$('.project-refs-select').on('change', function() {
return $(this).parents('form').submit();
});
$('.hide-no-ssh-message').on('click', function(e) {
2017-08-17 22:00:37 +05:30
Cookies.set('hide_no_ssh_message', 'false');
2016-09-13 17:45:13 +05:30
$(this).parents('.no-ssh-key-message').remove();
return e.preventDefault();
});
$('.hide-no-password-message').on('click', function(e) {
2017-08-17 22:00:37 +05:30
Cookies.set('hide_no_password_message', 'false');
2016-09-13 17:45:13 +05:30
$(this).parents('.no-password-message').remove();
return e.preventDefault();
});
this.projectSelectDropdown();
}
Project.prototype.projectSelectDropdown = function() {
new ProjectSelect();
$('.project-item-select').on('click', (function(_this) {
return function(e) {
return _this.changeProject($(e.currentTarget).val());
};
})(this));
return $('.js-projects-dropdown-toggle').on('click', function(e) {
e.preventDefault();
return $('.js-projects-dropdown').select2('open');
});
};
Project.prototype.changeProject = function(url) {
return window.location = url;
};
Project.prototype.initRefSwitcher = function() {
2017-08-17 22:00:37 +05:30
var refListItem = document.createElement('li');
var refLink = document.createElement('a');
refLink.href = '#';
2016-09-13 17:45:13 +05:30
return $('.js-project-refs-dropdown').each(function() {
var $dropdown, selected;
$dropdown = $(this);
selected = $dropdown.data('selected');
return $dropdown.glDropdown({
data: function(term, callback) {
return $.ajax({
url: $dropdown.data('refs-url'),
data: {
2017-08-17 22:00:37 +05:30
ref: $dropdown.data('ref'),
search: term
2016-09-29 09:46:39 +05:30
},
dataType: "json"
2016-09-13 17:45:13 +05:30
}).done(function(refs) {
return callback(refs);
});
},
selectable: true,
filterable: true,
2017-08-17 22:00:37 +05:30
filterRemote: true,
2016-09-13 17:45:13 +05:30
filterByText: true,
2017-09-10 17:25:29 +05:30
inputFieldName: $dropdown.data('input-field-name'),
2016-09-29 09:46:39 +05:30
fieldName: $dropdown.data('field-name'),
2016-09-13 17:45:13 +05:30
renderRow: function(ref) {
2017-08-17 22:00:37 +05:30
var li = refListItem.cloneNode(false);
2016-09-13 17:45:13 +05:30
if (ref.header != null) {
2017-08-17 22:00:37 +05:30
li.className = 'dropdown-header';
li.textContent = ref.header;
2016-09-13 17:45:13 +05:30
} else {
2017-08-17 22:00:37 +05:30
var link = refLink.cloneNode(false);
if (ref === selected) {
link.className = 'is-active';
}
link.textContent = ref;
link.dataset.ref = ref;
li.appendChild(link);
2016-09-13 17:45:13 +05:30
}
2017-08-17 22:00:37 +05:30
return li;
2016-09-13 17:45:13 +05:30
},
id: function(obj, $el) {
return $el.attr('data-ref');
},
toggleLabel: function(obj, $el) {
return $el.text().trim();
},
2017-08-17 22:00:37 +05:30
clicked: function(options) {
const { e } = options;
e.preventDefault();
2016-09-13 17:45:13 +05:30
if ($('input[name="ref"]').length) {
2017-08-17 22:00:37 +05:30
var $form = $dropdown.closest('form');
2017-09-10 17:25:29 +05:30
var $visit = $dropdown.data('visit');
var shouldVisit = $visit ? true : $visit;
2017-08-17 22:00:37 +05:30
var action = $form.attr('action');
var divider = action.indexOf('?') === -1 ? '?' : '&';
2017-09-10 17:25:29 +05:30
if (shouldVisit) {
gl.utils.visitUrl(`${action}${divider}${$form.serialize()}`);
}
2016-09-13 17:45:13 +05:30
}
}
});
});
};
return Project;
})();
2017-08-17 22:00:37 +05:30
}).call(window);