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(aElem, aDoc) { if (!aElem) return false; if (!aDoc) { if (this && this.document) aDoc = this.document else aDoc = window.document; }   do { if (aElem === doc) return true; } while (aElem = aElem.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 (window.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 = $(this); if ($this.text === _expandMsg) { expandContent; $this.text(_shrinkMsg); } else { shrinkContent; $this.text(_expandMsg); }       return false; }

if (!/\boasis-one-column\b/.test(document.body.className)) { var toolbar = $("#WikiaFooter > .toolbar > ul.tools"); if (toolbar.length) { $(document.createElement('a')) .prop('href', '#') .text(_expandMsg) .click(handleClick) .appendTo(document.createElement('li')) .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). * * The Edit page fixing only works in browsers with MutationObserver support. * Those are Firefox14+ or Chrome18+. */ $(function($) {   var _selectors = 'td.diff-addedline, td.diff-deletedline, td.diff-context';    _selectors = _selectors.replace(/(^|, )/g, '$1#wikiDiff table.diff ');    _selectors = _selectors.replace(/(,|$)/g, ' > div$1');

// jQuery1.7.2 bug: jQuery fails to select nodes if the selector contains a   //  #id which is the root or above the root of the sub-tree marked by the // context parameter (jQuery bug#12170). Workaround by using querySelectorAll //   var _match = document.createElement('div'); _match = _match.matches || _match.mozMatchesSelector || _match.webkitMatchesSelector;

function fixSingleNode(aIndex, aNode) // jQuery's each passes index first... {       aNode.normalize; // Merge adjacent text nodes to simplify this var n = aNode.firstChild; if (n.nodeType === 3) // n.TEXT_NODE === 3 n.data = n.data.replace(/^\s*?\n/, ""); n = aNode.lastChild; if (n.nodeType === 3) n.data = n.data.replace(/\n\s*$/, ""); }   var _observer, _dialog; function observeDialog(mutations) {       // mutations is an Array of MutationRecord /*interface MutationRecord { readonly attribute DOMString type; readonly attribute Node target; readonly attribute NodeList? addedNodes; //? means optional readonly attribute NodeList? removedNodes; readonly attribute Node? previousSibling; readonly attribute Node? nextSibling; readonly attribute DOMString? attributeName; readonly attribute DOMString? attributeNamespace; readonly attribute DOMString? oldValue; };*/       // The mutation list only generates mutations for direct changes. Adding // a whole sub-tree only generates one mutation for the root node that // was placed into the DOM (i.e. a node already in the DOM had a new child       // added). This is understandable but it means the mutations aren't very // useful directly, we need to do a full sub-tree search with selectors. var m, sel = _selectors, match = _match; for (var i = mutations.length ; i-- ; ) { m = mutations[i].addedNodes; for (var j = m.length ; j-- ; ) { $.each(m[j].querySelectorAll(sel), fixSingleNode); // Both jQuery and querySelectorAll will only match children, fix. if (match.call(m[j], sel)) fixSingleNode(0, m[j]); }       }    }    function observeBody(mutations) {       // This is way simpler and faster than walking the mutation list var node = document.getElementById('EditPageDialog'); if (node !== _dialog) { _dialog = node; _observer.disconnect; // Prevent leaking previous DOM sub-tree if (node) { $(_selectors, node).each(fixSingleNode); _observer.observe(node, {                   childList: true,                    subtree: true // All children of all sub-nodes                }); }       }    }

// Build custom stylesheet. var t = document.createElement('style'); t.setAttribute("type", "text/css"); t.appendChild(document.createTextNode(_selectors + "{" + "font-family: monospace;" // Chrome has a nice "bug" that causes it to render better than Firefox. // Chrome complies with pre+break-word. Firefox just applies normal text // processing rules (break at spaces, hyphens, etc) which sucks. + ($.browser.webkit           ? "white-space: pre; word-wrap: break-word;"            : "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 ((window.wgAction === 'historysubmit' || (window.mw.config && window.mw.config.get('wgAction') === 'view') && /^[^\?]+(?=.*?[\?&]diff)(?=.*?[\?&]oldid=)/.test(window.location.href))) {       var selector = _selectors.replace(/(^|, )/g, '$1#WikiaArticle '); $(selector).each(fixSingleNode); }

// 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')) { // We use the DOM4 MutationObserver to detect when the pop-up is shown. var observer_factory = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver; if (observer_factory) { var observer = new observer_factory(observeBody); _observer = new observer_factory(observeDialog); // We observe nodes added to the body *directly*, not any of its // children. This is more efficient since the overlay is always added // to and we won't get events caused by the WYSIWYG editor. observer.observe(document.body, {childList: true}); }   } });