Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const Header = function($element) {
  2.     this.$element = $element;
  3.     this.menuParent = 'header-nav';
  4.     this.header = 'header-nav-menus'; // top level ul
  5.     this.navLink = 'header-nav-menu-link'; // top level li
  6.     this.navTray = 'header-nav-menu-link-sub'; // dropdown menu
  7.     this.close = 'header-nav-menu-close'; // close
  8.     this.menuToggle = 'header-nav-toggle'; // mobile menu toggle
  9.     this.menuToggleLine = 'header-nav-toggle-line'; // mobile menu toggle
  10.     this.back = 'header-nav-menu-back'; // mobile menu toggle
  11.     this.navTrayDescription = 'header-nav-menu-link-sub-text-description';
  12.  
  13.  
  14.     this.trayElements = {
  15.         trayText: 'header-nav-menu-link-sub-text', // dropdown text
  16.         trayDescription: 'header-nav-menu-link-sub-text-description', // dropdown description for mobile
  17.         trayIcon: 'header-nav-menu-link-sub-icon', // dropdown icon
  18.         trayLinks: 'header-nav-menu-link-sub-links', // dropdown links
  19.         trayClose: 'header-nav-menu-close'
  20.     };
  21.  
  22.     this.classNavTrayShow = this.navTray + '-show';
  23.     this.classMenuToggle = this.menuToggle + '-show';
  24.  
  25.     this.$headerLinks = this.$element.find('.' + this.navLink + ' > a');
  26.     this.$close = this.$element.find('.' + this.close);
  27.     this.$menuToggle = $('.header').find('.' + this.menuToggle);
  28.     this.$back = this.$element.find('.' + this.back + ' span');
  29.     this.$description = this.$element.find('.' + this.navTrayDescription);
  30.     this.$modal = $('.modal');
  31.  
  32.     this.isMenuOpen = false;
  33.     this.$activeTray = null;
  34.  
  35.     this.init();
  36. };
  37.  
  38. Header.prototype = {
  39.     init: function() {
  40.         const self = this;
  41.  
  42.         self.$headerLinks.on('click', function(e) {
  43.             // check if tray exists
  44.             if ($(this).parent().find('.' + self.navTray).length > 0) {
  45.                 e.preventDefault();
  46.             }
  47.  
  48.             // close tray if clicked on link with same open tray
  49.             if (self.menuOpen && $(this).parent().find('.' + self.classNavTrayShow).length > 0) {
  50.                 self.menuOpen = false;
  51.                 self.hideTray(self.$activeTray);
  52.                 self.$activeTray = null;
  53.                 $(this).parent().removeClass(self.navLink + '-active');
  54.  
  55.                 // swap trays if new link clicked
  56.             } else if (self.menuOpen && $(this).parent().siblings().children('.' + self.classNavTrayShow).length > 0) {
  57.                 const $oldTray = $('.' + self.classNavTrayShow);
  58.                 const $newTray = $(this).parent().find('.' + self.navTray);
  59.                 const $parent = $(this).parent();
  60.                 $parent.addClass(self.navLink + '-active');
  61.  
  62.                 self.toggleTrayElements();
  63.                 setTimeout(function() {
  64.                     $newTray.addClass(self.classNavTrayShow);
  65.                     $oldTray.removeClass(self.classNavTrayShow);
  66.                     self.$activeTray = $newTray;
  67.                     $parent.siblings().removeClass(self.navLink + '-active');
  68.                 }, 300);
  69.                 setTimeout(function() {
  70.                     self.toggleTrayElements(true);
  71.                 }, 350);
  72.  
  73.                 // open tray if not open
  74.             } else {
  75.                 self.menuOpen = true;
  76.                 $('.content').addClass('content-overlay');
  77.                 self.$activeTray = $(this).parent().find('.' + self.navTray);
  78.                 $(this).parent().addClass(self.navLink + '-active');
  79.                 self.showTray(self.$activeTray);
  80.             }
  81.         });
  82.         // hide tray when you click on close
  83.         self.$close.on('click', function(e) {
  84.             self.hideTray();
  85.             $(this).parent().removeClass(self.navLink + '-active');
  86.         });
  87.  
  88.         self.$modal.on('click', function(e) {
  89.             self.hideTray();
  90.         });
  91.  
  92.         // mobile toggle
  93.         self.$menuToggle.on('click', function(e) {
  94.             e.preventDefault();
  95.  
  96.             // check if menu is open
  97.             if (self.$element.hasClass(self.menuParent + '--menuopen')) {
  98.                 if (self.$element.find(self.classNavTrayShow).length) {
  99.                     self.toggleTrayElements();
  100.                 }
  101.  
  102.                 self.$menuToggle.children().removeClass(self.menuToggleLine + '--cross  ');
  103.                 setTimeout(function() {
  104.                     self.$element.find('.' + self.navTray).removeClass(self.classNavTrayShow);
  105.                 }, 375);
  106.                 setTimeout(function() {
  107.                     self.$element.removeClass(self.menuParent + '--menuopen');
  108.                 }, 400);
  109.             } else {
  110.                 self.$menuToggle.children().addClass(self.menuToggleLine + '--cross ');
  111.                 self.$element.addClass(self.menuParent + '--menuopen');
  112.             }
  113.         });
  114.         // mobile back
  115.         self.$back.on('click', function(e) {
  116.             self.toggleTrayElements();
  117.             setTimeout(function() {
  118.                 self.$element.find('.' + self.classNavTrayShow).removeClass(self.classNavTrayShow);
  119.             }, 375);
  120.         });
  121.     },
  122.     // show dropdown
  123.     showTray: function($tray) {
  124.         const self = this;
  125.         $tray.addClass(self.classNavTrayShow);
  126.         self.modalToggle(true);
  127.         self.toggleTrayElements(true);
  128.     },
  129.     // hide dropdown
  130.     hideTray: function($tray) {
  131.         const self = this;
  132.         $tray = $tray || self.$activeTray;
  133.         self.toggleTrayElements();
  134.         self.modalToggle();
  135.         setTimeout(function() {
  136.             $tray.removeClass(self.classNavTrayShow);
  137.         }, 375);
  138.     },
  139.     // show/hide elements in dropdown
  140.     toggleTrayElements: function(toggle, $tray) {
  141.         const self = this;
  142.         const $trayEl = $tray || self.$activeTray;
  143.  
  144.         if (toggle) { // show Elements
  145.             Object.keys(self.trayElements).forEach(function(el, i) {
  146.                 $trayEl.parent().find('.' + self.trayElements[el]).addClass(self.trayElements[el] + '-show');
  147.             });
  148.         } else { // hide Elements
  149.             Object.keys(self.trayElements).forEach(function(el, i) {
  150.                 $trayEl.find('.' + self.trayElements[el]).removeClass(self.trayElements[el] + '-show');
  151.             });
  152.         }
  153.     },
  154.  
  155.     modalToggle: function(show) {
  156.         const self = this;
  157.         if (show) {
  158.             self.$modal.addClass('modal-show');
  159.             $('body').addClass('modal-open');
  160.         } else {
  161.             self.$modal.removeClass('modal-show');
  162.             $('body').removeClass('modal-open');
  163.         }
  164.     }
  165. };
  166.  
  167. $(function() {
  168.     Header($('.header-nav'));
  169.  
  170.     // responsive table/mobile tweaks
  171.     enquire.register('screen and (max-width:768px)', {
  172.         match: function() {
  173.             // move description div in mobile
  174.             $('.header-nav-menu-link-sub-links').each(function(i, v) {
  175.                 const description = $(this).parent().prev().find('.header-nav-menu-link-sub-text-description');
  176.                 $(this).after(description);
  177.             });
  178.         },
  179.         unmatch: function() {
  180.             // move description back on desktop
  181.             $('.header-nav-menu-link-sub').each(function(i, v) {
  182.                 const description = $(this).parent().prev().find('.header-nav-menu-link-sub-text-description');
  183.                 $(this).find('.header-nav-menu-link-sub-text').append(description);
  184.             });
  185.         }
  186.     });
  187. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement