MediaWiki:Common.js

From Aethermancer Wiki
Jump to navigation Jump to search

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.
mw.loader.getScript( 'https://commons.wiki.gg/index.php?title=MediaWiki:Common-base.js&action=raw&ctype=text/javascript' ).then(function(){
////////////////////////////////////////////////////////////////////////////////


/**
 * image size fix for <gallery>
 */
$(function(){
	$(".mw-gallery-traditional .thumb").each(function(){
		var $this = $(this);
		/* $this.css('display', 'flex'); // do this in common.css: .mw-gallery-traditional .thumb{ display: flex; } */
		$this.css('height', $this.height()+'px').find('>div').css('margin', 'auto');
		var $img = $this.find('img').first();
		if(!$img.length){
			return;
		}
		var $width = $img.attr('width') - 0; //cast to number.
		var $filewidth = $img.attr('data-file-width') - 0;
		if($filewidth < $width){
			var $fileheight = $img.attr('data-file-height');
			if( $filewidth * 2 > $width){
				$img.attr({'width': $filewidth, 'height': $fileheight});
			}else{
				$img.attr({'width': $filewidth*2, 'height': $fileheight*2});
			}
		}
	});
	$('.slider').each(function(){
		var $box = $(this);
		$prev = $('<div class="prev"/>').appendTo($box);
		$next = $('<div class="next"/>').appendTo($box);
		$box = $box.find('ul').first();
		var $current = $box.find('li').first();
		$box.find('li').each(function(){
			var $this = $(this);
			$this.data('left', $this.position().left);
			$this.css('background-image', 'url('+$this.find('img').first().attr('src')+')');
		});
		
		var timeout;
		
		var switchTo = function($item){
			$box.css('transform', 'translateX(-'+$item.data('left')+'px)');
		}
		var goNext = function(){
				clearTimeout(timeout);
				$current = $current.next('li');
				if($current.length === 0){
					$current = $box.find('li').first();
				}
				switchTo($current);
				timeout = setTimeout(goNext, 5000);
		};
		var goPrev = function(){
				clearTimeout(timeout);
				$current = $current.prev('li');
				if($current.length === 0){
					$current = $box.find('li').last();
				}
				switchTo($current);
				timeout = setTimeout(goNext, 5000);
		};

		timeout = setTimeout(goNext, 5000);
		
		$prev.on('click', goPrev);
		$next.on('click', goNext);
	});
});


////////////////////////////////////////////////////////////////////////////////
/*end of mw.loader.getScript().then callback*/ });

/* Any JavaScript here will be loaded for all users on every page load. */

	/**** mobile views ****/
	if ($(window).width() <= 768) {
		/* Wrap Tables for scrolling in mobile views 
		$('table').not('.mw-collapsible').not('.pi-horizontal-group').wrap(
			'<div style="max-width: 90vw; overflow-x: scroll" class="table-scroll-shadow"></div>'
		);*/
		
		/* Hide heading if infobox exists - shows duplicate information 
		var infobox = $('.portable-infobox');
		if (infobox.length > 0) {
		    $('.firstHeading').hide();
		}*/
	} 
    else {
        $(window).on("scroll", function() {
            var nav = document.getElementById("mw-panel");

            if (window.scrollY >= nav.offsetTop) {
                nav.classList.add("sticky");
            } else {
                nav.classList.remove("sticky");
            }
        });
    }
	
$(document).ready(function() {
	// Ctrl+I for italic
	$(document).keydown(function(e) {
		if (e.ctrlKey && e.keyCode === 73) {
			var textarea = $('#wpTextbox1').get(0);
			var startPos = textarea.selectionStart;
			var endPos = textarea.selectionEnd;
			var selectedText = textarea.value.substring(startPos, endPos);
			var newText = "''" + selectedText + "''";
			var beforeText = textarea.value.substring(0, startPos);
			var afterText = textarea.value.substring(endPos, textarea.value.length);
			textarea.value = beforeText + newText + afterText;
			textarea.setSelectionRange(endPos + 4, endPos + 4);
			e.preventDefault();
		}
	});
	
	// Ctrl+B for bold
	$(document).keydown(function(e) {
		if (e.ctrlKey && e.keyCode === 66) {
			var textarea = $('#wpTextbox1').get(0);
			var startPos = textarea.selectionStart;
			var endPos = textarea.selectionEnd;
			var selectedText = textarea.value.substring(startPos, endPos);
			var newText = "'''" + selectedText + "'''";
			var beforeText = textarea.value.substring(0, startPos);
			var afterText = textarea.value.substring(endPos, textarea.value.length);
			textarea.value = beforeText + newText + afterText;
			textarea.setSelectionRange(endPos + 4, endPos + 4);
			e.preventDefault();
		}
	});
});

/* show search dropdown results on mobile */
/* by vGhost on wiki.gg */
if ($(window).width() < 1000) {
	var $searchInput = $( 'div#simpleSearch #searchInput,div#simpleSearch input' );
	$searchInput.on('input',function(e){
		//trigger keydown with e object
		$(this).trigger(jQuery.Event('keydown', { 
			keyCode: e.keyCode,
			which: e.which, 
		}));
		$(this).trigger(jQuery.Event('keypress', { 
			keyCode: e.keyCode,
			which: e.which, 
		}));
	})
}