MediaWiki:Timeless.js: Difference between revisions

From Game Wiki - VortanMU
Created page with "MediaWiki:Timeless.js – comportamentos do menu, sem tocar no JS do tema: global mw, $: mw.loader.using( ['mediawiki.util'] ).then( function () { $(function () { const $win = $(window); const isMobile = () => window.matchMedia('(max-width: 850px)').matches; // raízes de navegação do Timeless const $menus = $('#mw-site-navigation .sidebar-inner, #mw-related-navigation .sidebar-inner'); function markHasSubmenu() { $menus.find(..."
 
No edit summary
Line 1: Line 1:
/* MediaWiki:Timeless.js  – comportamentos do menu, sem tocar no JS do tema */
$( function () {
/* global mw, $ */
// sidebar-chunk only applies to desktop-small, but the toggles are hidden at
// other resolutions regardless and the css overrides any visible effects.
var $dropdowns = $( '#personal, #p-variants-desktop, .sidebar-chunk' );


mw.loader.using( ['mediawiki.util'] ).then( function () {
/**
  $(function () {
* Desktop menu click-toggling
    const $win = $(window);
*
* We're not even checking if it's desktop because the classes in play have no effect
* on mobile regardless... this may break things at some point, though.
*/


    const isMobile = () => window.matchMedia('(max-width: 850px)').matches;
/**
* Close all dropdowns
*/
function closeOpen() {
$dropdowns.removeClass( 'dropdown-active' );
}


    // raízes de navegação do Timeless
/**
    const $menus = $('#mw-site-navigation .sidebar-inner, #mw-related-navigation .sidebar-inner');
* Click behaviour
*/
$dropdowns.on( 'click', function ( e ) {
// Check if it's already open so we don't open it again
// eslint-disable-next-line no-jquery/no-class-state
if ( $( this ).hasClass( 'dropdown-active' ) ) {
if ( $( e.target ).closest( $( 'h2, #p-variants-desktop h3' ) ).length > 0 ) {
// treat reclick on the header as a toggle
closeOpen();
}
// Clicked inside an open menu; don't do anything
} else {
closeOpen();
e.stopPropagation(); // stop hiding it!
$( this ).addClass( 'dropdown-active' );
}
} );
$( document ).on( 'click', function ( e ) {
if ( $( e.target ).closest( $dropdowns ).length > 0 ) {
// Clicked inside an open menu; don't close anything
} else {
closeOpen();
}
} );
} );


    function markHasSubmenu() {
mw.hook( 'wikipage.content' ).add( function ( $content ) {
      $menus.find('li').each(function () {
// Gotta wrap them for this to work; maybe later the parser etc will do this for us?!
        const $li = $(this);
$content.find( 'div > table:not( table table )' ).wrap( '<div class="content-table-wrapper"><div class="content-table"></div></div>' );
        if ($li.children('ul.submenu').length && !$li.hasClass('has-submenu')) {
$content.find( '.content-table-wrapper' ).prepend( '<div class="content-table-left"></div><div class="content-table-right"></div>' );
          $li.addClass('has-submenu');
        }
      });
    }


    // define automaticamente 2/3/4 colunas conforme quantidade de itens
/**
    function autoColumns() {
* Set up borders for experimental overflowing table scrolling
      $menus.find('ul.submenu').each(function () {
*
        const $u = $(this).removeClass('submenu-2-columns submenu-3-columns submenu-4-columns');
* I have no idea what I'm doing.
        const n = $u.children('li').length;
*
        if (n >= 20) $u.addClass('submenu-4-columns');
* @param {jQuery} $table
        else if (n >= 12) $u.addClass('submenu-3-columns');
*/
        else if (n >= 8) $u.addClass('submenu-2-columns');
function setScrollClass( $table ) {
      });
var $tableWrapper = $table.parent(),
    }
// wtf browser rtl implementations
scroll = Math.abs( $tableWrapper.scrollLeft() );


    // liga o comportamento de abrir/fechar
$tableWrapper.parent()
    function bindHandlers() {
// 1 instead of 0 because of weird rtl rounding errors or something
      // limpa handlers antigos para evitar duplicação
.toggleClass( 'scroll-left', scroll > 1 )
      $menus.find('li.has-submenu, li.has-submenu > a').off('.vtm');
.toggleClass( 'scroll-right', $table.outerWidth() - $tableWrapper.innerWidth() - scroll > 1 );
}


      if (isMobile()) {
$content.find( '.content-table' ).on( 'scroll', function () {
        // mobile: clique para abrir; segundo clique segue o link
setScrollClass( $( this ).children( 'table' ).first() );
        $menus.on('click.vtm', 'li.has-submenu > a', function (e) {
          const $li = $(this).parent();
          if (!$li.hasClass('active')) {
            e.preventDefault();
            $li.siblings('.active').removeClass('active');
            $li.addClass('active');
          } // se já estiver ativo, deixa o link seguir
        });


        // clique fora fecha
if ( $content.attr( 'dir' ) === 'rtl' ) {
        $(document).off('.vtmClose').on('click.vtmClose', function (e) {
$( this ).find( 'caption' ).css( 'margin-right', Math.abs( $( this ).scrollLeft() ) + 'px' );
          if ($(e.target).closest('li.has-submenu, #mw-site-navigation, #mw-related-navigation').length === 0) {
} else {
            $menus.find('li.has-submenu.active').removeClass('active');
$( this ).find( 'caption' ).css( 'margin-left', $( this ).scrollLeft() + 'px' );
          }
}
        });
} );
      } else {
        // desktop: hover com pequeno delay
        $menus.on('mouseenter.vtm', 'li.has-submenu', function () {
          $(this).addClass('active');
        }).on('mouseleave.vtm', 'li.has-submenu', function () {
          const $li = $(this);
          // delay curto para evitar "pisca"
          setTimeout(function () { $li.removeClass('active'); }, 120);
        });
      }
    }


    function init() {
/**
      markHasSubmenu();
* Mark overflowed tables for scrolling
      autoColumns();
*/
      bindHandlers();
function unOverflowTables() {
    }
$content.find( '.content-table > table' ).each( function () {
var $table = $( this ),
$wrapper = $table.parent().parent();
if ( $table.outerWidth() > $wrapper.outerWidth() ) {
$wrapper.addClass( 'overflowed' );
setScrollClass( $table );
} else {
$wrapper.removeClass( 'overflowed scroll-left scroll-right fixed-scrollbar-container' );
}
} );


    init();
// Set up sticky captions
    // reavalia em resize (debounce)
$content.find( '.content-table > table > caption' ).each( function () {
    $win.on('resize.vtm', mw.util.debounce(init, 200));
var $container, tableHeight,
  });
$table = $( this ).parent(),
});
$wrapper = $table.parent().parent();
 
