MediaWiki:Gadget-autocollapse.js

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
 1 /**
 2  * Automatically collapsed navboxes under certain conditions
 3  */
 4 (function($, mw){
 5 	if ($( ".mw-collapsible-toggle" ).length) expandMaps();
 6 	if (
 7 		!$('.navbox-autocollapse').length ||
 8 		mw.Uri().query.veaction != undefined
 9 	) return;
10 	mw.hook('wikipage.collapsibleContent').add(init);
11 	
12 	function init() {
13 		var $navboxes = $('.navbox'),
14 			// maximum number of navboxes before they all stay collapsed
15 			maxShow = 1,
16 			// maximum allowable height of navbox before it stays collapsed
17 			maxHeight = 300;
18 
19 		if ($navboxes.length > maxShow) {
20 			return;
21 		}
22 		
23 		$navboxes.each(function(i,box){
24 			var $box = $(box);
25 			if (!$box.hasClass('navbox-autocollapse')) return;
26 			$box.data('mw-collapsible').expand();
27 			if ( $box.height() > maxHeight ) $box.data('mw-collapsible').collapse();
28 		});
29 	}
30 	
31 	// handle collapsible maps
32 	function expandMaps() {
33 		$( ".mw-collapsible-toggle" ).on( "click keypress", function() {
34 		    const $this = $( this );
35 		    
36 		    if( $this.hasClass( "mw-collapsible-toggle-expanded" ) ) {
37 		        mw.hook( "wikipage.content" ).fire(
38 		            $( "a.mw-kartographer-map", $this.parents( ".mw-collapsible" ).first() ).parent()
39 		        );
40 		    }
41 		} );
42 	}
43 	//$(init);
44 	
45 })(jQuery, mediaWiki);