MediaWiki:Gadget-VFonLine.js

Versione del 30 ott 2014 alle 18:30 di Rotpunkt (discussione | contributi) (aggiunti spazi parentesi, tab, jslint=>jshint per mw:Manual:Coding conventions/JavaScript)

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-VFonLine.js
 * Aggiunge a OsservatiSpeciali e UltimeModifiche le funzionalità dette 'Vandal Fighter on Line' (VFOL)
 * Riscritto da zero a partire da:
 * 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 [[Utente:Rotpunkt]]
 */
/* global mediaWiki: false, jQuery: false */
/* jshint unused: 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 nelle prime quattro funzioni per poterle sostituire facilmente.
	function getState() {
		return $.cookie( 'wikiVFOL' ) === 'abilitato' ? 'abilitato' : 'disabilitato';
	}

	function setState( state ) {
		$.cookie( 'wikiVFOL', state, { expires: 365, path: '/' } );
	}

	function getList( id ) {
		var content = $.cookie( id === 'vfol1' ? 'wikiWhiteList' : 'wikiBlackList' );
		return content ? decodeURIComponent( content ) : '';
	}

	function setList( id, content ) {
		$.cookie( id === 'vfol1' ? 'wikiWhiteList' : 'wikiBlackList',
			encodeURIComponent( content ), { expires: 365, path: '/' } );
	}

	// Visualizza la finestra di dialogo per modificare whitelist e blacklist
	function showConfigDialog( id ) {
		var $textarea = $( '<textarea>' ).css( { width: '100%', height: '100%' } )
							.val( getList( id ).replace( /@/g, '\n' ) );
		$( '#gvfol-dialog' ).html( $textarea ).dialog( {
			title: 'VFOL Configurazione ' + ( id === 'vfol1' ? 'whitelist' : 'blacklist' ),
			width: 300,
			height: 400,
			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 ) {
		if ( visible ) {
			mw.util.addPortletLink( 'p-tb', '#', 'Modifica la whitelist', 'vfol1',
				'VFOL whitelist', '1', $( '#vfol' ).next( 'li' ).length ? $( '#vfol' ).next( 'li' ) : null );
			mw.util.addPortletLink( 'p-tb', '#', 'Modifica la blacklist', 'vfol2',
				'VFOL blacklist', '2', $( '#vfol1' ).next( 'li' ).length ? $( '#vfol1' ).next( 'li' ) : null );
			$( '#vfol1, #vfol2' ).click( function ( event ) {
				var id = $( this ).attr( 'id' );
				event.preventDefault();
				mw.loader.using( ['jquery.ui.dialog'], function () {
					showConfigDialog( id );
				} );
			} );
		} else {
			$( '#vfol1, #vfol2' ).remove();
		}
	}

	// Gestore del click sui link "wl" e "bl"
	function anchorHandler( username, action ) {
		var users, id, idx;
		id = action.substring( 1, 3 ) === 'wl' ? 'vfol1' : 'vfol2';
		users = getList( id ) === '' ? [] : getList( id ).split( '@' );
		idx = $.inArray( username, users );
		if ( action.charAt( 0 ) === '+' &&  idx === -1 ) {
			users.unshift( username );
		} else if ( action.charAt( 0 ) === '-' && idx !== -1 ) {
			users.splice( idx, 1 );
		}
		if ( id === 'vfol1' ) {
			users.sort();
		}
		setList( id, users.join( '@' ) );
		window.___location.reload();
	}

	// Modifica una entry in OsservatiSpeciali o UltimeModifiche
	function editEntry( $el, usersWl, usersBl, color ) {
		var $wl, $bl, inWl, inBl, username;
		$el.find( 'a.mw-userlink' ).each( function () {
			username = $( this ).text();
			inWl = $.inArray( username, usersWl ) !== -1;
			inBl = $.inArray( username, usersBl ) !== -1;
			// color
			if ( color === undefined ) {
				color = inWl ? '#ccffcc' : inBl ? '#ffcccc' :
						username.match( /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/ ) ? '#ffffbb' : '';
			}
			$el.css( 'background-color', color );
			// anchors
			$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();
					anchorHandler( username, $( this ).text() );
					return false;
				} );
			} );
			$( '<span>' ).append( ' ( ', $wl, ' ', $bl, ' )' ).insertAfter( $( this ) );
		} );
		return color;
	}

	// Modifica OsservatiSpeciali o UltimeModifiche
	function editWatchlist() {
		var usersWl, usersBl;
		usersWl = getList( 'vfol1' ).split( '@' );
		usersBl = getList( 'vfol2' ).split( '@' );
		$( 'h4' ).each( function () {
			// visualizzazione avanzata con modifiche raggruppate: gruppo di modifiche
			$( this ).next( 'div' ).find( 'table.mw-collapsible' ).each( function () {
				var edits = {};
				$( 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' : '' );
				} );
			} );
			// visualizzazione avanzata con modifiche raggruppate: modifiche singole
			$( 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 () {
		var state, portletLink;
		// setup portlet link
		state = getState();
		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' );
		// 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();
		}
	} );
}( mediaWiki, jQuery ) );