if ( $table.outerWidth() > $wrapper.outerWidth() ) {
$container = $( this ).parents( '.content-table-wrapper' );
$( this ).width( $content.width() );
tableHeight = $container.innerHeight() - $( this ).outerHeight();
 
$container.find( '.content-table-left' ).height( tableHeight );
$container.find( '.content-table-right' ).height( tableHeight );
}
} );
}
 
unOverflowTables();
$( window ).on( 'resize', unOverflowTables );
 
/**
* Sticky scrollbars maybe?!
*/
$content.find( '.content-table' ).each( function () {
var $table, $tableWrapper, $spoof, $scrollbar;
 
$tableWrapper = $( this );
$table = $tableWrapper.children( 'table' ).first();
 
// Assemble our silly crap and add to page
$scrollbar = $( '<div>' ).addClass( 'content-table-scrollbar inactive' ).width( $content.width() );
$spoof = $( '<div>' ).addClass( 'content-table-spoof' ).width( $table.outerWidth() );
$tableWrapper.parent().prepend( $scrollbar.prepend( $spoof ) );
} );
 
/**
* Scoll table when scrolling scrollbar and visa-versa lololol wut
*/
$content.find( '.content-table' ).on( 'scroll', function () {
// Only do this here if we're not already mirroring the spoof
var $mirror = $( this ).siblings( '.inactive' ).first();
 
$mirror.scrollLeft( $( this ).scrollLeft() );
} );
$content.find( '.content-table-scrollbar' ).on( 'scroll', function () {
var $mirror = $( this ).siblings( '.content-table' ).first();
 
// Only do this here if we're not already mirroring the table
// eslint-disable-next-line no-jquery/no-class-state
if ( !$( this ).hasClass( 'inactive' ) ) {
$mirror.scrollLeft( $( this ).scrollLeft() );
}
} );
 
