Commit 0bef61b1 by Georgina Mondino

Merge branch 'master' of http://git.anacsoft.com/Lusho/mini-web

2 parents 7cecb102 a7db7f62
/** /**
* 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;
}
var getDescendantByAttribute = (elem,attr,val) => {
if (elem == null || elem.getAttribute(attr) == val) return elem;
return Array.from(elem.children).reduce( (a,b) => {
let x = getDescendantByAttribute(a,attr,val);
let y = getDescendantByAttribute(b,attr,val);
return x == null ? y : x;
}, null);
}
/**
* Armado de formulario * Armado de formulario
*/ */
function removeField(b){ function removeTab(button){
while(b.getAttribute("name") != "Field") b = b.parentElement; let tabs = document.getElementById('tabs')
if(b.nodeElement == "body") return; tabs.removeChild(
b.parentElement.removeChild(b); getAncestorByAttribute(button,'name','Tab')
)
if (tabs.childElementCount == 0){
document.getElementById("ContinuarText").style.display = 'none';
document.getElementById("ContinuarButton").style.display = 'none';
}
} }
function addField(b){ function addField(b,n){
let newfield = document.createElement("div"); let newfield = document.createElement("div");
newfield.setAttribute("name","Field"); newfield.setAttribute("name","Field");
newfield.innerHTML = document.getElementById("fieldTemplate").innerHTML; newfield.innerHTML = document.getElementById("fieldTemplate").innerHTML;
let tab = b.parentElement; accessNumber(newfield).setAttribute("value",n);
// swap // swap
tab.removeChild(b); let container = b.parentElement;
tab.appendChild(newfield); container.appendChild(newfield);
tab.appendChild(b); container.appendChild(b);
}
function removeTab(b){
while(b.getAttribute("name") != "Tab") b = b.parentElement;
if(b.nodeElement == "body") return;
b.parentElement.removeChild(b);
} }
function addTab(t){ function addTab(t,n){
document.getElementById("ContinuarText").style.display = 'block'; document.getElementById("ContinuarText").style.display = 'block';
document.getElementById("ContinuarButton").style.display = 'block'; document.getElementById("ContinuarButton").style.display = 'block';
let newtab = document.createElement("div"); let newtab = document.createElement("div");
newtab.setAttribute("name","Tab"); newtab.setAttribute("name","Tab");
newtab.innerHTML = document.getElementById("tabTemplate").innerHTML; newtab.innerHTML = document.getElementById("tabTemplate").innerHTML;
accessNumber(newtab).setAttribute("value",n);
t.appendChild(newtab); t.appendChild(newtab);
} }
/** /**
* Lectura de información y formateado * Lectura de información y formateado
*/ */
function processField(field){ function processField(field){
let data = field.children[0];
return { return {
'Title' : field.children[0], 'Title' : data.children[1].value,
'Input' : field.children[1].selectedOptions[0].value, 'Input' : data.children[2].selectedOptions[0].value,
'Required' : field.children[2].checked, 'Required' : data.children[3].checked,
}; };
} }
function processTab(tab){ function processTab(tab){
let dict = {}; let dict = [];
for(let i = 0; i < tab.children.length; i++){ let container = getDescendantByAttribute(tab,"name","Field").parentElement;
if (tab.children[i].getAttribute("name") == "Field"){ for(let i = 0; i < container.children.length; i++){
dict[i] = processField(tab.children[i]); if (container.children[i].getAttribute("name") == "Field") {
dict.push(processField(container.children[i]));
} }
} }
return dict; return dict;
} }
function generate(tabs){ function generate(tabs){
let dict = {}; let dict = [];
for(let i = 0; i < tabs.children.length; i++){ for (let i = 0; i < tabs.children.length; i++) {
if (tabs.children[i].getAttribute("name") == "Tab"){ let c = tabs.children[i];
dict[i] = processTab(tabs.children[i]); if (c.getAttribute("name") == "Tab") {
dict.push({
"Title" : getDescendantByAttribute(c,"class","TabTitle").value,
"Fields" : processTab(c),
});
} }
} }
console.log(JSON.stringify(dict)); console.log(JSON.stringify(dict));
} }
function sortTabs(){ function sortChildren(container,number){
let tabs = document.getElementById("tabs"); let fixinput = a => number(a).nodeName == "INPUT" && number(a).type == "number" ? parseInt(number(a).value) : Infinity;
Array.from(tabs.children).sort(
(a,b) => a.children[0].value < b.children[0].value ? -1 : a.children[0].value > b.children[0].value ? 1 : 0 Array.from(container.children).sort(
(a,b) => fixinput(a) < fixinput(b) ? -1 : fixinput(a) > fixinput(b) ? 1 : 0
).forEach( ).forEach(
c => tabs.appendChild(c) c => container.appendChild(c)
) )
} }
function sortFields(tab){ function maxChild(container,number){
Array.from(tab.children).sort( let fixinput = a => number(a).nodeName == "INPUT" && number(a).type == "number" ? parseInt(number(a).value) : 0;
(a,b) => a.children[0].value < b.children[0].value ? -1 : a.children[0].value > b.children[0].value ? 1 : 0
).forEach( if (container.childElementCount == 0) return 0;
c => tabs.appendChild(c) let max = Array.from(container.children).reduce(
) (a,b) => fixinput(a) > fixinput(b) ? a : b
);
return fixinput(max);
} }
\ No newline at end of file \ No newline at end of file
...@@ -18,25 +18,28 @@ ...@@ -18,25 +18,28 @@
<br> <br>
<div id="tabs"></div> <div id="tabs"></div>
<div style="text-align: center;"> <div style="text-align: center;">
<button class="NewTabButton" onclick="addTab(document.getElementById('tabs'))"> <button class="NewTabButton"
onclick="let t = document.getElementById('tabs'); addTab(t, maxChild(t, accessNumber) + 1)">
+ +
</button> </button>
</div> </div>
<h3 id="ContinuarText">Cuando se encuentre conforme con el formulario presione Continuar</h3> <h3 id="ContinuarText">Cuando se encuentre conforme con el formulario presione Continuar</h3>
<button id="ContinuarButton"> Continuar </button> <button id="ContinuarButton" onclick="generate(document.getElementById('tabs'))"> Continuar </button>
</body> </body>
<!-- templates --> <!-- templates -->
<template id="tabTemplate"> <template id="tabTemplate">
<div class="TabCards"> <div class="TabCards">
<input type="number" class="TabNumber"> <input type="number" class="TabNumber"
onchange="sortChildren(getAncestorByAttribute(this,'name','Tab').parentElement, accessNumber)">
<input class="TabTitle" placeholder="Titulo del Tab" type="text"> <input class="TabTitle" placeholder="Titulo del Tab" type="text">
<button class="RemoveTabButton" onclick="removeTab(this)"> <button class="RemoveTabButton" onclick="removeTab(this)">
X X
</button> </button>
<br> <br>
<div style="text-align: center;"> <div style="text-align: center;">
<button class="NewInputButton" onclick="addField(this)"> <button class="NewInputButton"
onclick="addField(this, maxChild(this.parentElement,accessNumber) + 1)">
+ +
</button> </button>
</div> </div>
...@@ -46,7 +49,8 @@ ...@@ -46,7 +49,8 @@
<template id="fieldTemplate"> <template id="fieldTemplate">
<div class="InputContainer"> <div class="InputContainer">
<input type="number" class="InputNumber"> <input type="number" class="InputNumber"
onchange="sortChildren(getAncestorByAttribute(this,'name','Field').parentElement, accessNumber)">
<input type="text" id="Titulo" class="Input" placeholder="Título"> <input type="text" id="Titulo" class="Input" placeholder="Título">
<select name="input" class="Input"> <select name="input" class="Input">
...@@ -61,7 +65,8 @@ ...@@ -61,7 +65,8 @@
<option id="checkbox" value="checkbox">Selección</option> <option id="checkbox" value="checkbox">Selección</option>
</select> </select>
Obligatorio : <input type="checkbox"> Obligatorio : <input type="checkbox">
<button class="RemoveInputButton" onclick="removeField(this)"> <button class="RemoveInputButton"
onclick="let f = getAncestorByAttribute(this,'name','Field'); f.parentElement.removeChild(f)">
- -
</button> </button>
</div> </div>
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!