-
Notifications
You must be signed in to change notification settings - Fork 0
/
ezform.min.js
1 lines (1 loc) · 6.28 KB
/
ezform.min.js
1
"use strict";function postForm(e){this.ops={url:"",form:!1,formData:{},headers:{},validators:{items:{},messages:{}},method:"POST",notif:!0,notif_hide_time:3,beforeSubmit:function(){return!0},onSuccess:async function(){},onError:function(){}},this.data=function(){let e=this;if(!this.ops.headers.hasOwnProperty("X-CSRF-TOKEN")||this.ops.headers.hasOwnProperty("X-CSRF-TOKEN")&&""==this.ops.headers["X-CSRF-TOKEN"]){let e=document.querySelector('meta[name="csrf-token"]')?document.querySelector('meta[name="csrf-token"]').content:"";e=this.ops.form.querySelector('input[name="_token"]')?this.ops.form.querySelector('input[name="_token"]').value:e,this.ops.headers["X-CSRF-TOKEN"]=e}let t={};return Object.keys(this.ops.formData).forEach(o=>{t[o]=e.ops.formData[o].value}),{url:this.ops.url,form:this.ops.form,formData:t,isLoading:!1,notifbox:document.querySelector("#ppf-notif"),notif_hide_time:this.ops.notif_hide_time,headers:this.ops.headers,method:this.ops.method}},this.validate=function(e,t){if(!e)return{success:!1,message:"Data is empty",errors:["data is empty"]};let o=this,s=[{name:"required",action:function(e){return""!=e},message:"{name} is required"},{name:"email",action:function(e){return/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(e)},message:"{name} must be a valid email"},{name:"number",action:function(e){return/^[0-9]+$/.test(e)},message:"{name} must contains only numbers"},{name:"string",action:function(e){return/^[a-zA-Z]*$/g.test(e)},message:"{name} must be string"}],i=[];return Object.keys(e).forEach(r=>{let n=e[r].split("|"),a=o.data().formData,f=[];n.forEach(e=>{let o=s.find(t=>t.name==e);o&&!o.action(a[r])&&(t[r+"."+e]?f.push(t[r+"."+e].replaceAll("{name}",r.replaceAll("_"," ").toLowerCase())):f.push(o.message.replaceAll("{name}",r.replaceAll("_"," ").toLowerCase())))}),f.length>0&&(i=i.concat(f))}),i},this.events=function(){let e=this;this.data().form.addEventListener("submit",function(){event.preventDefault();let t=e.validate(e.ops.validators.items,e.ops.validators.messages);if(t.length>0)e.ops.notif&&e.notif("error",t.join(", ")),e.ops.onError(t);else{if(!e.ops.beforeSubmit())return;let t=new FormData,o=e.data(),s=o.formData;Object.keys(s).forEach(o=>{let i=s[o];"file"==e.ops.formData[o].getAttribute("type")&&e.ops.formData[o].files.length>0&&(i=e.ops.formData[o].files[0]),t.append(o,i)}),fetch(o.url,{method:o.method,body:t,headers:o.headers}).then(e=>e.json()).then(t=>{let o=[],s="";t.hasOwnProperty("errors")?(Object.keys(t.errors).forEach(e=>{o.push(t.errors[e])}),s=o.join(", "),e.ops.notif&&e.notif("error",s),e.ops.onError(t)):t.hasOwnProperty("success")&&!t.success?(s=t.message,t.hasOwnProperty("errors")&&(s=t.errors.join(",")),e.ops.notif&&e.notif("error",s),e.ops.onError(t)):(e.ops.notif&&e.notif("success",t.message),e.ops.onSuccess(t)),e.hideNotif()}).catch(t=>{let o=[],s="";t.hasOwnProperty("errors")?(Object.keys(t.errors).forEach(e=>{o.push(t.errors[e])}),s=o.join(", ")):s=t.message,e.ops.notif&&e.notif("error",s),e.ops.onError(t),e.hideNotif()})}})},this.hideNotif=function(){let e=this,t=1e3*this.data().notif_hide_time;setTimeout(function(){e.notif("","","hide")},t)},this.notif=function(e,t,o){let s=this;if(!this.data().notifbox){let e=document.createElement("div");e.setAttribute("id","ppf-notif"),e.onclick=function(){s.notif("","","hide")},e.classList.add("ppf-notif"),e.setAttribute("style","position:fixed;z-index:9999;display:none;font-size:16px;background:#fff;min-width:300px;max-width:90%;z-index:9999;box-shadow:0 1px 6px 0 rgba(0,0,0,.12),0 1px 6px 0 rgba(0,0,0,.12);bottom:20px;right:20px"),e.innerHTML='<div class="ppf-notif-content" style="padding:16px;height:100%;width:100%;display:flex;align-items:center"><div class="ppf-notif-icon" style="width:32px;height:32px;margin-right:8px"></div><span class="ppf-notif-message"></span></div>',document.querySelector("body").appendChild(e)}let i=this.data();"success"==e?i.notifbox.querySelector(".ppf-notif-icon").innerHTML='<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-circle-check" viewBox="0 0 24 24" stroke-width="1.5" stroke="#42ba96" fill="none" stroke-linecap="round" stroke-linejoin="round"><path fill="none" d="M0 0h24v24H0z" stroke="none"></path><path d="M12 12m-9 0a9 9 0 1 0 18 0a9 9 0 1 0 -18 0"></path><path d="M9 12l2 2l4 -4"></path></svg>':"error"==e&&(i.notifbox.querySelector(".ppf-notif-icon").innerHTML='<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-exclamation-circle" viewBox="0 0 24 24" stroke-width="1.5" stroke="#ff3333" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M12 12m-9 0a9 9 0 1 0 18 0a9 9 0 1 0 -18 0"/><path d="M12 9v4"/><path d="M12 16v.01"/></svg>'),t&&(i.notifbox.querySelector(".ppf-notif-message").textContent=t),o&&"show"!=o?"hide"==o&&(i.notifbox.classList.add("hide"),i.notifbox.style.display="none"):(i.notifbox.classList.add("show"),i.notifbox.style.display="block")},this.reset=function(){let e=this;Object.keys(this.ops.formData).forEach(t=>{e.ops.formData[t].value=""})},this.init=function(){let t=this;if(e)if(e.hasOwnProperty("form"))if(e.hasOwnProperty("url"))if(this.ops.form=document.querySelector(e.form),this.ops.url=e.url,this.ops.form){if(e.hasOwnProperty("formData")&&e.formData.forEach(e=>{t.ops.formData[e.name]=t.ops.form.querySelector('[name="'+e.name+'"]'),e.validate&&(t.ops.validators.items[e.name]=e.validate),e.validate_message&&Object.keys(e.validate_message).forEach(o=>{t.ops.validators.messages[e.name+"."+o]=e.validate_message[o]})}),e.hasOwnProperty("headers")){let t=this.ops.headers;this.ops.headers={...t,...e.headers}}e.hasOwnProperty("method")&&(this.ops.method=e.method),e.hasOwnProperty("notif")&&(this.ops.notif=e.notif),e.hasOwnProperty("beforeSubmit")&&(this.ops.beforeSubmit=e.beforeSubmit),e.hasOwnProperty("onSuccess")&&(this.ops.onSuccess=e.onSuccess),e.hasOwnProperty("onError")&&(this.ops.onError=e.onError),e.hasOwnProperty("notif_hide_time")&&(this.ops.notif_hide_time=e.notif_hide_time),this.events()}else console.log("ezform: form not found, init failed");else console.log("ezform: no form url provided, init failed");else console.log("ezform: no form provided, init failed");else console.log("ezform: no options provided, init failed")},this.init()}