Wikisłownikarz:Beau/hideSections.js

Z Wikisłownika – wolnego słownika wielojęzycznego

Uwaga: aby zobaczyć zmiany po opublikowaniu, może zajść potrzeba wyczyszczenia pamięci podręcznej przeglądarki.

  • Firefox / Safari: Przytrzymaj Shift podczas klikania Odśwież bieżącą stronę, lub naciśnij klawisze Ctrl+F5, lub Ctrl+R (⌘-R na komputerze Mac)
  • Google Chrome: Naciśnij Ctrl-Shift-R (⌘-Shift-R na komputerze Mac)
  • Internet Explorer / Edge: Przytrzymaj Ctrl, jednocześnie klikając Odśwież, lub naciśnij klawisze Ctrl+F5
  • Opera: Naciśnij klawisze Ctrl+F5.
// <pre>
// Author: [[pl:User:Beau]]
var sectionWrapperGadget = {
	/** Version of the gadget */
	version: 1,
	/** Array of wrapped sections */
	sections: [],

	init: function() {
		var sections = [];
		var currentSection = null;

		//var currentNode = mw.util.$content.children( 'div' ).children( 'h2' )[0];
		var currentNode = $( '#mw-content-text' ).children( 'h2' )[0];

		if ( !currentNode ) {
			return;
		}

		// Wrap the elements
		do {
			var nextNode = currentNode.nextSibling;

			if ( currentNode.tagName && currentNode.tagName == 'H2' ) {
				currentSection = {
					header: jQuery( currentNode ),
					container: jQuery( '<div/>' )
				};
				jQuery( currentNode ).after( currentSection.container );
				sections.push( currentSection );
			} else {
				currentSection.container.append( currentNode );
			}

			currentNode = nextNode;
		} while ( currentNode );

		this.sections = sections;
	}
};

if ( mw.config.get( "wgNamespaceNumber" ) == 0 && mw.config.get( "wgAction" ) == 'view' ) {
	jQuery( document ).ready( function() {
		sectionWrapperGadget.init();
	} );
}


/* Translatable strings */
mw.messages.set( {
	'hs-hide-others': 'ukryj pozostałe',
	'hs-show': 'pokaż',
	'hs-show-all': 'pokaż wszystkie'
} );

var hideSectionsGadget = {
	/** Version of the gadget */
	version: 1,
	/** Name of a cookie holding language preference */
	visibleLanguageCookie: mw.config.get( 'wgCookiePrefix' ) + '_visibleLanguage',

	/** Prepares section for the gadget */
	prepareSection: function( section ) {
		// Obtain a section language
		var m = section.header.text().match( /\(([^()]+?)\)/ );
		if ( m ) {
			section.hsLanguage = m[1];
		}

		// Append a link to the header
		var that = this;
		var anchor = section.header.find( 'span.mw-headline' ).attr( 'id' );
		var span = this.createLink( mw.msg( 'hs-hide-others' ), "#" + anchor, function() {
			that.switchSection( section );
		} );
		section.header.append( span );
		section.hsLink = jQuery( span );
		section.hsAnchor = anchor;
	},

	/** Shows all sections */
	showAll: function() {
		for ( var i in sectionWrapperGadget.sections ) {
			var currentSection = sectionWrapperGadget.sections[i];
			currentSection.container.show();
			currentSection.hsLink.find( 'a' ).empty().append( mw.msg( 'hs-hide-others' ) );
		}
		jQuery.cookie( this.visibleLanguageCookie, null );
	},

	/**
	 * Shows the specified section, the rest is hidden.
	 * Stores in a cookie language the user has chosen.
	 */
	switchSection: function( section ) {
		// Set the cookie
		if ( section.hsLanguage ) {
			jQuery.cookie( this.visibleLanguageCookie, section.hsLanguage, {
				expires: 30
			} );
		} else {
			jQuery.cookie( this.visibleLanguageCookie, null );
		}

		// Update the view
		for ( var i in sectionWrapperGadget.sections ) {
			var currentSection = sectionWrapperGadget.sections[i];

			if ( section == currentSection ) {
				currentSection.container.show();
				currentSection.hsLink.find( 'a' ).empty().append( mw.msg( 'hs-hide-others' ) );
			} else {
				currentSection.container.hide();
				currentSection.hsLink.find( 'a' ).empty().append( mw.msg( 'hs-show' ) );
			}
		}
	},

	/**
	 * Creates a header link for hiding and showing sections.
	 * @param text A label of the link.
	 * @param href An address for the link.
	 * @param onclick A handler for the click event.
	 */
	createLink: function( text, href, onclick ) {
		// Create a link
		var a = document.createElement( "a" );
		a.appendChild( document.createTextNode( text ) );
		a.href = href;
		a.onclick = onclick;

		// Create a container
		var span = document.createElement( "span" );
		span.appendChild( document.createTextNode( "[" ) );
		span.appendChild( a );
		span.appendChild( document.createTextNode( "]" ) );
		span.style.float = "none";
		span.style.fontSize = "x-small";
		span.style.fontWeight = "normal";
		return span;
	},

	/** Sets up the gadget */
	init: function() {
		if ( mw.util.getParamValue( 'printable' ) == 'yes' ) {
			return;
		}

		if ( !sectionWrapperGadget.sections.length ) {
			return;
		}

		// Find the first heading
		var firstHeading;
		var headers = document.getElementsByTagName( "h1" );

		for ( i = 0; i < headers.length; i++ ) {
			var header = headers[i];
			if ( header.className == "firstHeading" || header.id == "firstHeading" || header.className == "pagetitle" ) {
				firstHeading = header;
				break;
			}
		}

		if ( !firstHeading ) {
			firstHeading = document.getElementById( "section-0" );
		}

		if ( !firstHeading ) {
			return;
		}

		var that = this;

		// Append a link to the first heading
		var span = this.createLink( mw.msg( 'hs-show-all' ), "#", function() {
			that.showAll()
		} );
		if ( firstHeading.childNodes.length == 1 ) {
			firstHeading.appendChild( span );
		} else {
			firstHeading.insertBefore( span, firstHeading.childNodes[1] );
		}

		for ( var i in sectionWrapperGadget.sections ) {
			this.prepareSection( sectionWrapperGadget.sections[i] );
		}

		var visibleLanguage = jQuery.cookie( this.visibleLanguageCookie );
		if ( visibleLanguage !== null ) {
			var visibleSection;

			for ( var i in sectionWrapperGadget.sections ) {
				var section = sectionWrapperGadget.sections[i];
				if ( section.hsLanguage == visibleLanguage ) {
					visibleSection = section;
					break;
				}
			}

			if ( visibleSection ) {
				this.switchSection( visibleSection );

				if ( section.hsAnchor !== null && window.location.hash == "" ) {
					window.location.hash = '#' + section.hsAnchor;
				}

			}
		}
	}

};

if ( mw.config.get( "wgNamespaceNumber" ) == 0 && mw.config.get( "wgAction" ) == 'view' ) {
	jQuery( document ).ready( function() {
		hideSectionsGadget.init();
	} );
}