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

Contenuto cancellato Contenuto aggiunto
m nome parametro
m mw.util.addCSS
 
(6 versioni intermedie di 3 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 */
/*jslint unparam: true */
/*global window: false, document: false, mediaWiki: false, jQuery: false */
 
( function ( mw, $ ) {
'use strict';
 
// 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.
function getState() {
return $.cookie('wikiVFOL') === 'abilitato' ? 'abilitato' : 'disabilitato';
}
 
/**
function setState(state) {
* Restituisce il contenuto del cookie $.cookie('wikiVFOL', state,che {può expires:valere 365,'abilitato' path:o '/disabilitato' });.
*
}
* @return {string} Il contenuto del cookie 'wikiVFOL'
*/
function getState() {
return $.cookie( 'wikiVFOL' ) === 'abilitato' ? 'abilitato' : 'disabilitato';
}
 
/**
function getList(id) {
* Salva la stringa 'abilitato' o 'disabilitato' nel cookie 'wikiVFOL'.
var content = $.cookie(id === 'vfol1' ? 'wikiWhiteList' : 'wikiBlackList');
*
return content ? decodeURIComponent(content) : '';
* @param {string} state - la stringa da salvare.
}
*/
function setState( state ) {
$.cookie( 'wikiVFOL', state, { expires: 365, path: '/' } );
}
 
/**
function setList(id, content) {
* Restituisce il contenuto dei cookie per la whitelist e la blacklist.
$.cookie(id === 'vfol1' ? 'wikiWhiteList' : 'wikiBlackList',
*
encodeURIComponent(content), { expires: 365, path: '/' });
* @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' );
return content ? decodeURIComponent( content ) : '';
}
 
/**
// Visualizza la finestra di dialogo per modificare whitelist e blacklist
* Salva la whitelist o la blacklist nei relativi cookie.
function showConfigDialog(id) {
*
var $textarea = $('<textarea>').css({ width: '100%', height: '100%' })
* @param {string} id - L'identificativo per scegliere whitelist (vfol1) o blacklist(vfol2)
.val(getList(id).replace(/@/g, '\n'));
* @param {string} content - Il contenuto del cookie
$('#gvfol-dialog').html($textarea).dialog({
*/
title: 'VFOL Configurazione ' + (id === 'vfol1' ? 'whitelist' : 'blacklist'),
function setList( id, content ) {
width: 300,
$.cookie( id === 'vfol1' ? 'wikiWhiteList' : 'wikiBlackList',
height: 400,
encodeURIComponent( content ), { expires: 365, path: '/' } );
modal: true,
}
buttons: {
'Salva': function () {
var text = $textarea.val();
text = text.replace(/^\s*/gm, '');
text = text.replace(/\s*$/gm, '');
setList(id, text.replace(/\\n/g, '@'));
$(this).dialog('close');
},
'Annulla': function () {
$(this).dialog('close');
}
}
});
}
 
/**
function togglePortletLinks(visible) {
* Crea la finestra di dialogo per modificare whitelist e blacklist.
if (visible) {
*
mw.util.addPortletLink('p-tb', '#', 'Modifica la whitelist', 'vfol1',
* @return {Object} L'oggetto derivato da OO.ui.Dialog che rappresenta la finestra
'VFOL whitelist', '1', $('#vfol').next('li').length ? $('#vfol').next('li') : null);
*/
mw.util.addPortletLink('p-tb', '#', 'Modifica la blacklist', 'vfol2',
function buildConfigDialog() {
'VFOL blacklist', '2', $('#vfol1').next('li').length ? $('#vfol1').next('li') : null);
var style = '.gvf-container-button { width: 100%; text-align: center; margin-top: 10px }';
$('#vfol1, #vfol2').click(function (event) {
mw.util.addCSS( style );
var id = $(this).attr('id');
function ConfigDialog( config ) {
event.preventDefault();
ConfigDialog.parent.call( this, config );
mw.loader.using(['jquery.ui.dialog'], function () {
}
showConfigDialog(id);
OO.inheritClass( ConfigDialog, OO.ui.Dialog );
});
ConfigDialog.static.name = 'configDialog';
});
ConfigDialog.prototype.initialize = function () {
} else {
var self = this;
$('#vfol1, #vfol2').remove();
ConfigDialog.parent.prototype.initialize.call( this );
}
var textInput = new OO.ui.MultilineTextInputWidget( {
}
rows: 12
} );
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'
} );
}
 
