dist/formJS.min.js   A
last analyzed

Complexity

Total Complexity 42
Complexity/F 2.21

Size

Lines of Code 1
Function Count 19

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 42
eloc 1
mnd 23
bc 23
fnc 19
dl 0
loc 1
rs 9.0399
bpm 1.2105
cpm 2.2105
noi 4
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like dist/formJS.min.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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>&#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(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);
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...
Unused Code introduced by
The parameter e 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...
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...