Passed
Push — master ( 002ff1...1a2657 )
by Emmanuel
01:40
created

t.fn.formJS   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 1
rs 10
1
/*
2
 * Copyright (c) 2019.  JAGFx
3
 * @author: SMITH Emmanuel
4
 * @version: 2.1.0
5
 *
6
 */
7
8
!function(t){t.fn.formJS=function(e){var r=t.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>&#8987;</span>",success:"<span>&#10003;</span>",info:"<span>&#128712;</span>",warning:"<span>&#65111;</span>",error:"<span>&#10799;</span>"},form:{ajaxSettings:{contentType:!1},alertContainer:".formJS",btnSubmit:'.btn[type="submit"]',enableWriteAlert:!0},redirection:{message:"Automatic redirection in a second",delay:1100},callback:function(t){}},e);return this.each(function(){var e,a,n,s=t(this),i=s.attr("action"),o=s.attr("method"),l=s.find(r.form.btnSubmit),c=t.extend(r.alerts.unexpected,{type:"error"}),d=!1;return s.sendData=function(f){if(f.preventDefault(),!1===d){d=!0;try{if(0===l.length)throw"Unable to find submit button";if(""==o||null===o)throw"Undefined method of form";l.append(t(r.icons.loading).addClass("formJS-loading")).attr("disabled"),l.addClass("disabled"),a=window.FormData?new FormData(s[0]):null,n=null!==a?a:s.serialize(),e=t.extend(r.form.ajaxSettings,{url:i,type:o,data:n,processData:!1}),s.trigger("formjs:submit",[e,d]),t.ajax(e).done(function(e){try{if(0===e.length)throw"No data found on response";var a=t.parseJSON(e),n="";s.trigger("formjs:ajax-success",[e]),s.checkFeedbackStructure(a),a.type===r.keys.success&&a.hasOwnProperty("url")&&(n=" - "+r.redirection.message,setTimeout(function(){window.location.replace(a.url)},r.redirection.delay)),c.type=a.type,c.title=a.data.title,c.message=a.data.message+n}catch(t){s.logError("AjaxSuccessCallback",t,e)}}).fail(function(t){s.logError("AjaxFailCallback",t)}).always(function(){s.writeAlert()})}catch(t){s.logError("PreSubmit",t),s.writeAlert()}}},s.submit(s.sendData),s.on("formjs:send-form",s.sendData),s.checkFeedbackStructure=function(t){if(!t.hasOwnProperty("type"))throw'Invalid feedback structure: "type" missing';if(!t.hasOwnProperty("data"))throw'Invalid feedback structure: "data" missing';if(!t.data.hasOwnProperty("title"))throw'Invalid feedback structure: "data.title" missing';if(!t.data.hasOwnProperty("message"))throw'Invalid feedback structure: "data.message" missing';if(-1===Object.keys(r.keys).indexOf(t.type))throw'Invalid feedback structure: "type" wrong. Accepted values: '+Object.keys(r.keys).toString()},s.logError=function(t,e,r){var a=e.message||e;console.error("[FormJS]["+t+"] "+a),s.trigger("formjs:error",[t,a,r])},s.writeAlert=function(){if(s.trigger("formjs:write-alert",[c]),!0===r.form.enableWriteAlert){var e=t('<div class="alert formjs-'+r.keys[c.type]+'" role="alert" />').html('<div class="ico">\n\t\t\t\t\t\t\t\t'+r.icons[c.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>'+c.title+"</h4>\n\t\t\t\t\t\t\t\t<p>"+c.message+"</p>\n\t\t\t\t\t\t\t</div>").hide().fadeIn(300);t(r.form.alertContainer).empty().html(e)}t("html, body").animate({scrollTop:t(r.form.alertContainer).offset().top-55},300);var a=t(r.form.btnSubmit);void 0!==a&&a.length&&(a.find(".formJS-loading").remove(),a.removeClass("disabled"),d=!1),"success"===c.type||"info"===c.type?console.log(c.title+" - "+c.message):"error"===c.type?console.error(c.title+" - "+c.message):"warning"===c.type?console.warn(c.title+" - "+c.message):console.log(c.title+" - "+c.message),r.callback(c)},s.init=function(){if(0===s.find(r.form.alertContainer).length){var e=t("<div/>"),a=r.form.alertContainer.split("."),n=e,i="",o="";t.each(a,function(t,e){e.indexOf("#")>=0?i+=e.replace(/^#/,""):o+=e+" "}),i.length&&n.attr("id",i),o.length&&n.attr("class",o.trim()),s.prepend(n)}},s.init(),s})}}(jQuery);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
Comprehensibility introduced by
Usage of the sequence operator is discouraged, since it may lead to obfuscated code.

The sequence or comma operator allows the inclusion of multiple expressions where only is permitted. The result of the sequence is the value of the last expression.

This operator is most often used in for statements.

Used in another places it can make code hard to read, especially when people do not realize it even exists as a seperate operator.

This check looks for usage of the sequence operator in locations where it is not necessary and could be replaced by a series of expressions or statements.

var a,b,c;

a = 1, b = 1,  c= 3;

could just as well be written as:

var a,b,c;

a = 1;
b = 1;
c = 3;

To learn more about the sequence operator, please refer to the MDN.

Loading history...
Unused Code introduced by
The parameter t is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Coding Style introduced by
Consider using undefined instead of void(0). It is equivalent and more straightforward to read.
Loading history...