| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
( function () { |
| 5 |
'use strict'; |
| 6 |
|
| 7 |
const cascadeableLevels = require( './config.json' ).CascadingRestrictionLevels; |
| 8 |
const cascadeCheckbox = $( '#mwProtect-cascade' ).length && |
| 9 |
OO.ui.CheckboxInputWidget.static.infuse( $( '#mwProtect-cascade' ) ); |
| 10 |
const mwProtectUnchained = new OO.ui.CheckboxInputWidget( { |
| 11 |
selected: false, |
| 12 |
id: 'mwProtectUnchained' |
| 13 |
} ); |
| 14 |
const levelSelectors = $( '[id ^= mwProtect-level-]' ).toArray() |
| 15 |
.map( ( element ) => OO.ui.infuse( element ) ); |
| 16 |
const expiryInputs = $( '[id ^= mwProtect-][id $= -expires]' ).toArray() |
| 17 |
.map( ( element ) => OO.ui.infuse( element ) ); |
| 18 |
const expirySelectors = $( '[id ^= mwProtectExpirySelection-]' ).toArray() |
| 19 |
.map( ( element ) => OO.ui.infuse( element ) ); |
| 20 |
const chainedInputs = [ levelSelectors, expirySelectors, expiryInputs ]; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
function toggleUnchainedInputs( enable ) { |
| 29 |
chainedInputs.forEach( ( widgets ) => { |
| 30 |
widgets.slice( 1 ).forEach( ( widget ) => { |
| 31 |
widget.setDisabled( !enable ); |
| 32 |
} ); |
| 33 |
} ); |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
function areAllTypesMatching() { |
| 43 |
return chainedInputs.every( ( widgets ) => widgets.every( ( widget ) => widget.getValue() === widgets[ 0 ].getValue() ) ); |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
function isUnchained() { |
| 53 |
return mwProtectUnchained.isElementAttached() ? mwProtectUnchained.isSelected() : true; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
function getMaxLevel() { |
| 63 |
return Math.max.apply( Math, levelSelectors.map( ( widget ) => widget.dropdownWidget.getMenu().getItems().map( ( item ) => item.selected ).indexOf( true ) ) ); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
function setAllSelectors( val ) { |
| 73 |
levelSelectors.forEach( ( widget ) => { |
| 74 |
if ( typeof val === 'number' ) { |
| 75 |
widget.setValue( widget.dropdownWidget.getMenu().getItems()[ val ].getData() ); |
| 76 |
} else { |
| 77 |
widget.setValue( val ); |
| 78 |
} |
| 79 |
} ); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
function setAllToFirst( widgets ) { |
| 89 |
widgets.slice( 1 ).forEach( ( widget ) => { |
| 90 |
widget.setValue( widgets[ 0 ].getValue() ); |
| 91 |
} ); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
function updateCascadeAndExpire() { |
| 102 |
levelSelectors.forEach( ( val, index ) => { |
| 103 |
const disable = !val.getValue() || index && !isUnchained(); |
| 104 |
expirySelectors[ index ].setDisabled( disable ); |
| 105 |
expiryInputs[ index ].setDisabled( disable ); |
| 106 |
} ); |
| 107 |
if ( cascadeCheckbox ) { |
| 108 |
levelSelectors.some( ( widget ) => { |
| 109 |
if ( cascadeableLevels.indexOf( widget.getValue() ) === -1 ) { |
| 110 |
cascadeCheckbox.setSelected( false ).setDisabled( true ); |
| 111 |
return true; |
| 112 |
} |
| 113 |
cascadeCheckbox.setDisabled( false ); |
| 114 |
return false; |
| 115 |
} ); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
$( '#mw-Protect-Form' ).on( 'submit', () => { |
| 121 |
chainedInputs.forEach( ( widgets ) => { |
| 122 |
widgets.forEach( ( widget ) => { |
| 123 |
widget.setDisabled( false ); |
| 124 |
} ); |
| 125 |
} ); |
| 126 |
} ); |
| 127 |
|
| 128 |
|
| 129 |
expirySelectors.forEach( ( widget ) => { |
| 130 |
widget.on( 'change', ( val ) => { |
| 131 |
if ( isUnchained() ) { |
| 132 |
if ( val !== 'othertime' ) { |
| 133 |
expiryInputs[ expirySelectors.indexOf( widget ) ].setValue( '' ); |
| 134 |
} |
| 135 |
} else { |
| 136 |
setAllToFirst( expirySelectors ); |
| 137 |
if ( val !== 'othertime' ) { |
| 138 |
expiryInputs[ 0 ].setValue( '' ); |
| 139 |
setAllToFirst( expiryInputs ); |
| 140 |
} |
| 141 |
} |
| 142 |
} ); |
| 143 |
} ); |
| 144 |
|
| 145 |
|
| 146 |
expiryInputs.forEach( ( widget ) => { |
| 147 |
widget.on( 'change', ( val ) => { |
| 148 |
if ( isUnchained() ) { |
| 149 |
if ( val ) { |
| 150 |
expirySelectors[ expiryInputs.indexOf( widget ) ].setValue( 'othertime' ); |
| 151 |
} |
| 152 |
} else { |
| 153 |
setAllToFirst( expiryInputs ); |
| 154 |
if ( val ) { |
| 155 |
expirySelectors[ 0 ].setValue( 'othertime' ); |
| 156 |
setAllToFirst( expirySelectors ); |
| 157 |
} |
| 158 |
} |
| 159 |
} ); |
| 160 |
} ); |
| 161 |
|
| 162 |
|
| 163 |
levelSelectors.forEach( ( widget ) => { |
| 164 |
widget.on( 'change', ( val ) => { |
| 165 |
if ( !isUnchained() ) { |
| 166 |
setAllSelectors( val ); |
| 167 |
} |
| 168 |
updateCascadeAndExpire(); |
| 169 |
} ); |
| 170 |
} ); |
| 171 |
|
| 172 |
|
| 173 |
if ( $( '.oo-ui-panelLayout-framed .oo-ui-panelLayout-framed' ).length > 1 ) { |
| 174 |
mwProtectUnchained.on( 'change', () => { |
| 175 |
toggleUnchainedInputs( isUnchained() ); |
| 176 |
if ( !isUnchained() ) { |
| 177 |
setAllSelectors( getMaxLevel() ); |
| 178 |
setAllToFirst( expirySelectors ); |
| 179 |
setAllToFirst( expiryInputs ); |
| 180 |
} |
| 181 |
updateCascadeAndExpire(); |
| 182 |
} ).setSelected( !areAllTypesMatching() ); |
| 183 |
$( '.oo-ui-panelLayout-framed .oo-ui-panelLayout-framed' ).first().after( |
| 184 |
( new OO.ui.FieldLayout( mwProtectUnchained, { |
| 185 |
label: mw.msg( 'protect-unchain-permissions' ), |
| 186 |
align: 'inline' |
| 187 |
} ) ).$element |
| 188 |
); |
| 189 |
toggleUnchainedInputs( !areAllTypesMatching() ); |
| 190 |
} |
| 191 |
|
| 192 |
const reasonList = OO.ui.infuse( $( '#wpProtectReasonSelection' ) ); |
| 193 |
const reason = OO.ui.infuse( $( '#mwProtect-reason' ) ); |
| 194 |
|
| 195 |
|
| 196 |
mw.widgets.visibleCodePointLimitWithDropdown( reason, reasonList, mw.config.get( 'wgCommentCodePointLimit' ) - 75 ); |
| 197 |
|
| 198 |
updateCascadeAndExpire(); |
| 199 |
}() ); |