/**
// Gestore del click sui link "wl" e "bl"
* Aggiunge o rimuove i portlet link 'Modifica la whitelist' e 'Modifica la blacklist'.
function anchorHandler(username, action) {
*
var users, id, idx;
* @param {boolean} visible - Se impostato a true aggiunge i portlet link, altrimenti li rimuove
id = action.substring(1, 3) === 'wl' ? 'vfol1' : 'vfol2';
*/
users = getList(id) === '' ? [] : getList(id).split('@');
function togglePortletLinks( visible ) {
idx = $.inArray(username, users);
var configDialog, windowManager;
if (action.charAt(0) === '+' && idx === -1) {
if ( visible ) {
users.unshift(username);
mw.util.addPortletLink( 'p-tb', '#', 'Modifica la whitelist', 'vfol1',
} else if (action.charAt(0) === '-' && idx !== -1) {
'VFOL whitelist', '1', $( '#vfol' ).next( 'li' ).length ? $( '#vfol' ).next( 'li' ) : null );
users.splice(idx, 1);
mw.util.addPortletLink( 'p-tb', '#', 'Modifica la blacklist', 'vfol2',
}
'VFOL blacklist', '2', $( '#vfol1' ).next( 'li' ).length ? $( '#vfol1' ).next( 'li' ) : null );
if (id === 'vfol1') {
$( '#vfol1, #vfol2' ).click( function ( event ) {
users.sort();
var id = $( this ).attr( 'id' );
}
event.preventDefault();
setList(id, users.join('@'));
mw.loader.using( [ 'oojs-ui-core', 'oojs-ui-widgets', 'oojs-ui-windows' ] )
window.___location.reload();
.done( function () {
}
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 {
$( '#vfol1, #vfol2' ).remove();
}
}
 
/**
// Modifica una entry in OsservatiSpeciali o UltimeModifiche
* Gestore del click sui link "wl" e "bl".
function editEntry($el, usersWl, usersBl, color) {
*
var $wl, $bl, inWl, inBl, username;
* @param {string} username - L'utente sul quale si è effettuato il click
$el.find('a.mw-userlink').each(function () {
* @param {string} action - La stringa su cui si è cliccato (+wl, -wl, +bl o -bl)
username = $(this).text();
*/
inWl = $.inArray(username, usersWl) !== -1;
function anchorHandler( username, action ) {
inBl = $.inArray(username, usersBl) !== -1;
var id = action.substring( 1, 3 ) === 'wl' ? 'vfol1' : 'vfol2';
// color
var users = getList( id ) === '' ? [] : getList( ifid ).split(color ==='@' undefined) {;
var idx = $.inArray( username, users );
color = inWl ? '#ccffcc' : inBl ? '#ffcccc' :
if ( action.charAt( 0 ) === '+' && idx === -1 ) {
username.match(/^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/) ? '#ffffbb' : '';
users.unshift( username );
}
} else if ( action.charAt( 0 ) === '-' && idx !== -1 ) {
$el.css('background-color', color);
users.splice( idx, 1 );
// anchors
}
$wl = $('<a>').attr('href', '#').text(inWl ? '-wl' : '+wl');
if ( id === 'vfol1' ) {
$bl = $('<a>').attr('href', '#').text(inBl ? '-bl' : '+bl');
users.sort();
$.each([$wl, $bl], function (i, el) {
}
el.click(function () {
setList( id, users.join( '@' ) );
username = $(this).parent('span').prev('a.mw-userlink').text();
window.___location.reload();
anchorHandler(username, $(this).text());
}
return false;
});
});
$('<span>').append(' (', $wl, ' ', $bl, ')').insertAfter($(this));
});
return color;
}
 
/**
// Modifica OsservatiSpeciali o UltimeModifiche
* Funzione di functionutilità per editWatchlist(), modifica una {entry
* in OsservatiSpeciali o UltimeModifiche.
var usersWl, usersBl;
*
usersWl = getList('vfol1').split('@');
* @param {string} $el - L'elemento jQuery da modificare
usersBl = getList('vfol2').split('@');
* @param {string[]} usersWl - Utenti nella whitelist
$('h4').each(function () {
* @param {string[]} usersBl - Utenti nella blacklist
// visualizzazione avanzata con modifiche raggruppate: gruppo di modifiche
* @param {string} color - Colore da applicare alla riga, per le modifiche raggruppate
$(this).next('div').find('table.mw-collapsible').each(function () {
* @return {string} Il colore applicato alla riga, per le modifiche raggruppate
var edits = {};
*/
$(this).find('tr:not(:first)').each(function () {
var color = function editEntry( $(this)el, usersWl, usersBl, color ); {
var $wl, $bl, inWl, inBl, username;
edits[color] = true;
$el.find( 'a.mw-userlink' ).each( function () {
});
username = $( this ).text();
$(this).find('tr:first').each(function () {
inWl = $.inArray( username, usersWl ) !== -1;
editEntry($(this), usersWl, usersBl,
inBl = $.inArray( username, usersBl ) !== -1;
edits['#ffcccc'] ? '#ffcccc' :
// color
edits['#ffffbb'] ? '#ffffbb' :
if ( color === undefined ) {
!edits[''] ? '#ccffcc' : '');
color = inWl ? '#ccffcc' :
});
inBl ? '#ffcccc' :
});
$el.hasClass( 'mw-changeslist-user-unregistered' ) ? '#ffffbb' : '';
// visualizzazione avanzata con modifiche raggruppate: modifiche singole
}
$(this).next('div').find('table:not(.mw-collapsible) tr').each(function () {
$el.css( 'background-color', color );
editEntry($(this), usersWl, usersBl);
// anchors
});
$wl = $( '<a>' ).attr( 'href', '#' ).text( inWl ? '-wl' : '+wl' );
// visualizzazione standard senza modifiche raggruppate
$bl = $( '<a>' ).attr( 'href', '#' ).text( inBl ? '-bl' : '+bl' );
$(this).next('ul.special').find('li').each(function () {
$.each( [ $wl, $bl ], function ( i, el ) {
editEntry($(this), usersWl, usersBl);
el.click( function () {
});
username = $( this ).parent( 'span' ).prev( 'a.mw-userlink' ).text();
});
anchorHandler( username, $( this ).text() );
}
return false;
} );
} );
$( '<span>' ).append( ' ( ', $wl, ' ', $bl, ' )' ).insertAfter( $( this ) );
} );
return color;
}
 
