MediaWiki:Gadget-common.js
Ծանուցում. Հիշելուց հետո կատարված փոփոխությունները տեսնելու համար մաքրեք ձեր զննարկիչի հիշապահեստը. Mozilla / Firefox / Safari՝ Ctrl+Shift+R (Cmd+Shift+R Mac OS X-ում) Konqueror՝ F5 Opera՝ Tools→Preferences ընտրացանկից։ Internet Explorer՝ Ctrl+F5
/*mw.pageSave = function (nome,contenuto,sommario) {
if (sommario===undefined) sommario="Salvataggio pagina via mw.pageSave()";
new mw.Api().postWithToken( 'edit', {
action: 'edit',
title: nome,
summary: sommario,
text: contenuto,
}).done(function( data ) {
if ( data && data.edit && data.edit.result == 'Success' ) {
console.log("OK, pagina " + nome + " salvata");
} else {
alert("Errore nel salvataggio della pagina");
}
});
};
*/
function leggiBox(area) {
if (area === undefined) {
if (mw.config.get('wgCanonicalNamespace') == "Page" || $("#editBox").length===1) area = 1;
else area = 0;
}
return $('textarea')[area] === undefined ? '' : $('textarea')[area].value;
}
// importazione provvisoria dello script per identificazione e parsing della pagina Indice collegata
/* importScript("MediaWiki:Gadget-currentIndex.js");function incr2(testo) {
var r2=/\|[ —]*(\d+)[| —}];
var npag=r2.exec(testo);
if (npag!==null) {
testo=testo.replace(npag[0],npag[0].replace(npag[1],npag[1]*1+2));
}
return testo;
}
*/
// memorizza l'elemento attivo in ms.activeElement se l'elemento attivo fa parte di box di inserimento testo
// l'attivazione scatta all'evento focusin
$(document).ready(function() {
// di default il box principale di edit
mw.activeElement=$("#wpTextbox1")[0];
$("textarea, input[type!='checkbox'][type!='button'][type!='search'][type!='hidden'][type!='submit'][name!='wpAntispam']").focusin(function () {
mw.activeElement=this;
});
});
/* vecchia funzione selection/()
function selection(area) {
if (area === undefined) {
if (mw.config.get("wgCanonicalNamespace") == "Page" || $("#editBox").length===1) area = 1;
else area = 0;
}
var txtarea = $('textarea')[area];
var txt = $(txtarea).val();
var s = [];
s[0] = txt.substring(0, txtarea.selectionStart);
s[1] = txt.substring(txtarea.selectionStart, txtarea.selectionEnd);
s[2] = txt.substring(txtarea.selectionEnd);
return s;
}
*/
/* Nuova versione selection che usa mw.activeElement */
function selection(area){
/*
if(area===undefined){
if(mw.config.get("wgCanonicalNamespace")=="Page"||$("#editBox").length===1)
area=1;
else
area=0;
}
var txtarea=$('textarea')[area];
var txt=$(txtarea).val();
*/
var txt=mw.activeElement.value;
var s=[];
s[0]=txt.substring(0,mw.activeElement.selectionStart);
s[1]=txt.substring(mw.activeElement.selectionStart,mw.activeElement.selectionEnd);
s[2]=txt.substring(mw.activeElement.selectionEnd);
return s;
}
// restituisce mw.activeElement e una lista di tre stringhe che rappresentano il testo dell'elemento
// suddiviso in base alla selezione (pre, selezione, post)
function sel() {
var elemento=mw.activeElement;
var ss=elemento.selectionStart;
var se=elemento.selectionEnd;
var contenuto=elemento.value;
var s=[contenuto.substring(0,ss),contenuto.substring(ss,se),contenuto.substring(se)];
return [elemento, s];
}
// riscrive mw.activeElement e ristabilisce il focus/la selezione attiva
// riceve un array tipo [testo precedente, testo selezionato, testo seguente]
function scriviSel(t) {
mw.activeElement.value=t.join("");
mw.activeElement.selectionStart=t[0].length;
mw.activeElement.selectionEnd= t[0].length + t[1].length;
mw.activeElement.focus();
}
// deifenatore o_O
function dehyphen() {
scriviBox(leggiBox().replace(/([^|])[-¬] *\n([^ \n]*)[ ]*[\n]?/g,"$1$2\n"));
}
// funzione simil, tuttofare ma indispensabile a autoreCitato
function simil(s1, s2, nc) {
if (nc === undefined) nc = 4;
var cont1 = 0;
for (var i = 0; i <= (s1.length - nc); i += 1) {
if (s2.indexOf(s1.substring(i, i + nc)) != -1) cont1 += 1;
}
return cont1 / i;
}
function simil2(s1, s2, nc) {
if (nc === undefined) nc = 4;
var cont1 = 0;
for (var i = 0; i <= (s1.length - nc); i += 1) {
if (s2.indexOf(s1.substring(i, i + nc)) != -1) cont1 += 1;}
for (var j = 0; j <= (s2.length - nc); j += 1) {
if (s1.indexOf(s2.substring(j, j + nc)) != -1) cont1 += 1;
}
return cont1 / (i+j);
}
function piedipagina() {
var pdp=find_stringa(testo2PagPrec[2],"{"+"{PieDiPagina","}}",1);
var newPdp="";
if (pdp!=="") {
newPdp=incr2(pdp);
if (leggiBox(2).length===0) scriviBox("<references/>",2);
if (leggiBox(2).indexOf("{{PieDiPagina")===-1) {
scriviBox(leggiBox(2).replace("<references/>","<references/>"+newPdp),2);
}
}
}
// modifico per aggiungere consensualmente eventuale tl|PNMIS
function incr2(testo) {
var r2=/\|[ —]*(\d+)[| —}]*/;
var npag=r2.exec(testo);
if (npag!==null) {
testo=testo.replace(npag[0],npag[0].replace(npag[1],npag[1]*1+2));
}
return testo;
}
function newRi() {
if (mw.config.get("wgServer")=="//it.wikisource.org") {
// aggiungiamo un "deifenatore"
if ((mw.config.get('wgCanonicalNamespace')=="Page"||$("#editBox").length===1)) {
dehyphen();
piedipagina();
// elimino routine che sposta un'eventuale RigaIntestazione da body a header
// l'eventuale RigaIntestazione in header è sovrascritta
/*var riInNs0=find_stringa(leggiBox(),"{"+"{RigaIntestazione","}}",1,"{{");
if (riInNs0!=="") {
var riInHeader=find_stringa(leggiBox(0),"{"+"{RigaIntestazione","}}",1,"{{");
if (riInNs0!=="") scriviBox(leggiBox(0).replace(riInHeader,""),0);
scriviBox(riInNs0+leggiBox(0),0);
scriviBox(leggiBox().replace(riInNs0,""));
} */
console.log("Cambio newRi");
if (!(/\{\{RigaIntestazione\||\{\{Ri\|/i).test(leggiBox("0"))) {
ri = find_stringa(testo2PagPrec[0], "{"+"{RigaIntestazione", "}}", 1, "{{");
if (ri==="") {
testo2PagPrec[0]=testo2PagPrec[0].replace("{"+"{rh","{{Rh");
ri=find_stringa(testo2PagPrec[0], "{"+"{Rh", "}}", 1, "{{");
if (ri==="") ri="{"+"{RigaIntestazione|||}}";
}
r = /\d+/;
np = ri.match(r);
if (mw.config.get('wgTitle').indexOf("Drammaturgia di Lione Allacci.djvu") === 0) {
np1=np*1+4;
np2=np*1+5;
ri="{"+"{RigaIntestazione|"+np1+"||"+np2+"}}";
}
else {
ri = ri.replace(np, np * 1 + 2);
}
// routine gestione piedipagina
// tentiamo di eliminare la riga contenente l'intestazione
var r1=ri.replace(/\|/g,"").replace(/[{}]./g,"").replace("RigaIntestazione","");
var righe=leggiBox().split("\n");
var riga1=righe[0];
if (simil2(r1.toLocaleLowerCase(),riga1.toLocaleLowerCase(),2) > 0.5) {
righe.shift();
scriviBox($.trim(righe.join("\n")));
}
var pnmis=find_stringa(testoPagPrec[0],"{"+"{PNMIS","}}",1);
if (pnmis !== "") {
ri+="\n"+pnmis;
}
var tlt=find_stringa(testoPagPrec[1],"{{T|","}}",1);
if (tlt !== "") {
tlt=parseTemplate("T",tlt);
}
scriviBox(ri, "0");
}
}
} // fine caso it.source
if (mw.config.get("wgServer")=="//fr.wikisource.org") {
if (mw.config.get('wgCanonicalNamespace')=="Page" && !(/\{\{Nr\|/i).test(leggiBox("0"))) {
ri = find_stringa(testo2PagPrec[0], "{{Nr|", "}}", 1, "{{");
if (ri==="") {
testo2PagPrec[0]=testo2PagPrec[0].replace("{"+"{nr","{{Nr");
ri=find_stringa(testo2PagPrec[0], "{"+"{Nr", "}}", 1, "{{");
if (ri==="") ri="{{Nr|||}}";
}
r = /\d+/;
np = ri.match(r);
ri = ri.replace(np, np * 1 + 2);
scriviBox(ri, "0");
}
} // fine caso fr.source
// caso la.source
if (mw.config.get("wgServer")=="//la.wikisource.org") {
if (mw.config.get('wgCanonicalNamespace')=="Page" && !(/\{\{R[uh]/i).test(leggiBox("0"))) {
ri = find_stringa(testo2PagPrec[0], "{{Rh|", "}}", 1, "{{");
if (ri==="") {
testo2PagPrec[0]=testo2PagPrec[0].replace("{"+"{rh","{{Rh").replace("{{RunningHeader","{{Rh");
ri=find_stringa(testo2PagPrec[0], "{"+"{Rh", "}}", 1, "{{");
if (ri==="") ri="{{Rh|||}}";
}
r = /\d+/;
np = ri.match(r);
ri = ri.replace(np, np * 1 + 2);
scriviBox(ri, "0");
} // fine caso la.source
} // fine caso fr.source
}
//modifica un dato link settando il parametro par in fondo all'URL
function setURLVar(link, par, val) {
if (link) {
href = link.attr("href");
if (href) {
//rimuovi il parametro se c'era gia'
if (href.indexOf(par) != -1) {
regex = new RegExp(par + "=([a-z0-9]*)");
href = href.replace(regex, "");
}
if (href.indexOf("?") == -1) {
href += "?" + par + "=" + val;
} else {
href += "&" + par + "=" + val;
}
link.attr("href", href);
}
}
}
function scriviBox(testo, area, ss, se) {
if (area === undefined || area === "") {
if (mw.config.get('wgCanonicalNamespace') == "Page" || $("#editBox").length===1) {
area = 1;
} else {
area = 0;
}
}
$('textarea')[area].value = testo;
console.log("area:", area);
if (ss !== undefined && se !== undefined) {
$('textarea')[area].selectionStart = ss;
$('textarea')[area].selectionEnd = se;
}
}
/* conta il numero di occorrenze di stringa dentro testo; esce se testo === "" oppure stringa ==="" */
function count(testo, stringa) {
var n = 0;
if (testo !== "" && stringa !== "") {
while (testo.indexOf(stringa) > -1) {
n = n + 1;
testo = testo.replace(stringa, "");
}
}
return n;
}
/*
Ricerca nella stringa testo la sottostringa che inizia con idi e termina con idf. (idi e idf sono stringhe).
Se dc ("delimitatori compresi") è 1, restituisce la sottostringa completa di idi e idf; se dc è 0, restituisce la stringa
senza delimitatori; parametro opzionale con default 0.
Per risolvere correttamente il caso di ricerca di tag annidati, come i template, in cui l'identificatore iniziale ha una
parte non aspecifica, e una parte specifica, può essere passato un quinto parametro, che definisce la parte aspecifica dell'identificatore iniziale.
Esempio: volendo ottenere l'intero contenuto del template {{centrato|{{sc|Testo di prova}}|l=18em}}, il risultato corretto
NON proviene dalla ricerca find_stringa("{{centrato|","}}",1), perchè sarebbe {{centrato|{{sc|Testo di prova}}.
Impostando invece find_stringa("{{centrato|","}}",1,"{{"), visto che "{{" è la parte aspecifica del primo delimitatore,
si ottiene il risultato corretto, a prescindere dal numero dei template annidati.
Se la sottostringa non viene trovata, la funzione restituisce una stringa vuota "".
*/
function find_stringa(testo, idi, idf, dc, x) {
idip = testo.indexOf(idi);
idfp = testo.indexOf(idf, idip + idi.length) + idf.length;
if (idip > -1 && idfp > -1) {
if (x !== "") {
while (count(testo.slice(idip, idfp), x) > count(testo.slice(idip, idfp), idf)) {
idfp = testo.indexOf(idf, idfp) + idf.length;
}
}
if (dc === 0) {
vvalore = testo.slice(idip + idi.length, idfp - idf.length);
} else {
vvalore = testo.slice(idip, idfp);
}
} else {
vvalore = "";
}
return vvalore;
}
function cod(testo) {
var l = produciLista(testo, "{"+"{", "}}", 1, "{{");
for (var i = 0; i < l.length; i += 1) {
testo = testo.replace(l[i], l[i].replace(/\|/g, "__!__"));
}
l = produciLista(testo, "[[", "]]", 1, "[[");
for (var j = 0; j < l.length; j += 1) {
testo = testo.replace(l[j], l[j].replace(/\|/g, "__!__"));
}
return testo;
}
// delim true: con delimitatori, false: senza delimitatori
function produciLista(testo, s1, s2, delim, x) {
lista = [];
while (find_stringa(testo, s1, s2, true, x) > "") {
elemento = find_stringa(testo, s1, s2, true, x);
testo = testo.replace(elemento, "");
if (delim) {
lista.push(elemento);
} else {
lista.push(elemento.slice(s1.length, - s2.length));
}
}
return lista;
}
function parseTemplate(template, testo) {
if (testo === undefined) testo = leggiBox();
var cap=template.substring(0,1).toLocaleUpperCase()+template.substring(1);
testo=testo.replace("{{"+cap,"{{"+template);
var t = find_stringa(testo, "{"+"{" + template, "}}", 1, "{{");
var l = []; // lista delle keys
t = "0=" + t.substring(2, t.length - 2); // nome del template in parametro "0"
l.push["0"];
var ts = {};
var n = 1;
t = cod(t);
t = t.split("|");
// element for element
for (i = 0; i < t.length; i += 1) {
// case param is positional
if (t[i].indexOf("=") == -1) {
t[i] = n + "=" + t[i];
n = n + 1;
}
var els = [];
els[0] = t[i].substring(0, t[i].indexOf("=")).trim();
els[1] = t[i].substring(t[i].indexOf("=") + 1).trim();
if (els[1][els[1].length - 1] == "\n") els[1] = els[1].substring(0, els[1].length - 1);
ts[els[0]] = decod(els[1]);
l.push(els[0]);
}
return [ts, l];
}
function decod(testo) {
testo = testo.replace(/__!__/g, "|");
return testo;
}
function templateObj(nomeTemplate) {
data = parseTemplate(nomeTemplate);
this.keys = data[1];
this.dict = data[0];
}
function rewriteTemplate(x) {
var testo = "";
$.each(x[1], function (indice, valore) {
if (valore != "0") testo += " | " + valore + " = " + x[0][valore] + "\n";
});
testo = "{{" + x[0]["0"] + "\n" + testo + "}}\n";
testo = testo.replace(/\n\s\|\s\d*\s=\s/g, "\n | ");
return testo;
}
/********************* documentazione ***********************
Lo script legge via AJAX il contenuto della pagina di cui gli viene passato il titolo e restituisce una liste di tre elementi stringa:
1. eventuale wikicode header (solo nsPagina);
2. wikicode corpo (in tutti i casi);
3. eventuale wikicode footer (solo nsPagina)
Nota:
1. i caratteri & " < > sono contenuti come html entities
2. gli spazi sono conservati come tali
3. temporaneamente viene aggiunto ai codici template e ai tag html un colore testo
*/
function acchiappaWikicode(pagina, progetto) {
if (pagina === undefined) pagina = mw.config.get('wgPageName');
if (progetto === undefined) {
progetto = "";
} else {
progetto = "//" + progetto;
}
var contenuto = $.ajax({
url: progetto + "/w/index.php?action=raw&title=" + pagina,
async: false
}).responseText.replace(/</g, "<").replace(/>/g, ">");
var l = produciLista(contenuto, "{"+"{", "}}", 1, "{{");
for (var i = 0; i < l.length; i += 1) {
contenuto = contenuto.replace(l[i], '<span style="color: red;">' + l[i] + '</span>');
}
contenuto = contenuto.replace(/\</g, '<span style="color:blue"><').replace(/\>/g, '></span>').replace(/\n/g, "<br />");
if (mw.config.get('wgCanonicalNamespace') == "Page") {
var noincludeList = produciLista(contenuto, '<span style="color:blue"><noinclude></span>', '<span style="color:blue"></noinclude></span>', 1);
var header = noincludeList[0];
var footer = noincludeList[noincludeList.length - 1];
var body = find_stringa(contenuto, header, footer, 0);
contenuto = [header, body, footer];
} else {
contenuto = ["", contenuto, ""];
}
return contenuto;
}
function impostaBottoni(fun) {
$(document).ready(function () {
$('#wpSave').click(fun);
$('#wpPreview').click(fun);
$('#wpDiff').click(fun);
});
}
/* funzione di creazione della bottoniera
Dopo l'esecuzione, esiste #newtattoo */
function creaBottoniera() {
$('#' + (mw.user.options.get('skin') === 'modern' ? 'mw_' : '') + 'content')
.append('<div id="newtattoo" align="right" style="position:fixed;bottom:0; right:0; background-color:white; border: 1px solid; border-color:#F0F0F0; z-index:1500;"></div>');
}
/* newButton accetta parametri usuali oppure un oggetto; l'oggetto deve avere questa struttura:
newButton({nome:"...", funzione:"...", azione:"...", messaggio:"...", classe:"...", stile:"...", namespaces:"..."});
Sono obbligatori solo nome, funzione, azione
Namespaces deve contenere una sequenza di namespaces (Page per Pagina), separati da | , al di fuori dei quali il bottone non sarà visualizzato; ns0 può essere indicato con Main, Principale o con uno spazio.
*/
function newButton(nome, funzione, azione, messaggio) {
// azione è una stringa composta da v,e,s,h rispettivamente view,edit,submit,history
// se nome è un oggetto svolgilo
var p;
if (typeof(nome) === "string") {
p = {
nome: nome,
funzione: funzione,
azione: azione,
messaggio: messaggio
};
} else {
p = nome;
}
if (p.messaggio===undefined) p.messaggio="Nessun aiuto registrato";
if (p.classe===undefined) p.classe="baseButton";
if (p.stile===undefined) p.stile="display:inline";
if (p.namespaces!==undefined) {
var listaNamespaces=p.namespaces.split("|");
for (var i=0; i<listaNamespaces.length; i+=1) {
listaNamespaces[i]=$.trim(listaNamespaces[i]);
if (["ns0", "Main", "Principale"].indexOf(listaNamespaces[i]) !== -1) {
listaNamespaces[i] === "";
}
}
if ($.inArray(mw.config.get('wgCanonicalNamespace'),p.namespaces.split(","))==-1) return;
}
// Passo 1: se newtatooo non esiste, lo crea
if ($("#newtattoo").length===0) creaBottoniera();
// fine se azione=history
if (mw.config.get('wgAction') === "history") {
return;
}
// normalizzazione function
if (p.funzione.indexOf("(") === -1 && p.funzione.indexOf(")") === -1) {
p.funzione += "()";
}
// default: bottone presente in view, edit,
if (p.azione === undefined) {
p.azione = "ves";
}
// messaggio di default
if (p.messaggio === undefined) {
p.messaggio = "Nessun messaggio di aiuto registrato";
}
// valutazione condizione di visualizzazione
if (mw.config.get('wgAction') === "edit" && p.azione.indexOf("e") === -1) {
return;
}
if (mw.config.get('wgAction') === "view" && p.azione.indexOf("v") === -1) {
return;
}
if (mw.config.get('wgAction') === "submit" && p.azione.indexOf("s") === -1) {
return;
}
// caso pulsante=immagine
var html = "";
if (p.nome.substring(0, 2) == "//") {
html = '<img class="'+p.classe+'" style="'+p.stile+'" src="' + p.nome + '" onclick="' + p.funzione + '" title="' + p.messaggio +'"';
if (p.ondblclick!==undefined) html+=' ondblclick="'+p.ondblclick+'"';
html+='/>';
}
// caso pulsante=label
else {
html = '<button class="'+p.classe+'" style="'+p.stile+'" type="button" title="' + p.messaggio + '" onclick="' + p.funzione +'"';
if (p.ondblclick!==undefined) html+=' ondblclick="'+p.ondblclick+'"';
html+='><small>' + p.nome + '</small></button>';
}
$("#newtattoo").append($(html));
return false;
}
function splitRiga() {
var t=selection();
var ps=t[2].indexOf("\n");
var ts=t[2].substring(0,ps);
t[2]=t[2].substring(ps)+"\n"+ts;
scriviBox(t.join(""));
}
/**
* http://www.openjs.com/scripts/events/keyboard_shortcuts/
* Version : 2.01.B
* By Binny V A
* License : BSD
*/
var shortcut = {
'all_shortcuts':{},//All the shortcuts are stored in this array
'add': function(shortcut_combination,callback,opt) {
//Provide a set of default options
var default_options = {
'type':'keydown',
'propagate':false,
'disable_in_input':false,
'target':document,
'keycode':false
};
if(!opt) opt = default_options;
else {
for(var dfo in default_options) {
if(typeof opt[dfo] == 'undefined') opt[dfo] = default_options[dfo];
}
}
var ele = opt.target;
if(typeof opt.target == 'string') ele = document.getElementById(opt.target);
var ths = this;
shortcut_combination = shortcut_combination.toLowerCase();
//The function to be called at keypress
var func = function(e) {
e = e || window.event;
if(opt.disable_in_input) { //Don't enable shortcut keys in Input, Textarea fields
var element;
if(e.target) element=e.target;
else if(e.srcElement) element=e.srcElement;
if(element.nodeType==3) element=element.parentNode;
if(element.tagName == 'INPUT' || element.tagName == 'TEXTAREA') return;
}
//Find Which key is pressed
if (e.keyCode) code = e.keyCode;
else if (e.which) code = e.which;
var character = String.fromCharCode(code).toLowerCase();
if(code == 188) character=","; //If the user presses , when the type is onkeydown
if(code == 190) character="."; //If the user presses , when the type is onkeydown
var keys = shortcut_combination.split("+");
//Key Pressed - counts the number of valid keypresses - if it is same as the number of keys, the shortcut function is invoked
var kp = 0;
//Work around for stupid Shift key bug created by using lowercase - as a result the shift+num combination was broken
var shift_nums = {
"`":"~",
"1":"!",
"2":"@",
"3":"#",
"4":"$",
"5":"%",
"6":"^",
"7":"&",
"8":"*",
"9":"(",
"0":")",
"-":"_",
"=":"+",
";":":",
"'":"\"",
",":"<",
".":">",
"/":"?",
"\\":"|"
};
//Special Keys - and their codes
var special_keys = {
'esc':27,
'escape':27,
'tab':9,
'space':32,
'return':13,
'enter':13,
'backspace':8,
'scrolllock':145,
'scroll_lock':145,
'scroll':145,
'capslock':20,
'caps_lock':20,
'caps':20,
'numlock':144,
'num_lock':144,
'num':144,
'pause':19,
'break':19,
'insert':45,
'home':36,
'delete':46,
'end':35,
'pageup':33,
'page_up':33,
'pu':33,
'pagedown':34,
'page_down':34,
'pd':34,
'left':37,
'up':38,
'right':39,
'down':40,
'f1':112,
'f2':113,
'f3':114,
'f4':115,
'f5':116,
'f6':117,
'f7':118,
'f8':119,
'f9':120,
'f10':121,
'f11':122,
'f12':123
};
var modifiers = {
shift: { wanted:false, pressed:false},
ctrl : { wanted:false, pressed:false},
alt : { wanted:false, pressed:false},
meta : { wanted:false, pressed:false} //Meta is Mac specific
};
if(e.ctrlKey) modifiers.ctrl.pressed = true;
if(e.shiftKey) modifiers.shift.pressed = true;
if(e.altKey) modifiers.alt.pressed = true;
if(e.metaKey) modifiers.meta.pressed = true;
for(var i=0; k=keys[i],i<keys.length; i++) {
//Modifiers
if(k == 'ctrl' || k == 'control') {
kp++;
modifiers.ctrl.wanted = true;
} else if(k == 'shift') {
kp++;
modifiers.shift.wanted = true;
} else if(k == 'alt') {
kp++;
modifiers.alt.wanted = true;
} else if(k == 'meta') {
kp++;
modifiers.meta.wanted = true;
} else if(k.length > 1) { //If it is a special key
if(special_keys[k] == code) kp++;
} else if(opt.keycode) {
if(opt.keycode == code) kp++;
} else { //The special keys did not match
if(character == k) kp++;
else {
if(shift_nums[character] && e.shiftKey) { //Stupid Shift key bug created by using lowercase
character = shift_nums[character];
if(character == k) kp++;
}
}
}
}
if(kp == keys.length &&
modifiers.ctrl.pressed == modifiers.ctrl.wanted &&
modifiers.shift.pressed == modifiers.shift.wanted &&
modifiers.alt.pressed == modifiers.alt.wanted &&
modifiers.meta.pressed == modifiers.meta.wanted) {
callback(e);
if(!opt['propagate']) { //Stop the event
//e.cancelBubble is supported by IE - this will kill the bubbling process.
e.cancelBubble = true;
e.returnValue = false;
//e.stopPropagation works in Firefox.
if (e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
}
return false;
}
}
}
this.all_shortcuts[shortcut_combination] = {
'callback':func,
'target':ele,
'event': opt['type']
};
//Attach the function with the event
if(ele.addEventListener) ele.addEventListener(opt['type'], func, false);
else if(ele.attachEvent) ele.attachEvent('on'+opt['type'], func);
else ele['on'+opt['type']] = func;
},
//Remove the shortcut - just specify the shortcut and I will remove the binding
'remove':function(shortcut_combination) {
shortcut_combination = shortcut_combination.toLowerCase();
var binding = this.all_shortcuts[shortcut_combination];
delete(this.all_shortcuts[shortcut_combination])
if(!binding) return;
var type = binding['event'];
var ele = binding['target'];
var callback = binding['callback'];
if(ele.detachEvent) ele.detachEvent('on'+type, callback);
else if(ele.removeEventListener) ele.removeEventListener(type, callback, false);
else ele['on'+type] = false;
}
}
/*Chiamata senza parametri corregge gli apostrofi nel box di default (wpTextbox1[0] in ns0, wpTextbox1[1] in nsPagina.
In rari casi può essere forzata l'azione su un box non di default (box="0","1","2" ) */
function newApostrofi(box) {
var testo;
if (box === undefined) {
testo = leggiBox();
} else {
testo = leggiBox(box);
}
var testoCod = codifica(testo);
testoCod[0] = testoCod[0].replace(/'/g, '’').replace(/’’’’’/g, "'''''").replace(/’’’’/g, "''''").replace(/’’’/g, "'''").replace(/’’/g, "''");
testo = decodifica(testoCod[0], testoCod[1]);
if (box === undefined) {
scriviBox(testo);
} else {
scriviBox(testo, box);
}
}
// funzione per gestire l'icona Link a immagini IA in nsIndice
$(document).ready(function () {
if (mw.config.get('wgCanonicalNamespace')=="Index") {
var dataIA=$("span.data").data("fonte");
if (dataIA !== undefined) {
if (dataIA.indexOf("[")==-1) {
iaItem=find_stringa(dataIA+"/","/details/","/",0); //prendi cio' che si trova tra /details/ e il successivo /
}
else {
iaItem=find_stringa(dataIA,"/details/"," ",0); //prendi cio' che si trova tra /details/ e il successivo spazio
}
if (iaItem!=="") { // fonte contiene un link come stringa
iaImageLink="//archive.org/download/"+iaItem+"/"+iaItem+"_jp2.zip/";
$("div[title='IA HR images link'] a").attr("href",iaImageLink).removeClass("image").attr("target","_blank");
}
else {
$("div[title='IA HR images link']").remove();
}
}
}
});
// scrive una pagina generica
// dati è un oggetto:
// dati.nome
// dati.testo
// dati.sommario
function pagePut(dati) {
if(dati.nome===undefined || dati.sommario===undefined || dati.testo===undefined) {
alert("parametri pagePut insufficienti");
return;
}
$.ajax({
url: mw.util.wikiScript( 'api' ),
data: {
format: 'json',
action: 'edit',
title: dati.nome,
summary: dati.sommario,
text: dati.testo,
token: mw.user.tokens.get('csrfToken')
},
dataType: 'json',
type: 'POST',
success: function( data ) {
if ( data && data.edit && data.edit.result == 'Success' ) {
if (dati.reload) window.open(window.location.origin+window.location.pathname+"?action=purge","_self");
// purges current page
if (dati.messaggio) mw.notify(messaggio);
if (dati.feedback) alert(dati.feedback);
} else if ( data && data.error ) {
alert( 'Error: API returned error code "' + data.error.code + '": ' + data.error.info );
} else {
alert( 'Error: Unknown result from API.' );
}
},
error: function( xhr ) {
alert( 'Error: Request failed.' );
}
});
}
function pageGet(pagina) {
if (pagina===undefined) pagina=mw.config.get("wgPageName");
var contenuto = $.ajax({
url: "/w/index.php?action=raw&title=" + pagina,
async: false
}).responseText;
return contenuto;
}
// template è il nome del template di cui fare il parsing (es. RigaIntestazione )
// testo è il testo dove ricercare il template (default leggiBox() )
// restituisce un oggetto costituito da un dizionario e una lista
// il dizionario contiene le coppie nome paramtro - valore
// la lista contiene i nomi dei parametri, o cifre (in stringa) per i parametri posizionali
// il nome del template è associato al parametro con chiave "0" )
// esempio: da Pagina:Eneide (Caro).djvu/91 ilcomando:
// parseTemplate("RigaIntestazione",leggiBox(0))
// restituisce [{"0":"RigaIntestazione","1":"48","2":"{{Sc|l'eneide.}}","3":"[1158-1182]"},["0","1","2","3"]]
function parseTemplate(template, testo) {
function cod(testo) {
var l = produciLista(testo, "{"+"{", "}}", 1, "{{");
for (var i = 0; i < l.length; i += 1) {
testo = testo.replace(l[i], l[i].replace(/\|/g, "__!__"));
}
l = produciLista(testo, "[[", "]]", 1, "[[");
for (i = 0; i < l.length; i += 1) {
testo = testo.replace(l[i], l[i].replace(/\|/g, "__!__"));
}
return testo;
}
function decod(testo) {
testo = testo.replace(/__!__/g, "|");
return testo;
}
if (testo === undefined) testo = leggiBox();
var cap=template.substring(0,1).toLocaleUpperCase()+template.substring(1);
testo=testo.replace("{{"+cap,"{{"+template);
var t = find_stringa(testo, "{"+"{" + template, "}}", 1, "{{");
var l = []; // lista delle keys
t = "0=" + t.substring(2, t.length - 2); // nome del template in parametro "0"
l.push["0"];
var ts = {};
var n = 1;
t = cod(t);
t = t.split("|");
// element for element
for (i = 0; i < t.length; i += 1) {
// case param is positional
if (t[i].indexOf("=") == -1) {
t[i] = n + "=" + t[i];
n = n + 1;
}
var els = [];
els[0] = t[i].substring(0, t[i].indexOf("=")).trim();
els[1] = t[i].substring(t[i].indexOf("=") + 1).trim();
if (els[1][els[1].length - 1] == "\n") els[1] = els[1].substring(0, els[1].length - 1);
ts[els[0]] = decod(els[1]);
l.push(els[0]);
}
return [ts, l];
}
/*
function templateObj(nomeTemplate) {
data = parseTemplate(nomeTemplate);
this.keys = data[1];
this.dict = data[0];
}
*/
function rewriteTemplate(x) {
var testo = "";
$.each(x[1], function (indice, valore) {
if (valore != "0") testo += " | " + valore + " = " + x[0][valore] + "\n";
});
testo = "{{" + x[0]["0"] + "\n" + testo + "}}\n";
testo = testo.replace(/\n\s\|\s\d*\s=\s/g, "\n | ");
return testo;
}
/***************
La funzione codifica() riceve un testo markup wiki e restituisce un testo "puro" e una lista ordinata degli elementi
che vanno protetti da replace() pena l'interruzione di link ecc.
ATTENZIONE: funzione in fase di test
**************/
function codifica(testo) {
var l = [];
var res = ss(testo, l, "<math", "</math>", "");
res = ss(res[0], res[1], "<!--", "-->", "<");
res = ss(res[0], res[1], "{", "}", "{");
res = ss(res[0], res[1], "[", "]", "[");
res = ss(res[0], res[1], "<", ">", "<");
res = ss(res[0], res[1], "http://", " ", "");
res = ss(res[0], res[1], "https://", " ", "");
return res;
}
function ss(testo, l, tagi, tagf, x) {
while (find_stringa(testo, tagi, tagf, 1) > "") {
var el = find_stringa(testo, tagi, tagf, 1, tagi);
testo = testo.replace(el, "###el" + l.length + "###");
l.push(el);
}
return [testo, l];
}
// La funzione decodifica() riceve un testo codificato e la lista degli elementi protetti e restituisce un testo
// con gli elementi protetti risistemati al loro posto; è complementare a codifica()
function decodifica(testo, l) {
for (i = l.length - 1; i > -1; i = i - 1) {
testo = testo.replace("###el" + i + "###", l[i]);
}
return testo;
}
//Sostituisce tutti gli apostrofi dattilografici in tipografici, ma rispetta gli apostrofi di marckup wiki e gli apostrofi
//contenuti in: math, {{{}}}, {{}}, [[]], [], http:.....
function apostrofi(editbox) {
if (editbox === undefined) {
editbox = document.getElementsByName('wpTextbox1')[0];
}
var testoCod = codifica(editbox.value);
testoCod[0] = testoCod[0].replace(/'/g, '’').replace(/’’’’’/g, "'''''").replace(/’’’’/g, "''''").replace(/’’’/g, "'''").replace(/’’/g, "''");
editbox.value = decodifica(testoCod[0], testoCod[1]);
}
function autoPt() {
var t = [],
rFin = /\ (\w+’*\w+)-$/, // parola finale ifenata
rPtIniz = /^\{\{pt\|/i, // pt in testa sulla pagina corrente
rPtFin = /\{\{pt\|(.+)-\|\}\}/i, // pt di tipo finale
rHyp2 = /^[^ \n]+/,
hyp = "",
hyp2 = "",
l = [],
prePt = "";
t = selection();
// se c'è una selezione di testo....
if (t[1].length > 0) {
if (t[0].length > t[2].length) { //allora va applicato un pt-before...
t[1] = "{{Pt|" + t[1] + "|}}";
} else {
testoPagPrec[1] = testoPagPrec[1].replace(/\{\{pt\|/g, "{{Pt|");
l = produciLista(testoPagPrec[1], "{{Pt|", "}}", 1, "{{");
prePt = "";
if (l.length > 0) {
prePt = l[l.length - 1];
prePt = find_stringa(prePt, "{{Pt|", "|", 0);
}
t[1] = "{{Pt|" + t[1] + "|" + prePt.substring(0, prePt.length - 1) + t[1] + "}}";
}
scriviSel(t);
} else {// invece se non c'è alcuna selezione di testo...
t = $.trim(leggiBox());
// se la pagina precedente ha un pt di tipo finale
// e la pagina corrente non ha un pt di tipo iniziale
// allora applica un pt iniziale
if (rPtFin.exec(testoPagPrec[1]) !== null && rPtIniz.exec(t) === null) {
hyp = rPtFin.exec(testoPagPrec[1])[1];
hyp2 = rHyp2.exec(t);
t = "{{Pt|" + hyp2 + "|" + hyp + hyp2 + "}}" + t.replace(hyp2, "");
}
// se la pagina corrente termina con parola ifenata
// allora applica pt finale sulla
if (rFin.exec(t) !== null) {
hyp = rFin.exec(t)[1]; //parola ifenata senza trattino
t = t.substr(0, t.search(rFin) + 1) +
"{{Pt|" + hyp + "-|}}";
}
scriviBox(t);}
}
function refs() {
tt=selection();
t=tt[1].split("\n");
for (i=0;i<t.length;i+=1){
if ($.trim(t[i])!=="") t[i]="<ref>"+t[i]+"</ref>";
}
tt[1]=t.join("\n");
scriviBox(tt.join(""));
}