| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
( function () { |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
function toggleElement( $collapsible, expand, $defaultToggle, options ) { |
| 27 |
options = options || {}; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
if ( !$collapsible.jquery ) { |
| 33 |
return; |
| 34 |
} |
| 35 |
if ( $defaultToggle === undefined ) { |
| 36 |
$defaultToggle = null; |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
$collapsible.trigger( expand ? 'beforeExpand.mw-collapsible' : 'beforeCollapse.mw-collapsible' ); |
| 43 |
const hookCallback = function () { |
| 44 |
$collapsible.trigger( expand ? 'afterExpand.mw-collapsible' : 'afterCollapse.mw-collapsible' ); |
| 45 |
}; |
| 46 |
|
| 47 |
|
| 48 |
let $containers; |
| 49 |
if ( !options.plainMode && $collapsible.is( 'table' ) ) { |
| 50 |
|
| 51 |
|
| 52 |
if ( $collapsible.find( '> caption' ).length ) { |
| 53 |
$containers = $collapsible.find( '> * > tr' ); |
| 54 |
} else { |
| 55 |
$containers = $collapsible.find( '> tbody > tr' ); |
| 56 |
} |
| 57 |
if ( $defaultToggle ) { |
| 58 |
|
| 59 |
$containers = $containers.not( $defaultToggle.closest( 'tr' ) ); |
| 60 |
} |
| 61 |
|
| 62 |
} else if ( !options.plainMode && ( $collapsible.is( 'ul' ) || $collapsible.is( 'ol' ) ) ) { |
| 63 |
|
| 64 |
$containers = $collapsible.find( '> li' ); |
| 65 |
if ( $defaultToggle ) { |
| 66 |
|
| 67 |
$containers = $containers.not( $defaultToggle.parent() ); |
| 68 |
} |
| 69 |
} else { |
| 70 |
|
| 71 |
const $collapsibleContent = $collapsible.find( '> .mw-collapsible-content' ); |
| 72 |
|
| 73 |
|
| 74 |
if ( !options.plainMode && $collapsibleContent.length ) { |
| 75 |
$containers = $collapsibleContent; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
} else { |
| 80 |
$containers = $collapsible; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
$containers.toggle( expand ); |
| 85 |
hookCallback(); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
function togglingHandler( $toggle, $collapsible, e, options ) { |
| 100 |
options = options || {}; |
| 101 |
|
| 102 |
if ( e ) { |
| 103 |
if ( |
| 104 |
e.type === 'click' && |
| 105 |
e.target.nodeName.toLowerCase() === 'a' && |
| 106 |
$( e.target ).attr( 'href' ) |
| 107 |
) { |
| 108 |
|
| 109 |
return; |
| 110 |
} else if ( e.type === 'keydown' && e.which !== 13 && e.which !== 32 ) { |
| 111 |
|
| 112 |
return; |
| 113 |
} else { |
| 114 |
e.preventDefault(); |
| 115 |
e.stopPropagation(); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
let wasCollapsed; |
| 121 |
if ( options.wasCollapsed !== undefined ) { |
| 122 |
wasCollapsed = options.wasCollapsed; |
| 123 |
} else { |
| 124 |
|
| 125 |
wasCollapsed = $collapsible.hasClass( 'mw-collapsed' ); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
$collapsible.toggleClass( 'mw-collapsed', !wasCollapsed ); |
| 130 |
|
| 131 |
|
| 132 |
if ( options.toggleClasses ) { |
| 133 |
$toggle |
| 134 |
.toggleClass( 'mw-collapsible-toggle-collapsed', !wasCollapsed ) |
| 135 |
.toggleClass( 'mw-collapsible-toggle-expanded', wasCollapsed ); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
if ( options.toggleARIA ) { |
| 140 |
$toggle.attr( 'aria-expanded', wasCollapsed ? 'true' : 'false' ); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
if ( options.toggleText ) { |
| 145 |
const collapseText = options.toggleText.collapseText; |
| 146 |
const expandText = options.toggleText.expandText; |
| 147 |
|
| 148 |
const $textContainer = $toggle.find( '.mw-collapsible-text' ); |
| 149 |
if ( $textContainer.length ) { |
| 150 |
$textContainer.text( wasCollapsed ? collapseText : expandText ); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
toggleElement( $collapsible, !!wasCollapsed, $toggle, options ); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
function hashHandler() { |
| 165 |
const fragment = mw.util.getTargetFromFragment(); |
| 166 |
if ( !fragment ) { |
| 167 |
|
| 168 |
return; |
| 169 |
} |
| 170 |
|
| 171 |
const $parents = $( fragment ).parents( '.mw-collapsed' ); |
| 172 |
if ( !$parents.length ) { |
| 173 |
|
| 174 |
return; |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
$parents.each( function () { |
| 179 |
const $collapsible = $( this ); |
| 180 |
if ( $collapsible.data( 'mw-made-collapsible' ) ) { |
| 181 |
$collapsible.data( 'mw-collapsible' ).expand(); |
| 182 |
} else { |
| 183 |
|
| 184 |
|
| 185 |
$collapsible.removeClass( 'mw-collapsed' ); |
| 186 |
} |
| 187 |
} ); |
| 188 |
|
| 189 |
|
| 190 |
fragment.scrollIntoView(); |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
$.fn.makeCollapsible = function ( options ) { |
| 231 |
options = options || {}; |
| 232 |
|
| 233 |
this.each( function () { |
| 234 |
|
| 235 |
|
| 236 |
const $collapsible = $( this ).addClass( 'mw-collapsible' ); |
| 237 |
|
| 238 |
|
| 239 |
if ( $collapsible.data( 'mw-made-collapsible' ) ) { |
| 240 |
return; |
| 241 |
} else { |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
$collapsible.addClass( 'mw-made-collapsible' ) |
| 246 |
.data( 'mw-made-collapsible', true ); |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
const collapseText = options.collapseText || $collapsible.attr( 'data-collapsetext' ) || mw.msg( 'collapsible-collapse' ); |
| 251 |
const expandText = options.expandText || $collapsible.attr( 'data-expandtext' ) || mw.msg( 'collapsible-expand' ); |
| 252 |
|
| 253 |
|
| 254 |
let actionHandler = function ( e, opts ) { |
| 255 |
const defaultOpts = { |
| 256 |
toggleClasses: true, |
| 257 |
toggleARIA: true, |
| 258 |
toggleText: { collapseText: collapseText, expandText: expandText } |
| 259 |
}; |
| 260 |
opts = Object.assign( defaultOpts, options, opts ); |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
let $currentCollapsible = $.contains( $collapsible.get( 0 ), this ) ? |
| 267 |
$collapsible : |
| 268 |
$( this ).closest( '.mw-collapsible' ); |
| 269 |
|
| 270 |
if ( $currentCollapsible.length === 0 ) { |
| 271 |
$currentCollapsible = $collapsible; |
| 272 |
} |
| 273 |
|
| 274 |
togglingHandler( $( this ), $currentCollapsible, e, opts ); |
| 275 |
}; |
| 276 |
|
| 277 |
|
| 278 |
const buildDefaultToggleLink = function () { |
| 279 |
return $( '<span>' ) |
| 280 |
.addClass( 'mw-collapsible-text' ) |
| 281 |
.text( collapseText ) |
| 282 |
.wrap( '<button type="button" class="mw-collapsible-toggle mw-collapsible-toggle-default"></button>' ) |
| 283 |
.parent(); |
| 284 |
}; |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
let $customTogglers; |
| 289 |
if ( options.$customTogglers ) { |
| 290 |
$customTogglers = $( options.$customTogglers ); |
| 291 |
} else { |
| 292 |
let collapsibleId = $collapsible.attr( 'id' ) || ''; |
| 293 |
if ( collapsibleId.indexOf( 'mw-customcollapsible-' ) === 0 ) { |
| 294 |
collapsibleId = $.escapeSelector( collapsibleId ); |
| 295 |
$customTogglers = $( '.' + collapsibleId.replace( 'mw-customcollapsible', 'mw-customtoggle' ) ) |
| 296 |
.addClass( 'mw-customtoggle' ); |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
let $toggle; |
| 302 |
if ( $customTogglers && $customTogglers.length ) { |
| 303 |
actionHandler = function ( e, opts ) { |
| 304 |
const defaultOpts = {}; |
| 305 |
opts = Object.assign( defaultOpts, options, opts ); |
| 306 |
togglingHandler( $( this ), $collapsible, e, opts ); |
| 307 |
}; |
| 308 |
|
| 309 |
$toggle = $customTogglers; |
| 310 |
|
| 311 |
} else { |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
let $firstItem; |
| 317 |
if ( $collapsible.is( 'table' ) ) { |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
const $caption = $collapsible.find( '> caption' ); |
| 322 |
if ( $caption.length ) { |
| 323 |
$toggle = $caption.find( '> .mw-collapsible-toggle, .mw-collapsible-toggle-placeholder' ).first(); |
| 324 |
|
| 325 |
|
| 326 |
if ( !$toggle.length ) { |
| 327 |
$toggle = buildDefaultToggleLink().appendTo( $caption ); |
| 328 |
} |
| 329 |
} else { |
| 330 |
|
| 331 |
$firstItem = $collapsible.find( 'tr' ).first().find( 'th, td' ); |
| 332 |
$toggle = $firstItem.find( '> .mw-collapsible-toggle, .mw-collapsible-toggle-placeholder' ).first(); |
| 333 |
|
| 334 |
|
| 335 |
if ( !$toggle.length ) { |
| 336 |
$toggle = buildDefaultToggleLink().prependTo( $firstItem.eq( -1 ) ); |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
} else if ( $collapsible.parent().is( 'li' ) && |
| 341 |
$collapsible.parent().children( '.mw-collapsible' ).length === 1 && |
| 342 |
$collapsible.find( '> .mw-collapsible-toggle, .mw-collapsible-toggle-placeholder' ).length === 0 |
| 343 |
) { |
| 344 |
|
| 345 |
$toggle = buildDefaultToggleLink(); |
| 346 |
$collapsible.before( $toggle ); |
| 347 |
} else if ( $collapsible.is( 'ul' ) || $collapsible.is( 'ol' ) ) { |
| 348 |
|
| 349 |
$firstItem = $collapsible.find( 'li' ).first(); |
| 350 |
$toggle = $firstItem.find( '> .mw-collapsible-toggle, .mw-collapsible-toggle-placeholder' ).first(); |
| 351 |
|
| 352 |
|
| 353 |
if ( !$toggle.length ) { |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
const firstval = $firstItem.prop( 'value' ); |
| 358 |
if ( firstval === undefined || !firstval || firstval === '-1' || firstval === -1 ) { |
| 359 |
$firstItem.prop( 'value', '1' ); |
| 360 |
} |
| 361 |
$toggle = buildDefaultToggleLink(); |
| 362 |
$toggle.wrap( '<li class="mw-collapsible-toggle-li"></li>' ).parent().prependTo( $collapsible ); |
| 363 |
} |
| 364 |
|
| 365 |
} else { |
| 366 |
|
| 367 |
|
| 368 |
$toggle = $collapsible.find( '> .mw-collapsible-toggle, .mw-collapsible-toggle-placeholder' ).first(); |
| 369 |
|
| 370 |
|
| 371 |
if ( !$collapsible.find( '> .mw-collapsible-content' ).length ) { |
| 372 |
$collapsible.wrapInner( '<div class="mw-collapsible-content"></div>' ); |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
if ( !$toggle.length ) { |
| 377 |
$toggle = buildDefaultToggleLink().prependTo( $collapsible ); |
| 378 |
} |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
if ( $toggle.hasClass( 'mw-collapsible-toggle-placeholder' ) ) { |
| 385 |
const $realToggle = buildDefaultToggleLink(); |
| 386 |
$toggle.replaceWith( $realToggle ); |
| 387 |
$toggle = $realToggle; |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
$toggle.on( 'click.mw-collapsible keydown.mw-collapsible', actionHandler ) |
| 392 |
.attr( 'aria-expanded', 'true' ) |
| 393 |
.prop( 'tabIndex', 0 ); |
| 394 |
|
| 395 |
$( this ).data( 'mw-collapsible', { |
| 396 |
collapse: function () { |
| 397 |
actionHandler.call( $toggle.get( 0 ), null, { wasCollapsed: false } ); |
| 398 |
}, |
| 399 |
expand: function () { |
| 400 |
actionHandler.call( $toggle.get( 0 ), null, { wasCollapsed: true } ); |
| 401 |
}, |
| 402 |
toggle: function () { |
| 403 |
actionHandler.call( $toggle.get( 0 ), null, null ); |
| 404 |
} |
| 405 |
} ); |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
if ( options.collapsed || $collapsible.hasClass( 'mw-collapsed' ) ) { |
| 410 |
|
| 411 |
|
| 412 |
actionHandler.call( $toggle.get( 0 ), null, { wasCollapsed: false } ); |
| 413 |
} |
| 414 |
|
| 415 |
} ); |
| 416 |
|
| 417 |
|
| 418 |
window.addEventListener( 'hashchange', hashHandler ); |
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
mw.hook( 'wikipage.collapsibleContent' ).fire( this ); |
| 430 |
|
| 431 |
return this; |
| 432 |
}; |
| 433 |
|
| 434 |
|
| 435 |
hashHandler(); |
| 436 |
|
| 437 |
}() ); |