| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
function conditionGetField( $root, name ) { |
| 21 |
const nameFilter = function () { |
| 22 |
return this.name === name; |
| 23 |
}; |
| 24 |
let $found = $root.find( '[name]' ).filter( nameFilter ); |
| 25 |
if ( !$found.length ) { |
| 26 |
|
| 27 |
$found = $root.closest( 'form' ).find( '[name]' ).filter( nameFilter ); |
| 28 |
} |
| 29 |
return $found.length ? $found : null; |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
function getWidget( $field ) { |
| 41 |
const $widget = $field.closest( '.oo-ui-widget[data-ooui]' ); |
| 42 |
if ( $widget.length ) { |
| 43 |
return OO.ui.Widget.static.infuse( $widget ); |
| 44 |
} |
| 45 |
return null; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
function conditionParse( $root, spec ) { |
| 61 |
let v, fields, func; |
| 62 |
|
| 63 |
const op = spec[ 0 ]; |
| 64 |
let l = spec.length; |
| 65 |
switch ( op ) { |
| 66 |
case 'AND': |
| 67 |
case 'OR': |
| 68 |
case 'NAND': |
| 69 |
case 'NOR': { |
| 70 |
const funcs = []; |
| 71 |
fields = []; |
| 72 |
for ( let i = 1; i < l; i++ ) { |
| 73 |
if ( !Array.isArray( spec[ i ] ) ) { |
| 74 |
throw new Error( op + ' parameters must be arrays' ); |
| 75 |
} |
| 76 |
v = conditionParse( $root, spec[ i ] ); |
| 77 |
fields = fields.concat( v[ 0 ] ); |
| 78 |
funcs.push( v[ 1 ] ); |
| 79 |
} |
| 80 |
|
| 81 |
l = funcs.length; |
| 82 |
const valueChk = { AND: false, OR: true, NAND: false, NOR: true }; |
| 83 |
const valueRet = { AND: true, OR: false, NAND: false, NOR: true }; |
| 84 |
func = function () { |
| 85 |
for ( let j = 0; j < l; j++ ) { |
| 86 |
if ( valueChk[ op ] === funcs[ j ]() ) { |
| 87 |
return !valueRet[ op ]; |
| 88 |
} |
| 89 |
} |
| 90 |
return valueRet[ op ]; |
| 91 |
}; |
| 92 |
|
| 93 |
return [ fields, func ]; |
| 94 |
} |
| 95 |
|
| 96 |
case 'NOT': |
| 97 |
if ( l !== 2 ) { |
| 98 |
throw new Error( 'NOT takes exactly one parameter' ); |
| 99 |
} |
| 100 |
if ( !Array.isArray( spec[ 1 ] ) ) { |
| 101 |
throw new Error( 'NOT parameters must be arrays' ); |
| 102 |
} |
| 103 |
v = conditionParse( $root, spec[ 1 ] ); |
| 104 |
fields = v[ 0 ]; |
| 105 |
func = v[ 1 ]; |
| 106 |
return [ fields, function () { |
| 107 |
return !func(); |
| 108 |
} ]; |
| 109 |
|
| 110 |
case '===': |
| 111 |
case '!==': { |
| 112 |
if ( l !== 3 ) { |
| 113 |
throw new Error( op + ' takes exactly two parameters' ); |
| 114 |
} |
| 115 |
const $field = conditionGetField( $root, spec[ 1 ] ); |
| 116 |
if ( !$field ) { |
| 117 |
return [ [], function () { |
| 118 |
return false; |
| 119 |
} ]; |
| 120 |
} |
| 121 |
v = spec[ 2 ]; |
| 122 |
|
| 123 |
let widget; |
| 124 |
const getVal = function () { |
| 125 |
|
| 126 |
|
| 127 |
if ( widget === undefined ) { |
| 128 |
widget = getWidget( $field ); |
| 129 |
} |
| 130 |
|
| 131 |
if ( widget ) { |
| 132 |
if ( widget.supports( 'isSelected' ) ) { |
| 133 |
const selected = widget.isSelected(); |
| 134 |
return selected ? widget.getValue() : ''; |
| 135 |
} else { |
| 136 |
return widget.getValue(); |
| 137 |
} |
| 138 |
} else { |
| 139 |
if ( $field.prop( 'type' ) === 'radio' || $field.prop( 'type' ) === 'checkbox' ) { |
| 140 |
const $selected = $field.filter( ':checked' ); |
| 141 |
return $selected.length ? $selected.val() : ''; |
| 142 |
} else { |
| 143 |
return $field.val(); |
| 144 |
} |
| 145 |
} |
| 146 |
}; |
| 147 |
|
| 148 |
switch ( op ) { |
| 149 |
case '===': |
| 150 |
func = function () { |
| 151 |
return getVal() === v; |
| 152 |
}; |
| 153 |
break; |
| 154 |
case '!==': |
| 155 |
func = function () { |
| 156 |
return getVal() !== v; |
| 157 |
}; |
| 158 |
break; |
| 159 |
} |
| 160 |
|
| 161 |
return [ [ $field ], func ]; |
| 162 |
} |
| 163 |
|
| 164 |
default: |
| 165 |
throw new Error( 'Unrecognized operation \'' + op + '\'' ); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
function gatherOOUIModules( $fields ) { |
| 179 |
const $oouiFields = $fields.filter( '[data-ooui]' ); |
| 180 |
const modules = []; |
| 181 |
|
| 182 |
if ( $oouiFields.length ) { |
| 183 |
modules.push( 'mediawiki.htmlform.ooui' ); |
| 184 |
$oouiFields.each( function () { |
| 185 |
const data = $( this ).data( 'mw-modules' ); |
| 186 |
if ( data ) { |
| 187 |
|
| 188 |
const extraModules = data.split( ',' ); |
| 189 |
modules.push( ...extraModules ); |
| 190 |
} |
| 191 |
} ); |
| 192 |
} |
| 193 |
|
| 194 |
return modules; |
| 195 |
} |
| 196 |
|
| 197 |
mw.hook( 'htmlform.enhance' ).add( ( $root ) => { |
| 198 |
const $exclude = $root.find( '.mw-htmlform-autoinfuse-lazy' ) |
| 199 |
.find( '.mw-htmlform-hide-if, .mw-htmlform-disable-if' ); |
| 200 |
const $fields = $root.find( '.mw-htmlform-hide-if, .mw-htmlform-disable-if' ).not( $exclude ); |
| 201 |
|
| 202 |
|
| 203 |
mw.loader.using( gatherOOUIModules( $fields ) ).done( () => { |
| 204 |
$fields.each( function () { |
| 205 |
const $el = $( this ); |
| 206 |
|
| 207 |
let spec, $elOrLayout, $form; |
| 208 |
if ( $el.is( '[data-ooui]' ) ) { |
| 209 |
|
| 210 |
$elOrLayout = OO.ui.FieldLayout.static.infuse( $el ); |
| 211 |
$form = $elOrLayout.$element.closest( 'form' ); |
| 212 |
spec = $elOrLayout.condState; |
| 213 |
} else { |
| 214 |
$elOrLayout = $el; |
| 215 |
$form = $el.closest( 'form' ); |
| 216 |
spec = $el.data( 'condState' ); |
| 217 |
} |
| 218 |
|
| 219 |
if ( !spec ) { |
| 220 |
return; |
| 221 |
} |
| 222 |
|
| 223 |
let fields = []; |
| 224 |
const test = {}; |
| 225 |
[ 'hide', 'disable' ].forEach( ( type ) => { |
| 226 |
if ( spec[ type ] ) { |
| 227 |
const v = conditionParse( $form, spec[ type ] ); |
| 228 |
fields = fields.concat( fields, v[ 0 ] ); |
| 229 |
test[ type ] = v[ 1 ]; |
| 230 |
} |
| 231 |
} ); |
| 232 |
const func = function () { |
| 233 |
const shouldHide = spec.hide ? test.hide() : false; |
| 234 |
const shouldDisable = shouldHide || ( spec.disable ? test.disable() : false ); |
| 235 |
if ( spec.hide ) { |
| 236 |
|
| 237 |
$elOrLayout.toggle( !shouldHide ); |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
if ( $elOrLayout instanceof $ ) { |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
$elOrLayout.find( 'input, textarea, select' ).each( function () { |
| 252 |
const $this = $( this ); |
| 253 |
if ( shouldDisable ) { |
| 254 |
if ( $this.data( 'was-disabled' ) === undefined ) { |
| 255 |
$this.data( 'was-disabled', $this.prop( 'disabled' ) ); |
| 256 |
} |
| 257 |
$this.prop( 'disabled', true ); |
| 258 |
} else { |
| 259 |
$this.prop( 'disabled', $this.data( 'was-disabled' ) ); |
| 260 |
} |
| 261 |
} ); |
| 262 |
} else { |
| 263 |
|
| 264 |
if ( shouldDisable ) { |
| 265 |
if ( $elOrLayout.wasDisabled === undefined ) { |
| 266 |
$elOrLayout.wasDisabled = $elOrLayout.fieldWidget.isDisabled(); |
| 267 |
} |
| 268 |
$elOrLayout.fieldWidget.setDisabled( true ); |
| 269 |
} else if ( $elOrLayout.wasDisabled !== undefined ) { |
| 270 |
$elOrLayout.fieldWidget.setDisabled( $elOrLayout.wasDisabled ); |
| 271 |
} |
| 272 |
} |
| 273 |
}; |
| 274 |
|
| 275 |
const oouiNodes = fields.map( |
| 276 |
|
| 277 |
( $node ) => $node.closest( '.oo-ui-fieldLayout[data-ooui]' )[ 0 ] |
| 278 |
).filter( |
| 279 |
|
| 280 |
( node ) => !!node |
| 281 |
); |
| 282 |
|
| 283 |
|
| 284 |
mw.loader.using( gatherOOUIModules( $( oouiNodes ) ) ).done( () => { |
| 285 |
for ( let i = 0; i < fields.length; i++ ) { |
| 286 |
const widget = getWidget( fields[ i ] ); |
| 287 |
if ( widget ) { |
| 288 |
fields[ i ] = widget; |
| 289 |
} |
| 290 |
|
| 291 |
fields[ i ].on( 'change', func ); |
| 292 |
} |
| 293 |
func(); |
| 294 |
} ); |
| 295 |
} ); |
| 296 |
} ); |
| 297 |
} ); |