MediaWiki:InfoWidgets/code.js

var Widgets = function {

var widgets = []; var interval = false; var lastUpdate = false; var current;

var my = {

// presets:

activeTalks: function { return { params: { action: 'query', format: 'json', list: 'recentchanges', rctype: 'new|edit', rclimit: 30, rcshow: '!redirect', rcprop: 'title', rcnamespace: '1|3|5|7|9|11|13|15|110|111|401|500|501|502|503' }}; },

contribs: function (user) { if (!user) user = wgUserName; return { params: { action: 'query', format: 'json', list: 'usercontribs', uclimit: 30, ucuser: encodeURIComponent(user), ucprop: 'title' }}; },

newPages: function { return { params: { action: 'query', format: 'json', list: 'recentchanges', rctype: 'new', rclimit: 10, rcshow: '!redirect', rcprop: 'title', rcnamespace: 0 }}; },

recentChanges: function { return { params: { action: 'query', format: 'json', list: 'recentchanges', rctype: 'edit', rclimit: 30, rcshow: '!redirect', rcprop: 'title' }}; },

stubs: function { return { params: { action: 'query', format: 'json', list: 'categorymembers', cmtitle: 'Category:Article_stubs', cmlimit: 10, cmsort: 'timestamp' }}; },

watchlist: function { return { params: { action: 'query', format: 'json', list: 'watchlist', wlprop: 'title'}}; },

// main public functions:

add: function (widget) { if (!widget.selector) throw new Error('no id provided'); widget.container = $(widget.selector); if (!widget.container.length) throw new Error('widget container cannot be found'); if (!widget.params) throw new Error('no query parameters provided'); if (!widget.params['action'] || 'query' != widget.params['action']) throw new Error('this module is for queries only'); widget.params['format'] = 'json'; if (!widget.maxResults) widget.maxResults = 10; if (!$.isFunction(widget.format)) widget.format = format; if (!$.isFunction(widget.prepare)) widget.prepare = prepare; if (!$.isFunction(widget.success)) widget.success = success; if (!$.isFunction(widget.error)) widget.error = function {}; widgets.push(widget); },

start: function { cycleThroughWidgets; $(window).focus(function { cycleThroughWidgets; } ); $(window).blur(function { if (interval) window.clearInterval(interval); interval = false; }); } }

function queryWikiaAPI { current.container.empty; current.prepare; $.ajax({ type: 'GET', url: '/api.php', data: current.params, dataType: 'json', async: true, cache: false, success: function (response, textStatus, jqXHR) { var data = response.query[current.params['list']]; current.container.append(current.format(data)); current.success; }, error: current.error }); }

function cycleThroughWidgets { if (lastUpdate && Date.now - lastUpdate < 61000) return; lastUpdate = Date.now; if (!interval) { interval = window.setInterval( function { cycleThroughWidgets; }, 20000 ); } for (var i = 0; i < widgets.length; i++) { current = widgets[i]; queryWikiaAPI; } }

function format (data) { var html = ''; var titles = []; for (var i = 0; i < data.length && titles.length < current.maxResults; i++) { if (-1 == $.inArray(data[i].title, titles)) { titles.push(data[i].title); html += ''+data[i].title+''; } } return html.length ? ''+html+'' : ''; }

function prepare { current.container.css( {backgroundImage: 'url(loading.gif)', backgroundRepeat: 'no-repeat', backgroundPosition: 'center'} ); }

function success { current.container.css( {backgroundImage: 'none'} ); }

return my;

};