MediaWiki:Monobook.js

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

/* MONOBOOK SIDEBAR v2.3 */ /* To edit the sidebar items, edit MediaWiki:Monobook.js/Sidebar.js */ window.wgSidebar = ( window.wgSidebar || {} ); importScript( 'MediaWiki:Monobook.js/Sidebar.js' );

/** * MonobookSidebar: Sets sidebar submenus for elements of MonoBook, adding special * classes to pass the pointer over to allow the effect on all browsers. * * Date: 15 October 2010 * Copyright © 2010 Jesús Martínez Novo (User:Ciencia Al Poder) * * This program is free software; you can redistribute it and/or modify *  it under the terms of the GNU General Public License as published by *   the Free Software Foundation; either version 2 of the License, or *   (at your option) any later version */ MonobookSidebar = { re_s: / /g, re_p: /%/g, loadedMenus: [], init: function { if ( !window.wgSidebar ) { return; }		for ( var menu in wgSidebar ) { var item = document.getElementById( MonobookSidebar.getId( menu ) ); if ( !item ) { continue; }			var menuId = jQuery( item ).parents.get( 2 ).id; // Check it's a valid portlet item if ( !menuId || menuId == '' ) { continue; }			// Generate menu hierarchy MonobookSidebar.buildSubmenu( item, wgSidebar[menu] ); // Set events MonobookSidebar.setEvents( menuId ); }	},	buildSubmenu: function( el, arr ) { var ul = document.createElement( 'ul' ); ul.className = 'sub-menu'; for ( var i = 0; i < arr.length; i++ ) { var li = document.createElement( 'li' ); if ( typeof arr[i] == 'string' ) { var a = MonobookSidebar.linkFromText( arr[i] ); li.appendChild( a ); } else { for ( var menukey in arr[i] ) { a = MonobookSidebar.linkFromText( menukey ); li.appendChild( a ); MonobookSidebar.buildSubmenu( li, arr[i][menukey] ); }			}			ul.appendChild( li ); }		el.appendChild( ul ); el.className = 'with-sub-menu'; var em = document.createElement( 'em' ); em.appendChild( document.createTextNode( '\u203A' ) ); el.firstChild.appendChild( em ); },	setEvents: function( menuId ) { for ( var i = 0; i < MonobookSidebar.loadedMenus; i++ ) { if ( MonobookSidebar.loadedMenus[i] == menuId ) { return; }		}		jQuery( '#' + menuId ).children.eq( 1 ).children.eq( 0 ) .bind( 'mouseover', MonobookSidebar.mouseover ) .bind( 'mouseout', MonobookSidebar.mouseout ); MonobookSidebar.loadedMenus.push( menuId ); },	mouseover: function( e ) { var target = e.target; while ( target.tagName.toLowerCase != 'div' ) { if ( target.tagName.toLowerCase == 'a' ) { target = target.parentNode; }			if ( target.tagName.toLowerCase == 'li' ) { jQuery( target ).addClass( 'hover' ); }			target = target.parentNode; }	},	mouseout: function( e ) { var target = e.target; while ( target.tagName.toLowerCase != 'div' ) { if ( target.tagName.toLowerCase == 'a' ) { target = target.parentNode; }			if ( target.tagName.toLowerCase == 'li' ) { jQuery( target ).removeClass( 'hover' ); }			target = target.parentNode; }	},	linkFromText: function( txt ) { var article = , caption = , sepPos = txt.indexOf( '|' ); if ( sepPos > 0 ) { article = txt.substr( 0, sepPos ); caption = txt.substr( sepPos + 1 ); } else { article = caption = txt; }		article = article.replace( MonobookSidebar.re_s, '_' ); // removed encodeURIComponent, it was messing things up --Jack Phoenix var a = document.createElement( 'a' ); if ( article.length > 7 && article.substr( 0, 7 ) == 'http://' ) { a.setAttribute( 'href', article ); } else { article = article.replace( MonobookSidebar.re_s, '_' ); // removed encodeURIComponent, it was messing things up --Jack Phoenix // Replace encoded colons with normal colons -- added this. --Jack Phoenix article = article.replace( '%3A', ':' ); // added code ends here a.setAttribute( 'href', wgArticlePath.replace( '$1', article ) ); }		a.appendChild( document.createTextNode( caption ) ); return a;	}, getId: function( name ) { return 'n-' + encodeURIComponent( name.replace( MonobookSidebar.re_s, '-' ) ).replace( MonobookSidebar.re_p, '.' ); } };

/* Calling this code to initialize */ jQuery( MonobookSidebar.init );

/* Collapsible Portlets (experimental) */ function foldingPortlets { var portlets = getElementsByClassName( document.getElementById( 'column-one' ), 'div', 'portlet' ); var portskip = ['p-personal', 'p-cactions', 'p-logo', 'p-search', 'p-tbx', 'p-lang']; var first = true;

for( var i = 0; i < portlets.length; i++ ) { if( portskip.join( ' ' ).indexOf( portlets[i].id ) == -1 ) { var pd = portlets[i].getElementsByTagName( 'div' )[0]; var ph = portlets[i].getElementsByTagName( 'h5' )[0]; ph.className = 'portletCollapsible'; pd.setAttribute( 'id', 'pbody-' + i ); pd.style.display = 'none';

var link = document.createElement( 'a' ); var head = getAllText( ph ); while( ph.firstChild ) { ph.removeChild( ph.firstChild ); }			link.appendChild( document.createTextNode( head ) ); link.setAttribute( 'href', 'javascript:showPortlet(\'' + i + '\');' ); link.setAttribute( 'id', 'plink-' + i ); link.className = 'portletClosed'; ph.appendChild( link );

if( first == true ) { first = false; showPortlet( i ); }		}	} } if( skin == 'monobook' && !window.portletsNormal ) { addOnloadHook( foldingPortlets ); }

function getAllText( thing ) { if ( thing.nodeType == 3 ) { return thing.nodeValue; }	var text = new Array; var i = 0; while( thing.childNodes[i] ) { text[text.length] = getAllText( thing.childNodes[i] ); i++; }	return text.join( '' ); }

function showPortlet( id ) { var pd = document.getElementById( 'pbody-' + id ); var pl = document.getElementById( 'plink-' + id );

if( pd.style.display == 'none' ) { pd.style.display = 'block'; pl.className = 'portletOpened'; } else { pd.style.display = 'none'; pl.className = 'portletClosed'; } } /* End Collapsible Portlets */