-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbalfbar.js
587 lines (509 loc) · 19.8 KB
/
balfbar.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
(function($) {
$.fn.extend({
BalfBar: function(options) {
console.log('BalfBar v1.4');
var defaults = {
//When to add the fixed_menu class. 0 means always, -1 means never (or always, if you add the class manually) and a -2 means that it will be based on the position of the element
desktopFixedMenuStart: 0,
mobileFixedMenuStart: 0,
//Whether or not hover events should trigger, i.e. should hovering open the menu item
supportHover: true,
//Hide the menu on scroll
hideOnScroll: true,
//Hide the menu when mouse clicks or hovers elsewhere
hideOnMouseOut: true,
//When to start using mobile events (less than and inclusive)
mobileThreshold: 900,
//How long the menu takes to open
menuAnimationTime: 500,
//A callback function when the menu is toggled
menuToggleCallback: null,
//A callback function when the menu is toggled to fixed
fixedMenuCallback: null
};
var options = $.extend(defaults, options);
//Options to variables
var mobileThreshold = options.mobileThreshold;
var desktopFixedMenuStart = options.desktopFixedMenuStart;
var mobileFixedMenuStart = options.mobileFixedMenuStart;
var animationTime = options.menuAnimationTime;
var callBack = options.menuToggleCallback;
//Initial settings
//Current menu height
var currentMenuHeight = 0;
//Is mobile or not
var isMobile = false;
//The viewport to assess
var viewport = window;
return this.each(function() {
var this_menu = $(this);
var currentOffset = -1;
var parentOffset = -1;
if (!$(this_menu).hasClass("fixed_menu")) {
currentOffset = $(this_menu).offset().top;
parentOffset = $(this_menu).parent().offset().top;
}
$(this_menu).addClass("balfbar");
$(this_menu).addClass("loaded");
var mobileMenuType = "";
if ($(this_menu).hasClass("mobile_pushmenu")) {
mobileMenuType = "pushmenu";
} else if ($(this_menu).hasClass("mobile_sidebar")) {
mobileMenuType = "sidebar";
} else if ($(this_menu).hasClass("mobile_dropdown")) {
mobileMenuType = "dropdown";
}
var desktopMenuType = "";
if ($(this_menu).hasClass("desktop_sidebar")) {
desktopMenuType = "sidebar";
}
var desktop_topdown = false;
if ($(this_menu).hasClass("top_down")) {
desktop_topdown = true;
}
currentMenuHeight = $(this_menu).height();
//For touch
var touch_flag = false;
var menu_button = $(this_menu).find('.menu_button');
var items = $(this_menu).find('.items');
var item_holder = $(this_menu).find('.item_holder');
//Media tester
var mediaTester = $(this_menu).find('.media_tester');
//Old method to get the viewport
function getViewPort() {
var e = window,
a = 'inner';
if (!('innerWidth' in window)) {
a = 'client';
e = document.documentElement || document.body;
}
return {
width: e[a + 'Width'],
height: e[a + 'Height']
};
}
$(window).on("resize scroll", function() {
//Need to reset sizes
$(this_menu).find('.scrollable').css({
"max-height": "none"
}).removeClass("scrollable");
});
//Checks if the device is greater than the mobileThreshold
if (mobileThreshold > -1) {
function checkSize() {
//If the media tester exists then we can use it's display property to check whether we are mobile or not
if (mediaTester.length > 0) {
if (mediaTester.is(':visible')) {
isMobile = false;
} else {
isMobile = true;
}
} else {
if (mobileThreshold > -1) {
if (getViewPort().width <= mobileThreshold) {
isMobile = true;
}
}
currentMenuHeight = $(this_menu).height();
var s = $(this_menu).find('.scrollable');
$(s).css({
"max-height": "none"
}).removeClass("scrollable");
if (getViewPort().width > mobileThreshold) {
isMobile = false;
} else {
isMobile = true;
}
if (mobileMenuType == "dropdown") {
$(item_holder).css({
top: currentMenuHeight
});
}
}
if (isMobile) {
$(this_menu).addClass("is_mobile");
} else {
$(this_menu).removeClass("is_mobile");
}
}
checkSize();
$(window).on("ready resize", function() {
if (!$(this_menu).hasClass("fixed_menu")) {
currentOffset = $(this_menu).offset().top;
}
checkSize();
});
}
//Determine whether to add a fixed menu or not
function checkScrollPosition() {
//We need to determine whether or not it has passed the scroll point
var scroll = $(viewport).scrollTop();
function checkPosition(fixedStart) {
if (!$(this_menu).hasClass("fixed_menu")) {
currentOffset = $(this_menu).offset().top;
parentOffset = $(this_menu).parent().offset().top;
}
//Static means the user has specified when it will scroll. 0 means always
if (fixedStart == 0) {
return true;
}
if (scroll >= fixedStart && fixedStart > -1) {
return true;
}
if (currentOffset > -1 && (scroll >= parentOffset && fixedStart == -2)) {
return true;
}
return false;
}
return (!isMobile && checkPosition(desktopFixedMenuStart)) || (isMobile && checkPosition(mobileFixedMenuStart));
}
//Add fixed menu if one or the other is not -1
if (desktopFixedMenuStart != -1 || mobileFixedMenuStart != -1) {
function checkScroll() {
//If checkScrollPosition == true then add fixed classes
if (checkScrollPosition()) {
if (!$(this_menu).hasClass('fixed_menu')) {
$(this_menu).addClass("fixed_menu");
if (options.fixedMenuCallback != null) {
options.fixedMenuCallback(true);
}
}
} else {
//Remove classes
if ($(this_menu).hasClass('fixed_menu')) {
$(this_menu).removeClass("fixed_menu");
if (options.fixedMenuCallback != null) {
options.fixedMenuCallback(false);
}
}
}
}
$(document).on("ready scroll resize", function() {
checkScroll();
});
$(window).on("focus resize", function() {
checkScroll();
});
checkScroll();
}
//Each of these represents an interaction state
var mouseCanFire = true;
var hoverCanFire = true;
var enteredMenu = false;
var lastHoveredItem = null;
//Button
function toggleMenu() {
if (!touch_flag) {
touch_flag = true;
setTimeout(function() {
touch_flag = false;
}, 1000);
if (!($(this_menu).hasClass("animating"))) {
$(this_menu).addClass("animating");
var top_distance = 0;
if ($(this_menu).hasClass("fixed_menu")) {
top_distance = currentMenuHeight;
}
if (!$(this_menu).hasClass("out")) {
//var off = $(window).height() - $(item_holder).offset().top;
$(this_menu).addClass("out");
if (mobileMenuType == "dropdown") {
$(item_holder).show().css({
top: currentMenuHeight,
height: 0
}).animate({
height: $(item_holder).css("max-height")
}, animationTime, function() {
openMenuFinish();
});
} else if (mobileMenuType == "sidebar") {
$(item_holder).css({
display: "block",
top: top_distance
}).animate({
opacity: 1,
left: 0
}, animationTime, function() {
openMenuFinish();
});
} else if (mobileMenuType == "pushmenu") {
var w = $(item_holder).width();
$(item_holder).css({
display: "block",
top: top_distance
}).animate({
left: 0
}, animationTime, function() {
openMenuFinish();
});
$("html, body").addClass("balfbar_no_overflow");
$('.balfbar_pushmenu_container').animate({
left: w
}, animationTime);
}
} else {
if (mobileMenuType == "dropdown") {
$(item_holder).animate({
height: 0
}, animationTime, function() {
closeMenuFinish();
});
} else if (mobileMenuType == "sidebar") {
$(item_holder).animate({
opacity: 0,
left: "-20%"
}, animationTime, function() {
closeMenuFinish();
});
} else if (mobileMenuType == "pushmenu") {
$(item_holder).animate({
left: "-100%"
}, animationTime);
$("html, body").removeClass("balfbar_no_overflow");
$('.balfbar_pushmenu_container').animate({
left: 0
}, animationTime, function() {
closeMenuFinish();
});
}
}
}
}
}
function doCallback(value) {
if (typeof callBack === "function") {
callBack(value);
}
}
function openMenuFinish() {
$("html").addClass("balfbar_out");
$(this_menu).removeClass("animating");
//Call after complete
doCallback(true);
}
function closeMenuFinish() {
$(this_menu).removeClass("out");
$("html").removeClass("balfbar_out");
$(this_menu).removeClass("animating");
//Call after complete
doCallback(false);
}
//Click elsewhere
function propagateElsewhere(e) {
e.stopPropagation();
//If it's not the desktop sidebar
if (!(desktopMenuType == "sidebar" && !isMobile)) {
if ($(this_menu).hasClass("out")) {
toggleMenu();
}
setTimeout(function() {
if ($('.hover').length == 0) {
$(items).find('.open').removeClass("open");
$(this_menu).find('.scrollable').css({
"max-height": "none"
}).removeClass("scrollable");
}
}, 200);
}
}
function calculateTop(i) {
return $(i).offset().top - $(document).scrollTop();
}
function openMenu(mouseEvent, listItem) {
//Prevent it navigating to something
mouseEvent.stopPropagation();
var viewportWidth = getViewPort().width;
var viewportHeight = getViewPort().height;
//We have now entered the menu
enteredMenu = true;
console.log(desktopMenuType);
//Opens and positions all items
function openParents(item) {
var parentNode = $(item).parent();
var isTopLevel = parentNode != null && $(parentNode).hasClass("items");
//Find the first dropdown
$(item).addClass("open");
if (!isTopLevel) {
//We only open up to .items
openParents(parentNode);
}
if (!isMobile) {
if (desktopMenuType != "sidebar") {
var dropdown = $(item).children('ul.dropdown, div.megamenu');
//Set the left offset for this item's dropdown
var leftPosition = $(item).offset().left;
//If isTopLevel then it is a top level item
if (!isTopLevel) {
//If it's out of the window at the right, we need to attempt to get it so that it's not
leftPosition += $(item).width();
if (leftPosition + $(dropdown).width() >= viewportWidth || $(parentNode).hasClass("left_menu")) {
$(dropdown).addClass("left_menu");
} else {
$(dropdown).removeClass("left_menu");
}
//z-index is set based on depth because a left floating menu will possibly cover others
$(dropdown).css({
"z-index": $(dropdown).parents("ul").length
});
//Check top positioning
if (!$(dropdown).hasClass("scrollable")) {
//Calculate top offset
var topDistance = calculateTop(item);
var itemHeight = $(dropdown).height();
if ((topDistance + itemHeight) > viewportHeight) {
//This exceeds the height we deal with it
var newTopPosition = 0;
var maxHeight = $(window).height() - ($(dropdown).offset().top - $(window).scrollTop());
newTopPosition = (0 - topDistance) + 40;
$(dropdown).css({
"max-height": maxHeight
}).attr("data-max-top", newTopPosition).attr("data-max-base", newTopPosition + (viewportHeight - itemHeight)).addClass("scrollable");
}
}
} else {
//Top level dropdowns animate
if (!$(dropdown).hasClass("open") && ($(dropdown).hasClass("dropdown") || $(dropdown).hasClass("megamenu"))) {
if (!(desktopMenuType == "sidebar" && !isMobile)) {
$(dropdown).css({
"margin-top": 0 - currentMenuHeight
}).animate({
"margin-top": -1
}, 500);
} else {
$(dropdown).css({
"height": 0
}).animate({
"height": $(dropdown).height()
}, 500);
}
}
}
}
}
}
function closeChildren(m) {
$(m).find("open").removeClass("open");
$(m).find('.scrollable').css({
"max-height": "none"
}).removeClass("scrollable");
}
if ($(listItem).hasClass("open")) {
$(listItem).removeClass("open");
$(listItem).find('.scrollable').css({
"max-height": "none"
}).removeClass("scrollable");
closeChildren(listItem);
} else {
//var dds = $(listItem).children(".dropdown");
//Make all others disappear
$(items).find(".open").removeClass("open");
$(items).find('.scrollable').css({
"max-height": "none"
}).removeClass("scrollable");
//Open all parents of this item and position all
openParents(listItem);
}
}
//Button touch and click
$(menu_button).on("click", function() {
toggleMenu();
});
$(menu_button).on("touchstart", function() {
toggleMenu();
});
//Hide the menu and all open items if the user clicks elsewhere
if (options.hideOnMouseOut) {
//Click away (instant)
$(document).on("click", function(e) {
enteredMenu = false;
propagateElsewhere(e);
});
//Hover away (slow)
$(document).on("mouseover", function(e) {
enteredMenu = false;
setTimeout(function() {
if (enteredMenu == false && !isMobile) {
propagateElsewhere(e);
}
}, 100);
});
}
//Hide the menu when the document is resized or the user scrolls
if (options.hideOnScroll) {
//Hide mobile menu
$(document).on("scroll resize", function(e) {
if (!isMobile && !(desktopMenuType == "sidebar" && !isMobile)) {
if ($(this_menu).hasClass("out")) {
toggleMenu();
}
$(items).find('.open').removeClass("open");
}
});
}
//Desktop versions will ignore top-level links
$(items).find('a').each(function(e) {
var len = $(this).parent().find('ul').length;
if (len > 0) {
$(this).removeAttr("href");
$(this).on("click", function(e) {
e.preventDefault();
})
}
});
function clickOnItem(mouseEvent, listItem) {
setTimeout(function() {
if (mouseCanFire) {
hoverCanFire = false;
mouseCanFire = false;
openMenu(mouseEvent, listItem);
setTimeout(function() {
hoverCanFire = true;
}, 2000);
setTimeout(function() {
mouseCanFire = true;
}, 500);
}
}, 200);
}
//Click works for most devices, but some also like hover.
$(this_menu).find('.items > li, .dropdown > li, .megamenu li').on("click", function(e) {
clickOnItem(e, this);
});
/*
* Hover works on all devices above 901px, but stops being used below this.
* Devices such as iPad, iPhone, Win10 tabs will all use the click event by default anyway,
* and desktops with cursors will choose to use the click at this size as well.
*/
$(this_menu).find('.items > li, .dropdown > li, .megamenu li').on("mouseenter", function(mouseEvent) {
//Desktop sidebar has no hover
$(this).addClass('hover');
if (!(desktopMenuType == "sidebar" && !isMobile)) {
//If SupportHover is true, then we support hover events to open the menus
if (options.supportHover && !desktop_topdown) {
if ((hoverCanFire || lastHoveredItem != this) && !isMobile) {
mouseCanFire = false;
hoverCanFire = false;
lastHoveredItem = this;
openMenu(mouseEvent, this);
setTimeout(function() {
mouseCanFire = true;
}, 2000);
setTimeout(function() {
canFireHover = true;
lastHoveredItem = null;
}, 400);
}
}
}
}).on("mouseleave", function() {
//We handle hover events here to simplify the animation events for older browsers
$(this_menu).find('.hover').removeClass('hover');
});
$(this_menu).find(".items > li, .dropdown > li, .megamenu li, .item_holder > *, .item_holder").on("click mouseover", function(e) {
e.stopPropagation();
enteredMenu = true;
});
});
}
});
})(jQuery);