User:Alex brollo/cornersDependencies.js

From Wikisource
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
function selection(area) {
    if (area == undefined) {
        if (wgCanonicalNamespace == "Page") 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;
}

function scriviBox(testo, area, ss, se) {
    if (area === undefined || area === "") {
        if (wgCanonicalNamespace == "Page") {
            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;
    }
}

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;
}

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;
}

function decod(testo) {
    testo = testo.replace(/__!__/g, "|");
    return testo;
}

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];
}

function count(testo, stringa) {
    n = 0;
    while (testo.indexOf(stringa) > -1) {
        n = n + 1;
        testo = testo.replace(stringa, "");
    }
    return n;
}

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;
}