Aller au contenu

MediaWiki:Gadget-BugStatusUpdate.js

Une page de Wikipédia, l'encyclopédie libre.
Ceci est une version archivée de cette page, en date du 29 avril 2018 à 16:47 et modifiée en dernier par Od1n (discuter | contributions) (gestion propre du cas où la page contient plusieurs boîtes avec le même id phabricator). Elle peut contenir des erreurs, des inexactitudes ou des contenus vandalisés non présents dans la version actuelle.
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) ;

Firefox (sur GNU/Linux) / 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]]
 */

jQuery( function ( $ ) {
	var	$allTaskLinks = $( '.mw-trackedTemplate' ).find( 'a[title^="phabricator:T"], a[title^="phab:T"]' ),
		ids = [],
		target = 'https://tools.wmflabs.org/phabricator-bug-status/queryTasks';

	// Get the Phabricator task id numbers on the page
	// The template should provide a data attribute, for simplicity and probably better performance.  There
	// should also be a class, so it could quickly be found.  E.g.
	// data-phabricator-task="123" class="trakfab ..." ...
	// This could then be selected easily, and the number could be accessed with .data( 'phabricatorTask' )
	$allTaskLinks.each( function ( _, taskLink ) {
		var titleMatch = taskLink.title.match( /^phab(?:ricator):T(\d+)$/ );
		if ( titleMatch ) {
			ids.push( parseInt( titleMatch[1] ) );
		}
	} );

	// 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 ) },
		success: function ( data ) {

			var		color = {
						"resolved": "green"
					},
					statusProps = {
						'font-weight': 'bold',
						'text-transform': 'uppercase'
					},
					phid, taskInfo, taskNumber,
					$taskLinks;

			for ( phid in data ) {
				taskInfo = data[phid];
				taskNumber = taskInfo.id;
				// Find the right tasks to update
				$taskLinks = $allTaskLinks.filter( 'a[title^="phabricator:T' +  taskNumber + '"], a[title^="phab:T' +  taskNumber + '"]' );

				$taskLinks.each( function ( _, taskLink ) {
					var	$taskLink = $( taskLink ),
						$title, $template, $status;

					$title = $taskLink.find( '.trakfab-T' + taskNumber );
					if ( $title.length ) {
						$title.text( taskInfo.title );
					}

					$template = $taskLink.closest( '.mw-trackedTemplate' );
					if ( $template.length ) {
						// Find child, if exists
						// This is very fragile; this needs a class.
						$status = $template.children( 'span' );

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

						// Update the status element
						// This matches Template:Tracked itself, where only resolved has a color
						// defined for Phabricator (everything else is black).
						$status
							.text( taskInfo.statusName )
							.css( 'color', color[taskInfo.status] || 'black' );
					}
				} );
			}
		}
	} );
} );