*/
mw.loader.using(['mediawiki.api', 'jquery.textSelection'], function ()
{
$.fn.suggestWords = function(data, prefix) { var $area = $(this); var words = {}; var requests = new Array(); var openToken = new Array(); for(var i = 0 ; i < data.length ; i++) { words[data[i].id] = data[i].list; requests.push(data[i].id); openToken.push('{{'+prefix+data[i].id+'|'); } var closeToken = '}}'; var currentSuggestion = ''; var ignoreKeys = [8, 16, 17, 18, 37, 38, 39, 40, 46, 33, 34]; var getChunk = function() { var caretPosition = $area.textSelection('getCaretPosition'); var textBeforeCaret = $area.val().substr(0, caretPosition); var lastOpenToken = -1; var lastCloseToken = textBeforeCaret.lastIndexOf(closeToken); var tokenType = -1; for(var i = 0 ; i < requests.length ; i++) { var token = textBeforeCaret.lastIndexOf(openToken[i]); if(token > lastOpenToken) { lastOpenToken = token; tokenType = i; } } var lastNewLine = textBeforeCaret.lastIndexOf('\n'); if(lastOpenToken > lastCloseToken && lastOpenToken > lastNewLine) return [tokenType, textBeforeCaret.substr(lastOpenToken)]; else return null; }; $area.keyup(function(event) { if($.inArray(event.keyCode, ignoreKeys) > -1 || event.keyCode === 9) return; var chunk = getChunk(); if(chunk) { var type = chunk[0]; var wordChunk = chunk[1].substr(openToken[type].length); var suggests = words[requests[type]]; if(wordChunk.length > 0) for(var i = 0 ; i < suggests.length ; i++) if(suggests[i].toLowerCase().indexOf(wordChunk.toLowerCase()) === 0) { currentSuggestion = suggests[i]; $area.textSelection('encapsulateSelection', {peri: '', replace: true}); $area.textSelection('encapsulateSelection', {peri: suggests[i].substr(wordChunk.length)}); return; } } }); $area.keydown(function(event) { if($.inArray(event.keyCode, ignoreKeys) > -1) return; var chunk = getChunk(); if(chunk) { var type = chunk[0]; var wordChunk = chunk[1].substr(openToken[type].length); var suggests = words[requests[type]]; if(event.keyCode == 13) { event.preventDefault(); if(wordChunk.length > 0) { var caretPos = $area.textSelection('getCaretPosition', {startAndEnd: true}); if(caretPos[0] < caretPos[1]) { $area.textSelection('setSelection', {start: caretPos[0] - wordChunk.length, end: caretPos[1]}); $area.textSelection('encapsulateSelection', {peri: currentSuggestion, post: closeToken, replace: true}); } else $area.textSelection('encapsulateSelection', {post: closeToken, selectionStart:caretPos[0]+2}); } } else if(event.keyCode == 9) { event.preventDefault(); if(wordChunk.length > 0 && currentSuggestion) { var i = suggests.indexOf(currentSuggestion) + 1; while(i !== suggests.indexOf(currentSuggestion)) { if(i >= suggests.length) i = 0; if(suggests[i].toLowerCase().indexOf(wordChunk.toLowerCase()) === 0) { currentSuggestion = suggests[i]; $area.textSelection('encapsulateSelection', {peri: '', replace: true}); $area.textSelection('encapsulateSelection', {peri: suggests[i].substr(wordChunk.length)}); return; } i++; } currentSuggestion = ''; } } } else { var sel = $area.textSelection('getSelection'); var tokenId = openToken.indexOf(sel); if(event.keyCode == 9) { event.preventDefault(); tokenId++; if(tokenId >= openToken.length) tokenId = 0; $area.textSelection('encapsulateSelection', {peri: '', replace: true}); $area.textSelection('encapsulateSelection', {peri: openToken[tokenId]}); } else if(event.keyCode == 13 && tokenId > -1) { event.preventDefault(); $area.textSelection('encapsulateSelection', {post: ''}); } } }); }; // Ajoute les suggestions aux formulaires qui le nécessite var getDictionary test = function(category, res, cont, cmcontinue)
{
var api = new mw.Api();
api.get({
'action': 'queryask', 'listquery': 'categorymembers', 'cmtitle'[[-Has subobject:: '<q>[[Category:Zone]]</q>]]|?Carte|Sort=Carte'+category, 'continue': cont, 'cmcontinue': cmcontinue
})
.done(function(data)
{
for(var i = 0 ; i < data.queryconsole.categorymembers.length ; i++) res.pushlog(data.query.categorymembers[i].title); if(data['continue']) getDictionary(category, res, data['continue']['continue'], data['continue']['cmcontinue']);
});
};
var titre = $test("h1").text(); if(titre.indexOf('Créer') == 0 || titre.indexOf('Modifier') == 0) { if(titre.indexOf('Quête') > 0 && titre.indexOf('Quête') < titre.indexOf(':')) { var ennemis = new Array(); getDictionary('Ennemi', ennemis, '', ''); $("#sf_free_text").suggestWords([{id: 'Ennemi', list: ennemis}], 'Quête'); } }
});
});