Passed
Push — main ( fac3c3...502827 )
by Stefan
02:24
created

FormCKEdit.js ➔ loadEditor   F

Complexity

Conditions 18

Size

Total Lines 91
Code Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 61
dl 0
loc 91
rs 1.2
c 0
b 0
f 0
cc 18

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

Complexity

Complex classes like FormCKEdit.js ➔ loadEditor often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
function loadEditor()
2
{
3
    var oEditor = null;
0 ignored issues
show
Unused Code introduced by
The assignment to oEditor seems to be never used. If you intend to free memory here, this is not necessary since the variable leaves the scope anyway.
Loading history...
4
    
5
    var oTA = document.getElementById(g_oConfigFromPHP.CKEditor.editorID);
0 ignored issues
show
Bug introduced by
The variable g_oConfigFromPHP seems to be never declared. If this is a global, consider adding a /** global: g_oConfigFromPHP */ 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...
6
    if (!oTA) {
7
        displayJSError('Element [' + g_oConfigFromPHP.CKEditor.editorID + 'to be replaced by CKEditor not exists!', 'error');
8
        return;
9
    }
10
    // get initial size of textarea to replace
11
    var iHeight = oTA.offsetHeight;
12
    var iWidth = oTA.offsetWidth;
13
    oEditor = CKEDITOR.replace(g_oConfigFromPHP.CKEditor.editorID, g_oConfigFromPHP.CKEditor.editorOptions);
0 ignored issues
show
Bug introduced by
The variable CKEDITOR seems to be never declared. If this is a global, consider adding a /** global: CKEDITOR */ 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...
14
15
    // custom buttons
16
    if (Array.isArray(g_oConfigFromPHP.CKEditor.customButtons)) {
17
        let length = g_oConfigFromPHP.CKEditor.customButtons.length;
18
        for (let i = 0; i < length; i++) {
19
            let btn = g_oConfigFromPHP.CKEditor.customButtons[i];
20
            
21
            var exec = window[btn.func];
22
            if (typeof exec === "function") {
23
                oEditor.addCommand('cmd_' + btn.func, {exec: function(oEditor) {window[btn.func](oEditor);}});
24
                oEditor.ui.addButton(btn.func, {label: btn.name, command: 'cmd_' + btn.func, icon: btn.icon});
25
            } else {
26
                displayJSError('Handler for Custom Button [' + btn.func + '] is not defined!', 'error');
27
            }
28
        }
29
    }
30
31
    // resize to desired size
32
    CKEDITOR.on('instanceReady', function(event) {event.editor.resize(iWidth, iHeight);});
33
34
    if (g_oConfigFromPHP.CKEditor.editorData !== undefined) {
35
        oEditor.setData(g_oConfigFromPHP.CKEditor.editorData);
36
    }
37
38
    if (g_oConfigFromPHP.RichFilemanager === undefined) {
39
        return;
40
    }
41
    // check filemanager...
42
    CKEDITOR.on('dialogDefinition', function (event) {
43
        var editor = event.editor;
44
        var dialogDefinition = event.data.definition;
45
        var dialogName = event.data.name;
46
        var cleanUpFuncRef = CKEDITOR.tools.addFunction(function () {
0 ignored issues
show
Bug introduced by
The variable CKEDITOR seems to be never declared. If this is a global, consider adding a /** global: CKEDITOR */ 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...
47
            let oIFrame = document.getElementById('fm-iframeCKE');
48
            if (oIFrame) {
49
                oIFrame.remove();
50
            }
51
            document.body.style.overflowY = 'scroll';
52
        });
53
            
54
        var tabCount = dialogDefinition.contents.length;
55
        for (var i = 0; i < tabCount; i++) {
56
            var dialogTab = dialogDefinition.contents[i];
57
            if (!(dialogTab && typeof dialogTab.get === 'function')) {
58
                continue;
59
            }
60
                
61
            var browseButton = dialogTab.get('browse');
62
            if (browseButton !== null) {
63
                browseButton.hidden = false;
64
                var params = 
65
                    '?CKEditorFuncNum=' + CKEDITOR.instances[event.editor.name]._.filebrowserFn +
66
                    '&CKEditorCleanUpFuncNum=' + cleanUpFuncRef +
67
                    '&langCode=' + g_oConfigFromPHP.RichFilemanager.language +
0 ignored issues
show
Bug introduced by
The variable g_oConfigFromPHP seems to be never declared. If this is a global, consider adding a /** global: g_oConfigFromPHP */ 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...
68
                    '&CKEditor=' + event.editor.name;
69
                    
70
                if (dialogName == 'link') {
71
                    params += '&expandedFolder=' + g_oConfigFromPHP.RichFilemanager.expandFolder.browseLinkURL;
72
                } else if (dialogTab.id == 'info') {
73
                    params += '&expandedFolder=' + g_oConfigFromPHP.RichFilemanager.expandFolder.browseImageURL;
74
                } else {
75
                    params += '&expandedFolder=' + g_oConfigFromPHP.RichFilemanager.expandFolder.browseImageLinkURL;
76
                }
77
                browseButton.filebrowser.params = params;
78
                browseButton.onClick = function (dialog, i) {
0 ignored issues
show
Unused Code introduced by
The parameter i 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...
79
                    editor._.filebrowserSe = this;
80
                    
81
                    let oIFrame = document.createElement('iframe');
82
                    oIFrame.id = 'fm-iframeCKE';
83
                    oIFrame.className = 'fm-modal';
84
                    oIFrame.src = g_oConfigFromPHP.RichFilemanager.Path + dialog.sender.filebrowser.params;
0 ignored issues
show
Bug introduced by
The variable g_oConfigFromPHP seems to be never declared. If this is a global, consider adding a /** global: g_oConfigFromPHP */ 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
                    document.body.append(oIFrame);
86
                    document.body.style.overflowY = 'hidden';
87
                }
88
            }
89
        }
90
    });
91
}
92