Commit f873b0cc by Luciano Barletta

all done

1 parent 4c298a5b
...@@ -12,6 +12,16 @@ var getAncestorByAttribute = (elem,attr,val) => { ...@@ -12,6 +12,16 @@ var getAncestorByAttribute = (elem,attr,val) => {
return elem; return elem;
} }
var getDescendantByAttribute = (elem,attr,val) => {
if (elem.getAttribute(attr) == val) return elem;
Array.from(elem.children).reduce( (a,b) => {
let x = getDescendantByAttribute(a,attr,val);
let y = getDescendantByAttribute(b,attr,val);
return x == null ? x : y;
});
return null;
}
/** /**
* Armado de formulario * Armado de formulario
*/ */
...@@ -20,7 +30,7 @@ function removeTab(button){ ...@@ -20,7 +30,7 @@ function removeTab(button){
tabs.removeChild( tabs.removeChild(
getAncestorByAttribute(button,'name','Tab') getAncestorByAttribute(button,'name','Tab')
) )
if (tabs.childElementCount > 0){ if (tabs.childElementCount == 0){
document.getElementById("ContinuarText").style.display = 'none'; document.getElementById("ContinuarText").style.display = 'none';
document.getElementById("ContinuarButton").style.display = 'none'; document.getElementById("ContinuarButton").style.display = 'none';
} }
...@@ -32,9 +42,9 @@ function addField(b,n){ ...@@ -32,9 +42,9 @@ function addField(b,n){
newfield.innerHTML = document.getElementById("fieldTemplate").innerHTML; newfield.innerHTML = document.getElementById("fieldTemplate").innerHTML;
accessNumber(newfield).setAttribute("value",n); accessNumber(newfield).setAttribute("value",n);
// swap // swap
let tab = b.parentElement; let container = b.parentElement;
tab.appendChild(newfield); container.appendChild(newfield);
tab.appendChild(b); container.appendChild(b);
} }
function addTab(t,n){ function addTab(t,n){
...@@ -79,18 +89,21 @@ function generate(tabs){ ...@@ -79,18 +89,21 @@ function generate(tabs){
} }
function sortChildren(container,number){ function sortChildren(container,number){
fixparse let fixinput = a => number(a).nodeName == "INPUT" && number(a).type == "number" ? parseInt(number(a).value) : Infinity;
Array.from(container.children).sort( Array.from(container.children).sort(
(a,b) => number(a).value < number(b).value ? -1 : number(a).value > number(b).value ? 1 : 0 (a,b) => fixinput(a) < fixinput(b) ? -1 : fixinput(a) > fixinput(b) ? 1 : 0
).forEach( ).forEach(
c => tabs.appendChild(c) c => container.appendChild(c)
) )
} }
function maxChild(container,number){ function maxChild(container,number){
if (container.childElementCount == 0) return 1; let fixinput = a => number(a).nodeName == "INPUT" && number(a).type == "number" ? parseInt(number(a).value) : 0;
if (container.childElementCount == 0) return 0;
let max = Array.from(container.children).reduce( let max = Array.from(container.children).reduce(
(a,b) => number(a).value > number(b).value ? a : b (a,b) => fixinput(a) > fixinput(b) ? a : b
) )
return parseInt(number(max).value) == NaN ? 1 : parseInt(number(max).value) + 1; return fixinput(max);
} }
\ No newline at end of file \ No newline at end of file
...@@ -19,10 +19,7 @@ ...@@ -19,10 +19,7 @@
<div id="tabs"></div> <div id="tabs"></div>
<div style="text-align: center;"> <div style="text-align: center;">
<button class="NewTabButton" <button class="NewTabButton"
onclick="addTab( onclick="let t = document.getElementById('tabs'); addTab(t, maxChild(t, accessNumber) + 1)">
document.getElementById('tabs'),
maxChild(document.getElementById('tabs'),accessNumber)
)">
+ +
</button> </button>
</div> </div>
...@@ -33,7 +30,8 @@ ...@@ -33,7 +30,8 @@
<!-- templates --> <!-- templates -->
<template id="tabTemplate"> <template id="tabTemplate">
<div class="TabCards"> <div class="TabCards">
<input type="number" class="TabNumber" onchange="sortChildren(document.getElementById('tabs'),accessNumber)"> <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
...@@ -41,10 +39,7 @@ ...@@ -41,10 +39,7 @@
<br> <br>
<div style="text-align: center;"> <div style="text-align: center;">
<button class="NewInputButton" <button class="NewInputButton"
onclick="addField( onclick="addField(this, maxChild(this.parentElement,accessNumber) + 1)">
this,
maxChild(getAncestorByAttribute(this,'name','Tab'),accessNumber)
)">
+ +
</button> </button>
</div> </div>
...@@ -55,10 +50,7 @@ ...@@ -55,10 +50,7 @@
<div class="InputContainer"> <div class="InputContainer">
<input type="number" class="InputNumber" <input type="number" class="InputNumber"
onchange="sortChildren( onchange="sortChildren(getAncestorByAttribute(this,'name','Field').parentElement, accessNumber)">
getAncestorByAttribute(this,'name','Tab'),
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">
...@@ -74,7 +66,7 @@ ...@@ -74,7 +66,7 @@
</select> </select>
Obligatorio : <input type="checkbox"> Obligatorio : <input type="checkbox">
<button class="RemoveInputButton" <button class="RemoveInputButton"
onclick="getAncestorByAttribute(this,'name','Tab').removeChild(getAncestorByAttribute(this,'name','Field'))"> 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!