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

Contenuto cancellato Contenuto aggiunto
Nessun oggetto della modifica
m fix
 
(54 versioni intermedie di 2 utenti non mostrate)
Riga 1:
/**
// TRADUCI WIKILINK DALL'INGLESE
* Considera il testo selezionato come il titolo di una voce straniera, e lo
function getLingue()
* trasforma nel titolo dell'equivalente voce italiana
{
*/
value = mw.cookie.get( 'tradLinkLangs' );
 
if (value)
/* global mediaWiki, jQuery, OO */
{
( function( mw, $ ) {
splitted = value.split(',');
'use strict';
return [splitted[0], splitted[1], splitted[2], splitted[3], splitted[4], splitted[5], splitted[6], splitted[7], splitted[8]];
var cookieName = 'tradLinkLangs';
var windowManager, 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 }' +
'.tradlinks-error { color: red; font-size: 120%; font-weight: bold; text-align:center }'
);
function OptDialog( config ) {
OptDialog.parent.call( this, config );
}
// TODO Valutare se sia possibile usare un ProcessDialog
OO.inheritClass( OptDialog, OO.ui.Dialog );
OptDialog.static.name = 'optDialog';
// XXX Questo non viene visualizzato nei Dialog di base
OptDialog.static.title = 'Configurazione del tool TradLink';
OptDialog.prototype.initialize = function () {
var self = this;
OptDialog.parent.prototype.initialize.call( this );
 
this.errorContainer = new OO.ui.LabelWidget( {
label: ''
} );
this.addError = function ( error ) {
self.errorContainer.setLabel(
new OO.ui.HtmlSnippet( '<span class="tradlinks-error">' + error + '</span>' )
);
};
this.clearError = function () {
self.errorContainer.setLabel( '' );
};
 
this.fields = [];
var lingue = getLingue();
var count = 1;
lingue.forEach( function( val ) {
var input = new OO.ui.TextInputWidget( {
value: val || '',
maxLength: 3
} );
input.on( 'change', self.clearError );
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'
} );
 
var submitButton = new OO.ui.ButtonWidget(
{ label: 'Imposta' }
).on( 'click', function () { submitDialogHandler( self.fields ); } );
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.errorContainer.$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, new OO.ui.MessageDialog() ] );
windowManager.openWindow( optDialog );
}
else
return ['en', 'nl', 'de', 'sv', 'fr', 'ru', 'es', 'war', 'vi'];
}
 
/**
function tradLink(cod)
* Valida l'aggiunta di nuove lingue
{
* @param {array} formFields
lingue = getLingue();
*/
function submitDialogHandler( formFields ) {
var toTrad = mw.html.escape($('#wpTextbox1').textSelection('getSelection'));
new mw.Api().get( {
var trad ="";
action: "sitematrix",
$.getJSON("http://"+lingue[cod]+".wikipedia.org/w/api.php?callback=?",
smtype: "language",
{
smlangprop: "code|site",
action: "query",
smsiteprop: "url",
prop: "langlinks",
smlimit: 5000,
lllang: "it",
format: "json"
titles: toTrad,
} ).done( function ( data ) {
format: "json",
var lingue = formFields.map( function( f ) {
redirects: ""
return f.fieldWidget.getValue().trim();
},
} );
function(data) {
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")
{
trad = data.query.pages[key].langlinks[0]["*"];
try {
mw.html.escape($('#wpTextbox1').textSelection('encapsulateSelection', { pre: trad + "[", post: "]" }));
} catch (err) {}
document.getElementById('wpTextbox1').selectionStart = selStart + trad.length;
document.getElementById('wpTextbox1').selectionEnd = selEnd + trad.length + 2;
 
var filteredMatrix = {};
return;
for ( var i in data.sitematrix ) {
}
var siteData = data.sitematrix[i];
}
if ( lingue.indexOf( siteData.code ) > -1 ) {
}
filteredMatrix[siteData.code] = siteData.site;
try {
}
mw.html.escape($('#wpTextbox1').textSelection('encapsulateSelection', { pre: "", post: "[NO LINK]" }));
}
} catch (err) {}
document.getElementById('wpTextbox1').selectionStart = selEnd;
for ( var j in lingue ) {
document.getElementById('wpTextbox1').selectionEnd = selEnd + 9;
var code = lingue[j];
});
if ( code === 'it' ) {
}
optDialog.addError( 'Il codice "it" non può essere usato' );
return;
}
if ( !( code in filteredMatrix ) ) {
optDialog.addError( 'Codice lingua non riconosciuto: ' + code );
return;
}
var found = false;
for ( var s in filteredMatrix[code] ) {
if ( filteredMatrix[code][s].url.indexOf( '.wikipedia.org' ) > -1 ) {
found = true;
break;
}
}
if ( !found ) {
optDialog.addError( 'Non esiste una Wikipedia nella seguente lingua: ' + code );
return;
}
}
 
mw.cookie.set( cookieName, lingue.join(), 20 * 365 * 24 * 60 * 60 * 1000 );
optDialog.close();
} ).fail( function ( e ) {
console.error( 'Impossibile ottenere sitematrix: ' + e );
} );
}
 
/**
* @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();
 
new mw.ForeignApi( '//' + lingue[cod] + '.wikipedia.org/w/api.php' ).get( {
action: "query",
prop: "langlinks",
lllang: "it",
titles: $( '#wpTextbox1' ).textSelection( 'getSelection' ),
format: "json",
redirects: ""
} ).done( function( data ) {
var selStart = document.getElementById( 'wpTextbox1' ).selectionStart,
selEnd = document.getElementById( 'wpTextbox1' ).selectionEnd;
if (
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 + "|"
} );
document.getElementById( 'wpTextbox1' ).selectionStart = selStart + trad.length + 1;
document.getElementById( 'wpTextbox1' ).selectionEnd = selEnd + trad.length + 1;
return;
}
}
}
var noLinkLabel = "[NO LINK]";
$( '#wpTextbox1' ).textSelection( 'encapsulateSelection', {
pre: "",
post: noLinkLabel
} );
document.getElementById( 'wpTextbox1' ).selectionStart = selEnd;
document.getElementById( 'wpTextbox1' ).selectionEnd = selEnd + noLinkLabel.length;
} );
}
 
$( function () {
// Codice derivato da [[MediaWiki:Gadget-tb-base.js]] di [[Utente:Rotpunkt]]
$( '#wpTextbox1' ).keydown( function( event ) {
// setup hotkeys
if ( event.altKey && !event.shiftKey && !event.ctrlKey && !event.metaKey ) {
$('#wpTextbox1').keypress(function (event) {
if ( event.which === 48 ) {
var button, help = '';
event.preventDefault();
if (event.altKey && !event.shiftKey && !event.ctrlKey && !event.metaKey)
mw.loader.using( [ 'oojs-ui-core', 'oojs-ui-widgets', 'oojs-ui-windows', 'mediawiki.widgets' ] )
{
.done( showDialog )
if (event.which === 48) // configure
.fail( function() { console.error( 'Errore configurazione tradLink' ); } );
{
} else if ( event.which >= 49 && event.which < 57 ) {
event.preventDefault();
event.preventDefault();
$.each(hotkeys, function (hotkey, button) {
tradLink( event.which - 49 );
help += button.label + ' = Alt+' + hotkey + '\n';
}
});
}
alert('Hotkey attivi:\n' + help);
} );
}
} );
else if (event.which >= 49 && event.which < 57) // execute
}( mediaWiki, jQuery ) );
{
tradLink(event.which - 49);
event.preventDefault();
}
}
});