User:Nihiltres/nothingthree.js

/*

This nothingthree library is a collection of functions that are probably useful. They're created

or collected (as noted) by Nihiltres for his use on Wikipedia, and you can use them as you see fit.

There is no guarantee that they work; Nihiltres only tests them under Safari on Mac OS, though will

occasionally correct or minimize bugs that occur on other browsers. Enjoy! Note the importScript

activator at the bottom. You'll need a user nothingthree-config.js file as well as importing this.

  • /

/*global nothingthree: true, importScript, importStylesheetURI, jQuery, mw */

mw.loader.using(["mediawiki.util", "oojs-ui.styles.icons-editing-list"], function () {

window.nothingthree = {

settings: { //Holds nothingthree globals and defaults

topsHidden: false, //Tops start out shown, so this has to be false to start.

pageRCloaded: false, //Are revisions loaded? Has to be false to start.

specificAutoWatchNamespaces: [1, 2, 3, 8, 9], //Default auto-watch namespaces

monthlist: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"], //default names for months, allows for basic i18n

dateOrder: mw.user.options.get("date") //Default date order. Iff "mdy", nothingthree.util.stringifyDate's output changes slightly.

}, //end settings

util: {

linkFix: function (targetId, replacementText) { // Tab name fixer; drills down to bottom first-child of targeted ID, then replaces its text.

var drilldown = jQuery("#".concat(targetId));

while (drilldown.children().length > 0) {

drilldown = drilldown.children().first();

}

drilldown.text(replacementText); //jQuery.text() natively escapes HTML, AFAICT

}, //end linkfix

isMobile: function () { //really rather basic mobile-device detection here, but can be improved as needed. Mostly useful for tidy code.

return !!(navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i) || (navigator.userAgent.match(/Android/i)));

}, //end isMobile

parseDate: function (dateString) { //Turns the date strings that the MW API likes to use (e.g. "2011-04-06T22:24:52Z") into JS dates. It assumes everything's in UTC, and expects a string for input.

var dateParts = /^\s*(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d)Z\s*$/.exec(dateString);

if (dateParts) {

return new Date(Date.UTC(+dateParts[1], +dateParts[2] - 1, +dateParts[3], +dateParts[4], +dateParts[5], +dateParts[6]));

}

return new Date(NaN); //In case dateString isn't what we're expecting…

}, //end parseDate

stringifyDate: function (theDate) { //Turns a date object into a pretty UTC output string.

var dateParts, timeParts;

timeParts = ("0" + theDate.getUTCHours()).slice(-2) + ":" + ("0" + theDate.getUTCMinutes()).slice(-2);

if (nothingthree.settings.dateOrder === "mdy") {

dateParts = nothingthree.settings.monthlist[theDate.getUTCMonth()] + " " + theDate.getUTCDate() + ", " + theDate.getUTCFullYear();

} else {

dateParts = theDate.getUTCDate() + " " + nothingthree.settings.monthlist[theDate.getUTCMonth()] + " " + theDate.getUTCFullYear();

}

return (timeParts + ", " + dateParts + " (UTC)");

}, //end stringifyDate

formatInt: function (num) { //Formats integers with commas

var numString = num.toString();

while (/(\d+)(\d{3})/.test(numString)) {

numString = numString.replace(/(\d+)(\d{3})/, "$1,$2");

}

return numString;

}, //end formatInt

addPortletLink: function (obj) {

var $item, $portlet, $list;

if (!obj || !obj.portlet || !obj.portlet.jquery || !obj.portlet.length || !obj.text || (!obj.href && !obj.function)) {

return null;

}

$item = jQuery("

  • ").attr("id", (obj.id || null));

    $item.addClass("mw-list-item");

    $item.children().text(obj.text).attr({

    href: (obj.href || "#"),

    title: (obj.tooltip || null),

    accesskey: (obj.accesskey || null)

    }).updateTooltipAccessKeys();

    $portlet = obj.portlet.first();

    //if ($portlet.hasClass("vectorTabs")) {

    $item.find("a").wrapInner("");

    //}

    if ($portlet.hasClass("vector-menu-tabs")) {

    $item.addClass("vector-tab-noicon");

    }

    $list = $portlet.find("ul").first();

    if ($list.length === 0) {

    $list = jQuery("