Utente:Alex brollo/Base.js
Nota: dopo aver salvato, potrebbe essere necessario pulire la cache del proprio browser per vedere i cambiamenti.
- Firefox / Safari: tenere premuto il tasto delle maiuscole e fare clic su Ricarica, oppure premere Ctrl-F5 o Ctrl-R (⌘-R su Mac)
- Google Chrome: premere Ctrl-Shift-R (⌘-Shift-R su un Mac)
- Internet Explorer: tenere premuto il tasto Ctrl mentre si fa clic su Refresh, oppure premere Ctrl-F5
- Opera: svuotare completamente la cache dal menu Strumenti → Preferenze
/*<pre>*/
// Creo una div contenitore per dati globali
$("body").append($('<div id="DivData"></div>'));
// Importo da wikisource le due funzioni per leggere i parametri GET di un URL
$.getUrlVars=function (){var vars=[],hash;var hashes=window.location.href.slice(window.location.href.indexOf('?')+1).split('&');for(var i=0;i<hashes.length;i++){hash=hashes[i].split('=');vars.push(hash[0]);vars[hash[0]]=hash[1];}return vars;}
$.getUrlVar=function (name){return $.getUrlVars()[name];}
/* Funzioni di utilità generale
* by :it:s:User:Alex brollo
*
*/
// trasforma un template in un oggetto javasctipt [dizionario,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
var 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];
}
// utility per parseTemplate()
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 i = 0; i < l.length; i += 1) {
testo = testo.replace(l[i], l[i].replace(/\|/g, "__!__"));
}
return testo;
}
// reciproca di cod()
function decod(testo) {
testo = testo.replace(/__!__/g, "|");
return testo;
}
/*
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;
}
// utility per find_stringa
function count(testo,stringa){
var n=0;
while(testo.indexOf(stringa)>-1){
n=n+1;
testo=testo.replace(stringa,"");
}
return n;}
// derivata da find_stringa, lancia ripetutamente find_stringa ottenendo la lista dei risultati
function produciLista(testo, s1, s2, delim, x) {
lista = new Array();
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;
}
// legge il textbox di edit standard su pagine normali
function leggiBox(area) {
if (area == undefined) {
if (wgCanonicalNamespace == "Pagina") area = 1;
else area = 0;
}
return $('textarea')[area].value;
}
// reciproca di leggiBox(), scrive un testo nel textbox standard
function scriviBox(testo, area, ss, se) {
if (area == undefined || area == "") {
if (wgCanonicalNamespace == "Pagina") area = 1;
else area = 0;
}
$('textarea')[area].value = testo;
if (ss != undefined && se != undefined) {
$('textarea')[area].selectionStart = ss;
$('textarea')[area].selectionEnd = se;
}
}
// restituisce la selection del box di edit standard come lista di tre elementi [pre, selected, post]
function selection(area) {
if (area == undefined) {
if (wgCanonicalNamespace == "Pagina") 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);
if (s[1].lastIndexOf(" ") == s[1].length - 1) {
s[1] = s[1].substring(0, s[1].length - 1);
s[2] = " " + s[2];
}
return s;
}
/*</pre>*/