Passed
Push — v1 ( 772a02...85e446 )
by Andrew
10:41 queued 03:46
created

RedactorPlugins.richvariables   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 30
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 19
nc 4
nop 0
dl 0
loc 30
rs 9.45
c 0
b 0
f 0
1
/**
2
 * Rich Variables plugin for Craft CMS
3
 *
4
 * Rich Variables JS
5
 *
6
 * @author    nystudio107
7
 * @copyright Copyright (c) 2017 nystudio107
8
 * @link      https://nystudio107.com
9
 * @package   RichVariables
10
 * @since     1.0.0
11
 */
12
13
if (!RedactorPlugins) var RedactorPlugins = {};
0 ignored issues
show
Bug introduced by
The variable RedactorPlugins seems to be never initialized.
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...
Coding Style introduced by
Inline control structures are discouraged
Loading history...
14
15
// Grab the globals set Reference Tags from our controller
16
var request = new XMLHttpRequest();
17
request.open('GET', Craft.getActionUrl('rich-variables'), false);
0 ignored issues
show
Bug introduced by
The variable Craft seems to be never declared. If this is a global, consider adding a /** global: Craft */ 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...
18
request.onload = function() {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
19
    if (request.status >= 200 && request.status < 400) {
0 ignored issues
show
Comprehensibility Documentation Best Practice introduced by
This code block is empty. Consider removing it or adding a comment to explain.
Loading history...
20
    } else {
0 ignored issues
show
Comprehensibility Documentation Best Practice introduced by
This code block is empty. Consider removing it or adding a comment to explain.
Loading history...
21
    }
22
};
23
request.send();
24
25
// Add our Redactor plugin
26
RedactorPlugins.richvariables = function() {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
27
    return {
28
        init: function() {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
29
            var dropdown = {};
30
            var responseVars = JSON.parse(request.responseText);
31
            var _this = this;
0 ignored issues
show
Unused Code introduced by
The variable _this seems to be never used. Consider removing it.
Loading history...
32
33
            // Iterate through each menu item, adding them to our dropdown
34
            responseVars.variablesList.forEach(function(menuItem, index) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
35
                var key = 'point' + (index + 1);
36
                dropdown[key] = {
37
                    title: menuItem.title,
38
                    func: function(buttonName) {
0 ignored issues
show
Unused Code introduced by
The parameter buttonName 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
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
39
                        this.insert.raw('<ins>' + menuItem.text + '</ins>');
40
                    },
41
                };
42
            });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
43
            // Handle empty menu items
44
            if (responseVars.variablesList.length === 0) {
45
                dropdown.point1 = {
46
                    title: "No Globals Found",
47
                    func: function(buttonName) {
0 ignored issues
show
Unused Code introduced by
The parameter buttonName 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
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
48
                        // NOP
49
                    },
50
                };
51
            }
52
            // Add the button and dropdown
53
            var button = this.button.add('variables', 'Variables');
54
            this.button.addDropdown(button, dropdown);
55
            if (responseVars.useIconForMenu)
0 ignored issues
show
Coding Style introduced by
Inline control structures are discouraged
Loading history...
56
                this.button.setIcon(button, '<img src="' + responseVars.menuIconUrl + '" height="16" width="16" style="margin-top: -2px;">');
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...
57
        },
58
    };
59
};