Passed
Push — develop ( fb0a8b...d2cc07 )
by Andrew
05:50
created

RedactorPlugins.richvariables   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 3
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
nc 1
nop 1
dl 3
loc 3
rs 10
c 1
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 View Code Duplication
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...
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
14
15
// Grab the globals set Reference Tags from our controller
16
var request = new XMLHttpRequest();
0 ignored issues
show
Bug introduced by
The variable XMLHttpRequest seems to be never declared. If this is a global, consider adding a /** global: XMLHttpRequest */ 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...
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() {
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() {
27
    return {
28
        init: function() {
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) {
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...
39
                        this.insert.raw('<ins>' + menuItem.text + '</ins>');
40
                    },
41
                };
42
            });
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...
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)
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
};