User:Kellen/monobook.js

From Wikibooks, open books for an open world
Jump to navigation Jump to search
Note: After saving, changes may not occur immediately. Click here to learn how to bypass your browser's cache.
  • Mozilla / Firefox / Safari: hold down Shift while clicking Reload, or press Ctrl-Shift-R (Cmd-Shift-R on Apple Mac);
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5;
  • Konqueror: simply click the Reload button, or press F5;
  • Opera users may need to completely clear their cache in Tools→Preferences.
/* see [[/examples]] for basic usage */

/**
 * makeLink
 * makes a link with the specified text and href 
 *
 * param text: label text to give the link
 * param href: url for the link
 **/
function makeLink(text, href) {
    var link = document.createElement('A');
    link.appendChild(document.createTextNode(text));
    link.href = href;
    return link;
}

/**
 * makeNewToolsItem
 * makes a new item in the personal tools portlet with the specified id
 *
 * param id: id to give the new item
 * returns: a new item (li) on the personal tools list with specified id 
 **/
function makeNewToolsItem(id) {
  var node = document.createElement('LI');
  node.id = id;
  // add to list
  var preflist = document.getElementById('pt-userpage').parentNode;
  preflist.appendChild(node);  
  return node;
}

/**
 * insertAfter 
 * adds newElement to the DOM tree after the reference node
 * 
 * param reference: the node to insert after
 * param newElement: the element to insert 
 **/
function insertAfter(reference, newElement) {
  reference.parentNode.insertBefore(newElement,reference.nextSibling);
}

/** 
 * makeNew Portlet
 * make a new sidebar portlet
 * 
 * param id: id to give the containing div of the portlet
 * param label: visible label for the portlet, like "toolbox"
 * returns: an empty list (UL) to be filled with items (li's) 
 **/
function makeNewPortlet(id, label) {
  var node = document.createElement('DIV');
  node.id = id;
  node.className= 'portlet';
  var title = document.createElement('H5');
  title.appendChild(label);
  node.appendChild(title);
  var contentnode = document.createElement('DIV');
  contentnode.className = 'pBody';
  node.appendChild(contentnode);
  var listnode = document.createElement('UL');
  contentnode.appendChild(listnode);

  /* by default inserts under the 'toolbox'; use a different id 
     in place of 'p-tb' to move where the portlet gets put */
  insertAfter(document.getElementById('p-tb'), node);

  return listnode;
}

/**
 * addPortletItem
 * add an item to a portlet
 * 
 * param portlet: a list (UL, usually)
 * param item: a node, most often a link (A), to add to the list
 */
function addPortletItem(portlet, item) {
  var listitem = document.createElement('LI');
  listitem.appendChild(item);
  portlet.appendChild(listitem);
}

/* generates a bunch of cookbook-related interface bits */
function cookbook() {
  // make a cookbook item in the personal tools portlet
  cooknode = makeNewToolsItem('pt-cookbook');

  // wrap "Cookbook >" in a span so we can alter the style independently
  var cooklabel = document.createElement('SPAN');
  cooklabel.id = 'pt-cookbook-label';
  // add the links
  cooklabel.appendChild(makeLink('Cook', '/wiki/Cookbook:Table of Contents'));
  cooklabel.appendChild(makeLink('book', '/wiki/Cookbook_talk:Table of Contents'));
  cooklabel.appendChild(document.createTextNode('» '));

  // append everything to the cookbook item
  cooknode.appendChild(cooklabel);
  cooknode.appendChild(makeLink('Changes', '/w/index.php?limit=1000&title=Special%3ARecentchanges&namespace=102'));
  cooknode.appendChild(document.createTextNode('/'));
  cooknode.appendChild(makeLink('Index', '/w/index.php?title=Special%3AAllpages&from=&namespace=102'));

  // make cookbook portlet
  var cooktools = makeNewPortlet('p-cookbook', document.createTextNode('Cookbook'));
  // add a bunch of links to it
  addPortletItem(cooktools, makeLink('Main page', '/wiki/Cookbook:Table of Contents'));
  addPortletItem(cooktools, makeLink('Talk page', '/wiki/Cookbook_Talk:Table of Contents'));
  addPortletItem(cooktools, makeLink('Recent changes', '/w/index.php?limit=1000&title=Special%3ARecentchanges&namespace=102'));
  addPortletItem(cooktools, makeLink('All pages', '/w/index.php?title=Special%3AAllpages&from=&namespace=102'));
  addPortletItem(cooktools, makeLink('Work needed', '/wiki/Category:Cookbook_pages_needing_work'));
  addPortletItem(cooktools, makeLink('Stubs', '/wiki/Category:Cookbook_stubs'));
}

/* run cookbook() when a page is loaded */
if (window.addEventListener) window.addEventListener("load",cookbook,false);
else if (window.attachEvent) window.attachEvent("onload",cookbook);