-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.multiplelevelmenu.js
148 lines (129 loc) · 5.16 KB
/
jquery.multiplelevelmenu.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* MultiplLevelMenu v0.2.0
*
* Copyright (c) 2014 Joe Taylor (jtlr.co.uk / @_JTLR)
*
* Licensed under MIT
*
*/
(function($) {
$.fn.multipleLevelMenu = function(options) {
// Plugin defaults
var settings = $.extend({
// Elements
triggerElement: $('.multiple-level-menu_trigger'),
closeElement: $('.multiple-level-menu_close'),
wrapperElement: $('.multiple-level-menu_wrapper'),
// Non jquery selectors
listSelector: 'ul',
listItemSelector: 'li',
// Classes
activeClass: 'is-active',
// Options
disableAbove: false,
disableBelow: false,
preserveState: false,
closeOnNavigation: true,
eventType: 'click'
}, options),
menu = this,
$body = $('body'),
viewportWidth = window.innerWidth,
pluginCurrentlyEnabled;
function checkIfPluginEnabled() {
viewportWidth = window.innerWidth;
// If we're not above or below plugin disabling thresholds, enable plugin
if ((settings.disableAbove === false
|| settings.disableAbove > viewportWidth)
&& (settings.disableBelow === false
|| settings.disableBelow < viewportWidth)) {
pluginCurrentlyEnabled = true;
} else {
pluginCurrentlyEnabled = false;
menu.removeClass(settings.activeClass);
$(settings.listSelector, menu).removeClass(settings.activeClass);
settings.wrapperElement.removeClass(settings.activeClass);
settings.triggerElement.removeClass(settings.activeClass);
}
}
checkIfPluginEnabled();
$(window).bind('resize', function() {
checkIfPluginEnabled();
});
// Open/close menu when trigger is clicked
settings.triggerElement.bind(settings.eventType, function(event) {
event.stopPropagation();
if (pluginCurrentlyEnabled) {
if(menu.hasClass(settings.activeClass)) {
closeMenu();
} else {
openMenu();
}
}
});
// Don't allow click to propagate to wrapper and trigger close
menu.bind(settings.eventType, function(event) {
event.stopPropagation();
});
settings.closeElement.bind(settings.eventType, function(event) {
event.stopPropagation();
if (pluginCurrentlyEnabled) {
var $this = $(this),
$thisParent;
if ($this.parent(settings.listSelector).hasClass(settings.activeClass)) {
$thisParent = $this.parent(settings.listSelector);
} else {
$thisParent = $this.parents('nav');
settings.wrapperElement.removeClass(settings.activeClass);
settings.triggerElement.removeClass(settings.activeClass);
}
$thisParent.removeClass('is-active');
}
});
$(settings.listItemSelector).each(function() {
var $this = $(this);
if ($('ul', $this).length !== 0) {
$('> a', $this).bind(settings.eventType, function(event) {
event.preventDefault();
});
} else {
if (settings.closeOnNavigation) {
$('> a', $this).bind(settings.eventType, function() {
closeMenu();
});
}
}
});
$(settings.listItemSelector, menu).bind(settings.eventType, function(event) {
event.stopPropagation();
if (pluginCurrentlyEnabled) {
var $this = $(this),
$thisChildMenu = $(settings.listSelector, $this);
if ($thisChildMenu.length > 0) {
if ($thisChildMenu.hasClass(settings.activeClass)) {
$thisChildMenu.removeClass(settings.activeClass);
} else {
$thisChildMenu.addClass(settings.activeClass);
}
}
}
});
function openMenu() {
menu.addClass(settings.activeClass);
settings.wrapperElement.addClass(settings.activeClass)
.bind(settings.eventType, closeMenu);
settings.triggerElement.addClass(settings.activeClass);
}
function closeMenu() {
menu.removeClass(settings.activeClass);
settings.wrapperElement.removeClass(settings.activeClass)
.unbind(settings.eventType);
settings.triggerElement.removeClass(settings.activeClass);
// If preserve state isn't set remove active state from all sub menus
if (!settings.preserveState) {
$(settings.listSelector, menu).removeClass(settings.activeClass);
}
}
return menu;
};
} (jQuery));