/**
* Set active when actually over the table it applies to...
*/
function determineActiveSpoofScrollbars() {
$content.find( '.overflowed .content-table' ).each( function () {
var $scrollbar = $( this ).siblings( '.content-table-scrollbar' ).first();
 
// Skip caption
var captionHeight = $( this ).find( 'caption' ).outerHeight() || 0;
if ( captionHeight ) {
// Pad slightly for reasons
captionHeight += 8;
}
 
var tableTop = $( this ).offset().top,
tableBottom = tableTop + $( this ).outerHeight(),
viewBottom = window.scrollY + document.documentElement.clientHeight,
active = tableTop + captionHeight < viewBottom && tableBottom > viewBottom;
$scrollbar.toggleClass( 'inactive', !active );
} );
}
 
determineActiveSpoofScrollbars();
$( window ).on( 'scroll resize', determineActiveSpoofScrollbars );
 
 
function showContent(id) {
const conteudos = document.querySelectorAll('.nav-content');
conteudos.forEach((div) => {
div.classList.remove('show-content');
});
document.getElementById(id).classList.add('show-content');
}
 
/**
* Make sure tablespoofs remain correctly-sized?
*/
$( window ).on( 'resize', function () {
$content.find( '.content-table-scrollbar' ).each( function () {
var width = $( this ).siblings().first().find( 'table' ).first().width();
$( this ).find( '.content-table-spoof' ).first().width( width );
$( this ).width( $content.width() );
} );
} );
} );

Revision as of 10:02, 6 November 2025

$( function () {
	// sidebar-chunk only applies to desktop-small, but the toggles are hidden at
	// other resolutions regardless and the css overrides any visible effects.
	var $dropdowns = $( '#personal, #p-variants-desktop, .sidebar-chunk' );

	/**
	 * Desktop menu click-toggling
	 *
	 * We're not even checking if it's desktop because the classes in play have no effect
	 * on mobile regardless... this may break things at some point, though.
	 */

	/**
	 * Close all dropdowns
	 */
	function closeOpen() {
		$dropdowns.removeClass( 'dropdown-active' );
	}

	/**
	 * Click behaviour
	 */
	$dropdowns.on( 'click', function ( e ) {
		// Check if it's already open so we don't open it again
		// eslint-disable-next-line no-jquery/no-class-state
		if ( $( this ).hasClass( 'dropdown-active' ) ) {
			if ( $( e.target ).closest( $( 'h2, #p-variants-desktop h3' ) ).length > 0 ) {
				// treat reclick on the header as a toggle
				closeOpen();
			}
			// Clicked inside an open menu; don't do anything
		} else {
			closeOpen();
			e.stopPropagation(); // stop hiding it!
			$( this ).addClass( 'dropdown-active' );
		}
	} );
	$( document ).on( 'click', function ( e ) {
		if ( $( e.target ).closest( $dropdowns ).length > 0 ) {
			// Clicked inside an open menu; don't close anything
		} else {
			closeOpen();
		}
	} );
} );

