htdocs/js/responsive-menu.js   A
last analyzed

Complexity

Total Complexity 10
Complexity/F 1.43

Size

Lines of Code 135
Function Count 7

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 135
c 0
b 0
f 0
rs 10
wmc 10
mnd 3
bc 3
fnc 7
bpm 0.4285
cpm 1.4285
noi 0
1
/**
2
 * @preserve Setup the basis for the responsive menu.
3
 * @author Mikael Roos <[email protected]>
4
 * @see {@link https://github.com/janaxs/responsive-menu}
5
 */
6
(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