MediaWiki:Common.js

From MWStake
Revision as of 12:33, 10 June 2020 by Bryandamon (talk | contribs) (Added Time Zone Converter)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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.
jQuery(document).ready(function($) {
     var pops = function( elems ) {
         for (var i=0; i<elems.length; i++) {
             if ( !(' '+elems[i].className+' ').match( / pops / ) ) continue;
             var anchs = elems[i].getElementsByTagName('a');
             for (var j=0; j<anchs.length; j++) anchs[j].target = '_blank';
         }
     };
     var bc = document.getElementById('bodyContent');
     var tags = ['span', 'div', 'table', 'td', 'th'];
     for (var i=0; i<tags.length; i++) pops( bc.getElementsByTagName( tags[i] ) );
});

jQuery(document).ready(function($) {
  jQuery('.formcancel').each(function(index) {
    var buttontext = jQuery(this).html();
    var html = '<a onclick="window.history.back();">' + buttontext + '</a>';
    jQuery(this).html(html);
  });
});


/**
 * A Time Zone Converter based off Michael Schönitzer
 * https://meta.wikimedia.org/wiki/User:Michael_Sch%C3%B6nitzer_(WMDE)/timezoneconverter
 * https://meta.wikimedia.org/wiki/User:Michael_Sch%C3%B6nitzer_(WMDE)/timezoneconverter.js
 */
 function numberToSigndString(num) {
	if (num <= -10) {
		return String(num);
	} else if (num < 0) {
		return "-0" + String(-num);
	} else if (num < 10) {
		return "+0" + String(num);
	} else {
		return "+" + String(num);
	}
}

mw.hook("wikipage.content").add(function ($content) {
	/* Check if TZC is used by the page */
    converter = $content[0].querySelector('#tzc-timezoneconverter');
    if (converter === null) { return; }
    
    /* get timezone and locale */
	var timezonename = Intl.DateTimeFormat().resolvedOptions().timeZone;
	var timezoneoffset = numberToSigndString(-new Date().getTimezoneOffset()/60);
	var locale = Intl.DateTimeFormat().resolvedOptions().locale;
	
	/* Display timezone and locale */
    if (converter.querySelector('#tzc-enabled') !== null) {
	    converter.querySelector('#tzc-enabled').style.display = "";
    }
    if (converter.querySelector('#tzc-disabled') !== null) {
    	converter.querySelector('#tzc-disabled').style.display = "none";
    }
    if (converter.querySelector('#tzc-timezonename') !== null) {
    	converter.querySelector('#tzc-timezonename').innerText = timezonename;
    }
    if (converter.querySelector('#tzc-timezoneoffset') !== null) {
    	converter.querySelector('#tzc-timezoneoffset').innerText = timezoneoffset;
    }
    if (converter.querySelector('#tzc-locale') !== null) {
    	converter.querySelector('#tzc-locale').innerText = locale;
    }
    
    /* Search and convert dates */
    var events = $content[0].querySelectorAll('.tzc-event');
    events.forEach(function(event) {
    	if (event.dataset.datetime === null ) { return }
		var date = new Date(Date.parse(event.dataset.datetime));
		var localdate;
		if (event.classList.contains("tzc-timeonly")) {
			localdate = date.toLocaleTimeString();
		} else if (event.classList.contains("tzc-dateonly")) {
			localdate = date.toLocaleDateString();
		} else if (event.classList.contains("tzc-daytime")) {
			localdate = date.toLocaleTimeString([], {weekday: "long"});
		} else if (event.classList.contains("tzc-dtime")) {
			localdate = date.toLocaleTimeString([], {weekday: "short"});
		} else if (event.classList.contains("tzc-houronly")) {
			localdate = date.toLocaleTimeString([], {hour: "numeric"});
		} else {
			localdate = date.toLocaleString();
		}
		/* remove seconds if zero */
		if (localdate.slice(-3) === ":00") {
			localdate = localdate.slice(0, -3);
		}
		event.innerText = localdate;
		event.title = date.toLocaleString([], {"timeZoneName": 'short'});
    });
});