construct.js 2.72 KB
/**
 * Lambdas 
 */
var accessNumber = e => {
    if (e.childElementCount == 0) return 0;
    if (e.children[0].childElementCount == 0) return 0;
    return e.children[0].children[0];
}

var getAncestorByAttribute = (elem,attr,val) => {
    while (elem.getAttribute(attr) != val || elem == null) elem = elem.parentElement;
    return elem;
}

/**
 * Armado de formulario 
 */
function removeTab(button){
    let tabs = document.getElementById('tabs')
    tabs.removeChild(
        getAncestorByAttribute(button,'name','Tab')
    )
    if (tabs.childElementCount > 0){
        document.getElementById("ContinuarText").style.display = 'none';
        document.getElementById("ContinuarButton").style.display = 'none';
    }
}

function addField(b,n){
    let newfield = document.createElement("div");
    newfield.setAttribute("name","Field");
    newfield.innerHTML = document.getElementById("fieldTemplate").innerHTML;
    accessNumber(newfield).setAttribute("value",n);
    // swap
    let tab = b.parentElement;
    tab.appendChild(newfield);
    tab.appendChild(b);
}

function addTab(t,n){
    document.getElementById("ContinuarText").style.display = 'block';
    document.getElementById("ContinuarButton").style.display = 'block';
    let newtab = document.createElement("div");
    newtab.setAttribute("name","Tab");
    newtab.innerHTML = document.getElementById("tabTemplate").innerHTML;
    accessNumber(newtab).setAttribute("value",n);
    t.appendChild(newtab);
}

/**
 * Lectura de información y formateado
 */
function processField(field){
    return {
        'Title' : field.children[0],
        'Input' : field.children[1].selectedOptions[0].value,
        'Required' : field.children[2].checked,
    };
}

function processTab(tab){
    let dict = {};
    for(let i = 0; i < tab.children.length; i++){
        if (tab.children[i].getAttribute("name") == "Field"){
            dict[i] = processField(tab.children[i]);
        }
    }
    return dict;
}

function generate(tabs){
    let dict = {};
    for(let i = 0; i < tabs.children.length; i++){
        if (tabs.children[i].getAttribute("name") == "Tab"){
            dict[i] = processTab(tabs.children[i]);
        }
    }
    console.log(JSON.stringify(dict));
}

function sortChildren(container,number){
    fixparse
    Array.from(container.children).sort(
        (a,b) => number(a).value < number(b).value ? -1 : number(a).value > number(b).value ? 1 : 0
    ).forEach(
        c => tabs.appendChild(c)
    )
}

function maxChild(container,number){
    if (container.childElementCount == 0) return 1;
    let max = Array.from(container.children).reduce(
        (a,b) => number(a).value > number(b).value ? a : b
    )
    return parseInt(number(max).value) == NaN ? 1 : parseInt(number(max).value) + 1;
}