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

Contenuto cancellato Contenuto aggiunto
m Solite graffe che tanto opzionali non sono, esplicito che nei catch qualcosa ci andrebbe sempre, fosse anche solo un commento per spiegare come mai non ci interessa l'errore
tutto in una IIFE + use strict (e basta, anche se il diff fa paura)
Riga 10:
*/
 
/* global mediaWiki, jQuery, OO */
// Show the dialog in order to ask for the template parameters.
function( tradLink_showDialogfunction( datamw, page$ ) {
'use strict';
lingue = tradLink_getLingue();
 
// Show the dialog in order to ask for the template parameters.
var $dialog, $fieldset;
function tradLink_showDialog( data, page ) {
// create the dialog html
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;
$( '<legend>' ).text( "Elenco" ).appendTo( $fieldset );
// create the dialog html
count = 1;
$dialog = $( '#gtb-dialog' ).html( "Inserire l'elenco delle lingue da utilizzare per il tool TradLink" );
$.each( lingue, function( id, val ) {
$fieldset = $( '<fieldset>' ).css( 'border-color', 'gray' ).appendTo( $dialog );
$( '<label>' )
$( '<legend>' ).text( "Elenco" ).appendTo( $fieldset );
.attr( 'for', id )
.text( "Alt+" + count + ': '= )1;
$.each( lingue, function( id, val ) {
.appendTo( $fieldset );
$( '<label>' )
$fieldset
.append( $attr( '<input/>for', id )
.attrtext( "Alt+" + count + 'id',: id' )
.attrappendTo( 'type', 'text'$fieldset );
$fieldset
.attr( 'size', 3 )
.attrappend( 'value', $( val || '<input/>' ) ) )
.appendattr( '<br/>id', id );
.attr( 'type', 'text' )
count++;
.attr( 'size', 3 )
} );
.attr( 'value', ( val || '' ) ) )
// show the dialog
.append( '<br/>' );
var Buttons = {};
count++;
var OKText = 'OK';
var CancelText = 'Cancel';
Buttons[ OKText ] = function() {
var params = [];
count = 0;
$dialog.find( 'input:text' ).each( function() {
params[ count++ ] = $.trim( $( this ).val() );
} );
// show the dialog
lingue = params.join();
var dButtons = new Date(){};
var OKText = 'OK';
d.setTime( d.getTime() + ( 20 * 365 * 24 * 60 * 60 * 1000 ) ); // 20 years
var expiresCancelText = "expires=" + d.toUTCString()'Cancel';
Buttons[ OKText ] = function() {
document.cookie = "tradLinkLangs=" + lingue + "; " + expires;
var params = [];
$( this ).dialog( 'close' );
count = 0;
};
Buttons[ $dialog.find( CancelText'input:text' ] =).each( function() {
params[ count++ ] = $.trim( $( this ).dialogval( 'close') );
} );
lingue = params.join();
$dialog.dialog( {
var d = new Date();
title: "Configurazione del tool TradLink",
d.setTime( d.getTime() + ( 20 * 365 * 24 * 60 * 60 * 1000 ) ); // 20 years
width: 300,
var expires = "expires=" + d.toUTCString();
resizable: false,
document.cookie = "tradLinkLangs=" + lingue + "; " + expires;
modal: true,
$( this ).dialog( 'close' );
zIndex: 10000,
};
buttons: Buttons
Buttons[ CancelText ] = function() {
} );
$( this ).dialog( 'close' );
}
};
$dialog.dialog( {
title: "Configurazione del tool TradLink",
width: 300,
resizable: false,
modal: true,
zIndex: 10000,
buttons: Buttons
} );
}
 
function tradLink_getLingue() {
value = "";
var name = "tradLinkLangs=";
var ca = document.cookie.split( ';' );
for ( var i = 0; i < ca.length; i++ ) {
var c = ca[ i ];
while ( c.charAt( 0 ) === ' ' ) {
c = c.substring( 1 );
}
if ( c.indexOf( name ) === 0 ) {
value = c.substring( name.length, c.length );
}
}
 
if ( c.indexOf( name ) === 0 ) {
if ( value !== "" ) {
value = c.substring( name.length, c.length );
splitted = value.split( ',' );
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' ];
}
}
 
function tradLink_tradLink( cod ) {
if ( value !== "" ) {
splittedlingue = value.splittradLink_getLingue( ',' );
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' ];
}
}
 
var toTrad = mw.html.escape( $( '#wpTextbox1' ).textSelection( 'getSelection' ) );
function tradLink_tradLink( cod ) {
var trad = "";
lingue = tradLink_getLingue();
$.getJSON( "https://" + lingue[ cod ] + ".wikipedia.org/w/api.php?callback=?", {
action: "query",
prop: "langlinks",
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;
 
return;
var toTrad = mw.html.escape( $( '#wpTextbox1' ).textSelection( 'getSelection' ) );
var trad = "";
$.getJSON( "https://" + lingue[ cod ] + ".wikipedia.org/w/api.php?callback=?", {
action: "query",
prop: "langlinks",
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;
 
return;
}
}
} try {
mw.html.escape( $( '#wpTextbox1' ).textSelection( 'encapsulateSelection', {
try {
pre: "",
mw.html.escape( $( '#wpTextbox1' ).textSelection( 'encapsulateSelection', {
pre post: "[NO LINK]",
post:} "[NO) LINK]");
} )catch ( err ); {
// ...?
} catch ( err ) {
// ...?}
document.getElementById( 'wpTextbox1' ).selectionStart = selEnd;
}
document.getElementById( 'wpTextbox1' ).selectionStartselectionEnd = selEnd + 9;
} );
document.getElementById( 'wpTextbox1' ).selectionEnd = selEnd + 9;
}
} );
}
 
// 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 ) {
// configure
tradLink_showDialog();
event.preventDefault();
} else if ( event.which >= 49 && event.which < 57 ) {
// execute
tradLink_tradLink( event.which - 49 );
event.preventDefault();
}
}
} );
}( mediaWiki, jQuery ) );
} );