maxmegamenu.js 20 KB

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