MediaWiki:Gadget-BugStatusUpdate.js

Une page de Wikipédia, l'encyclopédie libre.
Note : après avoir enregistré la page, vous devrez forcer le rechargement complet du cache de votre navigateur pour voir les changements.

Mozilla / Firefox / Konqueror / Safari : maintenez la touche Majuscule (Shift) en cliquant sur le bouton Actualiser (Reload) ou pressez Maj-Ctrl-R (Cmd-R sur Apple Mac) ;

Chrome / Internet Explorer / Opera : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl-F5.
/*
 * Bug Status Update Gadget
 *
 * Authors:
 * Written by Rob Moen (robm).
 * Ported to Phabricator by Matthew Flaschen (Mattflaschen (WMF))
 *
 * Description:
 *  Finds and updates bug status templates on a page.
 *  Makes 1 JSONP request to phabricator-bug-status API (maintained by Matthew Flaschen)
 *  (which passes the request to Phabricator's Conduit API)
 * Source: [[mw:User:Robmoen/bugStatusUpdate.js]]
 */

// Associated template: [[Modèle:Phabricator]]

/* globals $ */

$( function ( $ ) {
	'use strict';

	var	$trackedTemplates = $( '#mw-content-text' ).find( '.phab-trackedTemplate' ),
		ids = [],
		target = 'https://tools.wmflabs.org/phabricator-bug-status/queryTasks';

	// Get the Phabricator task id numbers on the page
	// The template provides a data attribute (e.g. data-task-number="123"),
	// so the task number can be obtained with .dataset.taskNumber
	// This script also adds a class (e.g. class="... phab-taskNumber-123"),
	// so the element can easily be selected later
	$trackedTemplates.each( function ( _, template ) {
		var taskNumber = template.dataset.taskNumber;
		if ( taskNumber && /^\d+$/.test( taskNumber ) ) {
			ids.push( parseInt( taskNumber ) );
			template.classList.add( 'phab-taskNumber-' + taskNumber );
		}
	} );

	// Do not query if no ids were found
	if ( !ids.length ) {
		return;
	}

	// Remove duplicates
	ids = ids.filter( function ( value, index, self ) {
		return self.indexOf( value ) === index;
	} );

	// Make jsonp
	$.ajax( {
		url: target,
		dataType: 'jsonp',
		data: { ids: JSON.stringify( ids ) },
		timeout: 5000, // Give up if Tool Labs is being slow today
		success: function ( data ) {
			/* jshint forin: false, loopfunc: true */

			var	statusCommonProps = {
					'font-weight': 'bold',
					'text-transform': 'uppercase'
				},
				statusExtraProps = {
					'resolved': { 'color': 'green' },
					'critical': { 'color': 'red', 'font-size': '1.5em' }
				},
				phid, taskInfo;

			for ( phid in data ) {
				taskInfo = data[phid];

				// Update the corresponding templates
				$trackedTemplates.filter( '.phab-taskNumber-' + taskInfo.id ).each( function ( _, template ) {
					var	$template = $( template ),
						$title = $template.find( '.phab-taskTitle' ),
						$status = $template.find( '.phab-status' );

					// Update the task title
					$title.text( taskInfo.title );

					// Create the status element if it does not exist
					if ( !$status.length ) {
						$status = $( '<span></span>' )
							.addClass( 'phab-status' )
							.css( statusCommonProps );
						$template.append( '<br>', $status );
					}

					// Update the status element
					$status.text( taskInfo.statusName );

					// Possibly add extra CSS, to match Template:Phab itself
					if ( statusExtraProps[taskInfo.status] ) {
						$status.css( statusExtraProps[taskInfo.status] );
					}
				} );
			}
		}
	} );
} );