1/*jslint browser: true, white: true, this: 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 12 var defaults = { 13 event: $menu.attr("data-event"), 14 effect: $menu.attr("data-effect"), 15 effect_speed: parseInt($menu.attr("data-effect-speed")), 16 panel_width: $menu.attr("data-panel-width"), 17 panel_inner_width: $menu.attr("data-panel-inner-width"), 18 second_click: $menu.attr("data-second-click"), 19 vertical_behaviour: $menu.attr("data-vertical-behaviour"), 20 document_click: $menu.attr("data-document-click"), 21 breakpoint: $menu.attr("data-breakpoint"), 22 unbind_events: $menu.attr("data-unbind") 23 }; 24 25 plugin.settings = {}; 26 27 var items_with_submenus = $("li.mega-menu-megamenu.mega-menu-item-has-children," + 28 "li.mega-menu-flyout.mega-menu-item-has-children," + 29 "li.mega-menu-tabbed > ul.mega-sub-menu > li.mega-menu-item-has-children," + 30 "li.mega-menu-flyout li.mega-menu-item-has-children", menu); 31 32 plugin.hidePanel = function(anchor, immediate) { 33 if (!immediate && plugin.settings.effect == 'slide') { 34 anchor.siblings(".mega-sub-menu").animate({"height":"hide"}, plugin.settings.effect_speed, function() { 35 anchor.siblings(".mega-sub-menu").css("display", ""); 36 anchor.parent().removeClass("mega-toggle-on").triggerHandler("close_panel"); 37 }); 38 return; 39 } 40 41 if (immediate) { 42 anchor.siblings(".mega-sub-menu").css("display", "none").delay(plugin.settings.effect_speed).queue(function(){ 43 $(this).css("display", "").dequeue(); 44 }); 45 } 46 47 // pause video widget videos 48 anchor.siblings(".mega-sub-menu").find('.widget_media_video video').each(function() { 49 this.player.pause(); 50 }); 51 52 anchor.parent().removeClass("mega-toggle-on").triggerHandler("close_panel"); 53 plugin.addAnimatingClass(anchor.parent()); 54 }; 55 56 plugin.addAnimatingClass = function(element) { 57 if (plugin.settings.effect === "disabled") { 58 return; 59 } 60 61 $(".mega-animating").removeClass("mega-animating"); 62 63 var timeout = plugin.settings.effect_speed + parseInt(megamenu.timeout, 10); 64 65 element.addClass("mega-animating"); 66 67 setTimeout(function() { 68 element.removeClass("mega-animating"); 69 }, timeout ); 70 }; 71 72 plugin.hideAllPanels = function() { 73 $(".mega-toggle-on > a.mega-menu-link", $menu).each(function() { 74 plugin.hidePanel($(this), false); 75 }); 76 }; 77 78 plugin.hideSiblingPanels = function(anchor, immediate) { 79 anchor.parent().parent().find(".mega-toggle-on").children("a.mega-menu-link").each(function() { // all open children of open siblings 80 plugin.hidePanel($(this), immediate); 81 }); 82 }; 83 84 plugin.isDesktopView = function() { 85 return Math.max(window.outerWidth, $(window).width()) > plugin.settings.breakpoint; // account for scrollbars 86 }; 87 88 plugin.isMobileView = function() { 89 return !plugin.isDesktopView(); 90 }; 91 92 plugin.showPanel = function(anchor) { 93 $(".mega-animating").removeClass("mega-animating"); 94 95 if (plugin.isMobileView() && anchor.parent().hasClass("mega-hide-sub-menu-on-mobile")) { 96 return; 97 } 98 99 if (plugin.isDesktopView() && ($menu.hasClass("mega-menu-horizontal") || $menu.hasClass("mega-menu-vertical"))) { 100 plugin.hideSiblingPanels(anchor, true); 101 } 102 103 if ((plugin.isMobileView() && $menu.hasClass("mega-keyboard-navigation")) || plugin.settings.vertical_behaviour === "accordion") { 104 plugin.hideSiblingPanels(anchor, false); 105 } 106 107 plugin.calculateDynamicSubmenuWidths(anchor); 108 109 // apply jQuery transition (only if the effect is set to "slide", other transitions are CSS based) 110 if ( plugin.settings.effect == "slide" ) { 111 anchor.siblings(".mega-sub-menu").css("display", "none").animate({'height':'show'}, plugin.settings.effect_speed); 112 } 113 114 anchor.parent().addClass("mega-toggle-on").triggerHandler("open_panel"); 115 }; 116 117 plugin.calculateDynamicSubmenuWidths = function(anchor) { 118 // apply dynamic width and sub menu position 119 if (anchor.parent().hasClass("mega-menu-megamenu") && plugin.settings.panel_width && $(plugin.settings.panel_width).length > 0) { 120 if (plugin.isDesktopView()) { 121 var submenu_offset = $menu.offset(); 122 var target_offset = $(plugin.settings.panel_width).offset(); 123 124 anchor.siblings(".mega-sub-menu").css({ 125 width: $(plugin.settings.panel_width).outerWidth(), 126 left: (target_offset.left - submenu_offset.left) + "px" 127 }); 128 } else { 129 anchor.siblings(".mega-sub-menu").css({ 130 width: "", 131 left: "" 132 }); 133 } 134 } 135 136 // apply inner width to sub menu by adding padding to the left and right of the mega menu 137 if (anchor.parent().hasClass("mega-menu-megamenu") && plugin.settings.panel_inner_width && $(plugin.settings.panel_inner_width).length > 0) { 138 var target_width = 0; 139 140 if ($(plugin.settings.panel_inner_width).length) { 141 // jQuery selector 142 target_width = parseInt($(plugin.settings.panel_inner_width).width(), 10); 143 } else { 144 // we"re using a pixel width 145 target_width = parseInt(plugin.settings.panel_inner_width, 10); 146 } 147 148 var submenu_width = parseInt(anchor.siblings(".mega-sub-menu").innerWidth(), 10); 149 150 if (plugin.isDesktopView() && target_width > 0 && target_width < submenu_width) { 151 anchor.siblings(".mega-sub-menu").css({ 152 "paddingLeft": (submenu_width - target_width) / 2 + "px", 153 "paddingRight": (submenu_width - target_width) / 2 + "px" 154 }); 155 } else { 156 anchor.siblings(".mega-sub-menu").css({ 157 "paddingLeft": "", 158 "paddingRight": "" 159 }); 160 } 161 } 162 } 163 164 var bindClickEvents = function() { 165 var dragging = false;
166 167 $(document).on({ 168 "touchmove": function(e) { dragging = true; }, 169 "touchstart": function(e) { dragging = false; } 170 }); 171 172 $(document).on("click touchend", function(e) { // hide menu when clicked away from 173 if (!dragging && plugin.settings.document_click === "collapse" && ! $(e.target).closest(".mega-menu li").length ) { 174 plugin.hideAllPanels(); 175 } 176 dragging = false; 177 }); 178 179 $("> a.mega-menu-link", items_with_submenus).on("click.megamenu touchend.megamenu", function(e) { 180 if (plugin.isDesktopView() && $(this).parent().hasClass("mega-toggle-on") && $(this).parent().parent().parent().hasClass("mega-menu-tabbed")) { 181 e.preventDefault(); 182 return; 183 } 184 if (dragging) { 185 return; 186 } 187 if (plugin.isMobileView() && $(this).parent().hasClass("mega-hide-sub-menu-on-mobile")) { 188 return; // allow all clicks on parent items when sub menu is hidden on mobile 189 } 190 if (plugin.settings.second_click === "go" || $(this).parent().hasClass("mega-click-click-go")) { // check for second click 191 if (!$(this).parent().hasClass("mega-toggle-on")) { 192 e.preventDefault(); 193 plugin.showPanel($(this)); 194 } 195 } else { 196 e.preventDefault(); 197 198 if ($(this).parent().hasClass("mega-toggle-on")) { 199 plugin.hidePanel($(this), false); 200 } else { 201 plugin.showPanel($(this)); 202 } 203 } 204 }); 205 }; 206 207 var bindHoverEvents = function() { 208 items_with_submenus.on({ 209 mouseenter: function() { 210 plugin.unbindClickEvents(); 211 if (! $(this).hasClass("mega-toggle-on")) { 212 plugin.showPanel($(this).children("a.mega-menu-link")); 213 } 214 }, 215 mouseleave: function() { 216 if ($(this).hasClass("mega-toggle-on") && ! $(this).parent().parent().hasClass("mega-menu-tabbed")) { 217 plugin.hidePanel($(this).children("a.mega-menu-link"), false); 218 } 219 } 220 }); 221 }; 222 223 var bindHoverIntentEvents = function() { 224 items_with_submenus.hoverIntent({ 225 over: function () { 226 plugin.unbindClickEvents(); 227 if (! $(this).hasClass("mega-toggle-on")) { 228 plugin.showPanel($(this).children("a.mega-menu-link")); 229 } 230 }, 231 out: function () { 232 if ($(this).hasClass("mega-toggle-on") && ! $(this).parent().parent().hasClass("mega-menu-tabbed")) { 233 plugin.hidePanel($(this).children("a.mega-menu-link"), false); 234 } 235 }, 236 timeout: megamenu.timeout, 237 interval: megamenu.interval 238 }); 239 }; 240 241 var bindKeyboardEvents = function() { 242 var tab_key = 9; 243 var escape_key = 27; 244 245 $("body").on("keyup", function(e) { 246 var keyCode = e.keyCode || e.which; 247 248 if (keyCode === escape_key) { 249 $menu.removeClass("mega-keyboard-navigation"); 250 plugin.hideAllPanels(); 251 } 252 253 if ( $menu.hasClass("mega-keyboard-navigation") && ! $(event.target).closest(".mega-menu li").length ) { 254 $menu.removeClass("mega-keyboard-navigation"); 255 plugin.hideAllPanels(); 256 } 257 }); 258 259 $menu.parent().on("keyup", function(e) { 260 var keyCode = e.keyCode || e.which; 261 var active_link = $(e.target); 262 263 if (keyCode === tab_key) { 264 $menu.addClass("mega-keyboard-navigation"); 265 266 if ( active_link.parent().is(items_with_submenus) ) { 267 plugin.showPanel(active_link); 268 } else { 269 plugin.hideSiblingPanels(active_link); 270 } 271 272 if ( active_link.hasClass("mega-menu-toggle") ) { 273 active_link.toggleClass("mega-menu-open"); 274 } 275 } 276 }); 277 }; 278 279 plugin.unbindAllEvents = function() { 280 $("ul.mega-sub-menu, li.mega-menu-item, a.mega-menu-link", menu).off().unbind(); 281 }; 282 283 plugin.unbindClickEvents = function() { 284 $("a.mega-menu-link", menu).off("click.megamenu touchend.megamenu"); 285 }; 286 287 plugin.bindMegaMenuEvents = function() { 288 if (plugin.isDesktopView() && plugin.settings.event === "hover_intent") { 289 bindHoverIntentEvents(); 290 } 291 292 if (plugin.isDesktopView() && plugin.settings.event === "hover") { 293 bindHoverEvents(); 294 } 295 296 bindClickEvents(); // always bind click events for touch screen devices 297 bindKeyboardEvents(); 298 }; 299 300 plugin.monitorView = function() { 301 if (plugin.isDesktopView()) { 302 $menu.data("view", "desktop"); 303 } else { 304 $menu.data("view", "mobile"); 305 plugin.switchToMobile(); 306 } 307 308 plugin.checkWidth(); 309 310 $(window).resize(function() { 311 plugin.checkWidth(); 312 }); 313 }; 314 315 plugin.checkWidth = function() { 316 if ( plugin.isMobileView() && $menu.data("view") === "desktop" ) { 317 $menu.data("view", "mobile"); 318 plugin.switchToMobile(); 319 } 320 321 if ( plugin.isDesktopView() && $menu.data("view") === "mobile" ) { 322 $menu.data("view", "desktop"); 323 plugin.switchToDesktop(); 324 } 325 326 if ( plugin.isDesktopView() ) { 327 plugin.calculateDynamicSubmenuWidths($("li.mega-menu-megamenu.mega-toggle-on > a.mega-menu-link", $menu)); 328 } 329 }; 330 331 plugin.reverseRightAlignedItems = function() { 332 if ( ! $('body').hasClass('rtl') ) { 333 $menu.append($menu.children("li.mega-item-align-right").get().reverse()); 334 } 335 }; 336 337 plugin.switchToMobile = function() { 338 plugin.unbindAllEvents(); 339 plugin.bindMegaMenuEvents(); 340 plugin.reverseRightAlignedItems(); 341 plugin.hideAllPanels(); 342 }; 343 344 plugin.switchToDesktop = function() { 345 plugin.unbindAllEvents(); 346 plugin.bindMegaMenuEvents(); 347 plugin.reverseRightAlignedItems(); 348 plugin.hideAllPanels(); 349 }; 350 351 plugin.init = function() { 352 $menu.triggerHandler("before_mega_menu_init"); 353 plugin.settings = $.extend({}, defaults, options); 354 $menu.removeClass("mega-no-js"); 355 356 // mobile menu 357 $menu.siblings(".mega-menu-toggle").on("click", function(e) { 358 if ( $(e.target).is(".mega-menu-toggle-block, .mega-menu-toggle") ) { 359 $(this).toggleClass("mega-menu-open"); 360 } 361 }); 362 363 if (plugin.settings.unbind_events == 'true') { 364 plugin.unbindAllEvents(); 365 } 366 367 plugin.bindMegaMenuEvents(); 368 plugin.monitorView(); 369 $menu.triggerHandler("after_mega_menu_init"); 370 }; 371 372 plugin.init(); 373 }; 374 375 $.fn.maxmegamenu = function(options) { 376 return this.each(function() { 377 if (undefined === $(this).data("maxmegamenu")) { 378 var plugin = new $.maxmegamenu(this, options); 379 $(this).data("maxmegamenu", plugin); 380 } 381 }); 382 }; 383 384 $(function() { 385 $(".mega-menu").maxmegamenu(); 386 }); 387})(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.