1/** 2 * Project: Bootstrap Hover Dropdown 3 * Author: Cameron Spear 4 * Contributors: Mattia Larentis 5 * 6 * Dependencies: Bootstrap's Dropdown plugin, jQuery 7 * 8 * A simple plugin to enable Bootstrap dropdowns to active on hover and provide a nice user experience. 9 * 10 * License: MIT 11 * 12 * http://cameronspear.com/blog/bootstrap-dropdown-on-hover-plugin/ 13 */ 14;(function ($, window, undefined) { 15 // outside the scope of the jQuery plugin to 16 // keep track of all dropdowns 17 var $allDropdowns = $(); 18 19 // if instantlyCloseOthers is true, then it will instantly 20 // shut other nav items when a new one is hovered over 21 $.fn.dropdownHover = function (options) { 22 // don't do anything if touch is supported 23 // (plugin causes some issues on mobile) 24 if('ontouchstart' in document) return this; // don't want to affect chaining 25 26 // the element we really care about 27 // is the dropdown-toggle's parent 28 $allDropdowns = $allDropdowns.add(this.parent()); 29 30 return this.each(function () { 31 var $this = $(this), 32 $parent = $this.parent(), 33 defaults = { 34 delay: 300, 35 instantlyCloseOthers: true 36 }, 37 data = { 38 delay: $(this).data('delay'), 39 instantlyCloseOthers: $(this).data('close-others') 40 }, 41 showEvent = 'show.bs.dropdown', 42 hideEvent = 'hide.bs.dropdown', 43 // shownEvent = 'shown.bs.dropdown', 44 // hiddenEvent = 'hidden.bs.dropdown', 45 settings = $.extend(true, {}, defaults, options, data), 46 timeout; 47 48 $parent.hover(function (event) { 49 // so a neighbor can't open the dropdown 50 if(!$parent.hasClass('open') && !$this.is(event.target)) { 51 // stop this event, stop executing any code 52 // in this callback but continue to propagate 53 return true; 54 } 55 56 if(settings.instantlyCloseOthers === true) 57 $allDropdowns.removeClass('open'); 58 59 window.clearTimeout(timeout); 60 $parent.addClass('open'); 61 $this.trigger(showEvent); 62 }, function () { 63 timeout = window.setTimeout(function () { 64 $parent.removeClass('open'); 65 $this.trigger(hideEvent); 66 }, settings.delay); 67 }); 68 69 // this helps with button groups! 70 $this.hover(function () { 71 if(settings.instantlyCloseOthers === true) 72 $allDropdowns.removeClass('open'); 73 74 window.clearTimeout(timeout); 75 $parent.addClass('open'); 76 $this.trigger(showEvent); 77 }); 78 79 // handle submenus 80 $parent.find('.dropdown-submenu').each(function (){ 81 var $this = $(this); 82 var subTimeout; 83 $this.hover(function () { 84 window.clearTimeout(subTimeout); 85 $this.children('.dropdown-menu').show(); 86 // always close submenu siblings instantly 87 $this.siblings().children('.dropdown-menu').hide(); 88 }, function () { 89 var $submenu = $this.children('.dropdown-menu'); 90 subTimeout = window.setTimeout(function () { 91 $submenu.hide(); 92 }, settings.delay); 93 }); 94 }); 95 }); 96 }; 97 98 $(document).ready(function () { 99 // apply dropdownHover to all elements with the data-hover="dropdown" attribute 100 $('[data-hover="dropdown"]').dropdownHover(); 101 }); 102})(jQuery, this);
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.