MediaWiki:Gadget-VFonLine.js: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
m opzioni jshint
m mw.util.addCSS
 
(3 versioni intermedie di 2 utenti non mostrate)
Riga 1:
/**
* Gadget-VFonLine.js
* Aggiunge a Speciale:OsservatiSpeciali e Speciale:UltimeModifiche le funzionalità
* dette 'Vandal Fighter on Line' (VFOL).
* RiscrittoRiscrittura da zero a partire dadi:
* http://it.wikipedia.org/w/index.php?title=Wikipedia:Monobook.js/VFonLine.js&oldid=27133290
* http://it.wikipedia.org/w/index.php?title=MediaWiki:Gadget-VFonLine.js&oldid=38709825
*
* @author [[https://it.wikipedia.org/wiki/Utente:Rotpunkt]]
*/
/* global mediaWiki, jQuery, OO */
/*jshint unused: false */
/*global mediaWiki, jQuery */
 
( function ( mw, $ ) {
Riga 15:
 
// Per retrocompatibilità con il vecchio gadget, whitelist e blacklist sono contenute in due cookie.
// Le operazioni sui cookie sono isolate nelleper primepoterle quattroin funzioni per poterlefuturo sostituire facilmente.
 
/**
* Restituisce il contenuto del cookie 'wikiVFOL', che può valere 'abilitato' o 'disabilitato'.
*
* @return {string} Il contenuto del cookie 'wikiVFOL'
*/
function getState() {
return $.cookie( 'wikiVFOL' ) === 'abilitato' ? 'abilitato' : 'disabilitato';
}
 
/**
* Salva la stringa 'abilitato' o 'disabilitato' nel cookie 'wikiVFOL'.
*
* @param {string} state - la stringa da salvare.
*/
function setState( state ) {
$.cookie( 'wikiVFOL', state, { expires: 365, path: '/' } );
}
 
/**
* Restituisce il contenuto dei cookie per la whitelist e la blacklist.
*
* @param {string} id - L'identificativo per scegliere whitelist (vfol1) o blacklist(vfol2)
* @return {string} Il contenuto del cookie
*/
function getList( id ) {
var content = $.cookie( id === 'vfol1' ? 'wikiWhiteList' : 'wikiBlackList' );
Line 29 ⟶ 46:
}
 
/**
* Salva la whitelist o la blacklist nei relativi cookie.
*
* @param {string} id - L'identificativo per scegliere whitelist (vfol1) o blacklist(vfol2)
* @param {string} content - Il contenuto del cookie
*/
function setList( id, content ) {
$.cookie( id === 'vfol1' ? 'wikiWhiteList' : 'wikiBlackList',
Line 34 ⟶ 57:
}
 
/**
// Visualizza la finestra di dialogo per modificare whitelist e blacklist
* Crea la finestra di dialogo per modificare whitelist e blacklist.
function showConfigDialog( id ) {
*
var $textarea = $( '<textarea>' ).css( { width: '100%', height: '100%' } )
* @return {Object} L'oggetto derivato da OO.ui.Dialog che rappresenta la finestra
.val( getList( id ).replace( /@/g, '\n' ) );
*/
$( '#gvfol-dialog' ).html( $textarea ).dialog( {
function buildConfigDialog() {
title: 'VFOL Configurazione ' + ( id === 'vfol1' ? 'whitelist' : 'blacklist' ),
var style = '.gvf-container-button { width: 100%; text-align: center; margin-top: 10px }';
width: 300,
mw.util.addCSS( style );
height: 400,
function ConfigDialog( config ) {
modal: true,
ConfigDialog.parent.call( this, config );
buttons: {
}
'Salva': function () {
OO.inheritClass( ConfigDialog, OO.ui.Dialog );
var text = $textarea.val();
ConfigDialog.static.name = 'configDialog';
text = text.replace( /^\s*/gm, '' );
ConfigDialog.prototype.initialize = function () {
text = text.replace( /\s*$/gm, '' );
var self = this;
setList( id, text.replace( /\\n/g, '@' ) );
ConfigDialog.parent.prototype.initialize.call( this );
$( this ).dialog( 'close' );
var textInput = new OO.ui.MultilineTextInputWidget( {
},
'Annulla'rows: function () {12
} );
$( this ).dialog( 'close' );
var textInputLayout = new OO.ui.FieldLayout( textInput, {
}
} label: '',
align: 'top'
} );
var okButton = new OO.ui.ButtonWidget( {
label: 'Salva',
} ).on( 'click', function () {
var text = textInput.getValue();
text = text.replace( /^\s*/gm, '' );
text = text.replace( /\s*$/gm, '' );
setList( self.currId, text.replace( /\n/g, '@' ) );
self.close();
} );
var cancelButton = new OO.ui.ButtonWidget( {
label: 'Annulla'
} ).on( 'click', function () {
self.close();
} );
var buttons = new OO.ui.HorizontalLayout( {
items: [ okButton, cancelButton ],
classes: [ 'gvf-container-button' ]
} );
this.textInput = textInput;
this.textInputLayout = textInputLayout;
this.panelLayout = new OO.ui.PanelLayout( { padded: true, expanded: false } );
this.panelLayout.$element.append( textInputLayout.$element, buttons.$element );
this.$body.append( this.panelLayout.$element );
};
ConfigDialog.prototype.getBodyHeight = function () {
return this.panelLayout.$element.outerHeight( true );
};
return new ConfigDialog( {
size: 'small'
} );
}
 
/**
* Aggiunge o rimuove i portlet link 'Modifica la whitelist' e 'Modifica la blacklist'.
*
* @param {boolean} visible - Se impostato a true aggiunge i portlet link, altrimenti li rimuove
*/
function togglePortletLinks( visible ) {
var configDialog, windowManager;
if ( visible ) {
mw.util.addPortletLink( 'p-tb', '#', 'Modifica la whitelist', 'vfol1',
Line 67 ⟶ 127:
var id = $( this ).attr( 'id' );
event.preventDefault();
mw.loader.using( [ 'jquery.oojs-ui.dialog-core'], function'oojs-ui-widgets', ()'oojs-ui-windows' {] )
showConfigDialog.done( idfunction (); {
} if ( !configDialog ); {
configDialog = buildConfigDialog();
windowManager = new OO.ui.WindowManager();
$( 'body' ).append( windowManager.$element );
windowManager.addWindows( [ configDialog ] );
}
configDialog.textInput.setValue( getList( id ).replace( /@/g, '\n' ) );
configDialog.textInputLayout.setLabel( 'VFOL Configurazione ' + ( id === 'vfol1' ? 'whitelist' : 'blacklist' ) );
configDialog.currId = id;
windowManager.openWindow( configDialog );
} )
.fail( function () {
console.error( 'Impossibile avviare l\'accessorio VFonLine.' );
} );
} );
} else {
Line 76 ⟶ 149:
}
 
/**
// Gestore del click sui link "wl" e "bl"
* Gestore del click sui link "wl" e "bl".
*
* @param {string} username - L'utente sul quale si è effettuato il click
* @param {string} action - La stringa su cui si è cliccato (+wl, -wl, +bl o -bl)
*/
function anchorHandler( username, action ) {
var id = action.substring( 1, 3 ) === 'wl' ? 'vfol1' : 'vfol2';
var users, id, idx;
idvar users = action.substringgetList( 1, 3id ) === 'wl' ? 'vfol1'[] : getList( id ).split( 'vfol2@' );
var idx = $.inArray( username, users );
users = getList( id ) === '' ? [] : getList( id ).split( '@' );
idx = $.inArray( username, users );
if ( action.charAt( 0 ) === '+' && idx === -1 ) {
users.unshift( username );
Line 94 ⟶ 171:
}
 
/**
// Modifica una entry in OsservatiSpeciali o UltimeModifiche
* Funzione di utilità per editWatchlist, modifica una entry
* in OsservatiSpeciali o UltimeModifiche.
*
* @param {string} $el - L'elemento jQuery da modificare
* @param {string[]} usersWl - Utenti nella whitelist
* @param {string[]} usersBl - Utenti nella blacklist
* @param {string} color - Colore da applicare alla riga, per le modifiche raggruppate
* @return {string} Il colore applicato alla riga, per le modifiche raggruppate
*/
function editEntry( $el, usersWl, usersBl, color ) {
var $wl, $bl, inWl, inBl, username;
Line 103 ⟶ 189:
// color
if ( color === undefined ) {
color = inWl ? '#ccffcc' :
inBl ? '#ffcccc' :
username$el.matchhasClass( /^(?:[0'mw-9]{1,3}\.){3}[0changeslist-9]{1,3}$/user-unregistered' ) ? '#ffffbb' : '';
}
$el.css( 'background-color', color );
Line 110 ⟶ 197:
$wl = $( '<a>' ).attr( 'href', '#' ).text( inWl ? '-wl' : '+wl' );
$bl = $( '<a>' ).attr( 'href', '#' ).text( inBl ? '-bl' : '+bl' );
$.each( [ $wl, $bl ], function ( i, el ) {
el.click( function () {
username = $( this ).parent( 'span' ).prev( 'a.mw-userlink' ).text();
Line 122 ⟶ 209:
}
 
/**
// Modifica OsservatiSpeciali o UltimeModifiche
* Modifica la pagine Speciale:OsservatiSpeciali o Speciale:UltimeModifiche.
*/
function editWatchlist() {
var usersWl, usersBl= getList( 'vfol1' ).split( '@' );
usersWlvar usersBl = getList( 'vfol1vfol2' ).split( '@' );
usersBl = getList( 'vfol2' ).split( '@' );
$( 'h4' ).each( function () {
// visualizzazione avanzata con modifiche raggruppate: gruppo di modifiche
Line 133 ⟶ 221:
$( this ).find( 'tr:not( :first )' ).each( function () {
var color = editEntry( $( this ), usersWl, usersBl );
edits[ color ] = true;
} );
$( this ).find( 'tr:first' ).each( function () {
editEntry( $( this ), usersWl, usersBl,
edits[ '#ffcccc' ] ? '#ffcccc' :
edits[ '#ffffbb' ] ? '#ffffbb' :
!edits[ '' ] ? '#ccffcc' : '' );
} );
} );
Line 154 ⟶ 242:
 
$( function () {
var state, portletLink;
// setup portlet link
var state = getState();
var portletLink = mw.util.addPortletLink( 'p-tb', '#', 'VFonLine ' + state, 'vfol' );
$( portletLink ).click( function ( event ) {
event.preventDefault();
Line 166 ⟶ 253:
} );
togglePortletLinks( state === 'abilitato' );
// setup dialog
$( '<div>' ).attr( 'id', 'gvfol-dialog' ).appendTo( 'body' );
// modifica watchlist
if ( $.inArray( mw.config.get( 'wgPageName' ), [ 'Speciale:OsservatiSpeciali', 'Speciale:UltimeModifiche' ] ) !== -1 &&
state === 'abilitato' ) {
editWatchlist();