Conditions | 1 |
Paths | 1 |
Total Lines | 135 |
Code Lines | 39 |
Lines | 135 |
Ratio | 100 % |
Changes | 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:
1 | /** |
||
6 | View Code Duplication | (function() { |
|
|
|||
7 | "use strict"; |
||
8 | |||
9 | // Get the items needed for the menu to work. |
||
10 | var body = document.getElementsByTagName("body")[0]; |
||
11 | var menuButton = document.getElementById("rm-menu-button"); |
||
12 | var menu = document.getElementById("rm-menu"); |
||
13 | var menuMax = document.querySelector(".rm-max #rm-menu"); |
||
14 | var submenus = document.getElementsByClassName("rm-submenu-button"); |
||
15 | |||
16 | // To support WordPress submenus |
||
17 | var submenuswp = document.getElementsByClassName("sub-menu"); |
||
18 | |||
19 | |||
20 | |||
21 | /** |
||
22 | * Set the size of the max menu. |
||
23 | */ |
||
24 | var setMaxMenuSize = function() { |
||
25 | if (menuMax === null) { |
||
26 | return; |
||
27 | } |
||
28 | |||
29 | menuMax.style.width = window.innerWidth + "px"; |
||
30 | menuMax.style.height = window.innerHeight + "px"; |
||
31 | }; |
||
32 | |||
33 | setMaxMenuSize(); |
||
34 | |||
35 | |||
36 | /** |
||
37 | * Show submenu where ever a li item holds a submenu. Used as callback |
||
38 | * for li click events but only valid for the mobile version. The desktop |
||
39 | * version uses hover instead och click events. |
||
40 | */ |
||
41 | var showSubmenu = function(event) { |
||
42 | //console.log("Show submenu"); |
||
43 | |||
44 | if (this.parentNode.classList.contains("rm-desktop")) { |
||
45 | //console.log("Cancel show submenu"); |
||
46 | return; |
||
47 | } |
||
48 | |||
49 | this.parentElement.classList.toggle("rm-submenu-open"); |
||
50 | this.parentElement.querySelector("ul").classList.toggle("rm-show-submenu"); |
||
51 | |||
52 | //event.preventDefault(); |
||
53 | event.stopPropagation(); |
||
54 | }; |
||
55 | |||
56 | |||
57 | |||
58 | /** |
||
59 | * Add eventlisteners for each li holding a submenu |
||
60 | */ |
||
61 | Array.prototype.filter.call(submenus, function(element) { |
||
62 | element.addEventListener("click", showSubmenu); |
||
63 | //console.log("found submenu"); |
||
64 | }); |
||
65 | |||
66 | // To support WordPress menus |
||
67 | Array.prototype.filter.call(submenuswp, function(element) { |
||
68 | // Add a clickable button to (max) menu |
||
69 | var button = document.createElement("a"); |
||
70 | |||
71 | button.setAttribute("href", "#"); |
||
72 | button.setAttribute("class", "rm-submenu-button"); |
||
73 | button.addEventListener("click", showSubmenu); |
||
74 | element.parentNode.insertBefore(button, element.parentNode.firstChild); |
||
75 | |||
76 | //element.parentNode.addEventListener("click", showSubmenu); |
||
77 | //console.log("found submenuwp"); |
||
78 | }); |
||
79 | |||
80 | |||
81 | |||
82 | /** |
||
83 | * Show the menu when clicking on the closed (mobile) menu button. |
||
84 | */ |
||
85 | menuButton.addEventListener("click", function(event) { |
||
86 | // Toggle display of menu |
||
87 | menu.classList.toggle("rm-clicked"); |
||
88 | menuButton.classList.toggle("rm-clicked"); |
||
89 | body.classList.toggle("rm-modal"); |
||
90 | |||
91 | // Toggle between desktop and mobile menu when no max menu enabled. |
||
92 | if (menuMax === null) { |
||
93 | menu.classList.toggle("rm-mobile"); |
||
94 | menu.classList.toggle("rm-desktop"); |
||
95 | } |
||
96 | |||
97 | event.preventDefault(); |
||
98 | event.stopPropagation(); |
||
99 | }); |
||
100 | |||
101 | |||
102 | |||
103 | /** |
||
104 | * Prevent touch event scrolling & closing maxmenu on iOS |
||
105 | */ |
||
106 | /* |
||
107 | menuMax.addEventListener("scroll", function(event) { |
||
108 | event.stopPropagation(); |
||
109 | }); |
||
110 | */ |
||
111 | |||
112 | |||
113 | |||
114 | /** |
||
115 | * |
||
116 | */ |
||
117 | /* |
||
118 | var clearMenu = function (event) { |
||
119 | //console.log("Clear menu"); |
||
120 | // Add desktop and remove mobile, but not if max menu is enabled |
||
121 | if (menuMax === null) { |
||
122 | menu.classList.remove("rm-mobile"); |
||
123 | menu.classList.add("rm-desktop"); |
||
124 | } |
||
125 | |||
126 | // Remove clicked items |
||
127 | body.classList.remove("rm-modal"); |
||
128 | menuButton.classList.remove("rm-clicked"); |
||
129 | menu.classList.remove("rm-clicked"); |
||
130 | |||
131 | event.preventDefault(); |
||
132 | }; |
||
133 | */ |
||
134 | |||
135 | window.addEventListener("resize", function(/* event */) { |
||
136 | //clearMenu(event); |
||
137 | setMaxMenuSize(); |
||
138 | }); |
||
139 | //document.addEventListener("click", clearMenu); |
||
140 | })(); |
||
141 |