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

Contenuto cancellato Contenuto aggiunto
m -prefissi
m fix
 
(25 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 (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;
 
/**
* Basato su [[Special:Permalink/109422525]] e gli esempi su mw.org
* Polyfill per trimStart() (liste di browser non esaustive)
*/
function trimStringStartshowDialog( str ) {
if ( 'trimStart' in String.prototypeoptDialog ) {
windowManager.openWindow( optDialog );
return str.trimStart();
return;
} else if ( 'trimLeft' in String.prototype ) {
// Edge, Firefox 27-60, Chrome 31-65, Opera 18-52
return str.trimLeft();
} else {
// IE 9-11, Safari 9-11
return str.replace( /^\s+/, '' );
}
}
 
mw.util.addCSS(
// Show the dialog in order to ask for the template parameters.
'.tradlinks-buttons { width: 100%; text-align: center; margin-top: 10px }' +
function showDialog( data, page ) {
'.tradlinks-error { color: red; font-size: 120%; font-weight: bold; text-align:center }'
var lingue = getLingue(),
);
$dialog = $( '#gtb-dialog' ).html( "Inserire l'elenco delle lingue da utilizzare per il tool TradLink" ),
function OptDialog( config ) {
$fieldset = $( '<fieldset>' ).css( 'border-color', 'gray' ).appendTo( $dialog );
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( {
$( '<legend>' ).text( "Elenco" ).appendTo( $fieldset );
label: ''
var count = 1;
} );
$.each( lingue, function( id, val ) {
this.addError = function ( error ) {
$( '<label>' )
self.errorContainer.setLabel(
.attr( 'for', id )
new OO.ui.textHtmlSnippet( '<span class="Alt+tradlinks-error">' + counterror + ': </span>' )
.appendTo( $fieldset );
$fieldset};
this.append(clearError $(= '<input/>'function () {
self.attrerrorContainer.setLabel( 'id', id );
};
.attr( 'type', 'text' )
.attr( 'size', 3 )
.attr( 'value', ( val || '' ) ) )
.append( '<br/>' );
count++;
} );
// show the dialog
var Buttons = {},
OKText = 'OK',
CancelText = 'Cancel';
 
this.fields = [];
Buttons[ OKText ] = function() {
var paramslingue = []getLingue();
var count = 01;
$dialoglingue.findforEach( 'input:text' ).eachfunction( function(val ) {
var input = new OO.ui.TextInputWidget( {
params[ count++ ] = $( this ).val().trim();
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++;
} );
lingue = params.join();
var dfieldsetLayout = new DateOO.ui.FieldsetLayout(); {
items: this.fields,
d.setTime( d.getTime() + ( 20 * 365 * 24 * 60 * 60 * 1000 ) ); // 20 years
label: 'Elenco delle lingue da utilizzare per il tool TradLink'
var expires = "expires=" + d.toUTCString();
} );
document.cookie = "tradLinkLangs=" + lingue + "; " + expires;
 
$( this ).dialog( 'close' );
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 getLingue() {
* Valida l'aggiunta di nuove lingue
var value = "",
* @param {array} formFields
name = "tradLinkLangs=",
*/
ca = document.cookie.split( ';' );
function submitDialogHandler( formFields ) {
for ( var i = 0; i < ca.length; i++ ) {
new mw.Api().get( {
var c = trimStringStart( ca[ i ] );
action: "sitematrix",
if ( c.indexOf( name ) === 0 ) {
smtype: "language",
value = c.substring( name.length, c.length );
smlangprop: "code|site",
break;
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;
}
}
for ( var j in lingue ) {
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 );
return value !== ""
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( {
var toTrad = mw.html.escape( $( '#wpTextbox1' ).textSelection( 'getSelection' ) );
var trad = action: "query";,
prop: "langlinks",
$.getJSON( "https://" + lingue[ cod ] + ".wikipedia.org/w/api.php?callback=?", {
actionlllang: "queryit",
titles: $( '#wpTextbox1' ).textSelection( 'getSelection' ),
prop: "langlinks",
lllangformat: "itjson",
titlesredirects: toTrad,""
} ).done( function( data ) {
format: "json",
var selStart = document.getElementById( 'wpTextbox1' ).selectionStart,
redirects: ""
selEnd = document.getElementById( 'wpTextbox1' ).selectionEnd;
},
if (
function( data ) {
typeof( data.query ) != "undefined" &&
var selStart = document.getElementById( 'wpTextbox1' ).selectionStart,
typeof( data.query.pages ) != "undefined"
selEnd = document.getElementById( 'wpTextbox1' ).selectionEnd;
) {
if ( typeof( data ) != "undefined" &&
typeoffor ( var key in data.query.pages ) != "undefined" &&{
if ( typeof( data.query.pages[ key ] ) != "undefined" &&
typeof( data.query.pages[ key ].langlinks ) != "undefined" &&
) {
for typeof( var key in data.query.pages[ key ].langlinks[ 0 ] ) {!= "undefined" &&
if ( typeof( data.query.pages[ key ].langlinks[ 0 ][ "*" ] ) != "undefined" &&
) {
typeof( data.query.pages[ key ].langlinks ) != "undefined" &&
typeof(var trad = data.query.pages[ key ].langlinks[ 0 ] ) !=[ "undefined*" &&];
$( '#wpTextbox1' ).textSelection( 'encapsulateSelection', {
typeof( data.query.pages[ key ].langlinks[ 0 ][ "*" ] ) != "undefined"
) pre: {trad + "|"
} );
var trad = data.query.pages[ key ].langlinks[ 0 ][ "*" ];
document.getElementById( 'wpTextbox1' ).selectionStart = selStart + trad.length + 1;
try {
mwdocument.html.escape( $getElementById( '#wpTextbox1' ).textSelection(selectionEnd 'encapsulateSelection',= selEnd + trad.length + {1;
pre: trad + "[",return;
post: "]"
} ) );
} catch ( err ) {
// ...?
}
document.getElementById( 'wpTextbox1' ).selectionStart = selStart + trad.length;
document.getElementById( 'wpTextbox1' ).selectionEnd = selEnd + trad.length + 2;
 
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 ) {
if ( event.altKey && !event.shiftKey && !event.ctrlKey && !event.metaKey ) {
if ( event.which === 48 ) {
event.preventDefault();
// configure
mw.loader.using( [ 'oojs-ui-core', 'oojs-ui-widgets', 'oojs-ui-windows', 'mediawiki.widgets' ] )
showDialog();
.done( showDialog )
event.preventDefault();
.fail( function() { console.error( 'Errore configurazione tradLink' ); } );
} else if ( event.which >= 49 && event.which < 57 ) {
} else if ( event.which >= 49 && event.which < 57 ) {
// execute
tradLink( event.which - 49 preventDefault();
tradLink( event.preventDefault(which - 49 );
}
}
} );
} );
}( mediaWiki, jQuery ) );