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)
Migliorie sparse (devo ancora finire)
Riga 13:
( function( mw, $ ) {
'use strict';
 
/**
* 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+/, '' );
}
}
 
// Show the dialog in order to ask for the template parameters.
function tradLink_showDialog( data, page ) {
var lingue = tradLink_getLingue();,
$dialog = $( '#gtb-dialog' ).html( "Inserire l'elenco delle lingue da utilizzare per il tool TradLink" );,
$fieldset = $( '<fieldset>' ).css( 'border-color', 'gray' ).appendTo( $dialog );
 
var $dialog, $fieldset;
// create the dialog html
$dialog = $( '#gtb-dialog' ).html( "Inserire l'elenco delle lingue da utilizzare per il tool TradLink" );
$fieldset = $( '<fieldset>' ).css( 'border-color', 'gray' ).appendTo( $dialog );
$( '<legend>' ).text( "Elenco" ).appendTo( $fieldset );
var count = 1;
$.each( lingue, function( id, val ) {
$( '<label>' )
Riga 31 ⟶ 44:
$fieldset
.append( $( '<input/>' )
.attr( 'id', id )
.attr( 'type', 'text' )
.attr( 'size', 3 )
.attr( 'value', ( val || '' ) ) )
.append( '<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() .trim();
} );
lingue = params.join();
Riga 69 ⟶ 83:
 
function tradLink_getLingue() {
var value = "";,
var name = "tradLinkLangs=";,
var ca = document.cookie.split( ';' );
for ( var i = 0; i < ca.length; i++ ) {
var c = trimStringStart( ca[ i ] );
while ( c.charAt( 0 ) === ' ' ) {
c = c.substring( 1 );
}
if ( c.indexOf( name ) === 0 ) {
value = c.substring( name.length, c.length );
break;
}
}
 
if (return value !== "" ) {
splitted =? value.split( ',' );.slice( 0, 9 )
return: [ 'en', 'nl', 'de', 'sv', 'fr', 'ru', 'es', 'war', 'vi' ];
return [ splitted[ 0 ], splitted[ 1 ], splitted[ 2 ], splitted[ 3 ], splitted[ 4 ], splitted[ 5 ], splitted[ 6 ], splitted[ 7 ], splitted[ 8 ] ];
} else {
return [ 'en', 'nl', 'de', 'sv', 'fr', 'ru', 'es', 'war', 'vi' ];
}
}
 
Riga 104 ⟶ 113:
},
function( data ) {
var selStart = document.getElementById( 'wpTextbox1' ).selectionStart;,
selEnd = document.getElementById( 'wpTextbox1' ).selectionEnd;
if ( typeof( data ) != "undefined" &&
typeof( data.query ) != "undefined" &&
Riga 116 ⟶ 125:
typeof( data.query.pages[ key ].langlinks[ 0 ][ "*" ] ) != "undefined"
) {
var trad = data.query.pages[ key ].langlinks[ 0 ][ "*" ];
try {
mw.html.escape( $( '#wpTextbox1' ).textSelection( 'encapsulateSelection', {
Riga 147 ⟶ 156:
// setup hotkeys
$( '#wpTextbox1' ).keydown( function( event ) {
var key = event.charCode ? event.charCode : event.keyCode ? event.keyCode : 0;
var button, help = '';
if ( event.altKey && !event.shiftKey && !event.ctrlKey && !event.metaKey ) {
if ( event.which === 48 ) {