l-assiette/dist/js/base.min.js
2018-10-06 09:50:08 -05:00

2 lines
7.5 KiB
JavaScript

carritoUsuario=[];window.addEventListener("hashchange",()=>{render(decodeURI(window.location.hash))});const render=e=>{switch(e){case"#registro":abrirRegistro();break;case"#iniciar-sesion":abrirInicioSesion();break;case"#usuarios/":abrirUsuarios();break;case"#usuarios/mi-cuenta":abrirMiCuenta();break;case"#usuarios/pagos":abrirCarrito();break;case"#staff/":abrirStaff();break;default:return void abrirPagPrin()}},sectionP=document.getElementById("sectionP"),contenedorInicioRegistro=document.getElementById("contenedorInicio-Registro"),formInicioRegistro=document.getElementById("inicio_registro"),sectionS=document.getElementById("sectionS"),sectionCarrito=document.getElementById("sectionCarrito"),manipularTopBar=e=>{switch(e){case 1:cambiarProgreso(25);break;case 2:cancelarTransicion(),cambiarProgreso(50);break;case 3:cancelarTransicion(),cambiarProgreso(75);break;default:cancelarTransicion(),reiniciarTopBar()}},abrirUsuarios=()=>{const e=new XMLHttpRequest;reiniciarTopBar(),cerrarElemF(contenedorInicioRegistro),cerrarElemF(formInicioRegistro),cerrarElemF(sectionP),cerrarElemF(sectionCarrito),e.onreadystatechange=(()=>{200===e.status&&4===e.readyState?(cambiarProgreso(100).then(ocultarTopBar),abrirElemF(sectionS,()=>{sectionS.innerHTML=e.responseText,(()=>new Promise((e,t)=>{const r=new XMLHttpRequest;r.onreadystatechange=(()=>{if(200===r.status&&4===r.readyState){const t=JSON.parse(r.responseText),n=document.getElementById("productos");let a=document.createElement("div");a.className="row";let i=0;for(const e in t)if(t.hasOwnProperty(e)){const r=t[e];++i>=4&&(n.appendChild(a),n.appendChild(document.createElement("br")),(a=document.createElement("div")).className="row",i=1);const o=document.createElement("div");o.className="col-4 caja plato";const c=document.createElement("img");c.className="image",c.src=r.imgUrl,o.appendChild(c);const s=document.createElement("div");s.className="plato--Titulo",s.innerText=r.nombre,o.appendChild(s),o.appendChild(document.createElement("br"));const d=document.createElement("div");d.className="plato--Descripcion",d.innerText=r.descripcion,o.appendChild(d),o.appendChild(document.createElement("br"));const l=document.createElement("div");l.className="plato--Precio",l.innerText=`${r.precio}`,o.appendChild(l);const m=document.createElement("div");m.className="plato--Cantidad",m.appendChild(document.createTextNode("Cantidad: "));const p=document.createElement("span"),u=document.createElement("i");u.className="material-icons plato--Cantidad--Icon",u.innerText="remove",u.addEventListener("click",()=>{const e=parseInt(p.innerText);p.innerText=e>1?e-1:"1"}),m.appendChild(u),p.innerText="1",m.appendChild(p);const h=document.createElement("i");h.className="material-icons plato--Cantidad--Icon",h.innerText="add",h.addEventListener("click",()=>{const e=parseInt(p.innerText);p.innerText=e>0?e+1:"1"}),m.appendChild(h),o.appendChild(m),o.appendChild(document.createElement("br"));const E=document.createElement("button");E.className="botonAddCart",E.addEventListener("click",()=>{const r=parseInt(p.innerText);if(r>0){const n=t[e];n.cantidad=r,carritoUsuario.push(n),console.log(`JSON del usuario|>\n${JSON.stringify(carritoUsuario)}`),window.location.replace("./#usuarios/pagos")}else alert("")}),E.innerHTML="<i class='material-icons'>add_shopping_cart</i> Añadir al carrito",o.appendChild(E),a.appendChild(o)}n.appendChild(a),e()}}),r.open("POST","./usuarios/platos.json",!0),r.setRequestHeader("Content-type","application/x-www-form-urlencoded"),r.send()}))()})):manipularTopBar(e.readyState)}),e.open("POST","./usuarios/index.php",!0),e.setRequestHeader("Content-type","application/x-www-form-urlencoded"),e.send()},abrirMiCuenta=()=>{const e=new XMLHttpRequest;reiniciarTopBar(),cerrarElemF(contenedorInicioRegistro),cerrarElemF(formInicioRegistro),cerrarElemF(sectionP),cerrarElemF(sectionCarrito),e.onreadystatechange=(()=>{200===e.status&&4===e.readyState?(cambiarProgreso(100).then(ocultarTopBar),abrirElemF(sectionS,()=>{sectionS.innerHTML=e.responseText})):manipularTopBar(e.readyState)}),e.open("POST","./usuarios/mi-cuenta.php",!0),e.setRequestHeader("Content-type","application/x-www-form-urlencoded"),e.send()},abrirCarrito=()=>{reiniciarTopBar(),cerrarElemF(contenedorInicioRegistro),cerrarElemF(formInicioRegistro),cerrarElemF(sectionP),cerrarElemF(sectionS),abrirElemF(sectionCarrito)},abrirStaff=()=>{const e=new XMLHttpRequest;reiniciarTopBar(),cerrarElemF(contenedorInicioRegistro),cerrarElemF(formInicioRegistro),cerrarElemF(sectionP),cerrarElemF(sectionCarrito),e.onreadystatechange=(()=>{200===e.status&&4===e.readyState?(cambiarProgreso(100).then(ocultarTopBar),abrirElemF(sectionS,()=>{sectionS.innerHTML=e.responseText})):manipularTopBar(e.readyState)}),e.open("POST","./staff/",!0),e.setRequestHeader("Content-type","application/x-www-form-urlencoded"),e.send()},abrirInicioSesion=()=>{const e=new XMLHttpRequest;reiniciarTopBar(),cerrarElemF(contenedorInicioRegistro),cerrarElemF(sectionP),cerrarElemF(sectionCarrito),e.onreadystatechange=(()=>{if(200===e.status&&4===e.readyState){cambiarProgreso(100).then(ocultarTopBar);const t=document.getElementById("inicio_registro");abrirElemF(t,()=>{t.innerHTML=e.responseText})}else manipularTopBar(e.readyState)}),e.open("POST","./iniciar-sesion.php",!0),e.setRequestHeader("Content-type","application/x-www-form-urlencoded"),e.send()},abrirRegistro=()=>{const e=new XMLHttpRequest;reiniciarTopBar(),cerrarElemF(contenedorInicioRegistro),cerrarElemF(sectionP),cerrarElemF(sectionCarrito),e.onreadystatechange=(()=>{if(200===e.status&&4===e.readyState){cambiarProgreso(100).then(ocultarTopBar);const t=document.getElementById("inicio_registro");abrirElemF(t,()=>{t.innerHTML=e.responseText})}else manipularTopBar(e.readyState)}),e.open("POST","./registro.php",!0),e.setRequestHeader("Content-type","application/x-www-form-urlencoded"),e.send()},abrirPagPrin=()=>{const e=document.getElementById("inicio_registro");cerrarElemF(e,()=>e.innerHTML=""),cerrarElemF(sectionS,()=>sectionS.innerHTML=""),cerrarElemF(sectionCarrito),abrirElemF(contenedorInicioRegistro),abrirElemF(sectionP)},collapseSection=e=>new Promise((t,r)=>{let n=e.scrollHeight,a=e.style.transition;e.style.transition="",requestAnimationFrame(function(){e.style.height=n+"px",e.style.transition=a,requestAnimationFrame(function(){e.style.height="0px",setTimeout(()=>{t("Termine de ocultar we v':")},160)})}),e.setAttribute("data-collapsed","true")}),expandSection=e=>new Promise((t,r)=>{let n=e.scrollHeight;e.style.height=n+"px",e.addEventListener("transitionend",function(r){e.removeEventListener("transitionend",arguments.callee),e.style.height=null,setTimeout(()=>{t("Termine de ampliar we v':")},0)}),e.setAttribute("data-collapsed","false")}),abrirElemF=(e,t,r)=>{"true"===e.getAttribute("data-collapsed")?(void 0!==t&&t(),expandSection(e).then(()=>{void 0!==r&&r()})):collapseSection(e).then(()=>{void 0!==t&&t(),expandSection(e).then(()=>{void 0!==r&&r()})})},cerrarElemF=(e,t)=>{"false"===e.getAttribute("data-collapsed")&&collapseSection(e).then(()=>{void 0!==t&&t()})};render(decodeURI(window.location.hash));const carrito=new Vue({el:"#carrito",data:{item:carritoUsuario,descuento:0},computed:{cantidadItems:function(){let e=0;for(const t in this.item)e++;return e},subTotal:function(){let e=0;for(const t in this.item)if(this.item.hasOwnProperty(t)){const r=this.item[t];e+=r.precio*r.cantidad}return e},total:function(){return this.subTotal-this.descuento},noEstaVacio:function(){for(const e in this.item)return!0;return!1}},methods:{restarCantidad:function(e){const t=parseInt(e.cantidad);t>0&&(e.cantidad=t-1)},aumentarCantidad:function(e){const t=parseInt(e.cantidad);t>=0&&(e.cantidad=t+1)}}});