MediaWiki:Gadget-persistent-toc.js
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.
/**
* Adaptacja skryptu "Persistent Table of Contents for Wikipedia"
* Orygialny kod na licencji MIT: https://github.com/prtksxna/persistent-toc
* Modyfikacje na potrzeby Wikisłownika: [[User:Peter Bowman]]
*/
$( function () {
var $window, $mwPanel, $floatTOC, headingThreshold;
$window = $( window );
$mwPanel = $( '#mw-panel' );
headingThreshold = $window.height() / 5.0;
$floatTOC = $();
mw.hook( 'wikipage.content' ).add( function ( $content ) {
var tocLimit, scrollHandler, headingOffsets,
$toc = $content.find( '#toc' );
$mwPanel.show();
$floatTOC.remove();
$window.off( 'scroll.ptoc' );
if ( !$toc.length ) {
return;
}
$floatTOC = $toc
.clone()
.removeAttr( 'id' )
.addClass( 'floatTOC' )
.appendTo( 'body' )
.css( {
visibility: 'hidden',
opacity: 0
} );
// *** Usuwanie tytułu strony z nagłówków haseł ***
if ( mw.config.get( 'wgNamespaceNumber' ) === 0 ) {
$floatTOC.find( '.toctext' ).each( function () {
$( this ).text(
// http://stackoverflow.com/a/4189310
$( this ).find( 'span' ).filter( '*:not(:has("*"))' ).text()
);
} );
}
// Show the ToC ul even if its hidden
$floatTOC.find( 'ul' ).show();
// Hijack links so that we can scroll to the content
$floatTOC.find( 'a' ).click( function ( e ) {
$( 'html, body' ).animate( {
scrollTop: $content.find(
this.hash.replace( /\./g, '\\.' )
).offset().top - headingThreshold
} );
return false;
} );
tocLimit = $toc.offset().top + $toc.height();
headingOffsets = [];
// Get all heading positions
$content.find( '.mw-headline' ).each( function () {
headingOffsets.push( [ $( this ).attr( 'id' ), $( this ).offset().top ] );
} );
// For the window scroll event
scrollHandler = function () {
var $current,
scrollTop = $window.scrollTop();
if ( scrollTop > tocLimit ) {
$floatTOC.css( {
visibility: 'visible',
opacity: 1
} );
$mwPanel.hide();
// Highlight current
var highlight = false;
// Current section is above the first heading below the top of the screen
$.each( headingOffsets, function ( i, v ) {
// Skip first as there's no previous heading before the first
if ( i !== 0 && ( scrollTop + headingThreshold ) < v[ 1 ] ) {
highlight = headingOffsets[ i - 1 ][ 0 ];
return false;
}
} );
if ( highlight ) {
$current = $floatTOC.find( 'a[href="#' + highlight + '"]' );
$floatTOC.find( 'a' ).not( $current ).css( 'font-weight', '' );
$current.css( 'font-weight', 'bold' );
}
} else {
$floatTOC.css( {
visibility: 'hidden',
opacity: 0
} );
$mwPanel.show();
}
};
$window.on( 'scroll.ptoc', $.throttle( 250, scrollHandler ) );
} );
} );