/*
* Bug Status Update Gadget
* Author: ]
* Based on original code:
** Author: ] (robm on IRC)
** Source: ]
* Description:
* Finds and updates bug status templates on a page.
* Makes 1 JSONP request to Bugzilla JSON RPC api.
*/
(function($){
var ids = ,
target = 'https://bugzilla.wikimedia.org/jsonrpc.cgi';
/* Ugly way to compose the request parameters. Though, Bugzilla is happy with it */
var getParams = function (ids) {
return 'method=Bug.get&id=158¶ms=,"include_fields":}]';
};
/* Get the bug id numbers on the page */
$('.mw-trackedTemplate').each(function() {
var title = $(this).find('a').attr('title');
ids.push(title.split(':'));
});
/* Do not query if no ids were found */
if (!ids.length) {
return;
}
/* Make jsonp request */
$.ajax({
url: target,
dataType:'jsonp',
data: getParams(ids),
success: function (data) {
var color = {
"RESOLVED": "green",
"CRITICAL": "red"
},
statusProps = {
'font-weight': 'bold',
'font-size': '1.5em',
'text-transform': 'uppercase'
};
if(data.result.bugs) {
for(var b in data.result.bugs) {
/* Find the right bug to update */
var $item = $('.mw-trackedTemplate').find('a.id+'"]');
var title = $('.trakbug-'+data.result.bugs.id);
if(title) {
title.text( data.result.bugs.summary );
}
// COMPATIBILITY SECTION TOP FOR CURRENT SETUP
var titleBACK = $('#trakbug-'+data.result.bugs.id);
if(titleBACK) {
titleBACK.text( data.result.bugs.summary );
}
// COMPATIBILITY SECTION BOTTOM FOR CURRENT SETUP
if($item) {
/* Find child, if exists */
$status = $item
.parent()
.next('p')
.children('span');
/* Create the status element if it does not exist */
if($status.length === 0){
$item
.parent()
.parent()
.append(
$('<p />').append(
$('<span />').css(statusProps)
.text('Status')
)
);
}
/* Update the status element */
$item
.parent()
.next('p')
.children('span')
.css('color', color.status] || '#333')
.text(data.result.bugs.status);
$status = null;
}
}
}
}
});
})(jQuery);