Passed
Push — master ( eff472...6bcaf7 )
by Emmanuel
01:30
created

dist/formJS.min.js   A

Complexity

Total Complexity 32
Complexity/F 2.29

Size

Lines of Code 1
Function Count 14

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
cc 0
eloc 1
c 3
b 0
f 2
nc 18432
dl 0
loc 1
rs 9.84
wmc 32
mnd 4
bc 20
fnc 14
bpm 1.4285
cpm 2.2857
noi 4
1
!function(t){t.fn.formJS=function(e){var r=t.extend({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"]'},redirection:{message:"Automatic redirection in a second",delay:1100},callback:function(t){}},e);return this.each(function(){var e,a,n,i=t(this),s=i.attr("action"),o=i.attr("method"),l=i.find(r.form.btnSubmit),c=t.extend(r.alerts.unexpected,{type:"error"}),d=!1;i.submit(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(i[0]):null,n=null!==a?a:i.serialize(),e=t.extend(r.form.ajaxSettings,{url:s,type:o,data:n,processData:!1}),i.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="";i.trigger("formjs:ajax-success",[e]),i.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){i.logError("AjaxSuccessCallback",t,e)}}).fail(function(t){i.logError("AjaxFailCallback",t)}).always(function(){i.writeAlert()})}catch(t){i.logError("PreSubmit",t),i.writeAlert()}}}),i.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()},i.logError=function(t,e,r){var a=e.message||e;console.error("[FormJS]["+t+"] "+a),i.trigger("formjs:error",[t,a,r])},i.writeAlert=function(){i.trigger("formjs:write-alert",[c]);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)},i.init=function(){if(0===i.find(r.form.alertContainer).length){var e=t("<div/>"),a=r.form.alertContainer.split("."),n=e,s="",o="";t.each(a,function(t,e){e.indexOf("#")>=0?s+=e.replace(/^#/,""):o+=e+" "}),s.length&&n.attr("id",s),o.length&&n.attr("class",o.trim()),i.prepend(n)}},i.init()}),this}}(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...
Coding Style introduced by
Consider using undefined instead of void(0). It is equivalent and more straightforward to read.
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...