MediaWiki:Gadget-timer.js

From Support 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.
// [[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();
	});