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 === aDoc) return true; } while (aElem = aElem.parentNode); return false; } } // Mozilla MDN code. if ( !Array.prototype.forEach ) { Array.prototype.forEach = function(fn, scope) { for(var i = 0, len = this.length; i < len; ++i) { fn.call(scope || this, this[i], i, this); }   }  }

/** * 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")); document.body.className += " oasis-one-column"; }   function shrinkContent {       document.body.className = document.body.className.replace(          /(?:^|\s+)oasis-one-column\b/, ''        ); $("#WikiaRail").prepend($("#WikiaSearch")).show; }   function createClickHandler(aExpandFunc, aShrinkFunc) {       return function(event) { var $this = $(this); if ($this.text === _expandMsg) { aExpandFunc; $this.text(_shrinkMsg); } else { aShrinkFunc; $this.text(_expandMsg); }           return false; };   }

var handle_click; if (!/\boasis-one-column\b/.test(document.body.className)) { handle_click = createClickHandler(expandContent, shrinkContent); } else { var node = $('#EditPage .editpage-widemode-trigger'); if (node.length) { var edit_toggle = function { node.click; return false; } handle_click = createClickHandler(edit_toggle, edit_toggle); }   }    if (handle_click) { var toolbar = $("#WikiaFooter .toolbar > ul.tools"); if (toolbar.length) { $(document.createElement('a')) .prop('href', '#') .text(_expandMsg) .click(handle_click) .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, '$1table.diff ');    _selectors = _selectors.replace(/(,|$)/g, ' > div$1');

// jQuery hard constrains the given selectors to the context. If the selector // references any nodes which are above the context node in the tree then the // selector will always fail. I think this behaviour is stupid since CSS // selectors are supposed to read from right to left, not left to right but // whatever. querySelectorAll provides 'rooted at document and filter to   // context as ancestor' behaviour which does what we need. //   var _match = document.body; _match = _match.matches || _match.mozMatchesSelector || _match.webkitMatchesSelector;

function fixSingleNode(aNode, aIndex, aArray) // Array.forEach compatible {       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*$/, ""); }   // Array.forEach chokes on NodeLists. // querySelector NodeLists are dead, always convert getElements... to Arrays // instead of using them directly unless you aren't going to edit any DOM. function forEachNode(aNodeList, aCallback, aScope) {       var scope = aScope || aNodeList; for (var i = 0, l = aNodeList.length ; i < l ; ++i) { aCallback.call(scope, aNodeList[i], i, aNodeList); }   }    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; for (var i = mutations.length ; i-- ; ) { m = mutations[i].addedNodes; for (var j = m.length ; j-- ; ) { forEachNode(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) { forEachNode(node.querySelectorAll(_selectors), 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))) {       forEachNode(document.querySelectorAll(_selectors), 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}); }   } });