MediaWiki:Common.js

From PSP2i Wiki

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/* Any JavaScript here will be loaded for all users on every page load. */

/* Collapsible tables; reimplemented with mw-collapsible
	 * Styling is also in place to avoid FOUC
	 *
	 * Allows tables to be collapsed, showing only the header. See [[Help:Collapsing]].
	 * @version 3.0.0 (2018-05-20)
	 * @source https://www.mediawiki.org/wiki/MediaWiki:Gadget-collapsibleTables.js
	 * @author [[User:R. Koot]]
	 * @author [[User:Krinkle]]
	 * @author [[User:TheDJ]]
	 * @deprecated Since MediaWiki 1.20: Use class="mw-collapsible" instead which
	 * is supported in MediaWiki core. Shimmable since MediaWiki 1.32
	 *
	 * @param {jQuery} $content
	 * */
	function makeCollapsibleMwCollapsible( $content ) {
		var $tables = $content
			.find( 'table.collapsible:not(.mw-collapsible)' )
			.addClass( 'mw-collapsible' );

		$.each( $tables, function ( index, table ) {
			// mw.log.warn( 'This page is using the deprecated class collapsible. Please replace it with mw-collapsible.');
			if ( $( table ).hasClass( 'collapsed' ) ) {
				$( table ).addClass( 'mw-collapsed' );
				// mw.log.warn( 'This page is using the deprecated class collapsed. Please replace it with mw-collapsed.');
			}
		} );
		if ( $tables.length > 0 ) {
			mw.loader.using( 'jquery.makeCollapsible' ).then( function () {
				$tables.makeCollapsible();
			} );
		}
	}
	mw.hook( 'wikipage.content' ).add( makeCollapsibleMwCollapsible );

	/**
	 * Add support to mw-collapsible for autocollapse, innercollapse and outercollapse
	 *
	 * Maintainers: TheDJ
	 */
	function mwCollapsibleSetup( $collapsibleContent ) {
		var $element,
			$toggle,
			autoCollapseThreshold = 2;
		$.each( $collapsibleContent, function ( index, element ) {
			$element = $( element );
			if ( $element.hasClass( 'collapsible' ) ) {
				$element.find( 'tr:first > th:first' ).prepend( $element.find( 'tr:first > * > .mw-collapsible-toggle' ) );
			}
			if ( $collapsibleContent.length >= autoCollapseThreshold && $element.hasClass( 'autocollapse' ) ) {
				$element.data( 'mw-collapsible' ).collapse();
			} else if ( $element.hasClass( 'innercollapse' ) ) {
				if ( $element.parents( '.outercollapse' ).length > 0 ) {
					$element.data( 'mw-collapsible' ).collapse();
				}
			}
			// because of colored backgrounds, style the link in the text color
			// to ensure accessible contrast
			$toggle = $element.find( '.mw-collapsible-toggle' );
			if ( $toggle.length ) {
				// Make the toggle inherit text color
				if ( $toggle.parent()[ 0 ].style.color ) {
					$toggle.find( 'a' ).css( 'color', 'inherit' );
				}
			}
		} );
	}

	mw.hook( 'wikipage.collapsibleContent' ).add( mwCollapsibleSetup );


$(".more_info").click(function () {
    var $title = $(this).find(".title");
    if (!$title.length) {
        $(this).append('<span class="title">' + $(this).attr("title") + '</span>');
    } else {
        $title.remove();
    }
});

// FloatingTableHeader.js
// From https://archive.is/zJzTe, 
// with very slight modifications
// ported and adapted for Wikia Oasis by mfaizsyahmi
// ---
// Heavily modified by Paradox-

//---------------------------------------- Floatheader

$("table.floatheader").each(function() {
  $(this).wrap("<div class=\"divTableWithFloatingHeader\" style=\"position:relative\"></div>");
  
  //--- If there is no thead, put all header rows into a thead
  //--- Code taken from jQuery tablesorter (& modified)
  //--- https://phabricator.wikimedia.org/source/mediawiki/browse/master/resources/src/jquery.tablesorter/jquery.tablesorter.js$283
  if (!$("thead", this).length) {
    var thead = $("<thead>");
    $("> tbody > tr", this).each(function() {
      if ($("> td", this).length) {
        return false;
      }
      thead.append(this);
    });
    $("> tbody", this).before(thead);
  }
  
  var originalHeader = $("thead", this);
  var clonedHeader = originalHeader.clone().hide();
  originalHeader.after(clonedHeader);
  
  originalHeader.addClass("tableFloatingHeaderOriginal");
  clonedHeader.addClass("tableFloatingHeader");
  clonedHeader.css("position", "absolute");
  clonedHeader.css("top", "0");
  clonedHeader.css("left", $(this).css("margin-left"));
  
  //--- Floating Header cell fixes
  $("th", clonedHeader).each(function() {
    //--- Remove Sort Buttons
    if ($(this).hasClass("headerSort")) {
      $(this).removeClass("headerSort").removeAttr("tabindex").removeAttr("title");
    }
    
    //--- Remove mw-collapsible Toggle Button
    $(".mw-collapsible-toggle", this).remove();
    
    //--- Remove Transparency
    var bgcolors = $(this).css("background-color");
    if (bgcolors.charAt(3) == "a" && bgcolors != "rgba(0, 0, 0, 0)") {
      $(this).css("background-color", "rgb(" + bgcolors.match(/\d+/g).slice(0, 3).join(", ") + ")");
    }
  });
});

UpdateTableHeaders();
$(window).scroll(UpdateTableHeaders).resize(UpdateTableHeaders);

function UpdateTableHeaders() {
  $("div.divTableWithFloatingHeader").each(function() {
    var offset = $(this).offset();
    var scrollTop = $(window).scrollTop();
    var floatingHeader = $(".tableFloatingHeader", this);
    var topNavOffset = $(".fandom-sticky-header").height() || $(".global-navigation").height() || 0;
    console.log(topNavOffset)
    if (scrollTop > offset.top - topNavOffset && scrollTop + topNavOffset < offset.top + $(this).height()) {
      var originalHeader = $(".tableFloatingHeaderOriginal", this);
      
      floatingHeader.css("top", Math.min(scrollTop - offset.top + topNavOffset, $(this).height() - floatingHeader.height() - $("tr:last", this).height()) + "px").show();
      
      /* hack for cellpadding and cellspacing attributes: tr's width is increased by 2*cellspacing, and each header cell is reduced by 2*cellpadding */
      var insidetable = $(this).find($(".floatheader")).first();
      var cellspacing = +$(insidetable).attr("cellspacing") || 0;
      var cellpadding = +$(insidetable).attr("cellpadding") || 0;
      
      //--- Copy cell width & horizontal padding from original header -- only way to make the cells obey their width
      $("th", floatingHeader).each(function(i) {
        var oh = $("th", originalHeader).eq(i);
        $(this).width((Number(oh.css("width").replace("px","")) - 2*cellpadding) + "px");
        $(this).css("padding-left", oh.css("padding-left"));
        $(this).css("padding-right", oh.css("padding-right"));
      });
      
      //--- Copy width from original thead -- Add 1px to wikitables
      floatingHeader.css("width", originalHeader.width() + 2*cellspacing + +$("> table", this).hasClass("wikitable") + "px");
    }
    else {
      floatingHeader.hide();
    }
  });
}