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

Contenuto cancellato Contenuto aggiunto
fix case primo carattere
aggiunti spazi parentesi, tab, jslint=>jshint per mw:Manual:Coding conventions/JavaScript
Riga 9:
* @author [[Utente:Rotpunkt]]
*/
/* global document: false, mediaWiki: false, jQuery: false */
 
( function ( mw, $ ) {
'use strict';
 
// Classe per la funzione di autocompletamento
function AutoComplete( pos, text, el ) {
var position = pos,
initialText = text,
element = el,
pages = null,
currPage = 0,
currContinue = null,
prevContinue = null,
currText = text;
 
// Ritorna la lista delle pagine che iniziano con prefix ( in JSON )
function getPages( prefix, pcontinue, pageHandler ) {
$.ajax( {
url: mw.util.wikiScript( 'api' ),
data: {
action: 'query',
list: 'allpages',
apprefix: prefix,
apcontinue: pcontinue,
aplimit: '50',
apfilterredir: 'nonredirects',
format: 'json'
},
},
async: false,
dataType: 'json'
} )
})
.done( function ( data ) {
if ( data.query && data.query.allpages.length > 0 ) {
pageHandler( data.query.allpages, data['query-continue'] ?
data['query-continue'].allpages.apcontinue : null );
}
}
} );
});
}
}
 
// Inserisce il nome della pagina nell'area di modifica
function dump() {
// preserva il case del primo carattere
if ( initialText.length > 0 ) {
pages[currPage].title = initialText.charAt( 0 ) + pages[currPage].title.substring( 1 );
}
}
$( element ).textSelection( 'encapsulateSelection',
{ pre: pages[currPage].title, selectionStart: position,
selectionEnd: position + currText.length, replace: true } );
currText = pages[currPage].title;
// aggiorna currPage
if ( currPage >= pages.length - 1 ) {
if ( currContinue ) {
pages = null;
} else if ( prevContinue ) {
prevContinue = null;
pages = null;
} else {
currPage = 0;
}
}
} else {
currPage += 1;
}
}
}
}
 
AutoComplete.prototype.getCurrText = function () {
return currText;
};
};
 
AutoComplete.prototype.complete = function () {
// eventualmente ottiene la lista di pagine
if ( !pages ) {
getPages( initialText, currContinue, function ( retPages, pcontinue ) {
pages = retPages;
prevContinue = currContinue;
currContinue = pcontinue;
currPage = 0;
} );
});
}
}
if ( pages ) {
dump();
}
}
};
};
}
}
 
$(document).ready( function () {
// previene con Firefox l'azione di default del tasto Tab
var tabDefault = true, ac;
 
$( '#wpTextbox1, #wpUploadDescription' ).keydown( function ( event ) {
var pos, text;
if ( ( !event.altKey && !event.shiftKey && !event.ctrlKey && !event.metaKey && event.keyCode === 9 ) ||
( (!event.altKey && !event.shiftKey && event.ctrlKey && !event.metaKey && event.keyCode === 32 ) ) {
tabDefault = true;
pos = $( event.target ).textSelection( 'getCaretPosition' );
text = $( event.target ).val().replace( /\r\n/g, '\n' ).substring( 0, pos );
pos = text.lastIndexOf( '[[' ) + 2;
text = text.substring( pos );
if ( pos >= 2 && !/[\[\]\{\}\r\n\|]/.test( text ) ) {
tabDefault = false;
event.preventDefault();
if ( !ac || text !== ac.getCurrText() ) {
ac = new AutoComplete( pos, text, event.target );
}
}
ac.complete();
}
}
}
}
} });
$( '#wpTextbox1, #wpUploadDescription' ).keypress( function ( event ) {
if ( !event.altKey && !event.shiftKey && !event.ctrlKey && !event.metaKey && event.keyCode === 9 ) {
return tabDefault;
}
}
} });
} });
}( mediaWiki, jQuery ) );