assets/js/dist/amarkal-settings.min.js   D
last analyzed

Complexity

Total Complexity 67
Complexity/F 1.18

Size

Lines of Code 1
Function Count 57

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 98304
dl 0
loc 1
rs 4.3333
c 0
b 0
f 0
wmc 67
mnd 2
bc 60
fnc 57
bpm 1.0526
cpm 1.1754
noi 11

How to fix   Complexity   

Complexity

Complex classes like assets/js/dist/amarkal-settings.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(t){var i=window.jQuery;t.settings={init:function(){Amarkal.settings.fields.init(),Amarkal.settings.search.init(),Amarkal.settings.sections.init(),i("#amarkal-settings-form").amarkalUIForm()}},i(document).ready(function(){Amarkal.settings.init()}),Amarkal.settings.notifier={classes:["error","success","notice","info"],prefix:"amarkal-",timeout:null,$el:null,notify:function(t,i,a){clearTimeout(Amarkal.settings.notifier.timeout),Amarkal.settings.notifier.$el.removeAttr("class").addClass(Amarkal.settings.notifier.prefix+t).html("<p>"+i+"</p>"),void 0!==a&&(Amarkal.settings.notifier.timeout=setTimeout(function(){Amarkal.settings.notifier.clearNotifications()},a))},error:function(t,i){Amarkal.settings.notifier.notify("error",t,i)},success:function(t,i){Amarkal.settings.notifier.notify("success",t,i)},notice:function(t,i){Amarkal.settings.notifier.notify("notice",t,i)},info:function(t,i){Amarkal.settings.notifier.notify("info",t,i)},clearNotifications:function(){clearTimeout(this.timeout),this.$el.removeAttr("class").html("")},init:function(){this.$el=i("#amarkal-settings-notices")}},i(document).ready(function(){Amarkal.settings.notifier.init()}),Amarkal.settings.search={$input:null,activeSection:null,init:function(){this.$input=i("#settings-search"),this.$input.on("keyup",this.onKeyup.bind(this))},onKeyup:function(t){var a=this.$input.val().toLowerCase().replace(/[^\w\d\s]/g,"");if(a.length>0){var e=Amarkal.settings.fields.search(a);null===this.activeSection&&(this.activeSection=Amarkal.settings.sections.activeSection),Amarkal.settings.sections.deactivate(),Amarkal.settings.header.setSectionTitle("Field Search Results"),Amarkal.settings.header.setSectionSubtitle(""),Amarkal.settings.fields.hideAll(),Amarkal.settings.fields.show(e),i("#settings-search-results").addClass("visible").text(e.length?e.length+" settings found":"Nothing found")}else i("#settings-search-results").removeClass("visible").text(""),Amarkal.settings.sections.activate(this.activeSection),this.activeSection=null}},Amarkal.settings.sections={$links:null,$loader:null,data:null,activeSection:null,init:function(){this.data=JSON.parse(i("#sections-config").text()),this.$links=i(".amarkal-settings-sections li"),this.$loader=i("#amarkal-settings-loader"),this.$form=i("#amarkal-settings-form"),this.$fields=this.$form.find(".amarkal-settings-field"),this.initSections()},initSections:function(){if("object"==typeof this.data&&Object.keys(this.data).length>0){var t=this;this.$links.on("click",function(){t.activate(i(this).attr("data-slug"))}),this.activateInitialSection(),this.$loader.hide()}else Amarkal.settings.fields.showAll()},activateInitialSection:function(){""!==window.location.hash?this.activate(window.location.hash.substring(1)):this.activate(i(this.$links[0]).attr("data-slug"))},activate:function(t){this.activeSection!==t&&(this.activeSection=t,this.$links.removeClass("active").filter('[data-slug="'+t+'"]').addClass("active"),Amarkal.settings.header.setSectionTitle(this.getTitle(t)),Amarkal.settings.header.setSectionSubtitle(this.getSubtitle(t)),Amarkal.settings.fields.hideAll(),Amarkal.settings.fields.show(this.getSectionFields(t)),window.location="#"+t)},deactivate:function(){this.activeSection;this.$links.removeClass("active"),Amarkal.settings.fields.hideAll(),this.activeSection=null},flag:function(t,i){this.$links.filter('[data-slug="'+i+'"]').addClass("flag-"+t)},unflag:function(t,i){this.$links.filter('[data-slug="'+i+'"]').removeClass("flag-"+t)},unflagAll:function(){this.$links.removeClass("flag-error flag-notice")},getTitle:function(t){return this.data[t].title},getSubtitle:function(t){return this.data[t].subtitle},getSectionFields:function(t){return this.$fields.filter('[data-section="'+t+'"]')},changed:function(t){var a=!1;return this.getSectionFields(t).each(function(){if(i(this).find(".amarkal-ui-component").amarkalUIComponent("changed"))return a=!0,!1}),a}},Amarkal.settings.fields={$form:null,$fields:null,init:function(){this.$form=i("#amarkal-settings-form"),this.$fields=this.$form.find(".amarkal-settings-field");var t=this;i(".amarkal-ui-component").on("amarkal.change",function(a,e){i(this).amarkalUIComponent("changed")?(Amarkal.settings.fields.flag("notice",e.props.name),Amarkal.settings.sections.flag("notice",e.props.section),Amarkal.settings.notifier.notice('Settings have changed, click "Save" to apply them.')):Amarkal.settings.fields.unflag("notice",e.props.name),Amarkal.settings.sections.changed(e.props.section)||Amarkal.settings.sections.unflag("notice",e.props.section),t.$form.amarkalUIForm("changed")||Amarkal.settings.notifier.clearNotifications()}).on("amarkal.hide",function(){Amarkal.settings.fields.hide(i(this).parents(".amarkal-settings-field"))}).on("amarkal.show",function(){i(this).parents(".amarkal-settings-field").attr("data-section")===Amarkal.settings.sections.activeSection&&Amarkal.settings.fields.show(i(this).parents(".amarkal-settings-field"))})},search:function(t){var a=[],e=this.$form;return this.$fields.each(function(){var s=i(this),n=s.attr("data-name"),l=s.find("h3").text().toLowerCase(),r=s.find(".help-content").text();description=s.find(".description").text(),(""===n||e.amarkalUIForm("isVisible",n))&&(l.match(t)||description.match(t)||r.match(t))&&a.push(s)}),i(a).map(function(){return this.toArray()})},show:function(t){var a=this;t.each(function(){var t=i(this).find(".amarkal-ui-component"),e=t.amarkalUIComponent("getName");e&&!a.$form.amarkalUIForm("isVisible",e)||(i(this).addClass("visible"),t.amarkalUIComponent("refresh"))})},hide:function(t){t.removeClass("visible")},hideAll:function(){this.hide(this.$fields)},showAll:function(){this.show(this.$fields)},flag:function(t,i){this.$fields.filter('[data-name="'+i+'"]').addClass("flag-"+t)},unflag:function(t,i){this.$fields.filter('[data-name="'+i+'"]').removeClass("flag-"+t)},unflagAll:function(){this.$fields.removeClass("flag-error flag-notice")}},Amarkal.settings.header={setSectionTitle:function(t){i("#amarkal-settings-sec-title").text(t)},setSectionSubtitle:function(t){i("#amarkal-settings-sec-subtitle").text(t)}},i(document).ready(function(){i(".amarkal-settings-button").on("click",function(t){t.preventDefault();var a=i(this);a.data("disabled")||(i(".amarkal-settings-button").data("disabled",!0),a.addClass("processing"),Amarkal.settings[a.val()](function(){a.removeClass("processing"),i(".amarkal-settings-button").data("disabled",!1)}))})}),Amarkal.settings.save=function(t){Amarkal.settings._postData("save",function(a){if(i("#amarkal-settings-form").amarkalUIForm("setData",a.values,a.errors),Amarkal.settings._clearNotices(),i.isEmptyObject(a.errors))Amarkal.settings.notifier.success("Settings saved",3e3);else for(var e in a.errors){var s=i('[amarkal-component-name="'+e+'"]'),n=s.amarkalUIComponent("getProps");s.amarkalUIComponent("makeInvalid"),s.parent().children(".amarkal-settings-error").addClass("amarkal-visible").html(a.errors[e]),Amarkal.settings.notifier.error("Some errors have occured, see below for more information."),Amarkal.settings.sections.flag("error",n.section),Amarkal.settings.fields.flag("error",n.name)}t()})},Amarkal.settings.resetAll=function(t){Amarkal.settings._postData("reset_all",function(a){i("#amarkal-settings-form").amarkalUIForm("setData",a.values,a.errors),Amarkal.settings._clearNotices(),Amarkal.settings.notifier.success("Default settings applied",3e3),t()})},Amarkal.settings.resetSection=function(t){Amarkal.settings._postData("reset_section",function(a){i("#amarkal-settings-form").amarkalUIForm("setData",a.values,a.errors),Amarkal.settings._clearNotices(),Amarkal.settings.notifier.success("Default settings applied for the section <strong>"+Amarkal.settings.sections.getTitle(Amarkal.settings.sections.activeSection)+"</strong>",3e3),t()})},Amarkal.settings._clearNotices=function(){i(".amarkal-ui-component").amarkalUIComponent("reset"),i(".amarkal-settings-error").removeClass("amarkal-visible").html(""),Amarkal.settings.sections.unflagAll(),Amarkal.settings.fields.unflagAll()},Amarkal.settings._postData=function(t,a){var e=i("#amarkal-settings-form").amarkalUIForm("getData");i("#amarkal-settings-form").find('input[name^="_amarkal"]').each(function(){e[i(this).attr("name")]=i(this).val()}),e._amarkal_settings_section=Amarkal.settings.sections.activeSection,i.post(ajaxurl,{action:"amarkal_settings_"+t,data:e},a)}}("undefined"==typeof Amarkal?Amarkal={}:Amarkal);
0 ignored issues
show
Coding Style introduced by
Consider using undefined instead of void(0). It is equivalent and more straightforward to read.
Loading history...
introduced by
The result of the property access to this.activeSection is not used.
Loading history...
Complexity Best Practice introduced by
There is no return statement if i(this).find(".amarkal-u...lUIComponent("changed") is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

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...
Bug introduced by
The variable Amarkal seems to be never declared. If this is a global, consider adding a /** global: Amarkal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
Bug introduced by
The variable description seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.description.
Loading history...
Bug introduced by
The variable Amarkal seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.Amarkal.
Loading history...
Bug introduced by
The variable ajaxurl seems to be never declared. If this is a global, consider adding a /** global: ajaxurl */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

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...
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.

Consider:

if (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
2
//# sourceMappingURL=amarkal-settings.min.js.map