2022-03-02 08:16:31 +05:30
import { escape } from 'lodash' ;
import { _ _ , sprintf } from '~/locale' ;
2021-12-11 22:18:48 +05:30
import eventHub from '~/projects/new/event_hub' ;
2018-05-09 12:01:36 +05:30
2021-12-11 22:18:48 +05:30
// Values are from lib/gitlab/visibility_level.rb
const visibilityLevel = {
private : 0 ,
internal : 10 ,
public : 20 ,
} ;
2018-03-17 18:26:18 +05:30
2021-12-11 22:18:48 +05:30
function setVisibilityOptions ( { name , visibility , showPath , editPath } ) {
2021-03-08 18:12:59 +05:30
document . querySelectorAll ( '.visibility-level-setting .form-check' ) . forEach ( ( option ) => {
2021-12-11 22:18:48 +05:30
// Don't change anything if the option is restricted by admin
if ( option . classList . contains ( 'restricted' ) ) {
return ;
}
2018-03-17 18:26:18 +05:30
const optionInput = option . querySelector ( 'input[type=radio]' ) ;
2021-12-11 22:18:48 +05:30
const optionValue = optionInput ? parseInt ( optionInput . value , 10 ) : 0 ;
2018-03-17 18:26:18 +05:30
2021-12-11 22:18:48 +05:30
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 ( ) : '' ;
2022-03-02 08:16:31 +05:30
reason . innerHTML = sprintf (
_ _ (
'This project cannot be %{visibilityLevel} because the visibility of %{openShowLink}%{name}%{closeShowLink} is %{visibility}. To make this project %{visibilityLevel}, you must first %{openEditLink}change the visibility%{closeEditLink} of the parent group.' ,
) ,
{
visibilityLevel : optionName ,
name : escape ( name ) ,
visibility ,
openShowLink : ` <a href=" ${ showPath } "> ` ,
closeShowLink : '</a>' ,
openEditLink : ` <a href=" ${ editPath } "> ` ,
closeEditLink : '</a>' ,
} ,
false ,
) ;
2018-03-17 18:26:18 +05:30
}
2021-12-11 22:18:48 +05:30
} else {
option . classList . remove ( 'disabled' ) ;
optionInput . disabled = false ;
2018-03-17 18:26:18 +05:30
}
} ) ;
}
2021-12-11 22:18:48 +05:30
function handleSelect2DropdownChange ( namespaceSelector ) {
if ( ! namespaceSelector || ! ( 'selectedIndex' in namespaceSelector ) ) {
return ;
}
const selectedNamespace = namespaceSelector . options [ namespaceSelector . selectedIndex ] ;
setVisibilityOptions ( selectedNamespace . dataset ) ;
}
2018-03-17 18:26:18 +05:30
export default function initProjectVisibilitySelector ( ) {
2021-12-11 22:18:48 +05:30
eventHub . $on ( 'update-visibility' , setVisibilityOptions ) ;
2018-03-17 18:26:18 +05:30
const namespaceSelector = document . querySelector ( 'select.js-select-namespace' ) ;
if ( namespaceSelector ) {
2022-07-23 23:45:48 +05:30
const el = document . querySelector ( '.select2.js-select-namespace' ) ;
el . addEventListener ( 'change' , ( ) => handleSelect2DropdownChange ( namespaceSelector ) ) ;
2021-12-11 22:18:48 +05:30
handleSelect2DropdownChange ( namespaceSelector ) ;
2018-03-17 18:26:18 +05:30
}
}