Passed
Push — develop ( 73e773...170929 )
by Andrew
02:58
created

$R.add(ꞌpluginꞌ).init   A

Complexity

Conditions 1
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
nc 2
nop 1
dl 0
loc 19
rs 9.4285
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.18
11
 */
12
13
(function($R)
14
{
15
    $R.add('plugin', 'richvariables', {
16
        translations: {
17
            en: {
18
                "variables": "Variables"
19
            }
20
        },
21
        init: function(app)
22
        {
23
            this.app = app;
24
            this.lang = app.lang;
25
            this.inline = app.inline;
26
            this.toolbar = app.toolbar;
27
            this.insertion = app.insertion;
28
29
            // Grab the globals set Reference Tags from our controller
30
            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...
31
            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...
32
            request.onload = function() {
33
                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...
34
                } 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...
35
                }
36
            };
37
            request.send();
38
            this.request = request;
39
        },
40
        start: function()
41
        {
42
            var dropdown = {};
43
            var responseVars = JSON.parse(this.request.responseText);
44
45
            // Iterate through each menu item, adding them to our dropdown
46
            responseVars.variablesList.forEach(function(menuItem, index) {
47
                var key = 'point' + (index + 1);
48
                var refTag = '<ins>' + menuItem.text + '</ins>';
49
                dropdown[key] = {
50
                    title: menuItem.title,
51
                    api: 'plugin.richvariables.insert',
52
                    args: refTag
53
                };
54
            });
55
            // Handle empty menu items
56
            if (responseVars.variablesList.length === 0) {
57
                dropdown.point1 = {
58
                    title: "No Globals Found",
59
                    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...
60
                        // NOP
61
                    },
62
                };
63
            }
64
            // Add the button and dropdown
65
            var $button = this.toolbar.addButton('variables', { title: this.lang.get('variables') });
66
            $button.setDropdown(dropdown);
67
            if (responseVars.useIconForMenu) {
68
                $button.setIcon('<img src="' + responseVars.menuIconUrl + '" height="16" width="16" style="margin-top: -2px;">');
69
            }
70
        },
71
        insert: function(refTag)
72
        {
73
            this.insertion.insertRaw(refTag);
74
        }
75
    });
76
})(Redactor);
0 ignored issues
show
Bug introduced by
The variable Redactor seems to be never declared. If this is a global, consider adding a /** global: Redactor */ 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...
77