MediaWiki:Gadget-tradLink.js
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.
/**
* Considera il testo selezionato come il titolo di una voce straniera, e lo
* trasforma nel titolo dell'equivalente voce italiana
*/
/* global mediaWiki, jQuery, OO */
( function( mw, $ ) {
'use strict';
var cookieName = 'tradLinkLangs';
var optDialog;
/**
* Basato su [[Special:Permalink/109422525]] e gli esempi su mw.org
*/
function showDialog() {
if ( optDialog ) {
windowManager.openWindow( optDialog );
return;
}
mw.util.addCSS( '.tradlinks-buttons { width: 100%; text-align: center; margin-top: 10px }' );
function OptDialog( config ) {
OptDialog.parent.call( this, config );
}
OO.inheritClass( OptDialog, OO.ui.Dialog );
OptDialog.static.name = 'optDialog';
OptDialog.static.title = 'Configurazione del tool TradLink'; // XXX
OptDialog.prototype.initialize = function () {
var self = this;
OptDialog.parent.prototype.initialize.call( this );
this.fields = [];
var lingue = getLingue();
var count = 1;
lingue.forEach( function( val ) {
var input = new OO.ui.TextInputWidget( {
value: val || '',
maxLength: 3
} );
var field = new OO.ui.FieldLayout(
input,
{
label: 'Alt+' + count + ':',
align: 'left'
}
);
self.fields.push( field );
count++;
} );
var fieldsetLayout = new OO.ui.FieldsetLayout( {
items: this.fields,
label: 'Elenco delle lingue da utilizzare per il tool TradLink'
} );
function submitHandler() {
var lingue = self.fields.map( function( f ) {
return f.fieldWidget.getValue().trim();
} ).join();
mw.cookie.set( cookieName, lingue, 20 * 365 * 24 * 60 * 60 * 1000 );
optDialog.close();
}
var submitButton = new OO.ui.ButtonWidget(
{ label: 'Imposta' }
).on( 'click', submitHandler );
var cancelButton = new OO.ui.ButtonWidget(
{ label: 'Annulla' }
).on( 'click', function () { optDialog.close(); } );
var buttonsLayout = new OO.ui.HorizontalLayout( {
items: [ submitButton, cancelButton ],
classes: [ 'tradlinks-buttons' ]
} );
this.panelLayout = new OO.ui.PanelLayout( { padded: true, expanded: false } );
this.panelLayout.$element.append( fieldsetLayout.$element, buttonsLayout.$element );
this.$body.append( this.panelLayout.$element );
};
OptDialog.prototype.getBodyHeight = function () {
return this.panelLayout.$element.outerHeight( true );
};
optDialog = new OptDialog( {
size: 'small'
} );
windowManager = new OO.ui.WindowManager();
$( 'body' ).append( windowManager.$element );
windowManager.addWindows( [ optDialog ] );
windowManager.openWindow( optDialog );
}
/**
* @return {array} Di codici lingua
*/
function getLingue() {
var value = mw.cookie.get( cookieName );
return value !== null
? value.split( ',' ).slice( 0, 9 )
: [ 'en', 'nl', 'de', 'sv', 'fr', 'ru', 'es', 'war', 'vi' ];
}
/**
* Routine principale
* @param {int} cod Indice della lingua (0-based)
*/
function tradLink( cod ) {
var lingue = getLingue();
$.getJSON( "https://" + lingue[ cod ] + ".wikipedia.org/w/api.php?callback=?", {
action: "query",
prop: "langlinks",
lllang: "it",
titles: $( '#wpTextbox1' ).textSelection( 'getSelection' ),
format: "json",
redirects: ""
},
function( data ) {
var selStart = document.getElementById( 'wpTextbox1' ).selectionStart,
selEnd = document.getElementById( 'wpTextbox1' ).selectionEnd;
if ( typeof( data ) != "undefined" &&
typeof( data.query ) != "undefined" &&
typeof( data.query.pages ) != "undefined"
) {
for ( var key in data.query.pages ) {
if ( typeof( data.query.pages[ key ] ) != "undefined" &&
typeof( data.query.pages[ key ].langlinks ) != "undefined" &&
typeof( data.query.pages[ key ].langlinks[ 0 ] ) != "undefined" &&
typeof( data.query.pages[ key ].langlinks[ 0 ][ "*" ] ) != "undefined"
) {
var trad = data.query.pages[ key ].langlinks[ 0 ][ "*" ];
$( '#wpTextbox1' ).textSelection( 'encapsulateSelection', {
pre: trad + "[",
post: "]"
} );
document.getElementById( 'wpTextbox1' ).selectionStart = selStart + trad.length;
document.getElementById( 'wpTextbox1' ).selectionEnd = selEnd + trad.length + 2;
return;
}
}
}
var noLinkLabel = "[NO LINK]";
$( '#wpTextbox1' ).textSelection( 'encapsulateSelection', {
pre: "",
post: noLinkLabel
} );
document.getElementById( 'wpTextbox1' ).selectionStart = selEnd;
document.getElementById( 'wpTextbox1' ).selectionEnd = selEnd + noLinkLabel.length;
} );
}
// setup hotkeys
$( '#wpTextbox1' ).keydown( function( event ) {
if ( event.altKey && !event.shiftKey && !event.ctrlKey && !event.metaKey ) {
if ( event.which === 48 ) {
event.preventDefault();
mw.loader.using( [ 'oojs-ui-core', 'oojs-ui-widgets', 'oojs-ui-windows', 'mediawiki.widgets' ] )
.done( showDialog )
.fail( function() { console.error( 'Errore configurazione tradLink' ); } );
} else if ( event.which >= 49 && event.which < 57 ) {
event.preventDefault();
tradLink( event.which - 49 );
}
}
} );
}( mediaWiki, jQuery ) );