מדיה ויקי:Gadget-foldPortalMenus.js

From האנציקלופדיה היהודית
Revision as of 00:40, 27 January 2015 by בוט ייבוא ערכים (talk | contribs) (ייבוא: מדיה ויקי:Gadget-foldPortalMenus.js. מוויקיפדיה. ראה תורמים.)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Note: After saving, 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.
// Mediawiki:Gadget-foldPortalMenus.js
// allow folding of side-menus to emulate a feature which was removed from mediawiki.
$( function() {
	"use strict";
function doit() {
	var storageKey = 'portalFolded',
		skin = mw.config.get( 'skin' ),
		portalSelector,
		bodySelector,
		storage = $.jStorage.storageAvailable(),
		foldedClass = 'foldmenu-folded',
		expandedClass = 'foldmenu-expanded',
		heights = {},
		toggleElement = function( elem ) {
			var id = elem.closest( portalSelector ).attr( 'id' ),
				hide = ! elem.hasClass( foldedClass );
			elem.toggleClass( expandedClass, !hide )
				.toggleClass(foldedClass, hide )
				.siblings( bodySelector ).stop( true ).animate( { maxHeight: hide ? 0 : heights[id] } );
			if (! storage)
				return;
			$.jStorage.set( storageKey + id, hide );
		},
		preparePortalBody = function( index, elem ) {
			var $elem = $( elem ),
				id = $elem.closest( portalSelector ).attr( 'id' ),
				height = $elem.height();
			$elem.css( 'max-height', $elem.height() );
			heights[id] = height;
		},
		prepareHeader = function( selector ) {
			$( selector )
				.addClass( expandedClass )
				.prepend( $('<span>', { 'class': 'plus' } ).text( '+' ) )
				.prepend( $('<span>', { 'class': 'minus' } ).text( '-' ) )
				.click( function() { toggleElement( $(this) ) } );
		},
		recalcHeights = function() {
			$( portalSelector + ' ' + bodySelector ).each( function() {
				var $div = $( this ),
					id = $div.closest( 'div.portal' ).attr( 'id' ),
					clone = $div.clone()
						.css( { maxHeight: 50000, left: -5000, display: 'block' } )
						.appendTo( 'body' ),
					height = heights[id] = Math.max( (heights[id] || 0), clone.height() );
				$div.css( { maxHeight: height } );
				clone.remove();
			});
		},
		restoreState = function() {
			if (! storage) 
				return;
			$( portalSelector ).each( function( index, item ) {
				var $item = $( item ),
					id = $item.attr( 'id' ),
					$h3 = $( 'h3:first', item );
				if ( $.jStorage.get( storageKey + id ) && ! $h3.hasClass( foldedClass ) )
					toggleElement( $( 'h3:first', item ) );
			});
		};

	switch( skin ) {
		case 'vector':
			portalSelector = 'div.portal';
			bodySelector = 'div.body';
			break;
		case 'monobook':
			portalSelector = 'div.portlet';
			bodySelector = 'div.pBody';
			break;
		default:
			return;
	}
	
	$( portalSelector + ' h3#p-navigation-label' ).toggle( true );
	$( portalSelector + ' ' + bodySelector ).each( preparePortalBody );
	prepareHeader( portalSelector + ' h3' );
	restoreState();
	
	$( document ).on( 'new-portlet-link', function( event, portalId ) {
		recalcHeights();
		if ( ! portalId )
			return;
		var ps = '#' + portalId;
		preparePortalBody( 0, ps );
		prepareHeader( ps + ' h3' );
		restoreState();	
	});
} // doit
setTimeout(doit, 777);
});