User:Lunarity/global.js

"use strict";

/** * Utility function. * Determines whether the given DOMElement is in the document tree (true), or * detached from it (false). 2nd param is optional but can be used to check if * the node is descended from an arbitrary node instead of window.document. */ if (!window.isNodeInDocument) { window.isNodeInDocument = function(elem, doc) { if (!elem) return false; if (!doc) doc = window.document; do { if (elem === doc) return true; } while (elem = elem.parentNode); return false; } }

/** * JavaScript to Toggle the Wikia Rail on and off. * Adds a button to the toolbar that allows expand/shrink. * * Exploits oasis-one-column for formatting. one-column occurs normally * on special pages like Edit, RecentChanges, Diff, etc so we don't do * anything on those pages. (Edit still has the Rail but it provides a * built-in collapser for it) */

if (skin === "oasis") { $(function($) {   var _expandMsg = "Expand Content \u2501\u2501>", // 2501 is box-drawing dash        _shrinkMsg = "Shrink Content <\u2501\u2501";

function expandContent {       $("#WikiaRail").hide; $("#WikiaPageHeader").append($("#WikiaSearch").detach); $(document.body).addClass("oasis-one-column"); }   function shrinkContent {       $(document.body).removeClass("oasis-one-column"); $("#WikiaRail").prepend($("#WikiaSearch").detach).show; }   function handleClick(event) {       var $this = $(event.target); if ($this.text === _expandMsg) { expandContent; $this.text(_shrinkMsg); } else { shrinkContent; $this.text(_expandMsg); }       return false; }

if (!$(document.body).hasClass("oasis-one-column")) { var toolbar = $("#WikiaFooter > .toolbar > .tools"); if (toolbar.length) { $("", {               href: "#",                text: _expandMsg,            }).click(handleClick).appendTo("").parent.appendTo(toolbar); }   } }); }

/** * Fix the MediaWiki diffs (including AJAX ones). * Display the diffs in monospace font with white-space preservation. * We can't just use CSS for this because MediaWiki generates bad diffs; there are * unnecessary new-lines added at the start and end of each modified line in the * diff which obviously end up being shown unless we fix it. * This script strips the unnecessary new-line characters as well as setting * the inline styles to monospace/pre-wrap (Easier to manage all in one place). * * WARN: Live script. On Edit pages, it polls every few milliseconds to see if a *      preview pop-up is being shown. It will then fix the pop-up as well. */ $(function($) {   var match_selectors = 'td.diff-addedline div, td.diff-deletedline div, td.diff-context div';    function fixDiffNewlines($aDiffTable)    {        var rows = $(match_selectors, $aDiffTable);        $aDiffTable.hide; // Minimise reflows by setting display:none        var i = rows.length;        while (i--) {            /*rows[i].style.fontFamily = 'monospace';            rows[i].style.whiteSpace = 'pre-wrap';*/            rows[i].innerHTML = rows[i].innerHTML.replace(/(^\s*?\n|\n\s*$)/g, "");        }        $aDiffTable.show; // Reset    }    var last_popup = null;    function pollChanges    {        // Only do the look-up (heavy weight) if a pop-up wasn't open last time        // we checked, or the one we do know about has been closed (removed from // the DOM). [We don't want to fix the table more than once.]       if (!last_popup || !window.isNodeInDocument(last_popup)) {            var diffTable = $('.ArticlePreview table.diff');            if (diffTable.length) {                fixDiffNewlines(diffTable);                last_popup = diffTable[0];            } else {                // Avoid keeping the table alive after the pop-up is closed.                last_popup = null;            }        }    }    // Custom stylesheet.    var t = document.createElement('style');    t.setAttribute("type", "text/css");    t.appendChild(document.createTextNode(match_selectors + "{"        + "font-family: monospace;"        + "white-space: pre-wrap;"        + "}"));    document.head.appendChild(t);    t = null;    // If we're on a diff page then run the fix.    // Regex uses look-ahead (?= means "followed by"). The trick is that the line    // is not *consumed* by the look-ahead matches so the 2nd (or more) // look-aheads get to walk over the same characters. if (/^[^\?]+(?=.*[\?&]diff)(?=.*[\?&]oldid=)/.test(window.location.href)) { // :first avoids trickery, like tables in articles with .diff class t = $("#WikiaArticle table.diff").filter(':first'); if (t.length) { fixDiffNewlines(t); }   }    // If we're on an edit page then we need to watch for pop-up Diff overlays // mw.config is MediaWiki 1.18+, global is 1.16.1 if (window.wgIsEditPage === true || (window.mw.config && window.mw.config.get('wgAction') === 'edit')) { window.setInterval(pollChanges, 100); } });