MediaWiki:Gadget-nav-requisiti.js: differenze tra le versioni
Contenuto cancellato Contenuto aggiunto
utilizzo di OOjs UI e mw.Api |
aggiorno v2 a requisiti di voto arbcom, vedi discussione |
||
(8 versioni intermedie di 3 utenti non mostrate) | |||
Riga 9:
* @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;
var searchDialog2;
/**
* Ricerca
*
* @param {
* @param {function} infoHandler - La funzione da richiamare con i risultati
*/
function
new mw.Api().get( {
action: 'query',
Line 29 ⟶ 32:
format: 'json'
} ).done( function ( data ) {
} );
}
Line 35 ⟶ 38:
/**
* Ricerca i 500 contributi utente successivi alla data "start".
*
* @param {string}
* @param {string} start - La data di inizio
* @param {function} contribsHandler - */
function getUserContribs( user, start, contribsHandler ) {
Line 52 ⟶ 56:
contribsHandler( data.query.usercontribs );
} );
}
function verifyVotingEligibility( user, date, resultHandler ) {
$.get( `https://itwikiapi.toolforge.org/v1/users/${ user }/voting-eligibility/${ date }` )
.then( function ( data ) {
resultHandler( data );
} );
}
/**
* 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 ) {
Line 77 ⟶ 90:
/**
* Formatta le informazioni su data di registrazione, primo, 50esimo e 500esimo edit di un utente.
*
* @param {string}
* @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: ' + (registration ? parseTimestamp( registration ) : 'pre 2006') + '<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;
}
function formatResult2( result ) {
const registrationTS = result.requirements.registration.timestamp,
oldActivity = result.requirements.oldactivity,
recentActivity = result.requirements.recentactivity;
var info = '';
info += '<p><b>Registrazione</b>: ' + ( registrationTS ? parseTimestamp( registrationTS ) : 'pre 2006' ) + '</p>';
info += '<p><b>Attività minima complessiva</b><br>' + oldActivity.editcount + ' modifiche valide tra ';
info += parseTimestamp( oldActivity.interval.starttimestamp ) + ' e ' + parseTimestamp( oldActivity.interval.endtimestamp ) + '</p>';
info += '<p><b>Attività minima recente</b><br>' + recentActivity.editcount + ' modifiche valide tra ';
info += parseTimestamp( recentActivity.interval.starttimestamp ) + ' e ' + parseTimestamp( recentActivity.interval.endtimestamp ) + '</p>';
info += '<p><b>Diritto di voto</b>: ' + ( result.eligible ? 'sì' : 'no' ) + '</p>';
return info;
}
Line 93 ⟶ 123:
*/
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...' );
if (
getUserContribs( user, userInfo.registration || '2001', function ( contribs ) {
searchDialog.resultLabel.setLabel( $( '<p>' + formatResult( userInfo.registration, contribs ) + '</p>' ) );
} );
} else {
searchDialog.resultLabel.setLabel( $( '<p>L\'utente ' + user + ' non è registrato.</p>' ) );
}
} );
}
}
function searchHandler2() {
var user = $.trim( searchDialog2.textInput.getValue() );
var date = $.trim( searchDialog2.dateInput.getValue() );
if ( !user ) {
searchDialog2.resultLabel.setLabel( $( '<p>Il nome utente è obbligatorio.</p>' ) );
} else if ( !date ) {
searchDialog2.resultLabel.setLabel( $( '<p>Inserire una data valida.</p>' ) );
} else {
searchDialog2.resultLabel.setLabel( 'Ricerca in corso...' );
verifyVotingEligibility( user, date, function ( result ) {
if ( result.requirements.registration.requirementsatisfied ) {
searchDialog2.resultLabel.setLabel( $( formatResult2( result ) ) );
} else {
searchDialog2.resultLabel.setLabel( $( '<p>L\'utente ' + user + ' non è registrato.</p>' ) );
}
} );
Line 112 ⟶ 161:
/**
* 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 }';
function SearchDialog( config ) {
SearchDialog.parent.call( this, config );
Line 124 ⟶ 175:
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, {
Line 146 ⟶ 197:
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 buildSearchDialog2() {
var style = '.grv-container { height: 164px; margin: .4em 0 .6em }' +
'.grv-container-button { width: 100%; text-align: center }';
mw.util.addCSS( style );
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( {
required: true
} );
textInput.on( 'enter', searchHandler );
var textInputLayout = new OO.ui.FieldLayout( textInput, {
label: 'Nome utente:',
align: 'top'
} );
var $dateOverlay = $( '<div>' ).appendTo( this.$element );
var dateInput = new mw.widgets.DateInputWidget( {
required: true,
$overlay: $dateOverlay
} );
var dateInputLayout = new OO.ui.FieldLayout( dateInput, {
label: 'Data inizio procedura:',
align: 'top'
} );
var searchButton = new OO.ui.ButtonWidget( {
label: 'Cerca',
} ).on( 'click', searchHandler2 );
var cancelButton = new OO.ui.ButtonWidget( {
label: 'Annulla'
} ).on( 'click', function () {
searchDialog2.close();
} );
var buttons = new OO.ui.HorizontalLayout( {
items: [ searchButton, cancelButton ],
classes: [ 'grv-container-button' ],
align: 'top'
} );
this.textInput = textInput;
this.dateInput = dateInput;
this.resultLabel = resultLabel;
this.panelLayout = new OO.ui.PanelLayout( { padded: true, expanded: false } );
this.panelLayout.$element.append( textInputLayout.$element, dateInputLayout.$element, resultLabel.$element, buttons.$element );
this.$body.append( this.panelLayout.$element );
};
Line 159 ⟶ 272:
$( function () {
var windowManager;
var windowManager2;
var portletLink = mw.util.addPortletLink( 'p-navigation', '#', 'Verifica requisiti di voto' );
var portletLink2 = mw.util.addPortletLink( 'p-navigation', '#', 'Verifica requisiti di voto arbcom' );
$( portletLink ).click( function ( event ) {
event.preventDefault();
mw.loader.using( [ 'mediawiki.api', 'oojs-ui-core', 'oojs-ui-widgets',
'oojs-ui-windows', 'mediawiki.widgets.UserInputWidget' ]
.done( function () { if ( !searchDialog ) {
searchDialog = buildSearchDialog();
windowManager = new OO.ui.WindowManager();
$( 'body' ).append( windowManager.$element );
windowManager.addWindows( [ searchDialog ] );
}
windowManager.openWindow( searchDialog );
} )
.fail( function () {
console.error( 'Impossibile avviare l\'accessorio "requisiti".' );
} );
} );
$( portletLink2 ).click( function ( event ) {
event.preventDefault();
mw.loader.using( [ 'mediawiki.api', 'oojs-ui-core', 'oojs-ui-widgets',
'oojs-ui-windows', 'mediawiki.widgets.UserInputWidget',
'mediawiki.widgets.DateInputWidget' ] )
.done( function () {
if ( !searchDialog2 ) {
searchDialog2 = buildSearchDialog2();
windowManager2 = new OO.ui.WindowManager();
$( 'body' ).append( windowManager2.$element );
windowManager2.addWindows( [ searchDialog2 ] );
}
windowManager2.openWindow( searchDialog2 );
} );
} );
} );
|