1
|
|
|
!function(e){e.fn.formJS=function(t){var r=e.extend(!0,{alerts:{unexpected:{title:"Error",message:"Unexpected error occurred"},failSend:{title:"Error",message:"Unable to send data"}},keys:{success:"success",info:"info",warning:"warning",error:"error"},icons:{loading:"<span>⌛</span>",success:"<span>✓</span>",info:"<span>🛈</span>",warning:"<span>﹗</span>",error:"<span>⨯</span>"},form:{ajaxSettings:{contentType:!1},alertContainer:".formJS",btnSubmit:'.btn[type="submit"]',enableWriteAlert:!0},redirection:{message:"Automatic redirection in a second",delay:1100},callback:function(e){}},t);return this.each(function(){var t,n,a,s,i,o,c,l,d,f,u=e(this);return u.sendData=function(i){if(i.preventDefault(),!1===o){o=!0;try{if(0===a.length)throw"Unable to find submit button";if(""==n||null===n)throw"Undefined method of form";a.append(e(r.icons.loading).addClass("formJS-loading")).attr("disabled"),a.addClass("disabled"),f=!1,l=window.FormData?new FormData(u[0]):null,d=null!==l?l:u.serialize(),c=e.extend(r.form.ajaxSettings,{url:t,type:n,data:d,processData:!1}),u.trigger("formjs:submit",[c,o]),e.ajax(c).done(function(t){try{if(0===t.length)throw"No data found on response";var n=t instanceof Object?t:e.parseJSON(t),a="";u.trigger("formjs:ajax-success",[t]),u.checkFeedbackStructure(n),n.type===r.keys.success&&n.hasOwnProperty("url")&&(a=" - "+r.redirection.message,setTimeout(function(){window.location.replace(n.url)},r.redirection.delay)),u.responseIsAFeedback()?(s.type=n.type,s.title=n.data.title,s.message=n.data.message+a):u.responseIsAView()&&u.updateFormView(n.view)}catch(e){u.logError("AjaxSuccessCallback",e,t)}}).fail(function(e){u.logError("AjaxFailCallback",e)}).always(function(){u.writeAlert()})}catch(e){u.logError("PreSubmit",e)}}},u.checkFeedbackStructure=function(e){if(!e.hasOwnProperty("type"))throw'Invalid feedback structure: "type" missing';if(!e.hasOwnProperty("data")&&!e.hasOwnProperty("view"))throw'Invalid feedback structure: "data" or "view" missing';if(e.hasOwnProperty("data")){if(i="responseFeedback",!e.data.hasOwnProperty("title"))throw'Invalid feedback structure: "data.title" missing';if(!e.data.hasOwnProperty("message"))throw'Invalid feedback structure: "data.message" missing';if(-1===Object.keys(r.keys).indexOf(e.type))throw'Invalid feedback structure: "type" wrong. Accepted values: '+Object.keys(r.keys).toString()}e.hasOwnProperty("view")&&(i="responseView")},u.logError=function(e,t,n){var a=t.message||t;s.type=r.keys.error,console.error("[FormJS]["+e+"] "+a),u.trigger("formjs:error",[e,a,n]),f=!0},u.writeAlert=function(){if(u.responseIsAFeedback()||f){if(u.trigger("formjs:write-alert",[s]),!0===r.form.enableWriteAlert){var t=e('<div class="alert formjs-'+r.keys[s.type]+'" role="alert" />').html('<div class="ico">\n\t\t\t\t\t\t\t\t'+r.icons[s.type]+'\n\t\t\t\t\t\t\t</div>\n\t\t\t\t\t\t\t<div class="info">\n\t\t\t\t\t\t\t\t<h4>'+s.title+"</h4>\n\t\t\t\t\t\t\t\t<p>"+s.message+"</p>\n\t\t\t\t\t\t\t</div>").hide().fadeIn(300);e(r.form.alertContainer).empty().html(t)}e("html, body").animate({scrollTop:e(r.form.alertContainer).offset().top-55},300);var n=e(r.form.btnSubmit);void 0!==n&&n.length&&(n.find(".formJS-loading").remove(),n.removeClass("disabled"),o=!1),"success"===s.type||"info"===s.type?console.log(s.title+" - "+s.message):"error"===s.type?console.error(s.title+" - "+s.message):"warning"===s.type?console.warn(s.title+" - "+s.message):console.log(s.title+" - "+s.message),r.callback(s)}},u.updateFormView=function(e){u.responseIsAView()&&(u.html(e),u.init())},u.responseIsAFeedback=function(){return"responseFeedback"===i},u.responseIsAView=function(){return"responseView"===i},u.init=function(){if(u.initVariables(),0===u.find(r.form.alertContainer).length){var t=e("<div/>"),n=r.form.alertContainer.split("."),a=t,s="",i="";e.each(n,function(e,t){t.indexOf("#")>=0?s+=t.replace(/^#/,""):i+=t+" "}),s.length&&a.attr("id",s),i.length&&a.attr("class",i.trim()),u.prepend(a)}},u.initVariables=function(){t=u.attr("action"),n=u.attr("method"),a=u.find(r.form.btnSubmit),s=e.extend(r.alerts.unexpected,{type:"error"}),o=!1,i=void 0,u.initEvent()},u.initEvent=function(){u.submit(u.sendData),u.on("formjs:send-form",u.sendData)},u.init(),u})}}(jQuery); |