MediaWiki:Common.js: Difference between revisions
From VNDev Wiki
autocollapse support |
No edit summary |
||
Line 1: | Line 1: | ||
/** | /** | ||
* | * Keep code in MediaWiki:Common.js to a minimum as it is unconditionally | ||
* loaded for all users on every wiki page. If possible create a gadget that is | |||
* enabled by default instead of adding it here (since gadgets are fully | |||
* optimized ResourceLoader modules with possibility to add dependencies etc.) | |||
* | * | ||
* | * Since Common.js isn't a gadget, there is no place to declare its | ||
* dependencies, so we have to lazy load them with mw.loader.using on demand and | |||
* then execute the rest in the callback. In most cases these dependencies will | |||
* be loaded (or loading) already and the callback will not be delayed. In case a | |||
* dependency hasn't arrived yet it'll make sure those are loaded before this. | |||
*/ | */ | ||
mw.hook( 'wikipage.collapsibleContent' ).add( mwCollapsibleSetup ); | /* global mw, $ */ | ||
/* jshint strict:false, browser:true */ | |||
mw.loader.using( [ 'mediawiki.util' ] ).done( function () { | |||
/* Begin of mw.loader.using callback */ | |||
/** | |||
* Add support to mw-collapsible for autocollapse, innercollapse and outercollapse | |||
* | |||
* Maintainers: TheDJ | |||
*/ | |||
function mwCollapsibleSetup( $collapsibleContent ) { | |||
var $element, | |||
$toggle, | |||
autoCollapseThreshold = 2; | |||
$.each( $collapsibleContent, function ( index, element ) { | |||
$element = $( element ); | |||
if ( $element.hasClass( 'collapsible' ) ) { | |||
$element.find( 'tr:first > th:first' ).prepend( $element.find( 'tr:first > * > .mw-collapsible-toggle' ) ); | |||
} | |||
if ( $collapsibleContent.length >= autoCollapseThreshold && $element.hasClass( 'autocollapse' ) ) { | |||
$element.data( 'mw-collapsible' ).collapse(); | |||
} else if ( $element.hasClass( 'innercollapse' ) ) { | |||
if ( $element.parents( '.outercollapse' ).length > 0 ) { | |||
$element.data( 'mw-collapsible' ).collapse(); | |||
} | |||
} | |||
// because of colored backgrounds, style the link in the text color | |||
// to ensure accessible contrast | |||
$toggle = $element.find( '.mw-collapsible-toggle' ); | |||
if ( $toggle.length ) { | |||
// Make the toggle inherit text color | |||
if ( $toggle.parent()[ 0 ].style.color ) { | |||
$toggle.find( 'a' ).css( 'color', 'inherit' ); | |||
} | |||
} | |||
} ); | |||
} | |||
mw.hook( 'wikipage.collapsibleContent' ).add( mwCollapsibleSetup ); | |||
/* End of mw.loader.using callback */ | |||
} ); | |||
/* DO NOT ADD CODE BELOW THIS LINE */ |
Latest revision as of 23:43, 23 June 2022
/** * Keep code in MediaWiki:Common.js to a minimum as it is unconditionally * loaded for all users on every wiki page. If possible create a gadget that is * enabled by default instead of adding it here (since gadgets are fully * optimized ResourceLoader modules with possibility to add dependencies etc.) * * Since Common.js isn't a gadget, there is no place to declare its * dependencies, so we have to lazy load them with mw.loader.using on demand and * then execute the rest in the callback. In most cases these dependencies will * be loaded (or loading) already and the callback will not be delayed. In case a * dependency hasn't arrived yet it'll make sure those are loaded before this. */ /* global mw, $ */ /* jshint strict:false, browser:true */ mw.loader.using( [ 'mediawiki.util' ] ).done( function () { /* Begin of mw.loader.using callback */ /** * Add support to mw-collapsible for autocollapse, innercollapse and outercollapse * * Maintainers: TheDJ */ function mwCollapsibleSetup( $collapsibleContent ) { var $element, $toggle, autoCollapseThreshold = 2; $.each( $collapsibleContent, function ( index, element ) { $element = $( element ); if ( $element.hasClass( 'collapsible' ) ) { $element.find( 'tr:first > th:first' ).prepend( $element.find( 'tr:first > * > .mw-collapsible-toggle' ) ); } if ( $collapsibleContent.length >= autoCollapseThreshold && $element.hasClass( 'autocollapse' ) ) { $element.data( 'mw-collapsible' ).collapse(); } else if ( $element.hasClass( 'innercollapse' ) ) { if ( $element.parents( '.outercollapse' ).length > 0 ) { $element.data( 'mw-collapsible' ).collapse(); } } // because of colored backgrounds, style the link in the text color // to ensure accessible contrast $toggle = $element.find( '.mw-collapsible-toggle' ); if ( $toggle.length ) { // Make the toggle inherit text color if ( $toggle.parent()[ 0 ].style.color ) { $toggle.find( 'a' ).css( 'color', 'inherit' ); } } } ); } mw.hook( 'wikipage.collapsibleContent' ).add( mwCollapsibleSetup ); /* End of mw.loader.using callback */ } ); /* DO NOT ADD CODE BELOW THIS LINE */