MediaWiki:Gadget-SidebarTOC.js: Difference between revisions

Jump to navigation Jump to search
mNo edit summary
No edit summary
Line 1: Line 1:
mw.loader.using( 'mediawiki.toc', function () {
mw.loader.using( 'mediawiki.toc', function () {
$( function () {
$( function () {
var $window, $mwPanel, $floatTOC,
var $tocContainer = $( '<div>' )
tocLimit, headingOffsets, headingThreshold,
.addClass( 'mw-table-of-contents-container' )
$toc = $( '.toc' );
.appendTo( '#mw-content' ); // Append the ToC container under the content area


if ( !$toc.length ) {
var $floatTOC = $( '.toc' )
return;
}
 
$window = $( window );
$mwPanel = $( '#mw-panel' );
headingThreshold = $window.height() / 5.0;
$floatTOC = $toc
.clone()
.clone()
.removeAttr( 'id' )
.removeAttr( 'id' )
.addClass( 'floatTOC' )
.addClass( 'vector-sticky-toc-container' )
.appendTo( 'body' )
.appendTo( $tocContainer ); // Append the cloned ToC inside the ToC container
.css( {
});
visibility: 'hidden',
opacity: 0
} );
 
// Show the ToC ul even if it's hidden
$floatTOC.find( 'ul' ).show();
 
// Hijack links so that we can scroll to the content
$floatTOC.find( 'a' ).click( function ( e ) {
var target = $( this.hash.replace( /\./g, '\\.' ));
if (target.length) {
$( 'html, body' ).animate( {
scrollTop: target.offset().top - headingThreshold
} );
}
return false;
});
 
 
        tocLimit = $toc.offset().top + $toc.height();
        headingOffsets = [];
 
        // Get all heading positions
        $toc.find('a').each(function () {
            var id = $(this).attr('href').substring(1);
            var $heading = $('#' + id);
            if ($heading.length) {
                headingOffsets.push([id, $heading.offset().top]);
            }
        });
 
        // For the window scroll event
        $window.on('scroll', 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();
            }
        });
    });
});
});

Revision as of 06:49, 22 February 2024

mw.loader.using( 'mediawiki.toc', function () {
	$( function () {
		var $tocContainer = $( '<div>' )
			.addClass( 'mw-table-of-contents-container' )
			.appendTo( '#mw-content' ); // Append the ToC container under the content area

		var $floatTOC = $( '.toc' )
			.clone()
			.removeAttr( 'id' )
			.addClass( 'vector-sticky-toc-container' )
			.appendTo( $tocContainer ); // Append the cloned ToC inside the ToC container
	});
});