2018-03-09 15:18:59 -05:00
|
|
|
import $ from 'jquery';
|
2021-10-19 08:12:07 -04:00
|
|
|
import eventHub from '~/projects/new/event_hub';
|
2018-03-09 15:18:59 -05:00
|
|
|
|
2021-10-19 08:12:07 -04:00
|
|
|
// Values are from lib/gitlab/visibility_level.rb
|
|
|
|
const visibilityLevel = {
|
|
|
|
private: 0,
|
|
|
|
internal: 10,
|
|
|
|
public: 20,
|
|
|
|
};
|
2017-08-25 20:43:33 -04:00
|
|
|
|
2021-10-19 08:12:07 -04:00
|
|
|
function setVisibilityOptions({ name, visibility, showPath, editPath }) {
|
2020-12-23 19:10:25 -05:00
|
|
|
document.querySelectorAll('.visibility-level-setting .form-check').forEach((option) => {
|
2021-10-19 08:12:07 -04:00
|
|
|
// Don't change anything if the option is restricted by admin
|
|
|
|
if (option.classList.contains('restricted')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-25 20:43:33 -04:00
|
|
|
const optionInput = option.querySelector('input[type=radio]');
|
2021-10-19 08:12:07 -04:00
|
|
|
const optionValue = optionInput ? parseInt(optionInput.value, 10) : 0;
|
2017-08-25 20:43:33 -04:00
|
|
|
|
2021-10-19 08:12:07 -04:00
|
|
|
if (visibilityLevel[visibility] < optionValue) {
|
|
|
|
option.classList.add('disabled');
|
|
|
|
optionInput.disabled = true;
|
|
|
|
const reason = option.querySelector('.option-disabled-reason');
|
|
|
|
if (reason) {
|
|
|
|
const optionTitle = option.querySelector('.option-title');
|
|
|
|
const optionName = optionTitle ? optionTitle.innerText.toLowerCase() : '';
|
|
|
|
reason.innerHTML = `This project cannot be ${optionName} because the visibility of
|
2017-08-26 03:43:45 -04:00
|
|
|
<a href="${showPath}">${name}</a> is ${visibility}. To make this project
|
|
|
|
${optionName}, you must first <a href="${editPath}">change the visibility</a>
|
|
|
|
of the parent group.`;
|
2017-08-25 20:43:33 -04:00
|
|
|
}
|
2021-10-19 08:12:07 -04:00
|
|
|
} else {
|
|
|
|
option.classList.remove('disabled');
|
|
|
|
optionInput.disabled = false;
|
2017-08-25 20:43:33 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-10-19 08:12:07 -04:00
|
|
|
function handleSelect2DropdownChange(namespaceSelector) {
|
|
|
|
if (!namespaceSelector || !('selectedIndex' in namespaceSelector)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const selectedNamespace = namespaceSelector.options[namespaceSelector.selectedIndex];
|
|
|
|
setVisibilityOptions(selectedNamespace.dataset);
|
|
|
|
}
|
|
|
|
|
2017-08-25 20:43:33 -04:00
|
|
|
export default function initProjectVisibilitySelector() {
|
2021-10-19 08:12:07 -04:00
|
|
|
eventHub.$on('update-visibility', setVisibilityOptions);
|
|
|
|
|
2017-08-25 20:43:33 -04:00
|
|
|
const namespaceSelector = document.querySelector('select.js-select-namespace');
|
|
|
|
if (namespaceSelector) {
|
2021-10-19 08:12:07 -04:00
|
|
|
$('.select2.js-select-namespace').on('change', () =>
|
|
|
|
handleSelect2DropdownChange(namespaceSelector),
|
|
|
|
);
|
|
|
|
handleSelect2DropdownChange(namespaceSelector);
|
2017-08-25 20:43:33 -04:00
|
|
|
}
|
|
|
|
}
|