MediaWiki:Gadget-pathway.js

Z Wikibooks, biblioteki wolnych podręczników.

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: [[User:Piotr]]
 * license: BSD
 */

window.pathway = {
	version: 6,
	/** true if firstHeading is short */
	firstHeadingShort: null,
	/** true if navigation is visible */
	navVisible: false,
	namespaces: {
		0: true
	},
	$toggleHeadingLink: null,
	$firstHeadingSpan: null
};

/* Translatable strings */
mw.messages.set( {
	'pathway-collapse': 'zwiń',
	'pathway-expand': 'rozwiń',
	'pathway-go': 'Przejdź',
	'pathway-edit': 'Edytuj'
} );

// initialize patway
pathway.init = function() {
	var that = this;
	var namespace = mw.config.get( 'wgNamespaceNumber' );

	if ( !this.namespaces[namespace] ) {
		return;
	}

	var action = mw.config.get( 'wgAction' );
	this.fullTitle = mw.config.get( 'wgPageName' ).replace( /_/g, ' ' );
	this.shortTitle = this.fullTitle.replace( /^.*\//, '' );

	// Shorten first heading
	if ( this.shortTitle != this.fullTitle && action != 'edit' && action != 'submit' ) {
		var firstHeading = document.getElementById( "content" ).getElementsByTagName( "h1" )[0];

		if ( firstHeading ) {
			this.firstHeadingShort = true;

			this.$toggleHeadingLink = jQuery( '<a href="#" />' ).text( mw.msg( 'pathway-collapse' ) ).click( function() {
				that.toggleFirstHeading();
				event.preventDefault();
			} );

			this.$firstHeadingSpan = jQuery( firstHeading ).children( 'span:first-child' );
			if ( !this.$firstHeadingSpan.length ) {
				// Workaround for editsection gadget
				jQuery( firstHeading ).children( 'div.editsection' ).prepend( '[', this.$toggleHeadingLink, '] ' );
				this.$firstHeadingSpan = jQuery( firstHeading ).children( 'div' ).children( 'span[dir=auto]' );
			} else {
				var $div = jQuery( "<div/>" ).append( '[', this.$toggleHeadingLink, ']' );
				$div.css( 'float', 'right' );
				$div.css( 'font-size', 'x-small' );
				this.$firstHeadingSpan.before( $div );
			}

			this.toggleFirstHeading();
		}
	}

	// Show path
	jQuery( '#content span.subpages' ).remove();
	var content = document.createElement( "div" );
	content.setAttribute( "id", "pwContent" );
	jQuery( '#contentSub' ).prepend( content );

	var e = document.createElement( "div" );
	e.setAttribute( "id", "pwPathway" );

	// adding links (> book > chapter > section > etc. )
	var href = "";
	var i = 0;
	var j = this.fullTitle.indexOf( '/' );
	while ( j != -1 ) {
		var lnk = document.createElement( "a" );
		lnk.appendChild( document.createTextNode( this.fullTitle.slice( i, j ) ) );
		lnk.href = mw.util.getUrl( this.fullTitle.slice( 0, j ) );

		e.appendChild( document.createTextNode( " > " ) );
		e.appendChild( lnk );

		i = j + 1;
		j = this.fullTitle.indexOf( '/', i );
	}

	e.appendChild( document.createTextNode( " > " ) );
	e.appendChild( document.createTextNode( this.fullTitle.slice( i, this.fullTitle.length ) ) );


	// button to show or hide navigation
	var navBut = document.createElement( "a" );
	navBut.setAttribute( "id", "pwNavToggle" );
	navBut.href = '#';
	navBut.appendChild( document.createTextNode( " »" ) );
	jQuery( navBut ).click( function( event ) {
		that.toggleNavigation()
		event.preventDefault();
	} );
	e.appendChild( navBut );

	content.appendChild( e );

	// simple navigation
	var navDiv = document.createElement( "div" );
	navDiv.setAttribute( "id", "pwNav" );
	navDiv.style.display = "none";

	var input = document.createElement( "input" );
	input.setAttribute( "id", "pwNavInput" );
	input.type = "text";
	input.value = this.fullTitle;

	var buttonGo = document.createElement( "a" );
	buttonGo.setAttribute( "id", "pwNavGo" );
	buttonGo.href = '#';
	buttonGo.appendChild( document.createTextNode( mw.msg( 'pathway-go' ) ) );
	jQuery( buttonGo ).click( function( event ) {
		window.location = mw.util.getUrl( input.value );
		event.preventDefault();
	} );

	var buttonEdit = document.createElement( "a" );
	buttonEdit.setAttribute( "id", "pwNavEdit" );
	buttonEdit.href = '#';
	buttonEdit.appendChild( document.createTextNode( mw.msg( 'pathway-edit' ) ) );
	jQuery( buttonEdit ).click( function( event ) {
		window.location = mw.util.wikiScript() + "?action=edit&title=" + encodeURIComponent( input.value );
		event.preventDefault();
	} );

	var buttons = document.createElement( "span" );
	buttons.setAttribute( "id", "pwNavButtons" );
	buttons.appendChild( document.createTextNode( " " ) );

	buttons.appendChild( buttonGo );
	buttons.appendChild( document.createTextNode( ' | ' ) );
	buttons.appendChild( buttonEdit );

	navDiv.appendChild( input );
	navDiv.appendChild( buttons );
	content.appendChild( navDiv );
}

// lengthen or shorten first heading
pathway.toggleFirstHeading = function() {
	if ( this.firstHeadingShort ) {
		this.$firstHeadingSpan.text( this.shortTitle );
		this.$toggleHeadingLink.text( mw.msg( 'pathway-expand' ) );
	} else {
		this.$firstHeadingSpan.text( this.fullTitle );
		this.$toggleHeadingLink.text( mw.msg( 'pathway-collapse' ) );
	}
	this.firstHeadingShort = !this.firstHeadingShort;
}

// show or hide navigation
pathway.toggleNavigation = function() {
	var nav = document.getElementById( "pwNav" );
	var but = document.getElementById( "pwNavToggle" );
	if ( this.navVisible ) {
		this.navVisible = false;
		nav.style.display = "none";
		but.firstChild.data = " »";
	} else {
		this.navVisible = true;
		nav.style.display = "block";
		but.firstChild.data = " «";
	}
}

jQuery( document ).ready( function() {
	pathway.init();
} );

// </pre>