User:Gary/shortcuts.js

// Original: User:Jsimlo/shortcuts-dev.js

/*********************************************************** * INITIALIZATION ***********************************************************/ var shortcutsLinks = new Array; shortcutsStartSection(wgUserName ? wgUserName : 'shortcuts');

$(shortcutsInitialize);

/*********************************************************** * INTERFACE ***********************************************************/

/** * Starts a new section of links. Function for external use. */ function shortcutsStartSection(name) {	shortcutsLinks[shortcutsLinks.length] = { 'name': name, 'links': new Array }; }

/** * Adds new link into the current section of links. Function for external use. */ function shortcutsAddLink(name, article, onclick, disabled) {	var links = shortcutsLinks[shortcutsLinks.length - 1]['links']; links[links.length] = { 'name': name, 'article': article, 'onclick': onclick, 'disabled': disabled }; }

/*********************************************************** * IMPLEMENTATION ***********************************************************/

/** * Link maker. * Creates and returns an li element with the desired link. */ function shortcutsMakeLink(name, url, onclick, disabled) {	var a = $('').append(name); var li = $('').addClass('n-shortcut').attr('id', 't-' + name.toLowerCase.replace(' ', '-')).append(a); if (url.indexOf('http') == 0 || url.indexOf('javascript') == 0) a.attr('href', url); else a.attr('href', '/wiki/' + url.replace(/ /g, '_')); if (onclick) a.attr('onclick', onclick); if (disabled) a.css('color', 'black');

return li; }

/** * Portlet maker. * Creates and returns a portlet populated by list of links. */ function shortcutsMakePortlet(name, links) {	var title = $(' ').append(name); var ul = $('').attr('id', 'p-' + name); var body = $(' ').addClass('pBody').append(ul); var portlet = $(' ').attr('id', 'p-' + name).addClass('portlet').append(title).append(body); for (var i = 0; i < links.length; i++) ul.append(shortcutsMakeLink(links[i]['name'], links[i]['article'], links[i]['onclick'], links[i]['disabled'])); return portlet; }

/** * Main function. * Enumerates all non-empty sections, and adds new portlets beneath the old ones. */ function shortcutsMain {	var sidecol = $('#column-one'); if (!sidecol.length) return false;

for (var i = 0; i < shortcutsLinks.length; i++) {		if (shortcutsLinks[i]['links'].length > 0) {			sidecol.append(shortcutsMakePortlet(shortcutsLinks[i]['name'], shortcutsLinks[i]['links'])); }	}	// Move the "languages" section to the bottom of the column. $('#column-one').append($('#p-lang')); }

/** * Initializes the onload events. */ function shortcutsInitialize {	if (typeof shortcutsInit == 'function') {		shortcutsInit; shortcutsMain; } }