-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmenu-consume.js
216 lines (195 loc) · 8.63 KB
/
menu-consume.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/**
* self: Targeted Menu
* breakPoint: The point when the mobile menu is triggered (value in px)
* horizontalOffsets: Other items in the navigation area that the menu needs to
* sit next to. This params accepts an array of the class/ID names
* dropDownClass: The class name(s) of the More drop down that nav items are placed into
* menuItemText: Override the menu item text where items are placed
*/
(function ($) {
$.fn.menuConsume = function(options) {
//Default Options
var settings = $.extend({
self: $(this),
breakPoint: '768',
horizontalOffsets: new Array(),
dropDownClass: '.menuConsume-more',
menuItemText: 'More'
}, options);
//Initialize the default variables
//Width of the menu
var menuWidth = $(settings.self).outerWidth(true);
//Width of the header region
var headerWidth = $(settings.self).parent().width();
var wait;
//Timer to prevent over running checking functions and keep CPU ussuage down
//Check for if the browser is new enough that it would support media queries
//If the browser does not the respond.js plugin takes a second to load up media queries and this plugin only sees the mobile version of the site
//until the page is resized.
//The width of settings.horizontalOffsets widths combined
var offSetWidth = 0;
//Initialize the setTimeout to fix IE8
if($('html').hasClass('no-rgba')) {
//Check for if the browser is new enough that it would support media queries
//If the browser does not the respond.js plugin takes a second to load up media queries and this plugin only sees the mobile version of the site
//until the page is resized.
var wait = setTimeout(menuMore, 1000);
} else {
//For modern browsers
var wait = setTimeout(menuMore, 100);
}
$(window).resize(function(){
//We are preventing our functions from firing rapidly on the page reload.
//Refreshing our menu Width
menuWidth = settings.self.outerWidth(true);
//Refreshing our header region width
headerWidth = $(settings.self).parent().width();
//Timer Delay
var timerDelay = 500;
// The class no-rgba is a class provided from the modernizr plugin which will check what the browser supports
if($('html').hasClass('no-rgba')) {
timerDelay = 1000;
}
//Clear out our time to reset it
clearTimeout(wait);
if($(window).outerWidth(true) > settings.breakPoint) {
wait = setTimeout(function(){
menuMore();
}, timerDelay);
}else {
wait = setTimeout(function(){
jQuery(document).trigger('menu_consumed', true);
if($(settings.dropDownClass)[0]) {
menuMobileRelocate();
$(settings.dropDownClass).css('visibility', 'visible');
}
}, timerDelay);
}
}).resize();
function menuMore() {
//Clear out the offSetWidth to recalculate the offset widths
offSetWidth = 0;
// Loop through our horizontal offsets
for(var t = 0; t < settings.horizontalOffsets.length; t++) {
offSetWidth += $(settings.horizontalOffsets[t]).outerWidth(true);
}
if($(settings.dropDownClass).length === 0 && (menuWidth + offSetWidth) >= headerWidth) {
//Check if our More drop down contains any items and if the menu width
// of our menu plus offsets are wider than the header region
$(settings.self).append('<li class="' + settings.dropDownClass.replace(/\./g,' ') + '"><a href="#">' + settings.menuItemText + '</a><ul></ul></li>');
$(settings.dropDownClass + ' ul').css('position', 'absolute');
}
if ($(settings.dropDownClass).length !== 0 && $(window).outerWidth(true) < settings.breakPoint) {
//We check to see if the browser is past the breakpoint for triggering the mobile
//menu. If true we dump all menu items back into the main menu and remove the drop down
menuMobileRelocate();
$(settings.dropDownClass).css('visibility', 'visible');
}
else if((menuWidth + offSetWidth) >= headerWidth && $(settings.dropDownClass).length !== 0) {
//The menu is to wide so we dump menu items into the drop down
menuItemRemove();
$(settings.dropDownClass).css('visibility', 'visible');
$(document).trigger('menu_consumed', true);
}
else if((menuWidth + offSetWidth) <= headerWidth && $(settings.dropDownClass).length !== 0) {
//The menu is smaller so we try putting items back into the main menu
menuItemAdd();
$(settings.dropDownClass).css('visibility', 'visible');
$(document).trigger('menu_consumed', true);
}
if($(settings.dropDownClass + ' ul').children().length <= 0) {
//If things are in order we remove the More dropdown
$(settings.dropDownClass).remove();
$(document).trigger('menu_consumed', true);
}
}
function menuMobileRelocate() {
//Here the mobile menu has been triggered so we loop through and dump all
// of the Menu More Items back into the main navigation
var firstChild;
while ($(settings.dropDownClass + ' ul').children().length > 0) {
if($(settings.dropDownClass + ' ul').children().length >= 1){
firstChild = $(settings.dropDownClass + ' ul').children()[0];
$(settings.self).append($(firstChild));
if($(settings.dropDownClass + ' ul').children().length === 0) {
$(settings.dropDownClass).remove();
}
}else {
$(settings.dropDownClass).remove();
break;
}
}
}
function menuItemRemove() {
//Here we are moving items from the targeted menu into the dropdown menu
var children,
lastChild,
dropDownClasses,
hasClass = false,
//The bailout var is used to prevent an infinite loop that will lock browsers up
bailOut = 0;
$('.menuConsume-more > ul').css('visibility', 'visible');
while ((menuWidth + offSetWidth) >= headerWidth && bailOut < 300) {
bailOut++;
children = $(settings.self).children();
lastChild = children.length - 2;
for(var i = (children.length - 1); i >= 0; i--) {
dropDownClasses = settings.dropDownClass.split('.');
//Loop through the classes placed on our drop down
for(var h = 1; h < dropDownClasses.length; h++) {
if($(children[i]).hasClass(dropDownClasses[h])) {
hasClass = true;
}else {
hasClass = false;
break;
}
}
if($(children[i]).css('display') !== 'none' && hasClass !== true) {
//If the children aren't hidden the browser locks up
$(children[lastChild]).hide();
$(settings.dropDownClass + ' > ul').prepend(children[i]);
break;
}
}
menuWidth = settings.self.outerWidth(true);
headerWidth = $(settings.self).parent().width();
}
$('.menuConsume-more li').show();
}
function menuItemAdd() {
//Here we are moving items from the dropdown menu back into the targeted menu
var childCount;
//We loop through the drop down until the maximum width is reached.
while ((menuWidth + offSetWidth) <= headerWidth && $(settings.dropDownClass + ' ul').children().length > 0) {
if($(settings.dropDownClass + ' > ul').children().length >= 1){
var firstChild = $(settings.dropDownClass + ' ul').children()[0];
$(settings.self).append($(firstChild));
childCount = $(settings.dropDownClass + ' > ul').children('li').length;
if(childCount === 0) {
$(settings.dropDownClass).remove();
}
menuWidth = settings.self.outerWidth(true);
headerWidth = $(settings.self).parent().width();
if((menuWidth + offSetWidth) >= headerWidth) {
$(settings.dropDownClass + ' > ul').prepend($(firstChild));
break;
}
else {
$(settings.self).append($(settings.dropDownClass));
menuWidth = $(settings.self).outerWidth(true);
headerWidth = $(settings.self).parent().width();
//Removes the More drop down
if($(settings.dropDownClass + ' > ul').children().length === 0) {
$(settings.dropDownClass).remove();
}
}
}else {
$(settings.dropDownClass).remove();
menuWidth = settings.self.outerWidth(true);
headerWidth = $(settings.self).parent().width();
break;
}
}
}
}
}(jQuery));