| Conditions | 11 |
| Paths | 12 |
| Total Lines | 62 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
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 |
||
| 74 | document.addEventListener("click", function(e) { |
||
| 75 | console.log(e); |
||
| 76 | var target = e.target; |
||
| 77 | if (target.tagName.toUpperCase() == "I") { |
||
| 78 | var isClose = target.classList.contains(COLL); |
||
| 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; |
||
| 106 | |||
| 107 | var fadeDiv = document.createElement("div"); |
||
| 108 | fadeDiv.setAttribute("id", "fade"); |
||
| 109 | fadeDiv.classList.add("black_overlay"); |
||
| 110 | document.body.appendChild(fadeDiv); |
||
| 111 | |||
| 112 | var lightDiv = document.createElement("div"); |
||
| 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); |
||
| 120 | } else if (target.classList.contains("black_overlay")) { |
||
| 121 | var fadeDiv = document.getElementById("fade"); |
||
| 122 | fadeDiv.parentElement.removeChild(fadeDiv); |
||
| 123 | var lightDiv = document.getElementById("light"); |
||
| 124 | lightDiv.parentElement.removeChild(lightDiv); |
||
| 125 | } else if (target.className.toUpperCase() == DIV.toUpperCase()) { |
||
| 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 |