MediaWiki:Gadget-nav-requisiti.js: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
+ucprop, meno dati trasmessi
utilizzo di OOjs UI e mw.Api
Riga 1:
/**
* Gadget-nav-requisiti.js
* Aggiunge nella barra laterale un link "Verifica requisiti di voto" con il quale è possibileper visualizzare
* le informazioni su data di registrazione, primo, 50esimo e 500esimo edit di un utente.
*
* Questa è una riscrittura da zero a partire dadi:
* http://it.wikipedia.org/w/index.php?title=Wikipedia:Monobook.js/Requisiti.js&oldid=38597188
*
* @author [[https://it.wikipedia.org/wiki/Utente:Rotpunkt]]
*/
/*global mediaWiki, jQuery, OO */
 
( function ( mw, $ ) {
'use strict';
 
var windowManager, searchDialog, textInput, resultLabel;
var $dialog;
 
/**
// Ritorna la data di registrazione dell'utente
* Ricerca la data di registrazione di un utente.
* @param {string} user - il nome dell'utente.
* @param {function} registrationHandler - la funzione da richiamare con il risultato.
*/
function getRegistration( user, registrationHandler ) {
$new mw.ajaxApi().get( {
url: mw.util.wikiScript( 'api' ),
data: {
action: 'query',
list: 'users',
Line 26 ⟶ 28:
usprop: 'registration',
format: 'json'
} ).done( function ( data ) {
},
registrationHandler( data.query.users[0].registration );
dataType: 'json'
} );
.done( function ( data ) {
registrationHandler( data.query.users[0].registration );
} );
}
 
/**
// Ritorna i contributi dell'utente in formato JSON
* Ricerca i 500 contributi utente successivi alla data "start".
* @param {string} user - il nome dell'utente.
* @param {string} start - la data di inizio.
* @param {function} contribsHandler - la funzione da richiamare con i risultati.
*/
function getUserContribs( user, start, contribsHandler ) {
$new mw.ajaxApi().get( {
action: 'query',
url: mw.util.wikiScript( 'api' ),
datalist: {'usercontribs',
actionucuser: 'query'user,
listucprop: 'usercontribstimestamp',
ucuserucstart: userstart,
ucpropucdir: 'timestampnewer',
ucstartuclimit: start'500',
ucdirformat: 'newerjson',
} ).done( function ( data ) {
uclimit: '500',
contribsHandler( data.query.usercontribs );
format: 'json'
}, );
dataType: 'json'
} )
.done( function ( data ) {
contribsHandler( data.query.usercontribs );
} );
}
 
/**
// Parsifica un timestamp in date (1 gen 2001) e time (01:23)
* Parsifica un timestamp in date (1 gen 2001) e time (01:23).
* @param {string} timestamp - il timestamp da parsificare.
*/
function parseTimestamp( timestamp ) {
var date, hours, minutes, months;
Line 73 ⟶ 75:
}
 
/**
function buildResult( registration, contribs ) {
* Formatta le informazioni su data di registrazione, primo, 50esimo e 500esimo edit di un utente.
var info = '', totcontribs = 'l\'utente ha fatto solo ' + contribs.length + ' modifiche';
* @param {string} registration - la data di registrazione.
* @param {string} contribs - i primi 500 contributi.
*/
function formatResult( registration, contribs ) {
var info = '', totcontribs = 'ha fatto solo ' + contribs.length + ' modifiche';
info += 'Registrazione: ' + parseTimestamp( registration ) + '<br/>';
info += 'Prima modifica: ' + ( contribs.length > 0 ? parseTimestamp( contribs[0].timestamp ) : totcontribs ) + '<br/>';
Line 81 ⟶ 88:
return info;
}
 
/**
* Gestore del click sul pulsante "Cerca".
* Richiede il nome utente.
*/
function showDialogsearchHandler() {
var user = $.trim( textInput.getValue() );
var $label, $inputText, $spinner, $result;
if ( !$dialoguser ) {
resultLabel.setLabel( $( '<p>Il nome utente &egrave; obbligatorio.</p>' ) );
$dialog = $( '<div>' ).attr( 'id', 'gds-dialog' ).appendTo( 'body' );
} else {
resultLabel.setLabel( 'Ricerca in corso...' );
$label = $( '<label>' ).css( { display: 'block', 'margin': '10px 0 2px 0' } )
getRegistration( user, function ( registration ) {
.text( 'Nome utente:' );
if ( registration ) {
$inputText = $( '<input/>' ).attr( 'type', 'text' ).attr( 'size', 40 );
getUserContribs( user, registration, function ( contribs ) {
$result = $( '<p>' );
resultLabel.setLabel( $( '<p>' + formatResult( registration, contribs ) + '</p>' ) );
$spinner = $.createSpinner( { size: 'large', type: 'block' } ).css( 'margin', '10px' ).hide();
} );
$dialog.empty().append( $label, $inputText, $result, $spinner );
} else {
resultLabel.setLabel( $( '<p>L\'utente ' + user + ' non &egrave; registrato.</p>' ) );
$dialog.dialog( {
title: 'Accessorio verifica requisiti di voto',
position: { my: 'center', at: 'center', of: window },
buttons: {
'Cerca': function () {
if ( $spinner.is( ':visible' ) ) {
return;
}
var user = $.trim( $inputText.val() );
if ( user.length === 0 ) {
alert( 'Il nome utente è obbligatorio.' );
} else {
$result.html( 'Ricerca in corso...' );
$spinner.show();
getRegistration( user, function ( registration ) {
if ( registration ) {
getUserContribs( user, registration, function ( contribs ) {
$spinner.hide();
$result.html( buildResult( registration, contribs ) );
} );
} else {
$spinner.hide();
$result.empty();
alert( 'L\'utente ' + user + ' non è registrato' );
}
} );
}
},
'Annulla': function () {
$( this ).dialog( 'close' );
}
} );
}
}
 
/**
* Crea la finestra di dialogo per la ricerca delle informazioni utente.
*/
function buildSearchDialog() {
var style = '.grv-container { height: 80px }' +
'.grv-container-button { width: 100%; text-align: center }';
$( '<style>' ).text( style ).appendTo( 'head' );
function SearchDialog( config ) {
SearchDialog.parent.call( this, config );
}
OO.inheritClass( SearchDialog, OO.ui.Dialog );
SearchDialog.static.name = 'searchDialog';
SearchDialog.prototype.initialize = function () {
SearchDialog.parent.prototype.initialize.call( this );
resultLabel = new OO.ui.LabelWidget( {
classes: [ 'grv-container' ],
label: ' '
} );
textInput = new mw.widgets.UserInputWidget();
textInput.on( 'enter', searchHandler );
var textInputLayout = new OO.ui.FieldLayout( textInput, {
label: 'Nome utente:',
align: 'top'
} );
var searchButton = new OO.ui.ButtonWidget( {
label: 'Cerca',
} ).on( 'click', searchHandler );
var cancelButton = new OO.ui.ButtonWidget( {
label: 'Annulla'
} ).on( 'click', function () {
searchDialog.close();
} );
var buttons = new OO.ui.HorizontalLayout( {
items: [ searchButton, cancelButton ],
classes: [ 'grv-container-button' ]
} );
this.panelLayout = new OO.ui.PanelLayout( { padded: true, expanded: false } );
this.panelLayout.$element.append( textInputLayout.$element, resultLabel.$element, buttons.$element );
this.$body.append( this.panelLayout.$element );
};
SearchDialog.prototype.getBodyHeight = function () {
return this.panelLayout.$element.outerHeight( true );
};
return new SearchDialog( {
size: 'small'
} );
}
Line 136 ⟶ 162:
$( portletLink ).click( function ( event ) {
event.preventDefault();
mw.loader.using( [ 'jquerymediawiki.api', 'oojs-ui.dialog-core', 'jquery.spinneroojs-ui-widgets' ], function () {
'oojs-ui-windows', 'mediawiki.widgets.UserInputWidget' ], function () {
showDialog();
if ( !searchDialog ) {
searchDialog = buildSearchDialog();
windowManager = new OO.ui.WindowManager();
$( 'body' ).append( windowManager.$element );
windowManager.addWindows( [ searchDialog ] );
}
windowManager.openWindow( searchDialog );
} );
} );