Debian Codegraph @

skins/Timeless/resources/main.js

raw symbols SCIP
1 $( function () {
2 // sidebar-chunk only applies to desktop-small, but the toggles are hidden at
3 // other resolutions regardless and the css overrides any visible effects.
4 var $dropdowns = $( '#personal, #p-variants-desktop, .sidebar-chunk' );
5
6 /**
7 * Desktop menu click-toggling
8 *
9 * We're not even checking if it's desktop because the classes in play have no effect
10 * on mobile regardless... this may break things at some point, though.
11 */
12
13 /**
14 * Close all dropdowns
15 */
16 function closeOpen() {
17 $dropdowns.removeClass( 'dropdown-active' );
18 }
19
20 /**
21 * Click behaviour
22 */
23 $dropdowns.on( 'click', function ( e ) {
24 // Check if it's already open so we don't open it again
25 // eslint-disable-next-line no-jquery/no-class-state
26 if ( $( this ).hasClass( 'dropdown-active' ) ) {
27 if ( $( e.target ).closest( $( 'h2, #p-variants-desktop h3' ) ).length > 0 ) {
28 // treat reclick on the header as a toggle
29 closeOpen();
30 }
31 // Clicked inside an open menu; don't do anything
32 } else {
33 closeOpen();
34 e.stopPropagation(); // stop hiding it!
35 $( this ).addClass( 'dropdown-active' );
36 }
37 } );
38 $( document ).on( 'click', function ( e ) {
39 if ( $( e.target ).closest( $dropdowns ).length > 0 ) {
40 // Clicked inside an open menu; don't close anything
41 } else {
42 closeOpen();
43 }
44 } );
45 } );
46
47 mw.hook( 'wikipage.content' ).add( function ( $content ) {
48 // Gotta wrap them for this to work; maybe later the parser etc will do this for us?!
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 * Set up borders for experimental overflowing table scrolling
54 *
55 * I have no idea what I'm doing.
56 *
57 * @param {jQuery} $table
58 */
59 function setScrollClass( $table ) {
60 var $tableWrapper = $table.parent(),
61 // wtf browser rtl implementations
62 scroll = Math.abs( $tableWrapper.scrollLeft() );
63
64 $tableWrapper.parent()
65 // 1 instead of 0 because of weird rtl rounding errors or something
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 * Mark overflowed tables for scrolling
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 // Set up sticky captions
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 * Sticky scrollbars maybe?!
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 // Assemble our silly crap and add to page
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 * Scoll table when scrolling scrollbar and visa-versa lololol wut
132 */
133 $content.find( '.content-table' ).on( 'scroll', function () {
134 // Only do this here if we're not already mirroring the spoof
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 // Only do this here if we're not already mirroring the table
143 // eslint-disable-next-line no-jquery/no-class-state
144 if ( !$( this ).hasClass( 'inactive' ) ) {
145 $mirror.scrollLeft( $( this ).scrollLeft() );
146 }
147 } );
148
149 /**
150 * Set active when actually over the table it applies to...
151 */
152 function determineActiveSpoofScrollbars() {
153 $content.find( '.overflowed .content-table' ).each( function () {
154 var $scrollbar = $( this ).siblings( '.content-table-scrollbar' ).first();
155
156 // Skip caption
157 var captionHeight = $( this ).find( 'caption' ).outerHeight() || 0;
158 if ( captionHeight ) {
159 // Pad slightly for reasons
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 * Make sure tablespoofs remain correctly-sized?
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 } );