|
1 | | -( function ( $ ) { |
2 | | - |
3 | | - $( function () { |
4 | | - var $toc = $( '#toc' ), |
5 | | - $window = $( window ), |
6 | | - $mwPanel = $( '#mw-panel' ), |
7 | | - tocLimit = $toc.offset().top + $toc.height(), |
8 | | - $floatTOC = $toc |
9 | | - .clone() |
10 | | - .attr( 'id', '') |
11 | | - .addClass( 'floatTOC' ) |
12 | | - .appendTo( 'body' ) |
13 | | - .css( 'height', $window.height() ) |
14 | | - .hide(), |
15 | | - headingsOffset = []; |
16 | | - |
17 | | - // Get all heading positions |
18 | | - $('.mw-headline').each( function() { |
19 | | - headingsOffset.push( [ $(this).attr( 'id' ), $( this ).offset().top ] ); |
20 | | - } ); |
| 1 | +mw.loader.using( 'jquery.throttle-debounce', function () { |
| 2 | + $( function () { |
| 3 | + var $window, $mwPanel, $floatTOC, scrollHandler, |
| 4 | + tocLimit, headingOffsets, |
| 5 | + $toc = $( '#toc' ); |
| 6 | + |
| 7 | + if ( !$toc.length ) { |
| 8 | + return; |
| 9 | + } |
| 10 | + |
| 11 | + $window = $( window ); |
| 12 | + $mwPanel = $( '#mw-panel' ); |
| 13 | + $floatTOC = $toc |
| 14 | + .clone() |
| 15 | + .removeAttr( 'id' ) |
| 16 | + .addClass( 'floatTOC' ) |
| 17 | + .appendTo( 'body' ) |
| 18 | + .hide(); |
| 19 | + |
| 20 | + tocLimit = $toc.offset().top + $toc.height(); |
| 21 | + headingOffsets = []; |
21 | 22 |
|
22 | | - // For the window scroll event |
23 | | - var scroller = function () { |
24 | | - var scrollTop = $window.scrollTop(); |
25 | | - if ( scrollTop > tocLimit ) { |
26 | | - $floatTOC.show(); |
27 | | - $mwPanel.hide(); |
28 | | - |
29 | | - // Highlight current |
30 | | - $floatTOC.find( 'a' ).css( 'font-weight', 'normal' ) |
31 | | - var highlight = ""; |
32 | | - $.each( headingsOffset, function ( i, v ) { |
33 | | - if ( scrollTop < v[ 1 ]) { |
34 | | - highlight = headingsOffset[ i -1 ][ 0 ]; |
35 | | - return false; |
36 | | - } |
| 23 | + // Get all heading positions |
| 24 | + $('.mw-headline').each( function () { |
| 25 | + headingOffsets.push( [ $( this ).attr( 'id' ), $( this ).offset().top ] ); |
37 | 26 | } ); |
38 | | - $floatTOC.find( 'a[href="#' + highlight + '"]' ).css( 'font-weight', 'bold' ); |
39 | 27 |
|
40 | | - } else { |
41 | | - $floatTOC.hide(); |
42 | | - $mwPanel.show(); |
43 | | - } |
44 | | - } |
| 28 | + // For the window scroll event |
| 29 | + scrollHandler = function () { |
| 30 | + var $current, |
| 31 | + scrollTop = $window.scrollTop(); |
45 | 32 |
|
46 | | - $window.on( 'scroll', $.throttle( 250, scroller ) ); |
47 | | - } ); |
| 33 | + if ( scrollTop > tocLimit ) { |
| 34 | + $floatTOC.show(); |
| 35 | + $mwPanel.hide(); |
48 | 36 |
|
49 | | -} ) ( jQuery ); |
| 37 | + // Highlight current |
| 38 | + var highlight = false; |
| 39 | + // Current section is above the first heading below the top of the screen |
| 40 | + $.each( headingOffsets, function ( i, v ) { |
| 41 | + // Skip first as there's no previous heading before the first |
| 42 | + if ( i !== 0 && scrollTop < v[ 1 ] ) { |
| 43 | + highlight = headingOffsets[ i - 1 ][ 0 ]; |
| 44 | + return false; |
| 45 | + } |
| 46 | + } ); |
| 47 | + |
| 48 | + if ( highlight ) { |
| 49 | + $current = $floatTOC.find( 'a[href="#' + highlight + '"]' ); |
| 50 | + $floatTOC.find( 'a' ).not( $current ).css( 'font-weight', '' ); |
| 51 | + $current.css( 'font-weight', 'bold' ); |
| 52 | + } |
| 53 | + |
| 54 | + } else { |
| 55 | + $floatTOC.hide(); |
| 56 | + $mwPanel.show(); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + $window.on( 'scroll', $.throttle( 250, scrollHandler ) ); |
| 61 | + } ); |
| 62 | +} ); |
0 commit comments