MediaWiki:Gadget-nav-requisiti.js
Questa pagina definisce alcuni parametri di aspetto e comportamento generale di tutte le pagine. Per personalizzarli vedi Aiuto:Stile utente.
Nota: dopo aver salvato è necessario pulire la cache del proprio browser per vedere i cambiamenti (per le pagine globali è comunque necessario attendere qualche minuto). Per Mozilla / Firefox / Safari: fare clic su Ricarica tenendo premuto il tasto delle maiuscole, oppure premere Ctrl-F5 o Ctrl-R (Command-R su Mac); per Chrome: premere Ctrl-Shift-R (Command-Shift-R su un Mac); per Konqueror: premere il pulsante Ricarica o il tasto F5; per Opera può essere necessario svuotare completamente la cache dal menù Strumenti → Preferenze; per Internet Explorer: mantenere premuto il tasto Ctrl mentre si preme il pulsante Aggiorna o premere Ctrl-F5.
/**
* Gadget-nav-requisiti.js
* Aggiunge nella barra laterale un link "Verifica requisiti di voto" per visualizzare
* le informazioni su data di registrazione, primo, 50esimo e 500esimo edit di un utente.
*
* Questa è una riscrittura da zero di:
* 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';
// la finestra di dialogo per la ricerca delle informazioni utente
var searchDialog;
/**
* 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.Api().get( {
action: 'query',
list: 'users',
ususers: user,
usprop: 'registration',
format: 'json'
} ).done( function ( data ) {
registrationHandler( data.query.users[0].registration );
} );
}
/**
* 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.Api().get( {
action: 'query',
list: 'usercontribs',
ucuser: user,
ucprop: 'timestamp',
ucstart: start,
ucdir: 'newer',
uclimit: '500',
format: 'json'
} ).done( function ( data ) {
contribsHandler( data.query.usercontribs );
} );
}
/**
* Parsifica un timestamp in date (1 gen 2001) e time (01:23).
*
* @param {string} timestamp - Il timestamp da parsificare
* @return {string} La data nel formato '1 gen 2001 alle 01:23'
*/
function parseTimestamp( timestamp ) {
var date, hours, minutes, months;
date = new Date( timestamp );
hours = date.getHours();
minutes = date.getMinutes();
months = [
'gen', 'feb', 'mar', 'apr', 'mag', 'giu',
'lug', 'ago', 'set', 'ott', 'nov', 'dic'
];
return date.getDate() + ' ' + months[date.getMonth()] + ' ' +
date.getFullYear() + ' alle ' +
( hours < 10 ? '0' + hours : hours ) + ':' +
( minutes < 10 ? '0' + minutes : minutes );
}
/**
* Formatta le informazioni su data di registrazione, primo, 50esimo e 500esimo edit di un utente.
*
* @param {string} registration - La data di registrazione
* @param {string} contribs - I primi 500 contributi
* @return {string} Le informazioni formattate
*/
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/>';
info += '50esima modifica: ' + ( contribs.length >= 50 ? parseTimestamp( contribs[49].timestamp ) : totcontribs ) + '<br/>';
info += '500esima modifica: ' + ( contribs.length >= 500 ? parseTimestamp( contribs[499].timestamp ) : totcontribs );
return info;
}
/**
* Gestore del click sul pulsante "Cerca".
*/
function searchHandler() {
var user = $.trim( searchDialog.textInput.getValue() );
if ( !user ) {
searchDialog.resultLabel.setLabel( $( '<p>Il nome utente è obbligatorio.</p>' ) );
} else {
searchDialog.resultLabel.setLabel( 'Ricerca in corso...' );
getRegistration( user, function ( registration ) {
if ( registration ) {
getUserContribs( user, registration, function ( contribs ) {
searchDialog.resultLabel.setLabel( $( '<p>' + formatResult( registration, contribs ) + '</p>' ) );
} );
} else {
searchDialog.resultLabel.setLabel( $( '<p>L\'utente ' + user + ' non è registrato.</p>' ) );
}
} );
}
}
/**
* Crea la finestra di dialogo per la ricerca delle informazioni utente.
*
* @return {Object} L'oggetto derivato da OO.ui.Dialog che rappresenta la finestra
*/
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 );
var resultLabel = new OO.ui.LabelWidget( {
classes: [ 'grv-container' ],
label: ' '
} );
var 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.textInput = textInput;
this.resultLabel = resultLabel;
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'
} );
}
$( function () {
var windowManager;
var portletLink = mw.util.addPortletLink( 'p-navigation', '#', 'Verifica requisiti di voto' );
$( portletLink ).click( function ( event ) {
event.preventDefault();
mw.loader.using( [ 'mediawiki.api', 'oojs-ui-core', 'oojs-ui-widgets',
'oojs-ui-windows', 'mediawiki.widgets.UserInputWidget' ], function () {
if ( !searchDialog ) {
searchDialog = buildSearchDialog();
windowManager = new OO.ui.WindowManager();
$( 'body' ).append( windowManager.$element );
windowManager.addWindows( [ searchDialog ] );
}
windowManager.openWindow( searchDialog );
} );
} );
} );
}( mediaWiki, jQuery ) );