MediaWiki:Gadget-autocollapse.js

From Brighter Shores Wiki
Jump to navigation Jump to search

After saving, you may need to bypass your browser's cache to see the changes. For further information, see Wikipedia:Bypass your cache.

  • In most Windows and Linux browsers: Hold down Ctrl and press F5.
  • In Safari: Hold down ⇧ Shift and click the Reload button.
  • In Chrome and Firefox for Mac: Hold down both ⌘ Cmd+⇧ Shift and press R.
/**
 * Automatically collapsed navboxes under certain conditions
 */
(function($, mw){
	if ($( ".mw-collapsible-toggle" ).length) expandMaps();
	if (
		!$('.navbox-autocollapse').length ||
		mw.Uri().query.veaction != undefined
	) return;
	mw.hook('wikipage.collapsibleContent').add(init);
	
	function init() {
		var $navboxes = $('.navbox'),
			// maximum number of navboxes before they all stay collapsed
			maxShow = 1,
			// maximum allowable height of navbox before it stays collapsed
			maxHeight = 300;

		if ($navboxes.length > maxShow) {
			return;
		}
		
		$navboxes.each(function(i,box){
			var $box = $(box);
			if (!$box.hasClass('navbox-autocollapse')) return;
			$box.data('mw-collapsible').expand();
			if ( $box.height() > maxHeight ) $box.data('mw-collapsible').collapse();
		});
	}
	
	// handle collapsible maps
	function expandMaps() {
		$( ".mw-collapsible-toggle" ).on( "click keypress", function() {
		    const $this = $( this );
		    
		    if( $this.hasClass( "mw-collapsible-toggle-expanded" ) ) {
		        mw.hook( "wikipage.content" ).fire(
		            $( "a.mw-kartographer-map", $this.parents( ".mw-collapsible" ).first() ).parent()
		        );
		    }
		} );
	}
	//$(init);
	
})(jQuery, mediaWiki);