Passed
Branch develop (893f38)
by Hans Erik
06:54
created

permission.js ➔ ... ➔ jQuery.done   C

Complexity

Conditions 7
Paths 9

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 26
rs 6.7272
cc 7
nc 9
nop 1
1
/**
2
 * Function to send Permissions via Ajax to Com-Config Application Controller
3
 */
4
function sendPermissions(event) {
5
    // set the icon while storing the values
6
    var icon = document.getElementById('icon_' + this.id);
7
    icon.removeAttribute('class');
8
    icon.setAttribute('style', 'background: url(../media/system/images/modal/spinner.gif); display: inline-block; width: 16px; height: 16px');
9
    var iconSuccess = "text-success icon-save fa-save";
10
    var iconFail    = "text-error text-danger icon-remove fa-remove";
11
12
    //get values and prepare GET-Parameter
13
    var asset     = 'not';
14
    var component = getUrlParam('component');
15
    var option    = getUrlParam('option');
16
    var view      = getUrlParam('view');
17
    var title     = component;
18
    var value     = this.value;
19
20
    if (option == 'com_redcore' && view == 'config') {
21
        if (component == false)
22
            asset = 'root.1';
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...
23
        else
24
            asset = component;
25
    }
26
    else {
27
28
        if (document.getElementById('jform_title')) {
29
            title = document.getElementById('jform_title').value;
30
        }
31
32
        if (component != false && view != false) {
33
            asset = component + '.' + view + '.' + getUrlParam('id');
34
        }
35
        else if (component == false && view != false) {
36
            asset = option + '.' + view + '.' + getUrlParam('id');
37
        }
38
    }
39
40
    var id = this.id.replace('jform_rules_', '');
41
    var lastUnderscoreIndex = id.lastIndexOf('_');
42
43
    var permission_data = {
44
        comp   : asset,
45
        action : id.substring(0, lastUnderscoreIndex),
46
        rule   : id.substring(lastUnderscoreIndex + 1),
47
        value  : value,
48
        title  : title
49
    };
50
51
    // Remove js messages, if they exist.
52
    Joomla.removeMessages();
0 ignored issues
show
Bug introduced by
The variable Joomla seems to be never declared. If this is a global, consider adding a /** global: Joomla */ 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...
53
54
    // doing ajax request
55
    jQuery.ajax({
56
        method   : "POST",
57
        url      : document.getElementById('permissions-sliders').getAttribute('data-ajaxuri'),
58
        data     : permission_data,
59
        datatype : 'json'
60
    })
61
    .fail(function (jqXHR, textStatus, error) {
62
        // Remove the spinning icon.
63
        icon.removeAttribute('style');
64
65
        Joomla.renderMessages(Joomla.ajaxErrorsMessages(jqXHR, textStatus, error));
0 ignored issues
show
Bug introduced by
The variable Joomla seems to be never declared. If this is a global, consider adding a /** global: Joomla */ 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...
66
67
        icon.setAttribute('class', iconFail);
68
    })
69
    .done(function (response) {
70
        // Remove the spinning icon.
71
        icon.removeAttribute('style');
72
73
        if (response.data) {
74
            // Check if everything is OK
75
            if (response.data.result == true) {
76
                icon.setAttribute('class', iconSuccess);
77
78
                jQuery(event.target).parents().next('td').find('span')
79
                    .removeClass().addClass(response.data.class)
80
                    .html(response.data.text);
81
            }
82
        }
83
84
        // Render messages, if any. There are only message in case of errors.
85
        if (typeof response.messages == 'object' && response.messages !== null)
86
        {
87
            Joomla.renderMessages(response.messages);
0 ignored issues
show
Bug introduced by
The variable Joomla seems to be never declared. If this is a global, consider adding a /** global: Joomla */ 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...
88
89
            if (response.data && response.data.result == true)
90
                icon.setAttribute('class', iconSuccess);
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...
91
            else
92
                icon.setAttribute('class', iconFail);
93
        }
94
    });
95
}
96
97
/**
98
 * Function to get parameters out of the url
99
 */
100
function getUrlParam(variable) {
101
    var query = window.location.search.substring(1);
102
    var vars = query.split('&');
103
104
    for (var i=0;i<vars.length;i++) {
105
        var pair = vars[i].split('=');
106
107
        if (pair[0] == variable)
108
            return pair[1];
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...
109
    }
110
111
    return false;
112
}
113