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' ;
2022-10-11 01:57:18 +05:30
import { VISIBILITY _LEVELS _STRING _TO _INTEGER } from '~/visibility_level/constants' ;
2018-03-17 18:26:18 +05:30
2021-12-11 22:18:48 +05:30
function setVisibilityOptions ( { name , visibility , showPath , editPath } ) {
2022-08-13 15:12:31 +05:30
document . querySelectorAll ( '.visibility-level-setting .gl-form-radio' ) . 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
2022-10-11 01:57:18 +05:30
if ( VISIBILITY _LEVELS _STRING _TO _INTEGER [ visibility ] < optionValue ) {
2021-12-11 22:18:48 +05:30
option . classList . add ( 'disabled' ) ;
optionInput . disabled = true ;
const reason = option . querySelector ( '.option-disabled-reason' ) ;
if ( reason ) {
2022-08-13 15:12:31 +05:30
const optionTitle = option . querySelector ( '.js-visibility-level-radio span' ) ;
2021-12-11 22:18:48 +05:30
const optionName = optionTitle ? optionTitle . innerText . toLowerCase ( ) : '' ;
2022-10-11 01:57:18 +05:30
// eslint-disable-next-line no-unsanitized/property
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
}
}