Completed
Push — master ( 02773e...88af0d )
by Dongxin
23s
created

cument.addEventListener(ꞌclickꞌ)   C

Complexity

Conditions 11
Paths 12

Size

Total Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
c 1
b 0
f 0
nc 12
nop 1
dl 0
loc 62
rs 6.1722

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 config.js ➔ ... ➔ document.addEventListener(ꞌclickꞌ) 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
// Copyright © 2017 Tangdongxin
2
3
// Permission is hereby granted, free of charge, to any person obtaining
4
// a copy of this software and associated documentation files (the "Software"),
5
// to deal in the Software without restriction, including without limitation
6
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
7
// and/or sell copies of the Software, and to permit persons to whom the
8
// Software is furnished to do so, subject to the following conditions:
9
10
// The above copyright notice and this permission notice shall be included
11
// in all copies or substantial portions of the Software.
12
13
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
15
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
17
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
18
// TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
19
// OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
21
22
// ===========================================
23
// READ CONFIG
24
// ===========================================
25
function onConfig(result) {
26
    if (!result) {
27
        console.log("No Config find");
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
28
        fontStyle    = "Consolas";
0 ignored issues
show
Bug introduced by
The variable fontStyle seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.fontStyle.
Loading history...
29
        fontSize     = "14px";
0 ignored issues
show
Bug introduced by
The variable fontSize seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.fontSize.
Loading history...
30
        bgColor      = "#FDF6E3";
0 ignored issues
show
Bug introduced by
The variable bgColor seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.bgColor.
Loading history...
31
        intColor     = "#657A81";
0 ignored issues
show
Bug introduced by
The variable intColor seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.intColor.
Loading history...
32
        strColor     = "#2AA198";
0 ignored issues
show
Bug introduced by
The variable strColor seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.strColor.
Loading history...
33
        keyColor     = "#B58900";
0 ignored issues
show
Bug introduced by
The variable keyColor seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.keyColor.
Loading history...
34
        defaultColor = "#586E75";
0 ignored issues
show
Bug introduced by
The variable defaultColor seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.defaultColor.
Loading history...
35
36
        strictOnly   = false;
0 ignored issues
show
Bug introduced by
The variable strictOnly seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.strictOnly.
Loading history...
37
        hideDetails  = false;
0 ignored issues
show
Bug introduced by
The variable hideDetails seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.hideDetails.
Loading history...
38
        dontBeatify  = false;
0 ignored issues
show
Bug introduced by
The variable dontBeatify seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.dontBeatify.
Loading history...
39
        isHighlight  = false;
0 ignored issues
show
Bug introduced by
The variable isHighlight seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.isHighlight.
Loading history...
40
41
        strLength    = 300;
0 ignored issues
show
Bug introduced by
The variable strLength seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.strLength.
Loading history...
42
        return;
43
    }
44
    if (result && result[0]) {
45
        fontStyle    = result[0].fontStyle    || "Consolas";
46
        fontSize     = result[0].fontSize     || "14px";
47
        bgColor      = result[0].bgColor      || "#FDF6E3";
48
        intColor     = result[0].intColor     || "#657A81";
49
        strColor     = result[0].strColor     || "#2AA198";
50
        keyColor     = result[0].keyColor     || "#B58900";
51
        defaultColor = result[0].defaultColor || "#586E75";
52
53
        strictOnly   = result[0].strictOnly   || false;
54
        hideDetails  = result[0].hideDetails  || false;
55
        dontBeatify  = result[0].dontBeatify  || false;
56
        isHighlight  = result[0].isHighlight  || false;
57
        strLength    = result[0].strLength    || 300;
58
    } else {
59
        fontStyle    = result.fontStyle    || "Consolas";
60
        fontSize     = result.fontSize     || "14px";
61
        bgColor      = result.bgColor      || "#FDF6E3";
62
        intColor     = result.intColor     || "#657A81";
63
        strColor     = result.strColor     || "#2AA198";
64
        keyColor     = result.keyColor     || "#B58900";
65
        defaultColor = result.defaultColor || "#586E75";
66
67
        strictOnly   = result.strictOnly   || false;
68
        hideDetails  = result.hideDetails  || false;
69
        dontBeatify  = result.dontBeatify  || false;
70
        isHighlight  = result.isHighlight  || false;
71
        strLength    = result.strLength    || 300;
72
    }
73
74
    document.addEventListener("click", function(e) {
75
        console.log(e);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
76
        var target = e.target;
77
        if (target.tagName.toUpperCase() == "I") {
78
            var isClose = target.classList.contains(COLL);
0 ignored issues
show
Bug introduced by
The variable COLL seems to be never declared. If this is a global, consider adding a /** global: COLL */ 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...
79
            console.log(isClose);
80
            var classname = target.classList[0];
81
            console.log(classname);
82
            if (isClose) {
83
                target.removeAttribute("class");
84
                target.setAttribute("class", classname);
85
            } else {
86
                target.removeAttribute("class");
87
                target.setAttribute("class", classname + " C" + classname.substring(1));
88
            }
89
            e.preventDefault();
90
        } else if (target.tagName.toUpperCase() == "STR") {
91
            var parentElement = target.parentElement;
92
            var originStr = parentElement.innerHTML;
93
            parentElement.innerHTML = parentElement.getAttribute("content");
94
            parentElement.setAttribute("content", originStr);
95
        } else if (target.tagName.toUpperCase() == "JSON") {
96
            if (document.getElementById("light")) {
97
                var lightDiv = document.getElementById("light");
98
                lightDiv.parentElement.removeChild(lightDiv);
99
            }
100
            if (document.getElementById("fade")) {
101
                var fadeDiv = document.getElementById("fade");
102
                fadeDiv.parentElement.removeChild(fadeDiv);
103
            }
104
105
            var parentElement = target.parentElement;
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable parentElement already seems to be declared on line 91. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
106
107
            var fadeDiv = document.createElement("div");
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable fadeDiv already seems to be declared on line 101. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
108
            fadeDiv.setAttribute("id", "fade");
109
            fadeDiv.classList.add("black_overlay");
110
            document.body.appendChild(fadeDiv);
111
112
            var lightDiv = document.createElement("div");
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable lightDiv already seems to be declared on line 97. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
113
            lightDiv.setAttribute("id", "light");
114
            lightDiv.classList.add("white_content");
115
            document.body.appendChild(lightDiv);
116
117
            document.getElementById('light').style.display = 'block';
118
            document.getElementById('fade').style.display = 'block';
119
            draw(eval(parentElement.getAttribute("json")), lightDiv);
0 ignored issues
show
Security Performance introduced by
Calls to eval are slow and potentially dangerous, especially on untrusted code. Please consider whether there is another way to achieve your goal.
Loading history...
120
        } else if (target.classList.contains("black_overlay")) {
121
            var fadeDiv = document.getElementById("fade");
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable fadeDiv already seems to be declared on line 101. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
122
            fadeDiv.parentElement.removeChild(fadeDiv);
123
            var lightDiv = document.getElementById("light");
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable lightDiv already seems to be declared on line 97. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
124
            lightDiv.parentElement.removeChild(lightDiv);
125
        } else if (target.className.toUpperCase() == DIV.toUpperCase()) {
0 ignored issues
show
Bug introduced by
The variable DIV seems to be never declared. If this is a global, consider adding a /** global: DIV */ 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...
126
            if (isHighlight) {
127
                if (target.style.borderLeft) {
128
                    target.removeAttribute("style");
129
                } else {
130
                    target.style = "border-left:1px solid";
131
                }
132
            }
133
            console.log(target);
134
        }
135
    }, true);
136
}
137