Completed
Push — master ( bf061c...dc838b )
by Askupa
34:40
created

Amarkal.settings._clearNotices   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 9.4285
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.settings._clearNotices();
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...
13
        
14
        if(!$.isEmptyObject(res.errors)) {
15
            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...
16
                var $comp = $('[amarkal-component-name="'+name+'"]'),
17
                    instance = $comp.amarkalUIComponent('instance');
18
                
19
                $comp.amarkalUIComponent('makeInvalid');
20
                $comp.parent()
21
                     .children('.amarkal-settings-error')
22
                     .addClass('amarkal-visible')
23
                     .html(res.errors[name]);
24
            }
25
            Amarkal.settings.notifier.error('Some errors have occured, see below for more information.');
26
            Amarkal.settings.sections.flag('error', instance.props.section);
0 ignored issues
show
Bug introduced by
The variable instance seems to not be initialized for all possible execution paths.
Loading history...
27
        }
28
        else {
29
            Amarkal.settings.notifier.success('Settings saved', 3000);
30
        }
31
32
        $('#amarkal-settings-form').amarkalUIForm('setData', res.values, res.errors);
33
        
34
        done();
35
    });
36
};
37
38
/**
39
 * Asynchronously reset all settings in the current settings page to their 
40
 * default values and erase all data from the database. Shows a success 
41
 * notification upon completion.
42
 * 
43
 * @param {Function} done
44
 */
45
Amarkal.settings.resetAll = function( done ) 
46
{
47
    Amarkal.settings._postData('reset_all',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...
48
        
49
        Amarkal.settings._clearNotices();
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...
50
        Amarkal.settings.notifier.success('Default settings applied', 3000);
51
        $('#amarkal-settings-form').amarkalUIForm('setData', res.values, res.errors);
52
        
53
        done();
54
    });
55
};
56
57
/**
58
 * Asynchronously reset all settings in the current settings section to their 
59
 * default values and erase all section data from the database. Shows a success 
60
 * notification upon completion.
61
 * 
62
 * @param {Function} done
63
 */
64
Amarkal.settings.resetSection = function( done ) 
65
{
66
    Amarkal.settings._postData('reset_section',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...
67
        
68
        Amarkal.settings._clearNotices();
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...
69
        Amarkal.settings.notifier.success('Default settings applied for the section <strong>'+Amarkal.settings.sections.getTitle(Amarkal.settings.sections.activeSection)+'</strong>', 3000);
70
        $('#amarkal-settings-form').amarkalUIForm('setData', res.values, res.errors);
71
        
72
        done();
73
    });
74
};
75
76
/**
77
 * Reset all components and clear errors
78
 */
79
Amarkal.settings._clearNotices = function()
80
{
81
    // Reset all components
82
    $('.amarkal-ui-component').amarkalUIComponent('reset');
83
    $('.amarkal-settings-error').removeClass('amarkal-visible').html('');
84
    Amarkal.settings.sections.unflagAll();
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...
85
};
86
87
/**
88
 * Send serialized form data to be processed in the backend by the function given
89
 * in the 'action' variable.
90
 * 
91
 * @param {string} action
92
 * @param {Function} done
93
 */
94
Amarkal.settings._postData = function( action, done )
95
{
96
    var data = $('#amarkal-settings-form').amarkalUIForm('getData');
97
    $('#amarkal-settings-form').find('input[name^="_amarkal"]').each(function(){
98
        data[$(this).attr('name')] = $(this).val();
99
    });
100
101
    // Set the active section (if applicable)
102
    data['_amarkal_settings_section'] = Amarkal.settings.sections.activeSection;
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...
103
104
    $.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...
105
        action: 'amarkal_settings_'+action,
106
        data: data
107
    }, done);
108
};