MediaWiki:Common.js

From Truck Simulator 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.
/* Any JavaScript here will be loaded for all users on every page load. */

mw.loader.getScript( 'https://commons.wiki.gg/index.php?title=MediaWiki:Common-base.js&action=raw&ctype=text/javascript' );

	/*********************
	* WikiEditor Buttons *
	*********************/
	
	// Adopted from https://palia.wiki.gg/wiki/MediaWiki:Common.js by TheElm */
	
	var nameAndAmountMatch = (function( $textarea, tag, countFirst ) {
		// Get the selected text
		var selected = $textarea.textSelection( 'getSelection' );
		if ( selected ) {
			var match = selected.match(/^(?:(\d+) )?(.+)(?: (\d+))?$/);
			var amount;
			var text = match[2];
			
			if ( match[1] ) {
				amount = match[1];
				
				// Append the second match if there is one
				if (match[3]) {
					text += ' ' + match[3];
				}
			} else {
				amount = match[3];
			}
			
			if ( amount ) {
				if ( countFirst ) {
					tag += '|' + amount + '|' + text;
				} else {
					tag += '|' + text + '|' + amount;
				}
			} else {
				tag += '|' + text;
			}
			
			$textarea.textSelection( 'replaceSelection', '{{' + tag + '}}' );
		}
	});
	
	mw.hook('wikiEditor.toolbarReady').add(function( $textarea ) {
		$textarea.wikiEditor( 'addToToolbar', {
			section: 'main',
			groups: {
				templates: {
					tools: {
						Templates: {
							label: 'Templates',
							type: 'select',
							list: {
								Update: {
									label: '{{Update}}',
									action: {
										type: 'encapsulate',
										options: {
											pre: '{{Update|',
											post: '}}'
										}
									}
								},
								Future: {
									label: '{{Future}}',
									action: {
										type: 'encapsulate',
										options: {
											pre: '{{Future|',
											post: '}}'
										}
									}
								},
								Removed: {
									label: '{{Removed}}',
									action: {
										type: 'encapsulate',
										options: {
											pre: '{{Removed|',
											post: '}}'
										}
									}
								},
								Unconfirmed: {
									label: '{{Unconfirmed}}',
									action: {
										type: 'encapsulate',
										options: {
											pre: '{{Unconfirmed|',
											post: '}}'
										}
									}
								},
								Unused: {
									label: '{{Unused}}',
									action: {
										type: 'encapsulate',
										options: {
											pre: '{{Unused|',
											post: '}}'
										}
									}
								},
							    Cleanup: {
									label: '{{Cleanup}}',
									action: {
										type: 'encapsulate',
										options: {
											pre: '{{Cleanup|',
											post: '}}'
										}
									}
								},
								Citation: {
									label: '{{Citation required}}',
									action: {
										type: 'encapsulate',
										options: {
											pre: '{{Citation required|',
											post: '}}'
										}
									}
								}
							}
						}
					}
				}
			}
		});
	});	
/*
==========================================
Scroll detector for the fadeInUp animation
==========================================
*/
$( document ).ready(function() { 

$(window).scroll(function() { 
	$('.fadeInUpObject').each(function(){ 
	var imagePos = $(this).offset().top; 
	
	var topOfWindow = $(window).scrollTop(); 
		if (imagePos < topOfWindow+600) { 
			$(this).addClass("fadeInUp"); 
		}
			
	});
		
});

});


/* 
==========================================
Scroll detector for the pullDown animation 
==========================================
*/ 

$( document ).ready(function() { //when the page has loaded,

$(window).scroll(function() { 
	$('.pullDownObject').each(function(){ //For each element with the class "pullDownObject",
	var imagePos = $(this).offset().top; 
	
	var topOfWindow = $(window).scrollTop(); //(Defining topOfWindow)
		if (imagePos < topOfWindow+600) { //If the element is 600px away from topOfWindow,
			$(this).addClass("pullDown"); //Add the "pullDown" class to it, giving it the animation
		}
			
	});
		
});

});

/* 
==========================================
 Note that the pullDown class is defined elsewhere, in the wiki's CSS page
==========================================
*/ 

// [[Template:Time]], [[Template:Countdown]]
/* Scripts pcj and Surafbrov from https://warcraft.wiki.gg/wiki/MediaWiki:Common.js */

function timeInit() {
	function getDate(s) {
		s = s && s.match(/(\d{4})-(\d{2})-(\d{2}) (\d{1,2}):(\d{2})/);
		return s && Date.UTC(parseInt(s[1]), parseInt(s[2])-1, parseInt(s[3]), parseInt(s[4]), parseInt(s[5]));
	}
	function updateCountdown() {
		var $this = $(this), t = getDate($this.data("jst-time")), now = new Date();
		if (t && (t > now)) {
			var $d = $this.find(".jst-days"), $h = $this.find(".jst-hours"), $m = $this.find(".jst-minutes"), $s = $this.find(".jst-seconds");
			var ofs = (t - now)/1000 | 0 + ($s.length ? 0 : 60), d = (ofs / 86400) | 0, h = (ofs / 3600) | 0, m = (ofs / 60) | 0, s = ofs % 60;
			if ($d.length) h %= 24;
			if ($h.length) m %= 60;
			$d.toggleClass("jst-active", d).find(".jst-value").text(d);
			$h.toggleClass("jst-active", d || h).find(".jst-value").text(h);
			$m.toggleClass("jst-active", d || h || m).find(".jst-value").text(m);
			$s.toggleClass("jst-active", true).find(".jst-value").text(s);
			$this.addClass("jst-active");
		} else {
			$this.removeClass("jst-active");
			$this.text($this.data('jst-text-over') || "");
		}
	}
	function updateCountdowns() {
		$(".jst-countdown.jst-active").each(updateCountdown);
		if ($(".jst-countdown.jst-active").length) setTimeout(updateCountdowns, 1001);
	}
	$(".jst-countdown").addClass("jst-active");
	$(".jst-countdown .jst-alternative").remove();
	$(".jst-days > .jst-label").text("days");
	$(".jst-hours > .jst-label").text("hours");
	$(".jst-minutes > .jst-label").text("minutes");
	$(".jst-seconds > .jst-label").text("seconds");
	updateCountdowns();

	$(".jst-abstime").each(function() {
		var $this = $(this), t1 = getDate($this.data("jst-time")), t2 = getDate($this.data("jst-time2")), ta = getDate($this.data("jst-anchor"));
		if (!t1) return;
		var t1d = new Date(t1), nowDate = ta ? (new Date(ta)).toDateString() : (new Date()).toDateString();
		$this.text((t1d.toDateString() == nowDate ? t1d.toLocaleTimeString() : (t1d.toLocaleDateString() + ", " + t1d.toLocaleTimeString())) + (t2 ? " – " + (new Date(t2)).toLocaleTimeString() : ""));
	});
}

$(function() {
	timeInit();
	});