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($) {   // Arrow (symbol) for Unicode codings    var _expandMsg = "Expand Content \u2192", // 2192 is arrow-right        _shrinkMsg = "Shrink Content \u2190"; // 2190 is arrow-left

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;    var last_rowcount = 0;    function pollChanges    {        // Algorithm is:        // If no pop-up, check for one.        // The first time we find one, we count the rows then sleep, this is to        // try and avoid racing the builder JS.        // If we know about a pop-up and the pop-up is still in the document then // count the rows and check against the last count. If it hasn't changed // then we should be safe to process it. if (!last_popup || !window.isNodeInDocument(last_popup)) { var diffTable = $('.ArticlePreview table.diff'); if (diffTable.length) { last_popup = diffTable[0]; last_rowcount = last_popup.getElementsByTagName('tr').length; } else { // Discard the known pop-up to avoid wasting memory by keeping it               // alive. last_popup = null; }       } else { if (last_rowcount !== false) { var cnt = last_popup.getElementsByTagName('tr').length; if (cnt === last_rowcount) { fixDiffNewlines($(last_popup)); last_rowcount = false; } else { last_rowcount = cnt; }           }        }    }    // 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); } });