1/*jslint browser: true, white: true, this: true, long: true */ 2/*global console,jQuery,megamenu,window,navigator*/ 3 4/*! Max Mega Menu jQuery Plugin */ 5(function ( $ ) { 6 "use strict"; 7 8 $.maxmegamenu = function(menu, options) { 9 var plugin = this; 10 var $menu = $(menu); 11 var $wrap = $(menu).parent(); 12 var $toggle_bar = $menu.siblings(".mega-menu-toggle"); 13 var html_body_class_timeout; 14 15 var defaults = { 16 event: $menu.attr("data-event"), 17 effect: $menu.attr("data-effect"), 18 effect_speed: parseInt($menu.attr("data-effect-speed")), 19 effect_mobile: $menu.attr("data-effect-mobile"), 20 effect_speed_mobile: parseInt($menu.attr("data-effect-speed-mobile")), 21 panel_width: $menu.attr("data-panel-width"), 22 panel_inner_width: $menu.attr("data-panel-inner-width"), 23 mobile_force_width: $menu.attr("data-mobile-force-width"), 24 mobile_overlay: $menu.attr("data-mobile-overlay"), 25 mobile_state: $menu.attr("data-mobile-state"), 26 mobile_direction: $menu.attr("data-mobile-direction"), 27 second_click: $menu.attr("data-second-click"), 28 vertical_behaviour: $menu.attr("data-vertical-behaviour"), 29 document_click: $menu.attr("data-document-click"), 30 breakpoint: $menu.attr("data-breakpoint"), 31 unbind_events: $menu.attr("data-unbind"), 32 hover_intent_timeout: $menu.attr("data-hover-intent-timeout"), 33 hover_intent_interval: $menu.attr("data-hover-intent-interval") 34 }; 35 36 plugin.settings = {}; 37 38 var items_with_submenus = $("li.mega-menu-megamenu.mega-menu-item-has-children," + 39 "li.mega-menu-flyout.mega-menu-item-has-children," + 40 "li.mega-menu-tabbed > ul.mega-sub-menu > li.mega-menu-item-has-children," + 41 "li.mega-menu-flyout li.mega-menu-item-has-children", $menu); 42 43 var collapse_children_parents = $("li.mega-menu-megamenu li.mega-menu-item-has-children.mega-collapse-children > a.mega-menu-link", $menu); 44 45 plugin.addAnimatingClass = function(element) { 46 if (plugin.settings.effect === "disabled") { 47 return; 48 } 49 50 $(".mega-animating").removeClass("mega-animating"); 51 52 var timeout = plugin.settings.effect_speed + parseInt(plugin.settings.hover_intent_timeout, 10); 53 54 element.addClass("mega-animating"); 55 56 setTimeout(function() { 57 element.removeClass("mega-animating"); 58 }, timeout ); 59 }; 60 61 plugin.hideAllPanels = function() { 62 $(".mega-toggle-on > a.mega-menu-link", $menu).each(function() { 63 plugin.hidePanel($(this), false); 64 }); 65 }; 66 67 plugin.expandMobileSubMenus = function() { 68 if (plugin.settings.mobile_direction !== 'vertical') { 69 return; 70 } 71 72 $(".mega-menu-item-has-children.mega-expand-on-mobile > a.mega-menu-link", $menu).each(function() { 73 plugin.showPanel($(this), true); 74 }); 75 76 if ( plugin.settings.mobile_state == 'expand_all' ) { 77 $(".mega-menu-item-has-children:not(.mega-toggle-on) > a.mega-menu-link", $menu).each(function() { 78 plugin.showPanel($(this), true); 79 }); 80 } 81 82 if ( plugin.settings.mobile_state == 'expand_active' ) { 83 const activeItemSelectors = [ 84 "li.mega-current-menu-ancestor.mega-menu-item-has-children > a.mega-menu-link", 85 "li.mega-current-menu-item.mega-menu-item-has-children > a.mega-menu-link", 86 "li.mega-current-menu-parent.mega-menu-item-has-children > a.mega-menu-link", 87 "li.mega-current_page_ancestor.mega-menu-item-has-children > a.mega-menu-link", 88 "li.mega-current_page_item.mega-menu-item-has-children > a.mega-menu-link" 89 ]; 90 91 $menu.find(activeItemSelectors.join(', ')).each(function() { 92 plugin.showPanel($(this), true); 93 }); 94 } 95 } 96 97 plugin.hideSiblingPanels = function(anchor, immediate) { 98 anchor.parent().parent().find(".mega-toggle-on").children("a.mega-menu-link").each(function() { // all open children of open siblings 99 plugin.hidePanel($(this), immediate); 100 }); 101 }; 102 103 plugin.isDesktopView = function() { 104 var width = Math.max(document.documentElement.clientWidth || 0, window.innerWidth || 0); 105 return width > plugin.settings.breakpoint; 106 }; 107 108 plugin.isMobileView = function() { 109 return !plugin.isDesktopView(); 110 }; 111 112 plugin.showPanel = function(anchor, immediate) { 113 if ( $.isNumeric(anchor) ) { 114 anchor = $("li.mega-menu-item-" + anchor, $menu).find("a.mega-menu-link").first(); 115 } else if ( anchor.is("li.mega-menu-item") ) { 116 anchor = anchor.find("a.mega-menu-link").first(); 117 } 118 119 anchor.parent().triggerHandler("before_open_panel"); 120 121 anchor.parent().find("[aria-expanded]").first().attr("aria-expanded", "true"); 122 123 $(".mega-animating").removeClass("mega-animating"); 124 125 if (plugin.isMobileView() && anchor.parent().hasClass("mega-hide-sub-menu-on-mobile")) { 126 return; 127 } 128 129 if (plugin.isDesktopView() && ( $menu.hasClass("mega-menu-horizontal") || $menu.hasClass("mega-menu-vertical") ) && !anchor.parent().hasClass("mega-collapse-children")) { 130 plugin.hideSiblingPanels(anchor, true); 131 } 132 133 if ((plugin.isMobileView() && $wrap.hasClass("mega-keyboard-navigation")) || plugin.settings.vertical_behaviour === "accordion") { 134 plugin.hideSiblingPanels(anchor, false); 135 } 136 137 plugin.calculateDynamicSubmenuWidths(anchor); 138 139 // apply jQuery transition (only if the effect is set to "slide", other transitions are CSS based) 140 if ( plugin.shouldUseSlideAnimation(anchor, immediate) ) { 141 var speed = plugin.isMobileView() ? plugin.settings.effect_speed_mobile : plugin.settings.effect_speed; 142 143 anchor.siblings(".mega-sub-menu").css("display", "none").animate({"height":"show", "paddingTop":"show", "paddingBottom":"show", "minHeight":"show"}, speed, function() { 144 $(this).css("display", ""); 145 }); 146 } 147 148 anchor.parent().addClass("mega-toggle-on").triggerHandler("open_panel"); 149 }; 150 151 152 plugin.shouldUseSlideAnimation = function(anchor, immediate) { 153 154 if (immediate == true) { 155 return false;
156 } 157 158 if (anchor.parent().hasClass("mega-collapse-children")) { 159 return true; 160 } 161 162 if (plugin.isDesktopView() && plugin.settings.effect === "slide") { 163 return true; 164 } 165 166 if (plugin.isMobileView()) { 167 if (plugin.settings.effect_mobile === "slide") { 168 return true; 169 } 170 171 if ( plugin.isMobileOffCanvas() ) { 172 return plugin.settings.mobile_direction !== "horizontal"; 173 } 174 } 175 176 return false; 177 }; 178 179 180 plugin.hidePanel = function(anchor, immediate) { 181 if ( $.isNumeric(anchor) ) { 182 anchor = $("li.mega-menu-item-" + anchor, $menu).find("a.mega-menu-link").first(); 183 } else if ( anchor.is("li.mega-menu-item") ) { 184 anchor = anchor.find("a.mega-menu-link").first(); 185 } 186 187 anchor.parent().triggerHandler("before_close_panel"); 188 189 anchor.parent().find("[aria-expanded]").first().attr("aria-expanded", "false"); 190 191 if ( plugin.shouldUseSlideAnimation(anchor) ) { 192 var speed = plugin.isMobileView() ? plugin.settings.effect_speed_mobile : plugin.settings.effect_speed; 193 194 anchor.siblings(".mega-sub-menu").animate({"height":"hide", "paddingTop":"hide", "paddingBottom":"hide", "minHeight":"hide"}, speed, function() { 195 anchor.siblings(".mega-sub-menu").css("display", ""); 196 anchor.parent().removeClass("mega-toggle-on").triggerHandler("close_panel"); 197 }); 198 199 return; 200 } 201 202 if (immediate) { 203 anchor.siblings(".mega-sub-menu").css("display", "none").delay(plugin.settings.effect_speed).queue(function(){ 204 $(this).css("display", "").dequeue(); 205 }); 206 } 207 208 // pause video widget videos 209 anchor.siblings(".mega-sub-menu").find(".widget_media_video video").each(function() { 210 this.player.pause(); 211 }); 212 213 anchor.parent().removeClass("mega-toggle-on").triggerHandler("close_panel"); 214 plugin.addAnimatingClass(anchor.parent()); 215 }; 216 217 plugin.calculateDynamicSubmenuWidths = function(anchor) { 218 // apply dynamic width and sub menu position (only to top level mega menus) 219 if (anchor.parent().hasClass("mega-menu-megamenu") && anchor.parent().parent().hasClass("max-mega-menu") && plugin.settings.panel_width ) { 220 if (plugin.isDesktopView()) { 221 var submenu_offset = $menu.offset(); 222 var target_offset = $(plugin.settings.panel_width).offset(); 223 224 if ( plugin.settings.panel_width == '100vw' ) { 225 target_offset = $('body').offset(); 226 227 anchor.siblings(".mega-sub-menu").css({ 228 left: (target_offset.left - submenu_offset.left) + "px" 229 }); 230 } else if ( $(plugin.settings.panel_width).length > 0 ) { 231 anchor.siblings(".mega-sub-menu").css({ 232 width: $(plugin.settings.panel_width).outerWidth(), 233 left: (target_offset.left - submenu_offset.left) + "px" 234 }); 235 } 236 } else { 237 anchor.siblings(".mega-sub-menu").css({ 238 width: "", 239 left: "" 240 }); 241 } 242 } 243 244 // apply inner width to sub menu by adding padding to the left and right of the mega menu 245 if (anchor.parent().hasClass("mega-menu-megamenu") && anchor.parent().parent().hasClass("max-mega-menu") && plugin.settings.panel_inner_width && $(plugin.settings.panel_inner_width).length > 0) { 246 var target_width = 0; 247 248 if ($(plugin.settings.panel_inner_width).length) { 249 // jQuery selector 250 target_width = parseInt($(plugin.settings.panel_inner_width).width(), 10); 251 } else { 252 // we"re using a pixel width 253 target_width = parseInt(plugin.settings.panel_inner_width, 10); 254 } 255 256 anchor.siblings(".mega-sub-menu").css({ 257 "paddingLeft": "", 258 "paddingRight": "" 259 }); 260 261 var submenu_width = parseInt(anchor.siblings(".mega-sub-menu").innerWidth(), 10); 262 263 if (plugin.isDesktopView() && target_width > 0 && target_width < submenu_width) { 264 anchor.siblings(".mega-sub-menu").css({ 265 "paddingLeft": (submenu_width - target_width) / 2 + "px", 266 "paddingRight": (submenu_width - target_width) / 2 + "px" 267 }); 268 } 269 } 270 }; 271 272 plugin.bindClickEvents = function() { 273
274 if ( $wrap.data('has-click-events') === true ) { 275 return; 276 } 277 278 $wrap.data('has-click-events', true); 279 280 var dragging = false; 281 282 $(document).on({ 283 "touchmove": function(e) { dragging = true; }, 284 "touchstart": function(e) { dragging = false; } 285 }); 286 287 $(document).on("click touchend", function(e) { // hide menu when clicked away from 288 if (!dragging && plugin.settings.document_click === "collapse" && ! $(e.target).closest(".mega-menu-wrap").length ) { 289 plugin.hideAllPanels(); 290 plugin.hideMobileMenu(); 291 } 292 dragging = false; 293 }); 294 295 var clickable_parents = $("> a.mega-menu-link", items_with_submenus).add(collapse_children_parents); 296 297 clickable_parents.on("touchend.megamenu", function(e) { 298 if (plugin.settings.event === "hover_intent") { 299 plugin.unbindHoverIntentEvents(); 300 } 301 302 if (plugin.settings.event === "hover") { 303 plugin.unbindHoverEvents(); 304 } 305 }); 306 307 clickable_parents.on("click.megamenu", function(e) { 308 if ( $(e.target).hasClass('mega-indicator') ) { 309 return; 310 } 311 312 if (plugin.isDesktopView() && $(this).parent().hasClass("mega-toggle-on") && $(this).closest("ul.mega-sub-menu").parent().hasClass("mega-menu-tabbed") ) { 313 if (plugin.settings.second_click === "go") { 314 return; 315 } else { 316 e.preventDefault(); 317 return; 318 } 319 } 320 if (dragging) { 321 return; 322 } 323 if (plugin.isMobileView() && $(this).parent().hasClass("mega-hide-sub-menu-on-mobile")) { 324 return; // allow all clicks on parent items when sub menu is hidden on mobile 325 } 326 if ((plugin.settings.second_click === "go" || $(this).parent().hasClass("mega-click-click-go")) && $(this).attr("href") !== undefined) { // check for second click 327 if (!$(this).parent().hasClass("mega-toggle-on")) { 328 e.preventDefault(); 329 plugin.showPanel($(this)); 330 } 331 } else { 332 e.preventDefault(); 333 334 if ($(this).parent().hasClass("mega-toggle-on")) { 335 plugin.hidePanel($(this), false); 336 } else { 337 plugin.showPanel($(this)); 338 } 339 } 340 }); 341 342 if ( plugin.settings.second_click === "disabled" ) { 343 clickable_parents.off("click.megamenu"); 344 } 345 346 $(".mega-close-after-click:not(.mega-menu-item-has-children) > a.mega-menu-link", $menu).on("click", function() { 347 plugin.hideAllPanels(); 348 plugin.hideMobileMenu(); 349 }); 350 351 $("button.mega-close", $wrap).on("click", function(e) { 352 plugin.hideMobileMenu(); 353 }); 354 }; 355 356 plugin.bindHoverEvents = function() { 357 items_with_submenus.on({ 358 "mouseenter.megamenu" : function() { 359 plugin.unbindClickEvents(); 360 if (! $(this).hasClass("mega-toggle-on")) { 361 plugin.showPanel($(this).children("a.mega-menu-link")); 362 } 363 }, 364 "mouseleave.megamenu" : function() { 365 if ($(this).hasClass("mega-toggle-on") && ! $(this).hasClass("mega-disable-collapse") && ! $(this).parent().parent().hasClass("mega-menu-tabbed")) { 366 plugin.hidePanel($(this).children("a.mega-menu-link"), false); 367 } 368 } 369 }); 370 }; 371 372 plugin.bindHoverIntentEvents = function() { 373 items_with_submenus.hoverIntent({ 374 over: function () { 375 plugin.unbindClickEvents(); 376 if (! $(this).hasClass("mega-toggle-on")) { 377 plugin.showPanel($(this).children("a.mega-menu-link")); 378 } 379 }, 380 out: function () { 381 if ($(this).hasClass("mega-toggle-on") && ! $(this).hasClass("mega-disable-collapse") && ! $(this).parent().parent().hasClass("mega-menu-tabbed")) { 382 plugin.hidePanel($(this).children("a.mega-menu-link"), false); 383 } 384 }, 385 timeout: plugin.settings.hover_intent_timeout, 386 interval: plugin.settings.hover_intent_interval 387 }); 388 }; 389 390 plugin.isMobileOffCanvas = function() { 391 return plugin.settings.effect_mobile === 'slide_left' || plugin.settings.effect_mobile === 'slide_right';
392 } 393 394 plugin.bindKeyboardEvents = function() { 395 const tab_key = 9; 396 const escape_key = 27; 397 const enter_key = 13; 398 const left_arrow_key = 37; 399 const up_arrow_key = 38; 400 const right_arrow_key = 39; 401 const down_arrow_key = 40; 402 const space_key = 32; 403 const $firstFocusable = $menu.find("a.mega-menu-link").first(); 404 const $lastFocusable = $wrap.find("button.mega-close").first(); 405 406 var isMobileOffCanvasHorizontal = function() { 407 return plugin.isMobileOffCanvas() && plugin.settings.mobile_direction === 'horizontal'; 408 } 409 410 var shouldTrapFocusInCurrentSubMenu = function() { 411 return isMobileOffCanvasHorizontal() && ( keyCode === up_arrow_key || keyCode === down_arrow_key || keyCode === tab_key ); 412 } 413 414 $lastFocusable.on('keydown.megamenu', function(e) { 415 var keyCode = e.keyCode || e.which; 416 417 if ( plugin.isMobileView() && plugin.isMobileOffCanvas() && keyCode === tab_key && ! e.shiftKey ) { 418 e.preventDefault(); 419 $firstFocusable.trigger('focus'); 420 } 421 }); 422 423 $firstFocusable.on('keydown.megamenu', function(e) { 424 var keyCode = e.keyCode || e.which; 425 426 if ( plugin.isMobileView() && plugin.isMobileOffCanvas() && keyCode === tab_key && e.shiftKey) { 427 e.preventDefault(); 428 $lastFocusable.trigger('focus'); 429 } 430 }); 431 432 $wrap.on("keyup.megamenu", ".max-mega-menu, .mega-menu-toggle", function(e) { 433 var keyCode = e.keyCode || e.which; 434 var active_link = $(e.target); 435 436 if (keyCode === tab_key) { 437 $wrap.addClass("mega-keyboard-navigation"); 438 plugin.bindClickEvents(); // Windows Narrator ignores the Enter keypress, so ensure click events are available when pressing tab 439 440 if ( plugin.isDesktopView() && keyCode === tab_key && active_link.is(".mega-menu-link") && active_link.parent().parent().hasClass('max-mega-menu') ) { 441 plugin.hideAllPanels(); 442 } 443 } 444 }); 445 446 $wrap.on("keydown.megamenu", "a.mega-menu-link, .mega-indicator, .mega-menu-toggle-block, .mega-menu-toggle-animated-block button, button.mega-close", function(e) { 447 448 if ( ! $wrap.hasClass("mega-keyboard-navigation") ) { 449 return; 450 } 451 452 var keyCode = e.keyCode || e.which; 453 var active_link = $(e.target); 454 455 if ( keyCode === space_key && active_link.is(".mega-menu-link") ) { 456 e.preventDefault(); 457 458 // pressing space on a parent item will always toggle the sub menu 459 if ( active_link.parent().is(items_with_submenus) ) { 460 if ( active_link.parent().hasClass("mega-toggle-on") && ! active_link.closest("ul.mega-sub-menu").parent().hasClass("mega-menu-tabbed") ) { 461 plugin.hidePanel(active_link); 462 } else { 463 plugin.showPanel(active_link); 464 } 465 } 466 } 467 468 if ( keyCode === space_key && active_link.is("mega-indicator") ) { 469 e.preventDefault(); 470 471 if ( active_link.parent().parent().hasClass("mega-toggle-on") && ! active_link.closest("ul.mega-sub-menu").parent().hasClass("mega-menu-tabbed") ) { 472 plugin.hidePanel(active_link.parent()); 473 } else { 474 plugin.showPanel(active_link.parent()); 475 } 476 } 477 478 if ( keyCode === escape_key ) { 479 var submenu_open = $(".mega-toggle-on", $menu).length !== 0; 480 481 if ( submenu_open ) { 482 var focused_menu_item = $menu.find(":focus"); 483
484 if ( focused_menu_item.closest('.mega-menu-flyout.mega-toggle-on').length !== 0 ) { 485 var nearest_parent_of_focused_item_li = focused_menu_item.closest('.mega-toggle-on'); 486 var nearest_parent_of_focused_item_a = $("> a.mega-menu-link", nearest_parent_of_focused_item_li); 487 plugin.hidePanel(nearest_parent_of_focused_item_a); 488 nearest_parent_of_focused_item_a.trigger('focus'); 489 } 490 491 if ( focused_menu_item.closest('.mega-menu-megamenu.mega-toggle-on').length !== 0 ) { 492 var nearest_parent_of_focused_item_li = focused_menu_item.closest('.mega-menu-megamenu.mega-toggle-on'); 493 var nearest_parent_of_focused_item_a = $("> a.mega-menu-link", nearest_parent_of_focused_item_li); 494 plugin.hidePanel(nearest_parent_of_focused_item_a); 495 nearest_parent_of_focused_item_a.trigger('focus'); 496 } 497 } 498 499 if ( plugin.isMobileView() && ! submenu_open ) { 500 plugin.hideMobileMenu(); 501 } 502 } 503 504 if ( keyCode === space_key || keyCode === enter_key ) { 505 if ( active_link.is(".mega-menu-toggle-block button, .mega-menu-toggle-animated-block button") ) { 506 e.preventDefault(); 507 508 if ( $toggle_bar.hasClass("mega-menu-open") ) { 509 plugin.hideMobileMenu(); 510 } else { 511 plugin.showMobileMenu(); 512 513 html_body_class_timeout = setTimeout(function() { 514 $menu.find("a.mega-menu-link").first().trigger('focus'); 515 }, plugin.settings.effect_speed_mobile); 516 } 517 } 518 } 519 520 if ( keyCode === enter_key ) { // ignored by windows narrator 521 522 // pressing enter on an arrow will toggle the sub menu 523 if ( active_link.is(".mega-indicator") ) { 524 if ( active_link.closest("li.mega-menu-item").hasClass("mega-toggle-on") && ! active_link.closest("ul.mega-sub-menu").parent().hasClass("mega-menu-tabbed") ) { 525 plugin.hidePanel(active_link.parent()); 526 } else { 527 plugin.showPanel(active_link.parent()); 528 } 529 530 return; 531 } 532 // pressing enter on a parent link 533 if ( active_link.parent().is(items_with_submenus) ) { 534 535 // when clicking on the parent of a hidden submenu, follow the link 536 if ( plugin.isMobileView() && active_link.parent().is(".mega-hide-sub-menu-on-mobile") ) { 537 return; 538 } 539 540 // pressing enter on a parent item without a link will toggle the sub menu 541 if ( active_link.is("[href]") === false ) { 542 if ( active_link.parent().hasClass("mega-toggle-on") && ! active_link.closest("ul.mega-sub-menu").parent().hasClass("mega-menu-tabbed") ) { 543 plugin.hidePanel(active_link); 544 } else { 545 plugin.showPanel(active_link); 546 } 547 548 return; 549 } 550 551 // pressing enter on a parent item will first open the sub menu, then follow the link 552 if ( active_link.parent().hasClass("mega-toggle-on") && ! active_link.closest("ul.mega-sub-menu").parent().hasClass("mega-menu-tabbed") ) { 553 return; 554 } else { 555 e.preventDefault(); 556 plugin.showPanel(active_link); 557 } 558 } 559 } 560 561 if ( shouldTrapFocusInCurrentSubMenu() ) { 562 var focused_item = $(":focus", $menu); 563 564 // if the menu doesn't have focus, focus the first menu item
565 if ( focused_item.length === 0) { 566 e.preventDefault(); 567 $("> li.mega-menu-item:visible", $menu).find("> a.mega-menu-link, .mega-search span[role=button]").first().trigger('focus'); 568 return; 569 } 570 571 // try to find the next item at the same level 572 var next_item_to_focus = focused_item.parent().nextAll("li.mega-menu-item:visible").find("> a.mega-menu-link, .mega-search span[role=button]").first(); 573 574 // can't find another item in the same level, attempt to skip back to the top 575 if ( next_item_to_focus.length === 0 && focused_item.closest(".mega-menu-megamenu").length !== 0 ) { 576 // are we inside a megamenu? find the 'back' button and focus on that 577 var all_li_parents = focused_item.parentsUntil(".mega-menu-megamenu"); 578 579 if ( focused_item.is(all_li_parents.find("a.mega-menu-link").last()) ) { 580 next_item_to_focus = all_li_parents.find(".mega-back-button:visible > a.mega-menu-link").first(); 581 } 582 } 583 584 // skip back to the top of non-megamenu menus 585 if ( next_item_to_focus.length === 0 ) { 586 next_item_to_focus = focused_item.parent().prevAll("li.mega-menu-item:visible").find("> a.mega-menu-link, .mega-search span[role=button]").first(); 587 } 588 589 if ( next_item_to_focus.length !== 0 ) { 590 e.preventDefault(); 591 next_item_to_focus.trigger('focus'); 592 } 593 594 } 595 596 var shouldGoToNextTopLevelItem = function() { 597 return ( ( keyCode === right_arrow_key && plugin.isDesktopView() ) || ( keyCode === down_arrow_key && plugin.isMobileView() ) ) && $menu.hasClass("mega-menu-horizontal"); 598 } 599 600 var shouldGoToPreviousTopLevelItem = function() { 601 return ( ( keyCode === left_arrow_key && plugin.isDesktopView() ) || ( keyCode === up_arrow_key && plugin.isMobileView() ) ) && $menu.hasClass("mega-menu-horizontal"); 602 } 603 604 if ( shouldGoToNextTopLevelItem() ) { 605 e.preventDefault(); 606 607 var next_top_level_item = $("> .mega-toggle-on", $menu).nextAll("li.mega-menu-item:visible").find("> a.mega-menu-link, .mega-search span[role=button]").first(); 608 609 if (next_top_level_item.length === 0) { 610 next_top_level_item = $(":focus", $menu).parent().nextAll("li.mega-menu-item:visible").find("> a.mega-menu-link, .mega-search span[role=button]").first(); 611 } 612 613 if (next_top_level_item.length === 0) { 614 next_top_level_item = $(":focus", $menu).parent().parent().parent().nextAll("li.mega-menu-item:visible").find("> a.mega-menu-link, .mega-search span[role=button]").first(); 615 } 616 617 plugin.hideAllPanels(); 618 next_top_level_item.trigger('focus'); 619 } 620 621 if ( shouldGoToPreviousTopLevelItem() ) { 622 e.preventDefault(); 623 624 var prev_top_level_item = $("> .mega-toggle-on", $menu).prevAll("li.mega-menu-item:visible").find("> a.mega-menu-link, .mega-search span[role=button]").last(); 625 626 if (prev_top_level_item.length === 0) { 627 prev_top_level_item = $(":focus", $menu).parent().prevAll("li.mega-menu-item:visible").find("> a.mega-menu-link, .mega-search span[role=button]").last(); 628 } 629 630 if (prev_top_level_item.length === 0) { 631 prev_top_level_item = $(":focus", $menu).parent().parent().parent().prevAll("li.mega-menu-item:visible").find("> a.mega-menu-link, .mega-search span[role=button]").last(); 632 } 633 634 plugin.hideAllPanels(); 635 prev_top_level_item.trigger('focus'); 636 } 637 }); 638 639 $wrap.on("focusout.megamenu", function(e) { 640 if ( $wrap.hasClass("mega-keyboard-navigation") ) { 641 setTimeout(function() { 642 var menu_has_focus = $wrap.find(":focus").length > 0; 643 if (! menu_has_focus) { 644 $wrap.removeClass("mega-keyboard-navigation"); 645 plugin.hideAllPanels(); 646 plugin.hideMobileMenu(); 647 } 648 }, 10); 649 } 650 }); 651 }; 652 653 plugin.unbindAllEvents = function() { 654 $("ul.mega-sub-menu, li.mega-menu-item, li.mega-menu-row, li.mega-menu-column, a.mega-menu-link, .mega-indicator", $menu).off().unbind(); 655 }; 656 657 plugin.unbindClickEvents = function() { 658 if ( $wrap.hasClass('mega-keyboard-navigation') ) { 659 return; 660 } 661 662 // collapsable parents always have a click event 663 $("> a.mega-menu-link", items_with_submenus).not(collapse_children_parents).off("click.megamenu touchend.megamenu"); 664
665 $wrap.data('has-click-events', false); 666 }; 667 668 plugin.unbindHoverEvents = function() { 669 items_with_submenus.off("mouseenter.megamenu mouseleave.megamenu"); 670 }; 671 672 plugin.unbindHoverIntentEvents = function() { 673 items_with_submenus.off("mouseenter mouseleave").removeProp("hoverIntent_t").removeProp("hoverIntent_s"); // hoverintent does not allow namespaced events 674 }; 675 676 plugin.unbindKeyboardEvents = function() { 677 $wrap.off("keyup.megamenu keydown.megamenu focusout.megamenu"); 678 }; 679 680 plugin.unbindMegaMenuEvents = function() { 681 if (plugin.settings.event === "hover_intent") { 682 plugin.unbindHoverIntentEvents(); 683 } 684 685 if (plugin.settings.event === "hover") { 686 plugin.unbindHoverEvents(); 687 } 688 689 plugin.unbindClickEvents(); 690 plugin.unbindKeyboardEvents(); 691 }; 692 693 plugin.bindMegaMenuEvents = function() { 694 plugin.unbindMegaMenuEvents(); 695 696 if (plugin.isDesktopView() && plugin.settings.event === "hover_intent") { 697 plugin.bindHoverIntentEvents(); 698 } 699 700 if (plugin.isDesktopView() && plugin.settings.event === "hover") { 701 plugin.bindHoverEvents(); 702 } 703 704 plugin.bindClickEvents(); // always bind click events for touch screen devices 705 plugin.bindKeyboardEvents(); 706 }; 707 708 plugin.checkWidth = function() { 709 if ( plugin.isMobileView() && $menu.data("view") === "desktop" ) { 710 plugin.switchToMobile(); 711 } 712 713 if ( plugin.isDesktopView() && $menu.data("view") === "mobile" ) { 714 plugin.switchToDesktop(); 715 } 716 717 plugin.calculateDynamicSubmenuWidths($("> li.mega-menu-megamenu > a.mega-menu-link", $menu)); 718 }; 719 720 plugin.reverseRightAlignedItems = function() { 721 if ( ! $("body").hasClass("rtl") && $menu.hasClass("mega-menu-horizontal") && $menu.css("display") !== 'flex' ) { 722 $menu.append($menu.children("li.mega-item-align-right").get().reverse()); 723 } 724 }; 725 726 plugin.addClearClassesToMobileItems = function() { 727 $(".mega-menu-row", $menu).each(function() { 728 $("> .mega-sub-menu > .mega-menu-column:not(.mega-hide-on-mobile)", $(this)).filter(":even").addClass("mega-menu-clear"); // :even is 0 based 729 }); 730 }; 731 732 plugin.initDesktop = function() { 733 $menu.data("view", "desktop"); 734 plugin.bindMegaMenuEvents(); 735 plugin.initIndicators(); 736 }; 737 738 plugin.initMobile = function() { 739 plugin.switchToMobile(); 740 }; 741 742 plugin.switchToDesktop = function() { 743 $menu.data("view", "desktop"); 744 plugin.bindMegaMenuEvents(); 745 plugin.reverseRightAlignedItems(); 746 plugin.hideAllPanels(); 747 plugin.hideMobileMenu(true); 748 $menu.removeAttr('role'); 749 $menu.removeAttr('aria-modal'); 750 $menu.removeAttr('aria-hidden'); 751 }; 752 753 plugin.switchToMobile = function() { 754 $menu.data("view", "mobile"); 755 756 if (plugin.isMobileOffCanvas() && $toggle_bar.is(":visible") ) { 757 $menu.attr('role', 'dialog'); 758 $menu.attr('aria-modal', 'true'); 759 $menu.attr('aria-hidden', 'true'); 760 } 761 762 plugin.bindMegaMenuEvents(); 763 plugin.initIndicators(); 764 plugin.reverseRightAlignedItems(); 765 plugin.addClearClassesToMobileItems(); 766 plugin.hideAllPanels(); 767 plugin.expandMobileSubMenus(); 768 769 }; 770 771 plugin.initToggleBar = function() { 772 $toggle_bar.on("click", function(e) { 773 if ( $(e.target).is(".mega-menu-toggle, .mega-menu-toggle-custom-block *, .mega-menu-toggle-block, .mega-menu-toggle-animated-block, .mega-menu-toggle-animated-block *, .mega-toggle-blocks-left, .mega-toggle-blocks-center, .mega-toggle-blocks-right, .mega-toggle-label, .mega-toggle-label span") ) { 774 e.preventDefault(); 775 776 if ($(this).hasClass("mega-menu-open")) { 777 plugin.hideMobileMenu(); 778 } else { 779 plugin.showMobileMenu(); 780 } 781 } 782 }); 783 }; 784 785 plugin.initIndicators = function() { 786 $menu.off('click.megamenu', '.mega-indicator'); 787 788 $menu.on('click.megamenu', '.mega-indicator', function(e) { 789 e.preventDefault(); 790 e.stopPropagation(); 791 792 if ( $(this).closest(".mega-menu-item").hasClass("mega-toggle-on") ) { 793 if ( ! $(this).closest("ul.mega-sub-menu").parent().hasClass("mega-menu-tabbed") || plugin.isMobileView() ) { 794 plugin.hidePanel($(this).parent(), false); 795 } 796 } else { 797 plugin.showPanel($(this).parent(), false); 798 } 799 }); 800 }; 801 802 plugin.hideMobileMenu = function(force) { 803 force = force || false; 804 805 if ( ! $toggle_bar.is(":visible") && ! force ) { 806 return; 807 } 808 809 $menu.attr("aria-hidden", "true"); 810 811 html_body_class_timeout = setTimeout(function() { 812 $("body").removeClass($menu.attr("id") + "-mobile-open"); 813 $("html").removeClass($menu.attr("id") + "-off-canvas-open"); 814 }, plugin.settings.effect_speed_mobile); 815 816 if ($wrap.hasClass("mega-keyboard-navigation")) { 817 $(".mega-menu-toggle-block button, button.mega-toggle-animated", $toggle_bar).first().trigger('focus'); 818 } 819 820 $(".mega-toggle-label, .mega-toggle-animated", $toggle_bar).attr("aria-expanded", "false"); 821 822 if (plugin.settings.effect_mobile === "slide" && ! force ) { 823 $menu.animate({"height":"hide"}, plugin.settings.effect_speed_mobile, function() { 824 $menu.css({ 825 width: "", 826 left: "", 827 display: "" 828 }); 829 830 $toggle_bar.removeClass("mega-menu-open"); 831 }); 832 } else { 833 $menu.css({ 834 width: "", 835 left: "", 836 display: "" 837 }); 838 839 $toggle_bar.removeClass("mega-menu-open"); 840 } 841 842 843 $menu.triggerHandler("mmm:hideMobileMenu"); 844 }; 845 846 plugin.showMobileMenu = function() { 847 if ( ! $toggle_bar.is(":visible") ) { 848 return; 849 } 850 851 clearTimeout(html_body_class_timeout); 852 853 $("body").addClass($menu.attr("id") + "-mobile-open"); 854 855 plugin.expandMobileSubMenus(); 856 857 if ( plugin.isMobileOffCanvas() ) { 858 $("html").addClass($menu.attr("id") + "-off-canvas-open"); 859 } 860 861 if (plugin.settings.effect_mobile === "slide") { 862 $menu.animate({"height":"show"}, plugin.settings.effect_speed_mobile, function() { 863 $(this).css("display", ""); 864 }); 865 } 866 867 $(".mega-toggle-label, .mega-toggle-animated", $toggle_bar).attr("aria-expanded", "true"); 868 869 $toggle_bar.addClass("mega-menu-open"); 870 871 plugin.toggleBarForceWidth(); 872 873 $menu.attr("aria-hidden", "false"); 874 $menu.triggerHandler("mmm:showMobileMenu"); 875 }; 876 877 plugin.toggleBarForceWidth = function() { 878 if ($(plugin.settings.mobile_force_width).length && ( plugin.settings.effect_mobile === "slide" || plugin.settings.effect_mobile === "disabled" ) ) { 879 var submenu_offset = $toggle_bar.offset(); 880 var target_offset = $(plugin.settings.mobile_force_width).offset(); 881 882 $menu.css({ 883 width: $(plugin.settings.mobile_force_width).outerWidth(), 884 left: (target_offset.left - submenu_offset.left) + "px" 885 }); 886 } 887 }; 888 889 plugin.doConsoleChecks = function() { 890 if (plugin.settings.mobile_force_width != "false" && ! $(plugin.settings.mobile_force_width).length && ( plugin.settings.effect_mobile === "slide" || plugin.settings.effect_mobile === "disabled" ) ) { 891 console.warn('Max Mega Menu #' + $wrap.attr('id') + ': Mobile Force Width element (' + plugin.settings.mobile_force_width + ') not found'); 892 } 893 894 const cssWidthRegex = /^((\d+(\.\d+)?(px|%|em|rem|vw|vh|ch|ex|cm|mm|in|pt|pc))|auto)$/i; 895 896 if (plugin.settings.panel_width !== undefined && ! cssWidthRegex.test(plugin.settings.panel_width) && ! $(plugin.settings.panel_width).length ) { 897 console.warn('Max Mega Menu #' + $wrap.attr('id') + ': Panel Width (Outer) element (' + plugin.settings.panel_width + ') not found'); 898 } 899 900 if (plugin.settings.panel_inner_width !== undefined && ! cssWidthRegex.test(plugin.settings.panel_inner_width) && ! $(plugin.settings.panel_inner_width).length ) { 901 console.warn('Max Mega Menu #' + $wrap.attr('id') + ': Panel Width (Inner) element (' + plugin.settings.panel_inner_width + ') not found'); 902 } 903 } 904 905 plugin.init = function() { 906 $menu.triggerHandler("before_mega_menu_init"); 907 plugin.settings = $.extend({}, defaults, options); 908 909 if (window.console) { 910 plugin.doConsoleChecks(); 911 } 912 913 $menu.removeClass("mega-no-js"); 914 915 plugin.initToggleBar(); 916 917 if (plugin.settings.unbind_events === "true") { 918 plugin.unbindAllEvents(); 919 } 920 921 $(window).on("load", function() { 922 plugin.calculateDynamicSubmenuWidths($("> li.mega-menu-megamenu > a.mega-menu-link", $menu)); 923 }); 924 925 if ( plugin.isDesktopView() ) { 926 plugin.initDesktop(); 927 } else { 928 plugin.initMobile(); 929 } 930 931 $(window).on("resize", function() { 932 plugin.checkWidth(); 933 }); 934 935 $menu.triggerHandler("after_mega_menu_init"); 936 }; 937 938 plugin.init(); 939 }; 940 941 $.fn.maxmegamenu = function(options) { 942 return this.each(function() { 943 if (undefined === $(this).data("maxmegamenu")) { 944 var plugin = new $.maxmegamenu(this, options); 945 $(this).data("maxmegamenu", plugin); 946 } 947 }); 948 }; 949 950 $(function() { 951 $(".max-mega-menu").maxmegamenu(); 952 }); 953}( jQuery ));
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.