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

Contenuto cancellato Contenuto aggiunto
tutto in una IIFE + use strict (e basta, anche se il diff fa paura)
m fix
 
(30 versioni intermedie di uno stesso utente non sono mostrate)
Riga 1:
/**
* Considera il testo selezionato come il titolo di una voce straniera, e lo
*
*Considera il testo selezionato come il titolo di una voce straniera, e lo trasforma nel titolo dell'equivalente voce italiana
*
* Autore : [[:it:User:Jalo]]
*
* Lo script contiene funzioni (tradLink_showDialog e keypress) derivate da:
* http://it.wikipedia.org/w/index.php?title=MediaWiki:Gadget-tb-base.js&oldid=66478020
* scritto da [[:it:User:Rotpunkt]]
*/
 
Riga 13 ⟶ 7:
( function( mw, $ ) {
'use strict';
var cookieName = 'tradLinkLangs';
var windowManager, optDialog;
 
/**
// Show the dialog in order to ask for the template parameters.
* Basato su [[Special:Permalink/109422525]] e gli esempi su mw.org
function tradLink_showDialog( data, page ) {
*/
lingue = tradLink_getLingue();
function showDialog() {
if ( optDialog ) {
windowManager.openWindow( optDialog );
return;
}
 
mw.util.addCSS(
var $dialog, $fieldset;
'.tradlinks-buttons { width: 100%; text-align: center; margin-top: 10px }' +
// create the dialog html
'.tradlinks-error { color: red; font-size: 120%; font-weight: bold; text-align:center }'
$dialog = $( '#gtb-dialog' ).html( "Inserire l'elenco delle lingue da utilizzare per il tool TradLink" );
);
$fieldset = $( '<fieldset>' ).css( 'border-color', 'gray' ).appendTo( $dialog );
function OptDialog( config ) {
$( '<legend>' ).text( "Elenco" ).appendTo( $fieldset );
OptDialog.parent.call( this, config );
count = 1;
}
$.each( lingue, function( id, val ) {
// TODO Valutare se sia possibile usare un ProcessDialog
$( '<label>' )
OO.inheritClass( OptDialog, OO.ui.Dialog );
.attr( 'for', id )
OptDialog.static.name = 'optDialog';
.text( "Alt+" + count + ': ' )
// XXX Questo non viene visualizzato nei Dialog di base
.appendTo( $fieldset );
OptDialog.static.title = 'Configurazione del tool TradLink';
$fieldset
OptDialog.prototype.initialize = function () {
.append( $( '<input/>' )
.attr(var 'id',self id= )this;
OptDialog.parent.prototype.initialize.call( this );
.attr( 'type', 'text' )
 
.attr( 'size', 3 )
this.errorContainer = new OO.ui.LabelWidget( {
.attr( 'value', ( val || '' ) ) )
.append(label: '<br/>' );
count++;
} );
// show the dialog
var Buttons = {};
var OKText = 'OK';
var CancelText = 'Cancel';
Buttons[ OKText ] = function() {
var params = [];
count = 0;
$dialog.find( 'input:text' ).each( function() {
params[ count++ ] = $.trim( $( this ).val() );
} );
this.addError = function ( error ) {
lingue = params.join();
self.errorContainer.setLabel(
var d = new Date();
new OO.ui.HtmlSnippet( '<span class="tradlinks-error">' + error + '</span>' )
d.setTime( d.getTime() + ( 20 * 365 * 24 * 60 * 60 * 1000 ) ); // 20 years
);
var expires = "expires=" + d.toUTCString();
};
document.cookie = "tradLinkLangs=" + lingue + "; " + expires;
$( this ).dialog(clearError 'close'= 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 );
};
Buttons[ CancelText ]OptDialog.prototype.getBodyHeight = function () {
$(return this ).dialogpanelLayout.$element.outerHeight( 'close'true );
};
optDialog = new OptDialog( {
$dialog.dialog( {
size: 'small'
title: "Configurazione del tool TradLink",
width: 300,
resizable: false,
modal: true,
zIndex: 10000,
buttons: Buttons
} );
windowManager = new OO.ui.WindowManager();
$( 'body' ).append( windowManager.$element );
windowManager.addWindows( [ optDialog, new OO.ui.MessageDialog() ] );
windowManager.openWindow( optDialog );
}
 
/**
function tradLink_getLingue() {
* Valida l'aggiunta di nuove lingue
value = "";
* @param {array} formFields
var name = "tradLinkLangs=";
*/
var ca = document.cookie.split( ';' );
function submitDialogHandler( formFields ) {
for ( var i = 0; i < ca.length; i++ ) {
new mw.Api().get( {
var c = ca[ i ];
action: "sitematrix",
while ( c.charAt( 0 ) === ' ' ) {
smtype: "language",
c = c.substring( 1 );
smlangprop: "code|site",
smsiteprop: "url",
smlimit: 5000,
format: "json"
} ).done( function ( data ) {
var lingue = formFields.map( function( f ) {
return f.fieldWidget.getValue().trim();
} );
 
var filteredMatrix = {};
for ( var i in data.sitematrix ) {
var siteData = data.sitematrix[i];
if ( lingue.indexOf( siteData.code ) > -1 ) {
filteredMatrix[siteData.code] = siteData.site;
}
}
if ( c.indexOf( name ) === 0 ) {
for ( var j in lingue ) {
value = c.substring( name.length, c.length );
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 );
if ( value !== "" ) {
splitted = valueoptDialog.splitclose( ',' );
} ).fail( function ( e ) {
return [ splitted[ 0 ], splitted[ 1 ], splitted[ 2 ], splitted[ 3 ], splitted[ 4 ], splitted[ 5 ], splitted[ 6 ], splitted[ 7 ], splitted[ 8 ] ];
console.error( 'Impossibile ottenere sitematrix: ' + e );
} else {
} );
return [ 'en', 'nl', 'de', 'sv', 'fr', 'ru', 'es', 'war', 'vi' ];
}
}
 
/**
function tradLink_tradLink( cod ) {
* @return {array} Di codici lingua
lingue = tradLink_getLingue();
*/
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' ];
}
 
/**
var toTrad = mw.html.escape( $( '#wpTextbox1' ).textSelection( 'getSelection' ) );
* Routine principale
var trad = "";
* @param {int} cod Indice della lingua (0-based)
$.getJSON( "https://" + lingue[ cod ] + ".wikipedia.org/w/api.php?callback=?", {
*/
action: "query",
function tradLink( cod ) {
prop: "langlinks",
var lingue = getLingue();
lllang: "it",
titles: toTrad,
format: "json",
redirects: ""
},
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;
 
new mw.ForeignApi( '//' + lingue[cod] + '.wikipedia.org/w/api.php' ).get( {
return;
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;
}
}
try {}
var noLinkLabel = "[NO LINK]";
mw.html.escape( $( '#wpTextbox1' ).textSelection( 'encapsulateSelection', {
$( '#wpTextbox1' ).textSelection( 'encapsulateSelection', {
pre: "",
postpre: "[NO LINK]",
}post: ) );noLinkLabel
} catch ( err ) {
// ...?
}
document.getElementById( 'wpTextbox1' ).selectionStart = selEnd;
document.getElementById( 'wpTextbox1' ).selectionEnd = selEnd + 9;
} );
document.getElementById( 'wpTextbox1' ).selectionStart = selEnd;
document.getElementById( 'wpTextbox1' ).selectionEnd = selEnd + noLinkLabel.length;
} );
}
 
$( function () {
// setup hotkeys
$( '#wpTextbox1' ).keydown( function( event ) {
var if key =( event.charCodealtKey ?&& !event.charCodeshiftKey :&& !event.keyCodectrlKey ?&& !event.keyCodemetaKey :) 0;{
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' ] )
if ( event.which === 48 ) {
.done( showDialog )
// configure
.fail( function() { console.error( 'Errore configurazione tradLink' ); } );
tradLink_showDialog();
} else if ( event.which >= 49 && event.which < 57 ) {
event.preventDefault();
event.preventDefault();
} else if ( event.which >= 49 && event.which < 57 ) {
tradLink( event.which - 49 );
// execute
}
tradLink_tradLink( event.which - 49 );
event.preventDefault();
}
} );
} );
}( mediaWiki, jQuery ) );