MediaWiki:Gadget-blockNotificationButton.js: diferenças entre revisões

Origem: Wikipédia, a enciclopédia livre.
Conteúdo apagado Conteúdo adicionado
m
É preciso esperar que o documento esteja pronto para poder usar $( '.mw-htmlform-submit' )
Linha 141: Linha 141:
};
};


if ( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Block'
if ( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Block' ) {
&& !$( '.mw-htmlform-submit' ).length
$( function(){
if( !$( '.mw-htmlform-submit' ).length ){
bbn.run();
) {
}
$( bbn.run );
} );
}
}



Revisão das 14h31min de 26 de abril de 2014

/** Button of block notification
 *
 * Adds an button to notify the user after block
 *
 * @author [[pt:User:!Silent]]
 * @date 13/apr/2012
 * @updated 25/apr/2014
 */
/* global jQuery, mediaWiki */
/* jshint laxbreak:true */

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

mw.messages.set( {
	'bbn-checkingBlockRegister': 'Consultando os registros de bloqueio do usuário...',
	'bbn-editUserDiscussion': 'Editando a página de discussão do usuário...',
	'bbn-sectionTitle': 'Notificação de bloqueio',
	'bbn-summarySufix': ', usando um [[MediaWiki:Gadget-BlockNotificationsButton.js|gadget]]',
	'bbn-success': 'Mensagem de bloqueio enviada com sucesso (<a href="$1#Notifica.C3.A7.C3.A3o_de_bloqueio">Abrir</a>).',
	'bbn-apiError': 'Erro: A API retornou o código de erro "$1": $2',
	'bbn-unknownError': 'Erro: Resultado desconhecido da API.',
	'bbn-requestFail': 'Erro: a requisão falhou.',
	'bbn-buttonText': 'Enviar uma notificação de bloqueio',
	'bbn-successBlock': 'Bloqueio bem sucedido'
} );

/**
 * @class bbn
 */
var bbn = {};

/**
 * Messages
 * @see [[mw:ResourceLoader/Default_modules#mediaWiki.message]]
 * @return {string}
 */
bbn.message = function( /*name[, $1[, $2[, ... ]]]*/ ) {
	return mw.message.apply( this, arguments ).plain();
};

/**
 * Translate to the portuguese the duration of the blocks
 *
 * @param {string} duration The duration of block
 * @return {string}
 */
bbn.translateDuration = function( duration ) {
	var translation;

	duration = duration.split( ' ' );

	if ( duration[ 0 ] === 'infinite' ) {
		return 'tempo indeterminado';
	}

	translation = {
		'minute': 'minuto',
		'hour': 'hora',
		'day': 'dia',
		'week': 'semana',
		'month': 'mês',
		'year': 'ano'
	};

	if ( duration[ 0 ] !== '1' ) {
		translation.month = 'meses';
	}

	return duration[ 0 ] + ' '
		+ translation[ duration[ 1 ].replace( /s$/, '' ) ]
		+ ( ( translation.month === 'meses' && duration[ 1 ].indexOf( 'month' ) === -1 ) ? 's' : '' );
};

/**
 * Send the notify
 */
bbn.sendNotify = function() {
	var logevents,
		userNameBlocked = $( '#mw-content-text' ).find( 'a' ).html();

	mw.notify( bbn.message( 'bbn-checkingBlockRegister' ) );

	$.getJSON( mw.util.wikiScript( 'api' ), {
		action: 'query',
		list: 'logevents',
		format: 'json',
		leprop: 'title|user|timestamp|details|comment',
		lelimit: '1',
		leuser: mw.config.get( 'wgUserName' ),
		letitle: 'User:' + userNameBlocked
	} ).done( function( data ) {
		logevents = data.query.logevents[ 0 ];

		mw.notify( bbn.message( 'bbn-editUserDiscussion' ) );

		( new mw.Api() ).editPage( {
			title: 'User talk:' + userNameBlocked,
			section: 'new',
			sectiontitle: bbn.message( 'bbn-sectionTitle' ),
			text: '{' + '{subst:Bloqueado'
				+ ( ( logevents.block.flags.indexOf( 'nousertalk' ) === -1 ) ? '-disc' : '' )
				+ '|1=' + bbn.translateDuration( logevents.block.duration )
				+ '|2=' + logevents.comment
				+ '.}} ~~' + '~~',
			summary: bbn.message( 'bbn-sectionTitle' ) + bbn.message( 'bbn-summarySufix' ),
			done: {
				success: function() {
					mw.notify(
						$.parseHTML(
							bbn.message( 'bbn-success', mw.util.getUrl( 'User talk:' + userNameBlocked ) )
						)
					);
				},
				apiError: function( data ) {
					mw.notify( bbn.message( 'bbn-apiError', data.code, data.info ) );
					$( '#bbn-sendMsg' ).attr( 'disabled', 'false' );
				},
				unknownError: function() {
					mw.notify( bbn.message( 'bbn-unknownError' ) );
					$( '#bbn-sendMsg' ).attr( 'disabled', 'false' );
				}
			}
		} ).fail( function() {
			mw.notify( bbn.message( 'bbn-requestFail' ) );
		} );
	} );
};

/**
 * Run the gadget
 */
bbn.run = function() {
	$( '#mw-content-text' ).append(
		$( '<input type="button" id="bbn-sendMsg" value="' + bbn.message( 'bbn-buttonText' ) + '" />' ).click( function() {
			bbn.sendNotify();
			$( this ).attr( 'disabled', 'true' );
		} )
	);

};

if ( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Block' ) {
	$( function(){
		if( !$( '.mw-htmlform-submit' ).length ){
			bbn.run();
		}
	} );
}

}( mediaWiki, jQuery ) );

// [[Categoria:!Código-fonte de scripts|Botão de notificação de bloqueio]]