1/*! 2 * SlickNav Responsive Mobile Menu v1.0.3 3 * (c) 2015 Josh Cope 4 * licensed under MIT 5 */ 6;(function ($, document, window) { 7 var 8 // default settings object. 9 defaults = { 10 label: 'MENU', 11 duplicate: true, 12 duration: 200, 13 easingOpen: 'swing', 14 easingClose: 'swing', 15 closedSymbol: '►', 16 openedSymbol: '▼', 17 prependTo: '.responsive-menu', 18 parentTag: 'a', 19 closeOnClick: false, 20 allowParentLinks: false, 21 nestedParentLinks: true, 22 showChildren: false, 23 removeIds: false, 24 removeClasses: false, 25 brand: '', 26 init: function () {}, 27 beforeOpen: function () {}, 28 beforeClose: function () {}, 29 afterOpen: function () {}, 30 afterClose: function () {} 31 }, 32 mobileMenu = 'slicknav', 33 prefix = 'slicknav'; 34 35 function Plugin(element, options) { 36 this.element = element; 37 38 // jQuery has an extend method which merges the contents of two or 39 // more objects, storing the result in the first object. The first object 40 // is generally empty as we don't want to alter the default options for 41 // future instances of the plugin 42 this.settings = $.extend({}, defaults, options); 43 44 this._defaults = defaults; 45 this._name = mobileMenu; 46 47 this.init(); 48 } 49 50 Plugin.prototype.init = function () { 51 var $this = this, 52 menu = $(this.element), 53 settings = this.settings, 54 iconClass, 55 menuBar; 56 57 // clone menu if needed 58 if (settings.duplicate) { 59 $this.mobileNav = menu.clone(); 60 //remove ids from clone to prevent css issues 61 $this.mobileNav.removeAttr('id'); 62 $this.mobileNav.find('*').each(function (i, e) { 63 $(e).removeAttr('id'); 64 }); 65 } else { 66 $this.mobileNav = menu; 67 68 // remove ids if set 69 $this.mobileNav.removeAttr('id'); 70 $this.mobileNav.find('*').each(function (i, e) { 71 $(e).removeAttr('id'); 72 }); 73 } 74 75 // remove classes if set 76 if (settings.removeClasses) { 77 $this.mobileNav.removeAttr('class'); 78 $this.mobileNav.find('*').each(function (i, e) { 79 $(e).removeAttr('class'); 80 }); 81 } 82 83 // styling class for the button 84 iconClass = prefix + '_icon'; 85 86 if (settings.label === '') { 87 iconClass += ' ' + prefix + '_no-text'; 88 } 89 90 if (settings.parentTag == 'a') { 91 settings.parentTag = 'a href="#"'; 92 } 93 94 // create menu bar 95 $this.mobileNav.attr('class', prefix + '_nav'); 96 menuBar = $('<div class="' + prefix + '_menu"></div>'); 97 if (settings.brand !== '') { 98 var brand = $('<div class="' + prefix + '_brand">'+settings.brand+'</div>'); 99 $(menuBar).append(brand); 100 } 101 $this.btn = $( 102 ['<' + settings.parentTag + ' aria-haspopup="true" tabindex="0" class="' + prefix + '_btn ' + prefix + '_collapsed">', 103 '<span class="' + prefix + '_menutxt">' + settings.label + '</span>', 104 '<span class="' + iconClass + '">', 105 '<span class="' + prefix + '_icon-bar"></span>', 106 '<span class="' + prefix + '_icon-bar"></span>', 107 '<span class="' + prefix + '_icon-bar"></span>', 108 '</span>', 109 '</' + settings.parentTag + '>' 110 ].join('') 111 ); 112 $(menuBar).append($this.btn); 113 $(settings.prependTo).prepend(menuBar); 114 menuBar.append($this.mobileNav); 115 116 // iterate over structure adding additional structure 117 var items = $this.mobileNav.find('li'); 118 $(items).each(function () { 119 var item = $(this), 120 data = {}; 121 data.children = item.children('ul').attr('role', 'menu'); 122 item.data('menu', data); 123 124 // if a list item has a nested menu 125 if (data.children.length > 0) { 126 127 // select all text before the child menu 128 // check for anchors 129 130 var a = item.contents(), 131 containsAnchor = false;
vendor: 12,310 bytes, lines 132-469
132 nodes = []; 133 134 $(a).each(function () { 135 if (!$(this).is('ul')) { 136 nodes.push(this); 137 } else { 138 return false; 139 } 140 141 if($(this).is("a")) { 142 containsAnchor = true; 143 } 144 }); 145 146 var wrapElement = $( 147 '<' + settings.parentTag + ' role="menuitem" aria-haspopup="true" tabindex="-1" class="' + prefix + '_item"/>' 148 ); 149 150 // wrap item text with tag and add classes unless we are separating parent links 151 if ((!settings.allowParentLinks || settings.nestedParentLinks) || !containsAnchor) { 152 var $wrap = $(nodes).wrapAll(wrapElement).parent(); 153 $wrap.addClass(prefix+'_row'); 154 } else 155 $(nodes).wrapAll('<span class="'+prefix+'_parent-link '+prefix+'_row"/>').parent(); 156 157 item.addClass(prefix+'_collapsed'); 158 item.addClass(prefix+'_parent'); 159 160 // create parent arrow. wrap with link if parent links and separating 161 var arrowElement = $('<span class="'+prefix+'_arrow">'+settings.closedSymbol+'</span>'); 162 163 if (settings.allowParentLinks && !settings.nestedParentLinks && containsAnchor) 164 arrowElement = arrowElement.wrap(wrapElement).parent(); 165 166 //append arrow 167 $(nodes).last().after(arrowElement); 168 169 170 } else if ( item.children().length === 0) { 171 item.addClass(prefix+'_txtnode'); 172 } 173 174 // accessibility for links 175 item.children('a').attr('role', 'menuitem').click(function(event){ 176 //Ensure that it's not a parent 177 if (settings.closeOnClick && !$(event.target).parent().closest('li').hasClass(prefix+'_parent')) { 178 //Emulate menu close if set 179 $($this.btn).click(); 180 } 181 }); 182 183 //also close on click if parent links are set 184 if (settings.closeOnClick && settings.allowParentLinks) { 185 item.children('a').children('a').click(function (event) { 186 //Emulate menu close 187 $($this.btn).click(); 188 }); 189 190 item.find('.'+prefix+'_parent-link a:not(.'+prefix+'_item)').click(function(event){ 191 //Emulate menu close 192 $($this.btn).click(); 193 }); 194 } 195 }); 196 197 // structure is in place, now hide appropriate items 198 $(items).each(function () { 199 var data = $(this).data('menu'); 200 if (!settings.showChildren){ 201 $this._visibilityToggle(data.children, null, false, null, true); 202 } 203 }); 204 205 // finally toggle entire menu 206 $this._visibilityToggle($this.mobileNav, null, false, 'init', true); 207 208 // accessibility for menu button 209 $this.mobileNav.attr('role','menu'); 210 211 // outline prevention when using mouse 212 $(document).mousedown(function(){ 213 $this._outlines(false); 214 }); 215 216 $(document).keyup(function(){ 217 $this._outlines(true); 218 }); 219 220 // menu button click 221 $($this.btn).click(function (e) { 222 e.preventDefault(); 223 $this._menuToggle(); 224 }); 225 226 // click on menu parent 227 $this.mobileNav.on('click', '.' + prefix + '_item', function (e) { 228 e.preventDefault(); 229 $this._itemClick($(this)); 230 }); 231 232 // check for enter key on menu button and menu parents 233 $($this.btn).keydown(function (e) { 234 var ev = e || event; 235 if(ev.keyCode == 13) { 236 e.preventDefault(); 237 $this._menuToggle(); 238 } 239 }); 240 241 $this.mobileNav.on('keydown', '.'+prefix+'_item', function(e) { 242 var ev = e || event; 243 if(ev.keyCode == 13) { 244 e.preventDefault(); 245 $this._itemClick($(e.target)); 246 } 247 }); 248 249 // allow links clickable within parent tags if set 250 if (settings.allowParentLinks && settings.nestedParentLinks) { 251 $('.'+prefix+'_item a').click(function(e){ 252 e.stopImmediatePropagation(); 253 }); 254 } 255 }; 256 257 //toggle menu 258 Plugin.prototype._menuToggle = function (el) { 259 var $this = this; 260 var btn = $this.btn; 261 var mobileNav = $this.mobileNav; 262 263 if (btn.hasClass(prefix+'_collapsed')) { 264 btn.removeClass(prefix+'_collapsed'); 265 btn.addClass(prefix+'_open'); 266 } else { 267 btn.removeClass(prefix+'_open'); 268 btn.addClass(prefix+'_collapsed'); 269 } 270 btn.addClass(prefix+'_animating'); 271 $this._visibilityToggle(mobileNav, btn.parent(), true, btn); 272 }; 273 274 // toggle clicked items 275 Plugin.prototype._itemClick = function (el) { 276 var $this = this; 277 var settings = $this.settings; 278 var data = el.data('menu'); 279 if (!data) { 280 data = {}; 281 data.arrow = el.children('.'+prefix+'_arrow'); 282 data.ul = el.next('ul'); 283 data.parent = el.parent(); 284 //Separated parent link structure 285 if (data.parent.hasClass(prefix+'_parent-link')) { 286 data.parent = el.parent().parent(); 287 data.ul = el.parent().next('ul'); 288 } 289 el.data('menu', data); 290 } 291 if (data.parent.hasClass(prefix+'_collapsed')) { 292 data.arrow.html(settings.openedSymbol); 293 data.parent.removeClass(prefix+'_collapsed'); 294 data.parent.addClass(prefix+'_open'); 295 data.parent.addClass(prefix+'_animating'); 296 $this._visibilityToggle(data.ul, data.parent, true, el); 297 } else { 298 data.arrow.html(settings.closedSymbol); 299 data.parent.addClass(prefix+'_collapsed'); 300 data.parent.removeClass(prefix+'_open'); 301 data.parent.addClass(prefix+'_animating'); 302 $this._visibilityToggle(data.ul, data.parent, true, el); 303 } 304 }; 305 306 // toggle actual visibility and accessibility tags 307 Plugin.prototype._visibilityToggle = function(el, parent, animate, trigger, init) { 308 var $this = this; 309 var settings = $this.settings; 310 var items = $this._getActionItems(el); 311 var duration = 0; 312 if (animate) { 313 duration = settings.duration; 314 } 315 316 if (el.hasClass(prefix+'_hidden')) { 317 el.removeClass(prefix+'_hidden'); 318 //Fire beforeOpen callback 319 if (!init) { 320 settings.beforeOpen(trigger); 321 } 322 el.slideDown(duration, settings.easingOpen, function(){ 323 324 $(trigger).removeClass(prefix+'_animating'); 325 $(parent).removeClass(prefix+'_animating'); 326 327 //Fire afterOpen callback 328 if (!init) { 329 settings.afterOpen(trigger); 330 } 331 }); 332 el.attr('aria-hidden','false'); 333 items.attr('tabindex', '0'); 334 $this._setVisAttr(el, false); 335 } else { 336 el.addClass(prefix+'_hidden'); 337 338 //Fire init or beforeClose callback 339 if (!init){ 340 settings.beforeClose(trigger); 341 }else if (trigger == 'init'){ 342 settings.init(); 343 } 344 345 el.slideUp(duration, this.settings.easingClose, function() { 346 el.attr('aria-hidden','true'); 347 items.attr('tabindex', '-1'); 348 $this._setVisAttr(el, true); 349 el.hide(); //jQuery 1.7 bug fix 350 351 $(trigger).removeClass(prefix+'_animating'); 352 $(parent).removeClass(prefix+'_animating'); 353 354 //Fire init or afterClose callback 355 if (!init){ 356 settings.afterClose(trigger); 357 } 358 else if (trigger == 'init'){ 359 settings.init(); 360 } 361 }); 362 } 363 }; 364 365 // set attributes of element and children based on visibility 366 Plugin.prototype._setVisAttr = function(el, hidden) { 367 var $this = this; 368 369 // select all parents that aren't hidden 370 var nonHidden = el.children('li').children('ul').not('.'+prefix+'_hidden'); 371 372 // iterate over all items setting appropriate tags 373 if (!hidden) { 374 nonHidden.each(function(){ 375 var ul = $(this); 376 ul.attr('aria-hidden','false'); 377 var items = $this._getActionItems(ul); 378 items.attr('tabindex', '0'); 379 $this._setVisAttr(ul, hidden); 380 }); 381 } else { 382 nonHidden.each(function(){ 383 var ul = $(this); 384 ul.attr('aria-hidden','true'); 385 var items = $this._getActionItems(ul); 386 items.attr('tabindex', '-1'); 387 $this._setVisAttr(ul, hidden); 388 }); 389 } 390 }; 391 392 // get all 1st level items that are clickable 393 Plugin.prototype._getActionItems = function(el) { 394 var data = el.data("menu"); 395 if (!data) { 396 data = {}; 397 var items = el.children('li'); 398 var anchors = items.find('a'); 399 data.links = anchors.add(items.find('.'+prefix+'_item')); 400 el.data('menu', data); 401 } 402 return data.links; 403 }; 404 405 Plugin.prototype._outlines = function(state) { 406 if (!state) { 407 $('.'+prefix+'_item, .'+prefix+'_btn').css('outline','none'); 408 } else { 409 $('.'+prefix+'_item, .'+prefix+'_btn').css('outline',''); 410 } 411 }; 412 413 Plugin.prototype.toggle = function(){ 414 var $this = this; 415 $this._menuToggle(); 416 }; 417 418 Plugin.prototype.open = function(){ 419 var $this = this; 420 if ($this.btn.hasClass(prefix+'_collapsed')) { 421 $this._menuToggle(); 422 } 423 }; 424 425 Plugin.prototype.close = function(){ 426 var $this = this; 427 if ($this.btn.hasClass(prefix+'_open')) { 428 $this._menuToggle(); 429 } 430 }; 431 432 $.fn[mobileMenu] = function ( options ) { 433 var args = arguments; 434 435 // Is the first parameter an object (options), or was omitted, instantiate a new instance 436 if (options === undefined || typeof options === 'object') { 437 return this.each(function () { 438 439 // Only allow the plugin to be instantiated once due to methods 440 if (!$.data(this, 'plugin_' + mobileMenu)) { 441 442 // if it has no instance, create a new one, pass options to our plugin constructor, 443 // and store the plugin instance in the elements jQuery data object. 444 $.data(this, 'plugin_' + mobileMenu, new Plugin( this, options )); 445 } 446 }); 447 448 // If is a string and doesn't start with an underscore or 'init' function, treat this as a call to a public method. 449 } else if (typeof options === 'string' && options[0] !== '_' && options !== 'init') { 450 451 // Cache the method call to make it possible to return a value 452 var returns; 453 454 this.each(function () { 455 var instance = $.data(this, 'plugin_' + mobileMenu); 456 457 // Tests that there's already a plugin-instance and checks that the requested public method exists 458 if (instance instanceof Plugin && typeof instance[options] === 'function') { 459 460 // Call the method of our plugin instance, and pass it the supplied arguments. 461 returns = instance[options].apply( instance, Array.prototype.slice.call( args, 1 ) ); 462 } 463 }); 464 465 // If the earlier cached method gives a value back return the value, otherwise return this to preserve chainability. 466 return returns !== undefined ? returns : this; 467 } 468 }; 469}(jQuery, document, window));
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.