MediaWiki:ToolbarLinks/code.js

/** * ToolbarLinks.js * @file Enables additional of custom toolbar links * @author Eizen  * @license Apache-2.0 * @external "mediawiki.util" * @external "wikia.window" * @external "jQuery" */

/*jslint browser, this:true */ /*global mw, jQuery, window, require */

require(["mw", "wikia.window"], function (mw, wk) {   "use strict";

var ToolbarLinks = { createToolbarLink: function ($linkText, $address) { var $href;

if (arguments.length === 2) { $href = wk.wgArticlePath.replace("$1", $address); } else { $href = "#"; }

return mw.html.element("li", {               "class": "overflow"            }, new mw.html.Raw( mw.html.element("a", {                   title: $linkText,                    href: $href                }, $linkText) ));       },        returnNode: function  { switch (wk.skin) { case "oasis": case "wikia": return ".toolbar .tools"; case "monobook": case "wowwiki": case "uncyclopedia": return "#p-tb ul"; }       },        constructLink: function ($address, $linkText) { var $element = this.createToolbarLink($linkText, $address); var $desiredNode = this.returnNode;

jQuery($element).appendTo($desiredNode); },       importScript: function ($script, $linkText) { var $element = this.createToolbarLink($linkText); var $desiredNode = this.returnNode;

jQuery($element).appendTo($desiredNode).click(function {                wk.importScriptPage($script, "dev");                jQuery(this).remove;            }); },       init: function  { if (!window.customToolbarLinks || window.isToolbarLinksLoaded) { return; }           window.isToolbarLinksLoaded = true;

window.customToolbarLinks.forEach(function ($link) {               if ($link.action === "link") {                    ToolbarLinks.constructLink($link.address, $link.title);                } else if ($link.action === "import") {                    ToolbarLinks.importScript($link.script, $link.title);                }            }); }   };

jQuery(ToolbarLinks.init); });