MediaWiki:Common.js: Difference between revisions

From MWStake
Jump to navigation Jump to search
No edit summary
(Added Time Zone Converter)
 
(One intermediate revision by one other user not shown)
Line 20: Line 20:
});
});


/*
 
jQuery(document).ready( function() {
/**
  jQuery('.srf-eventcalendar').each(function() {
* A Time Zone Converter based off Michael Schönitzer
     jQuery(this).on('srf.eventcalendar.eventRender', function(event, handler) {
* https://meta.wikimedia.org/wiki/User:Michael_Sch%C3%B6nitzer_(WMDE)/timezoneconverter
      handler.element.find('.fc-event-title').each(function(index, element) {
* https://meta.wikimedia.org/wiki/User:Michael_Sch%C3%B6nitzer_(WMDE)/timezoneconverter.js
        jQuery.ajax({
*/
          type: "POST",
function numberToSigndString(num) {
          url: wgScriptPath + "/api.php",
if (num <= -10) {
          data:  {
return String(num);
            action: "getSemanticTitle",
} else if (num < 0) {
            format: "json",
return "-0" + String(-num);
            titles: element.innerHTML
} else if (num < 10) {
          },
return "+0" + String(num);
          dataType: "json",
} else {
          success: function(data) {
return "+" + String(num);
            element.innerHTML = data.getSemanticTitle[0].result;
}
          }
}
        } );
 
      } );
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'});
    });
});

Latest revision as of 12:33, 10 June 2020

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'});
    });
});