mw.hook( 'wikipage.content' ).add( function ( $content ) {
	// Gotta wrap them for this to work; maybe later the parser etc will do this for us?!
	$content.find( 'div > table:not( table table )' ).wrap( '<div class="content-table-wrapper"><div class="content-table"></div></div>' );
	$content.find( '.content-table-wrapper' ).prepend( '<div class="content-table-left"></div><div class="content-table-right"></div>' );

	/**
	 * Set up borders for experimental overflowing table scrolling
	 *
	 * I have no idea what I'm doing.
	 *
	 * @param {jQuery} $table
	 */
	function setScrollClass( $table ) {
		var $tableWrapper = $table.parent(),
			// wtf browser rtl implementations
			scroll = Math.abs( $tableWrapper.scrollLeft() );

		$tableWrapper.parent()
			// 1 instead of 0 because of weird rtl rounding errors or something
			.toggleClass( 'scroll-left', scroll > 1 )
			.toggleClass( 'scroll-right', $table.outerWidth() - $tableWrapper.innerWidth() - scroll > 1 );
	}

	$content.find( '.content-table' ).on( 'scroll', function () {
		setScrollClass( $( this ).children( 'table' ).first() );

		if ( $content.attr( 'dir' ) === 'rtl' ) {
			$( this ).find( 'caption' ).css( 'margin-right', Math.abs( $( this ).scrollLeft() ) + 'px' );
		} else {
			$( this ).find( 'caption' ).css( 'margin-left', $( this ).scrollLeft() + 'px' );
		}
	} );

	/**
	 * Mark overflowed tables for scrolling
	 */
	function unOverflowTables() {
		$content.find( '.content-table > table' ).each( function () {
			var $table = $( this ),
				$wrapper = $table.parent().parent();
			if ( $table.outerWidth() > $wrapper.outerWidth() ) {
				$wrapper.addClass( 'overflowed' );
				setScrollClass( $table );
			} else {
				$wrapper.removeClass( 'overflowed scroll-left scroll-right fixed-scrollbar-container' );
			}
		} );

		// Set up sticky captions
		$content.find( '.content-table > table > caption' ).each( function () {
			var $container, tableHeight,
				$table = $( this ).parent(),
				$wrapper = $table.parent().parent();

			if ( $table.outerWidth() > $wrapper.outerWidth() ) {
				$container = $( this ).parents( '.content-table-wrapper' );
				$( this ).width( $content.width() );
				tableHeight = $container.innerHeight() - $( this ).outerHeight();

				$container.find( '.content-table-left' ).height( tableHeight );
				$container.find( '.content-table-right' ).height( tableHeight );
			}
		} );
	}

	unOverflowTables();
	$( window ).on( 'resize', unOverflowTables );

	/**
	 * Sticky scrollbars maybe?!
	 */
	$content.find( '.content-table' ).each( function () {
		var $table, $tableWrapper, $spoof, $scrollbar;

		$tableWrapper = $( this );
		$table = $tableWrapper.children( 'table' ).first();

		// Assemble our silly crap and add to page
		$scrollbar = $( '<div>' ).addClass( 'content-table-scrollbar inactive' ).width( $content.width() );
		$spoof = $( '<div>' ).addClass( 'content-table-spoof' ).width( $table.outerWidth() );
		$tableWrapper.parent().prepend( $scrollbar.prepend( $spoof ) );
	} );

	/**
	 * Scoll table when scrolling scrollbar and visa-versa lololol wut
	 */
	$content.find( '.content-table' ).on( 'scroll', function () {
		// Only do this here if we're not already mirroring the spoof
		var $mirror = $( this ).siblings( '.inactive' ).first();

		$mirror.scrollLeft( $( this ).scrollLeft() );
	} );
	$content.find( '.content-table-scrollbar' ).on( 'scroll', function () {
		var $mirror = $( this ).siblings( '.content-table' ).first();

		// Only do this here if we're not already mirroring the table
		// eslint-disable-next-line no-jquery/no-class-state
		if ( !$( this ).hasClass( 'inactive' ) ) {
			$mirror.scrollLeft( $( this ).scrollLeft() );
		}
	} );

	/**
	 * Set active when actually over the table it applies to...
	 */
	function determineActiveSpoofScrollbars() {
		$content.find( '.overflowed .content-table' ).each( function () {
			var $scrollbar = $( this ).siblings( '.content-table-scrollbar' ).first();

			// Skip caption
			var captionHeight = $( this ).find( 'caption' ).outerHeight() || 0;
			if ( captionHeight ) {
				// Pad slightly for reasons
				captionHeight += 8;
			}

			var tableTop = $( this ).offset().top,
				tableBottom = tableTop + $( this ).outerHeight(),
				viewBottom = window.scrollY + document.documentElement.clientHeight,
				active = tableTop + captionHeight < viewBottom && tableBottom > viewBottom;
			$scrollbar.toggleClass( 'inactive', !active );
		} );
	}

	determineActiveSpoofScrollbars();
	$( window ).on( 'scroll resize', determineActiveSpoofScrollbars );


	function showContent(id) {
		const conteudos = document.querySelectorAll('.nav-content');
		conteudos.forEach((div) => {
			div.classList.remove('show-content');
		});
		document.getElementById(id).classList.add('show-content');
	}

	/**
	 * Make sure tablespoofs remain correctly-sized?
	 */
	$( window ).on( 'resize', function () {
		$content.find( '.content-table-scrollbar' ).each( function () {
			var width = $( this ).siblings().first().find( 'table' ).first().width();
			$( this ).find( '.content-table-spoof' ).first().width( width );
			$( this ).width( $content.width() );
		} );
	} );
} );