User:The Transhumanist/Workshop boilerplate/addPortletLink

mw.util.addPortletLink
This adds a menu item to one of MediaWiki's menus. Use "p-tb" to signify the toolbox menu on the sidebar menu.

First you stick it in a variable, for example, "portletlink":

It has up to 7 parameters. Only 3 are used above.

General usage:

It's components:
 * : the ResourceLoader module to add links to the portlets.
 * : the id of the portlet (that is, menu) where the new menu item is to be placed. The various menus ("portlets") are::
 * : Navigation section in left sidebar
 * : Interaction section in left sidebar
 * : Toolbox section in left sidebar
 * : Print/export section in left sidebar
 * Personal toolbar at the top of the page
 * Upper right tabs in Vector only (read, edit, history, watch, etc.)
 * Drop-down menu containing move, etc. (in Vector); subject/talk links and action links in other skins
 * : Link to a Wikipedia or external page (the initial purpose of portletlink was to link somewhere)
 * : Text that displays in the menu (the title of the
 * : HTML id (optional)
 * : Tooltip to display on mouseover (optional)
 * : Shortcut key press (optional)
 * : id of the existing portlet link to place the new portlet link before (optional) (Don't forget: ids have a leading "#")

The optional fields must be included in the above order. To skip a field without changing it, use the value null, that is, no space between the quotes for that parameter.

To place the menu items in alphabetical order, and so that they don't move around in the menu, for your last menu item specify the id of an existing menu item to anchor it. Then set "next node" for the next to last item as the id for the menu item you just set, and so on.

See the complete documentation at https://www.mediawiki.org/wiki/ResourceLoader/Modules#addPortletLink and Help:Customizing toolbars.

Important: All we've done so far above is assign mw.util.addPortletLink to a variable. It won't do anything until we bind the variable to a click handler (see below).

click handler
To make a menu item that does something when you click on it, you have to "bind" mw.util.addPortletLink, via its variable, to a handler. Like this:

(The variable used in this example is "portletlink").

The "handler" is the part between the curly brackets.

To read about function(e), see what does e mean in this function definition? jQuery's event objects are explained here: http://api.jquery.com/category/events/event-object/ is short for, one of jQuery's event objects.

What is the default being prevented? Portletlink's default action is to link somewhere. We don't want it to do that, and so that is what  is for.