| 1 |
$( function () { |
| 2 |
|
| 3 |
|
| 4 |
var $dropdowns = $( '#personal, #p-variants-desktop, .sidebar-chunk' ); |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
function closeOpen() { |
| 17 |
$dropdowns.removeClass( 'dropdown-active' ); |
| 18 |
} |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
$dropdowns.on( 'click', function ( e ) { |
| 24 |
|
| 25 |
|
| 26 |
if ( $( this ).hasClass( 'dropdown-active' ) ) { |
| 27 |
if ( $( e.target ).closest( $( 'h2, #p-variants-desktop h3' ) ).length > 0 ) { |
| 28 |
|
| 29 |
closeOpen(); |
| 30 |
} |
| 31 |
|
| 32 |
} else { |
| 33 |
closeOpen(); |
| 34 |
e.stopPropagation(); |
| 35 |
$( this ).addClass( 'dropdown-active' ); |
| 36 |
} |
| 37 |
} ); |
| 38 |
$( document ).on( 'click', function ( e ) { |
| 39 |
if ( $( e.target ).closest( $dropdowns ).length > 0 ) { |
| 40 |
|
| 41 |
} else { |
| 42 |
closeOpen(); |
| 43 |
} |
| 44 |
} ); |
| 45 |
} ); |
| 46 |
|
| 47 |
mw.hook( 'wikipage.content' ).add( function ( $content ) { |
| 48 |
|
| 49 |
$content.find( 'div > table:not( table table )' ).wrap( '<div class="content-table-wrapper"><div class="content-table"></div></div>' ); |
| 50 |
$content.find( '.content-table-wrapper' ).prepend( '<div class="content-table-left"></div><div class="content-table-right"></div>' ); |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
function setScrollClass( $table ) { |
| 60 |
var $tableWrapper = $table.parent(), |
| 61 |
|
| 62 |
scroll = Math.abs( $tableWrapper.scrollLeft() ); |
| 63 |
|
| 64 |
$tableWrapper.parent() |
| 65 |
|
| 66 |
.toggleClass( 'scroll-left', scroll > 1 ) |
| 67 |
.toggleClass( 'scroll-right', $table.outerWidth() - $tableWrapper.innerWidth() - scroll > 1 ); |
| 68 |
} |
| 69 |
|
| 70 |
$content.find( '.content-table' ).on( 'scroll', function () { |
| 71 |
setScrollClass( $( this ).children( 'table' ).first() ); |
| 72 |
|
| 73 |
if ( $content.attr( 'dir' ) === 'rtl' ) { |
| 74 |
$( this ).find( 'caption' ).css( 'margin-right', Math.abs( $( this ).scrollLeft() ) + 'px' ); |
| 75 |
} else { |
| 76 |
$( this ).find( 'caption' ).css( 'margin-left', $( this ).scrollLeft() + 'px' ); |
| 77 |
} |
| 78 |
} ); |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
function unOverflowTables() { |
| 84 |
$content.find( '.content-table > table' ).each( function () { |
| 85 |
var $table = $( this ), |
| 86 |
$wrapper = $table.parent().parent(); |
| 87 |
if ( $table.outerWidth() > $wrapper.outerWidth() ) { |
| 88 |
$wrapper.addClass( 'overflowed' ); |
| 89 |
setScrollClass( $table ); |
| 90 |
} else { |
| 91 |
$wrapper.removeClass( 'overflowed scroll-left scroll-right fixed-scrollbar-container' ); |
| 92 |
} |
| 93 |
} ); |
| 94 |
|
| 95 |
|
| 96 |
$content.find( '.content-table > table > caption' ).each( function () { |
| 97 |
var $container, tableHeight, |
| 98 |
$table = $( this ).parent(), |
| 99 |
$wrapper = $table.parent().parent(); |
| 100 |
|
| 101 |
if ( $table.outerWidth() > $wrapper.outerWidth() ) { |
| 102 |
$container = $( this ).parents( '.content-table-wrapper' ); |
| 103 |
$( this ).width( $content.width() ); |
| 104 |
tableHeight = $container.innerHeight() - $( this ).outerHeight(); |
| 105 |
|
| 106 |
$container.find( '.content-table-left' ).height( tableHeight ); |
| 107 |
$container.find( '.content-table-right' ).height( tableHeight ); |
| 108 |
} |
| 109 |
} ); |
| 110 |
} |
| 111 |
|
| 112 |
unOverflowTables(); |
| 113 |
$( window ).on( 'resize', unOverflowTables ); |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
$content.find( '.content-table' ).each( function () { |
| 119 |
var $table, $tableWrapper, $spoof, $scrollbar; |
| 120 |
|
| 121 |
$tableWrapper = $( this ); |
| 122 |
$table = $tableWrapper.children( 'table' ).first(); |
| 123 |
|
| 124 |
|
| 125 |
$scrollbar = $( '<div>' ).addClass( 'content-table-scrollbar inactive' ).width( $content.width() ); |
| 126 |
$spoof = $( '<div>' ).addClass( 'content-table-spoof' ).width( $table.outerWidth() ); |
| 127 |
$tableWrapper.parent().prepend( $scrollbar.prepend( $spoof ) ); |
| 128 |
} ); |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
$content.find( '.content-table' ).on( 'scroll', function () { |
| 134 |
|
| 135 |
var $mirror = $( this ).siblings( '.inactive' ).first(); |
| 136 |
|
| 137 |
$mirror.scrollLeft( $( this ).scrollLeft() ); |
| 138 |
} ); |
| 139 |
$content.find( '.content-table-scrollbar' ).on( 'scroll', function () { |
| 140 |
var $mirror = $( this ).siblings( '.content-table' ).first(); |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
if ( !$( this ).hasClass( 'inactive' ) ) { |
| 145 |
$mirror.scrollLeft( $( this ).scrollLeft() ); |
| 146 |
} |
| 147 |
} ); |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
function determineActiveSpoofScrollbars() { |
| 153 |
$content.find( '.overflowed .content-table' ).each( function () { |
| 154 |
var $scrollbar = $( this ).siblings( '.content-table-scrollbar' ).first(); |
| 155 |
|
| 156 |
|
| 157 |
var captionHeight = $( this ).find( 'caption' ).outerHeight() || 0; |
| 158 |
if ( captionHeight ) { |
| 159 |
|
| 160 |
captionHeight += 8; |
| 161 |
} |
| 162 |
|
| 163 |
var tableTop = $( this ).offset().top, |
| 164 |
tableBottom = tableTop + $( this ).outerHeight(), |
| 165 |
viewBottom = window.scrollY + document.documentElement.clientHeight, |
| 166 |
active = tableTop + captionHeight < viewBottom && tableBottom > viewBottom; |
| 167 |
$scrollbar.toggleClass( 'inactive', !active ); |
| 168 |
} ); |
| 169 |
} |
| 170 |
|
| 171 |
determineActiveSpoofScrollbars(); |
| 172 |
$( window ).on( 'scroll resize', determineActiveSpoofScrollbars ); |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
$( window ).on( 'resize', function () { |
| 178 |
$content.find( '.content-table-scrollbar' ).each( function () { |
| 179 |
var width = $( this ).siblings().first().find( 'table' ).first().width(); |
| 180 |
$( this ).find( '.content-table-spoof' ).first().width( width ); |
| 181 |
$( this ).width( $content.width() ); |
| 182 |
} ); |
| 183 |
} ); |
| 184 |
} ); |