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

Contenuto cancellato Contenuto aggiunto
Conversione a OOUI e fix vari
m fix
 
(19 versioni intermedie di uno stesso utente non sono mostrate)
Riga 7:
( function( mw, $ ) {
'use strict';
 
var cookieName = 'tradLinkLangs';
var optDialog;
var windowManager, optDialog;
 
/**
* Polyfill per trimStart() (liste di browser non esaustive)
*/
function trimStringStart( str ) {
if ( 'trimStart' in String.prototype ) {
return str.trimStart();
} 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+/, '' );
}
}
 
/**
Riga 34 ⟶ 20:
}
 
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'; // XXX
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();
Riga 52 ⟶ 56:
maxLength: 3
} );
input.on( 'change', self.clearError );
var field = new OO.ui.FieldLayout(
input,
Riga 67 ⟶ 72:
label: 'Elenco delle lingue da utilizzare per il tool TradLink'
} );
 
function submitHandler() {
var lingue = [];
self.fields.forEach( function( f ) {
lingue.push( f.fieldWidget.getValue().trim() );
} );
var d = new Date();
d.setTime( d.getTime() + ( 20 * 365 * 24 * 60 * 60 * 1000 ) ); // 20 years
var expires = "expires=" + d.toUTCString();
document.cookie = "tradLinkLangs=" + lingue.join() + "; " + expires;
optDialog.close();
}
 
var submitButton = new OO.ui.ButtonWidget(
{ label: 'Imposta' }
).on( 'click', submitHandlerfunction () { submitDialogHandler( self.fields ); } );
var cancelButton = new OO.ui.ButtonWidget(
{ label: 'Annulla' }
Riga 93 ⟶ 86:
 
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 );
};
Riga 104 ⟶ 97:
windowManager = new OO.ui.WindowManager();
$( 'body' ).append( windowManager.$element );
windowManager.addWindows( [ optDialog, new OO.ui.MessageDialog() ] );
windowManager.openWindow( optDialog );
}
 
/**
* @returnValida {array}l'aggiunta Didi codicinuove lingualingue
* @param {array} formFields
*/
function getLinguesubmitDialogHandler( formFields ) {
new mw.Api().get( {
var value = "",
name =action: "tradLinkLangs=sitematrix",
smtype: "language",
ca = document.cookie.split( ';' );
smlangprop: "code|site",
for ( var i in ca ) {
smsiteprop: "url",
var c = trimStringStart( ca[ i ] );
smlimit: 5000,
if ( c.indexOf( name ) === 0 ) {
format: "json"
value = c.substring( name.length );
} ).done( function ( data ) {
break;
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' ];
Riga 130 ⟶ 168:
/**
* Routine principale
* @param {stringint} cod CodiceIndice della lingua (0-based)
*/
function tradLink( cod ) {
var lingue = getLingue();
 
$new mw.getJSONForeignApi( "https:'//"' + lingue[ cod ] + "'.wikipedia.org/w/api.php?callback=?",' ).get( {
action: "query",
prop: "langlinks",
lllang: "it",
titles: $( '#wpTextbox1' ).textSelection( 'getSelection' ),
format: "json",
redirects: ""
} ).done( function( data ) {
},
var selStart = document.getElementById( 'wpTextbox1' ).selectionStart,
function( data ) {
var selStartselEnd = document.getElementById( 'wpTextbox1' ).selectionStart,selectionEnd;
if (
selEnd = document.getElementById( 'wpTextbox1' ).selectionEnd;
if ( typeof( data.query ) != "undefined" &&
typeof( data.query.pages ) != "undefined" &&
) {
typeof( data.query.pages ) != "undefined"
for ( var key in data.query.pages ) {
) {
forif ( var key intypeof( data.query.pages[ key ] ) {!= "undefined" &&
if ( typeof( data.query.pages[ key ].langlinks ) != "undefined" &&
typeof( data.query.pages[ key ].langlinks[ 0 ] ) != "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', {
var trad = data.query.pages[ key ].langlinks[ 0 ][ "*" ];
pre: trad + "|"
$( '#wpTextbox1' ).textSelection( 'encapsulateSelection', {
pre:} trad + "[",);
document.getElementById( 'wpTextbox1' ).selectionStart = selStart + trad.length + 1;
post: "]"
document.getElementById( 'wpTextbox1' ).selectionEnd = selEnd + trad.length + 1;
} );
return;
document.getElementById( 'wpTextbox1' ).selectionStart = selStart + trad.length;
document.getElementById( 'wpTextbox1' ).selectionEnd = selEnd + trad.length + 2;
 
return;
}
}
}
}
var noLinkLabel = "[NO LINK]";
var noLinkLabel = "[NO LINK]";
$( '#wpTextbox1' ).textSelection( 'encapsulateSelection', {
pre: "",
post: noLinkLabel
} );
document.getElementById( 'wpTextbox1' ).selectionStart = selEnd;
document.getElementById( 'wpTextbox1' ).selectionEnd = selEnd + noLinkLabel.length;
} );
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();
mw.loader.using( [ 'oojs-ui-core', 'oojs-ui-widgets', 'oojs-ui-windows', 'mediawiki.widgets' ] )
mw.loader.using( [ 'oojs-ui-core', 'oojs-ui-widgets', 'oojs-ui-windows', 'mediawiki.widgets' ] )
.done( showDialog )
.done( showDialog )
.fail( function() { console.error( 'Errore configurazione tradLink' ); } );
} 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( event.which - 49 );
event.preventDefault();
}
} );
} );
}( mediaWiki, jQuery ) );