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