/**
$(document).ready(function () {
* Modifica la pagine Speciale:OsservatiSpeciali o Speciale:UltimeModifiche.
var state, portletLink;
*/
// setup portlet link
function editWatchlist() {
state = getState();
var usersWl = getList( 'vfol1' ).split( '@' );
portletLink = mw.util.addPortletLink('p-tb', '#', 'VFonLine ' + state, 'vfol');
var usersBl = getList( 'vfol2' ).split( '@' );
$(portletLink).click(function (event) {
$( 'h4' ).each( function () {
event.preventDefault();
// visualizzazione avanzata con modifiche raggruppate: gruppo di modifiche
state = getState() === 'abilitato' ? 'disabilitato' : 'abilitato';
$( this ).next( 'div' ).find( 'table.mw-collapsible' ).each( function () {
setState(state);
var edits = {};
$(portletLink).find('a').text('VFonLine ' + state);
$( this ).find( 'tr:not( :first )' ).each( function () {
togglePortletLinks(state === 'abilitato');
var color = editEntry( $( this ), usersWl, usersBl );
});
edits[ color ] = true;
togglePortletLinks(state === 'abilitato');
} );
// setup dialog
$( this ).find( 'tr:first' ).each( function () {
$('<div>').attr('id', 'gvfol-dialog').appendTo('body');
editEntry( $( this ), usersWl, usersBl,
// modifica watchlist
edits[ '#ffcccc' ] ? '#ffcccc' :
if ($.inArray(mw.config.get('wgPageName'), ['Speciale:OsservatiSpeciali', 'Speciale:UltimeModifiche']) !== -1 &&
edits[ '#ffffbb' ] ? '#ffffbb' :
state === 'abilitato') {
!edits[ '' ] ? '#ccffcc' : '' );
editWatchlist();
} );
}
} });
// visualizzazione avanzata con modifiche raggruppate: modifiche singole
}(mediaWiki, jQuery));
$( this ).next( 'div' ).find( 'table:not( .mw-collapsible ) tr' ).each( function () {
editEntry( $( this ), usersWl, usersBl );
} );
// visualizzazione standard senza modifiche raggruppate
$( this ).next( 'ul.special' ).find( 'li' ).each( function () {
editEntry( $( this ), usersWl, usersBl );
} );
} );
}
 
$( function () {
// setup portlet link
var state = getState();
var portletLink = mw.util.addPortletLink( 'p-tb', '#', 'VFonLine ' + state, 'vfol' );
$( portletLink ).click( function ( event ) {
event.preventDefault();
state = getState() === 'abilitato' ? 'disabilitato' : 'abilitato';
setState( state );
$( portletLink ).find( 'a' ).text( 'VFonLine ' + state );
togglePortletLinks( state === 'abilitato' );
} );
togglePortletLinks( state === 'abilitato' );
// modifica watchlist
if ( $.inArray( mw.config.get( 'wgPageName' ), [ 'Speciale:OsservatiSpeciali', 'Speciale:UltimeModifiche' ] ) !== -1 &&
state === 'abilitato' ) {
editWatchlist();
}
} );
}( mediaWiki, jQuery ) );