Difference between revisions of "MediaWiki:Monobook.js"

From Sudo Room
Jump to navigation Jump to search
(try making sidebar menus collapsible)
 
m (change whitespace?)
Line 7: Line 7:
 
// Use this code ONLY for the Monobook skin.          //
 
// Use this code ONLY for the Monobook skin.          //
 
/////////////////////////////////////////////////////////
 
/////////////////////////////////////////////////////////
+
 
 
$( document ).ready( function() {
 
$( document ).ready( function() {
 
// Set the default expanded items by their headline
 
// Set the default expanded items by their headline
Line 13: Line 13:
 
// Set the basic-name for the cookies, which save the current state of expanding
 
// Set the basic-name for the cookies, which save the current state of expanding
 
var expandCookieName = 'disdance_project_wiki_nav_expanded_';
 
var expandCookieName = 'disdance_project_wiki_nav_expanded_';
+
 
 
var maxHeights = [];
 
var maxHeights = [];
 
var expandeds = [];
 
var expandeds = [];
Line 19: Line 19:
 
initNav();
 
initNav();
 
});
 
});
+
 
 
function initNav() {
 
function initNav() {
 
$( '#p-logo' ).css({'position': 'relative', 'display': 'block'});
 
$( '#p-logo' ).css({'position': 'relative', 'display': 'block'});
Line 27: Line 27:
 
var str = $( this ).html();
 
var str = $( this ).html();
 
labels[id] = str;
 
labels[id] = str;
+
 
 
if ( $.cookie( expandCookieName + id ) == 'false' ) {
 
if ( $.cookie( expandCookieName + id ) == 'false' ) {
 
expandeds[id] = false;
 
expandeds[id] = false;
Line 45: Line 45:
 
} );
 
} );
 
}
 
}
+
 
 
function minimize( target ) {
 
function minimize( target ) {
 
var id = $( target ).parent().attr( 'id' );
 
var id = $( target ).parent().attr( 'id' );
Line 54: Line 54:
 
$( target ).html( str );
 
$( target ).html( str );
 
}
 
}
+
 
 
function maximize( target ) {
 
function maximize( target ) {
 
var id = $( target ).parent().attr( 'id' );
 
var id = $( target ).parent().attr( 'id' );
Line 64: Line 64:
 
$( target ).html( str );
 
$( target ).html( str );
 
}
 
}
+
 
 
function toggleNav( e ) {
 
function toggleNav( e ) {
 
var id = $(e.target ).parent().attr( 'id' );
 
var id = $(e.target ).parent().attr( 'id' );
Line 74: Line 74:
 
}
 
}
 
}
 
}
+
 
 
///////////////////////////////////////////////////////
 
///////////////////////////////////////////////////////
 
///////////////////////////////////////////////////////
 
///////////////////////////////////////////////////////

Revision as of 20:25, 27 December 2013

/* Any JavaScript here will be loaded for users using the MonoBook skin */

// https://www.mediawiki.org/wiki/Manual:Interface/Sidebar#Make_expanding.2Fcollapsing_sidebar_for_all_users_.28Monobook_skin_only.29

/////////////////////////////////////////////////////////
// Code snippet to make your sidebar items expandable  //
// Use this code ONLY for the Monobook skin.           //
/////////////////////////////////////////////////////////

$( document ).ready( function() {
	// Set the default expanded items by their headline
	var defaultExpandItems = ['Navigation', 'Orga'];
	// Set the basic-name for the cookies, which save the current state of expanding
	var expandCookieName = 'disdance_project_wiki_nav_expanded_';

	var maxHeights = [];
	var expandeds = [];
	var labels = [];
	initNav();
});

function initNav() {
	$( '#p-logo' ).css({'position': 'relative', 'display': 'block'});
	$( '.generated-sidebar h5,#p-tb h5 ').each( function( i ) {
		var id = $( this ).parent().attr( 'id' );
		maxHeights[id] = $( this ).next( 'div' ).height();
		var str = $( this ).html();
		labels[id] = str;

		if ( $.cookie( expandCookieName + id ) == 'false' ) {
			expandeds[id] = false;
			minimize( $( this ) );
		} else if ( $.cookie( expandCookieName + id ) == 'true' ) {
			expandeds[id] = true;
			maximize( $( this ) );
		} else if ( defaultExpandItems.indexOf( str ) == -1 ) {
			expandeds[id] = false;
			minimize( $( this ) );
		} else {
			expandeds[id] = true;
			maximize( $( this ) );
		}
		$( this ).css({'cursor': 'pointer'});
		$( this ).click( toggleNav );
	} );
}

function minimize( target ) {
	var id = $( target ).parent().attr( 'id' );
	// You can change the expires parameter to save the cookie longer/shorter than 7 days like in this code
	$.cookie( expandCookieName + id, 'false', { expires: 7} );
	var str = labels[id] + '  ►';
	$( target ).next( 'div' ).animate({'height': '0px'});
	$( target ).html( str );
}

function maximize( target ) {
	var id = $( target ).parent().attr( 'id' );
	// You can change the expires parameter to save the cookie longer/shorter than 7 days like in this code
	$.cookie( expandCookieName + id, 'true', { expires: 7} );
	var str = labels[id] + '  ▼';
	var newHeight = maxHeights[id];
	$( target ).next( 'div' ).animate({'height': newHeight + 'px'});
	$( target ).html( str );
}

function toggleNav( e ) {
	var id = $(e.target ).parent().attr( 'id' );
	expandeds[id] = !expandeds[id];
	if( expandeds[id] == true ) {
		maximize( e.target );
	} else {
		minimize( e.target );
	}
}

///////////////////////////////////////////////////////
///////////////////////////////////////////////////////