1(function(window) { 2 3 'use strict'; 4 5 /** 6 * Extend Object helper function. 7 */ 8 function extend(a, b) { 9 for(var key in b) { 10 if(b.hasOwnProperty(key)) { 11 a[key] = b[key]; 12 } 13 } 14 return a; 15 } 16 17 /** 18 * Each helper function. 19 */ 20 function each(collection, callback) { 21 for (var i = 0; i < collection.length; i++) { 22 var item = collection[i]; 23 callback(item); 24 } 25 } 26 27 /** 28 * Menu Constructor. 29 */ 30 function Menu(options) { 31 this.options = extend({}, this.options); 32 extend(this.options, options); 33 this._init(); 34 } 35 36 /** 37 * Menu Options. 38 */ 39 Menu.prototype.options = { 40 wrapper: '#content-wrap', // The content wrapper 41 type: 'push-left', // The menu type 42 menuOpenerClass: '.open-button', // The menu opener class names (i.e. the buttons) 43 maskId: '#content-mask' // The ID of the mask 44 }; 45 46 /** 47 * Initialise Menu. 48 */ 49 Menu.prototype._init = function() { 50 this.body = document.body; 51 this.wrapper = document.querySelector(this.options.wrapper); 52 this.mask = document.querySelector(this.options.maskId); 53 this.menu = document.querySelector('#c-menu--' + this.options.type); 54 this.closeBtn = this.menu.querySelector('.c-menu__close'); 55 this.menuOpeners = document.querySelectorAll(this.options.menuOpenerClass); 56 this._initEvents(); 57 }; 58 59 /** 60 * Initialise Menu Events. 61 */ 62 Menu.prototype._initEvents = function() { 63 // Event for clicks on the close button inside the menu. 64 this.closeBtn.addEventListener('click', function(e) { 65 e.preventDefault(); 66 this.close(); 67 }.bind(this)); 68 69 // Event for clicks on the mask. 70 this.mask.addEventListener('click', function(e) { 71 e.preventDefault(); 72 this.close(); 73 }.bind(this)); 74 }; 75 76 /** 77 * Open Menu. 78 */ 79 Menu.prototype.open = function() { 80 this.body.classList.add('has-active-menu'); 81 this.wrapper.classList.add('has-' + this.options.type); 82 this.menu.classList.add('is-active'); 83 this.mask.classList.add('is-active'); 84 this.disableMenuOpeners(); 85 // set the focus on the first link when opening 86 var menuUL = document.getElementsByClassName('c-menu__items')[0]; 87 menuUL.querySelectorAll("a")[0].focus(); 88 }; 89 90 /** 91 * Close Menu. 92 */ 93 Menu.prototype.close = function() { 94 this.body.classList.remove('has-active-menu'); 95 this.wrapper.classList.remove('has-' + this.options.type); 96 this.menu.classList.remove('is-active'); 97 this.mask.classList.remove('is-active'); 98 this.enableMenuOpeners(); 99 // set the focus on the mobile menu toggle button; wait one second, or else we get a hiccup effect on the slide-left, like it slides too far left 100 setTimeout(() => { 101 var toggle = document.getElementById('toggle'); 102 toggle.focus(); 103 }, 1000); 104 }; 105 106 /** 107 * Disable Menu Openers. 108 */ 109 Menu.prototype.disableMenuOpeners = function() { 110 each(this.menuOpeners, function(item) { 111 item.disabled = true; 112 }); 113 }; 114 115 /** 116 * Enable Menu Openers. 117 */ 118 Menu.prototype.enableMenuOpeners = function() { 119 each(this.menuOpeners, function(item) { 120 item.disabled = false; 121 }); 122 }; 123 124 /** 125 * Add to global namespace. 126 */ 127 window.Menu = Menu; 128 129})(window); 130 131var pushLeft = new Menu({ 132 wrapper: '#content-wrap', 133 type: 'push-left', 134 menuOpenerClass: '.open-button', 135 maskId: '#content-mask' 136}); 137 138var pushLeftBtn = document.querySelector('#toggle'); 139 140pushLeftBtn.addEventListener('click', function(e) { 141 e.preventDefault; 142 pushLeft.open(); 143}); 144 145// Find all menu links 146var navLinks = document.querySelectorAll('.c-menu__link'); 147// For each menu link 148var index = 0, length = navLinks.length; 149for ( ; index < length; index++) { 150 // Attach click event, on click call close function 151 navLinks[index].addEventListener('click', 152 function () { 153 slideLeft.close(); 154 } 155 ); 156}
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.