Completed
Push — master ( 8098f9...5a8583 )
by
unknown
03:55
created

public_html/js/MobileMenuAPI.js   A

Size

Lines of Code 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
rs 10
c 0
b 0
f 0
noi 9
1
2
var MobileMenu = {};
3
4
MobileMenu.debug = true;
5
6
if (!window.console) {
7
    var console = {};
8
}
9
10
if (!console.log) {
0 ignored issues
show
Bug introduced by
The variable console does not seem to be initialized in case !window.console on line 6 is false. Are you sure this can never be the case?
Loading history...
11
    console.log = function () {};
12
}
13
14
// Window Event Listener =======================================================
15
16
MobileMenu.addWindowEventListener = function (event, myFunction) {
17
18
    if (window.addEventListener) {
19
20
        window.addEventListener(event, myFunction, false);
21
22
    } else if (window.attachEvent) {
23
24
        var onEvent = "on" + event;
25
26
        window.attachEvent(onEvent, myFunction);
27
28
    }
29
30
};
31
32
// Mobile Navigation Triggers ==================================================
33
34
MobileMenu.mobileNavClickListener = function (e) {
35
36
    var mobileMenuTrigger = document.getElementById("pageHeroMobileTrigger");
0 ignored issues
show
Unused Code introduced by
The variable mobileMenuTrigger seems to be never used. Consider removing it.
Loading history...
37
38
    var mainMenu = document.getElementById("pageHeroNavigationMenu");
39
40
    e.preventDefault();
41
42
    if (this.classList.contains("active")) {
43
44
        this.classList.remove("active");
45
46
        mainMenu.classList.remove("active");
47
48
        document.body.style.overflowY = "auto";
49
50
    } else {
51
52
        this.classList.add("active");
53
54
        mainMenu.classList.add("active");
55
56
        document.body.style.overflowY = "hidden";
57
58
    }
59
60
};
61
62
MobileMenu.mobileNavKeyListener = function (e) {
63
64
    if (e.keyCode == 13) {
65
66
        var mobileMenuTrigger = document.getElementById("pageHeroMobileTrigger");
0 ignored issues
show
Unused Code introduced by
The variable mobileMenuTrigger seems to be never used. Consider removing it.
Loading history...
67
68
        var mainMenu = document.getElementById("pageHeroNavigationMenu");
69
70
        e.preventDefault();
71
72
        if (this.classList.contains("active")) {
73
74
            this.classList.remove("active");
75
76
            mainMenu.classList.remove("active");
77
78
            document.body.style.overflowY = "auto";
79
80
        } else {
81
82
            this.classList.add("active");
83
84
            mainMenu.classList.add("active");
85
86
            document.body.style.overflowY = "hidden";
87
88
        }
89
90
    }
91
92
};
93
94
MobileMenu.setMobileNavTriggers = function () {
95
96
    // Gets all elements on the page with "accordion-trigger".
97
    var mobileMenuTrigger = document.getElementById("pageHeroMobileTrigger");
98
99
    // Checks for a click.
100
    mobileMenuTrigger.addEventListener("click", MobileMenu.mobileNavClickListener);
101
102
    // Checks for an Enter key click.
103
    mobileMenuTrigger.addEventListener("keydown", MobileMenu.mobileNavKeyListener);
104
105
};
106
107
MobileMenu.addWindowEventListener("load", MobileMenu.setMobileNavTriggers);
108
109
// Mobile Navigation Tab Order =================================================
110
111
MobileMenu.mobileNavTabListener = function (e) {
112
113
    // Get the Main Menu
114
    var mainMenu = document.getElementById("pageHeroNavigationMenu");
115
116
    // Get Mobile Menu Trigger
117
    var menuTrigger = document.querySelector(".page-hero__mobile-trigger");
118
119
    // Get Mobile Menu Items
120
    var menuItems = mainMenu.querySelectorAll(".page-hero__navigation-item");
121
122
    // Get Last Menu Item
123
    var lastMenuItem = menuItems[menuItems.length - 1]
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
124
125
    if (e.keyCode == 9) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if e.keyCode == 9 is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
126
127
        if (this === lastMenuItem) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if this === lastMenuItem is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
128
129
            e.preventDefault();
130
131
            menuTrigger.focus();
132
133
            return false;
134
135
        }
136
137
    }
138
139
}
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
140
141
MobileMenu.setMobileNavTabTriggers = function (e) {
0 ignored issues
show
Unused Code introduced by
The parameter e 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...
142
143
    // Get the Main Menu
144
    var mainMenu = document.getElementById("pageHeroNavigationMenu");
145
146
    // Get Mobile Menu Items
147
    var menuItems = mainMenu.querySelectorAll(".page-hero__navigation-item");
148
149
    // Set Triggers
150
    for (var i = 0; i < menuItems.length; i++) {
151
152
        menuItems[i].addEventListener("keydown", MobileMenu.mobileNavTabListener);
153
154
    }
155
156
}
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
157
158
MobileMenu.addWindowEventListener("load", MobileMenu.setMobileNavTabTriggers);