Completed
Push — master ( 939158...7af867 )
by Askupa
02:04
created

Amarkal.settings._updateValues   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 8.8571
c 0
b 0
f 0
cc 5
nc 3
nop 2
1
/**
2
 * Asynchronously save all settings in the current settings page to the database.
3
 * Shows an error notification if errors occur. Shows a success notification
4
 * otherwise.
5
 * 
6
 * @param {Function} done
7
 */
8
Amarkal.settings.save = function( done ) 
0 ignored issues
show
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...
9
{
10
    Amarkal.settings._postData('save',function(res){
0 ignored issues
show
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...
11
        
12
        $('.amarkal-ui-component').amarkalUIComponent('reset');
13
        console.log(res.errors);
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...
14
        if(!$.isEmptyObject(res.errors)) {
15
            var error = '', i = 0;
16
            for(var name in res.errors) {
0 ignored issues
show
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...
17
                if(i > 0) error += '<br>';
0 ignored issues
show
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...
18
                error += res.errors[name];
19
                $('[amarkal-component-name="'+name+'"]').amarkalUIComponent('makeInvalid');
20
                i++;
21
            }
22
            Amarkal.settings.notifier.error(error);
0 ignored issues
show
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...
23
        }
24
        else {
25
            Amarkal.settings.notifier.success('Settings saved', 2000);
26
        }
27
28
        Amarkal.settings._updateValues(res.values, res.errors);
29
        
30
        done();
31
    });
32
};
33
34
/**
35
 * Asynchronously reset all settings in the current settings page to their 
36
 * default values and erase all data from the database. Shows a success 
37
 * notification upon completion.
38
 * 
39
 * @param {Function} done
40
 */
41
Amarkal.settings.reset = function( done ) 
42
{
43
    Amarkal.settings._postData('reset',function(res){
0 ignored issues
show
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...
44
        
45
        $('.amarkal-ui-component').amarkalUIComponent('reset');
46
        
47
        Amarkal.settings.notifier.success('Default settings applied', 2000);
0 ignored issues
show
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...
48
        Amarkal.settings._updateValues(res.values, res.errors);
49
        
50
        done();
51
    });
52
};
53
54
/**
55
 * Update all components in the current settings page with the given values.
56
 * 
57
 * @param {Object} values
58
 * @param {Array} errors
59
 */
60
Amarkal.settings._updateValues = function( values, errors )
61
{
62
    for(var name in values) {
0 ignored issues
show
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...
63
        var value = values[name],
64
            $comp = $('[amarkal-component-name="'+name+'"]');
65
        
66
        if( typeof errors !== 'undefined' && 
67
            !errors.hasOwnProperty(name) && 
68
            $comp.hasClass('amarkal-ui-component')) {
69
        
70
            $comp.amarkalUIComponent('setValue', value);
71
        }
72
    }
73
};
74
75
/**
76
 * Send serialized form data to be processed in the backend by the function given
77
 * in the 'action' variable.
78
 * 
79
 * @param {string} action
80
 * @param {Function} done
81
 */
82
Amarkal.settings._postData = function( action, done )
0 ignored issues
show
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...
83
{
84
    $.post(ajaxurl, {
0 ignored issues
show
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...
85
        action: 'amarkal_settings_'+action,
86
        data: $('#amarkal-settings-form').serialize()
87
    }, done);
88
};