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

Contenuto cancellato Contenuto aggiunto
m Jalo ha spostato la pagina Utente:Rotpunkt/Gadgets/Gadget-VFonLine.js a MediaWiki:Gadget-VFonLine.js senza lasciare redirect
aggiunti spazi parentesi, tab, jslint=>jshint per mw:Manual:Coding conventions/JavaScript
Riga 8:
* @author [[Utente:Rotpunkt]]
*/
/* global window: false, document: false, mediaWiki: false, jQuery: false */
/*jslint unparam: true */
/* jshint unused: false */
/*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 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 );
} );
});
} });
}
}
 
$(document).ready( 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 ) );