Skip to content

Commit d847dd6

Browse files
committed
Don't break chaining on mobile and add variable keywords for BS events (and always bind them to $this).
1 parent 963cad1 commit d847dd6

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

Diff for: bootstrap-hover-dropdown.js

+25-20
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,23 @@
1111
*
1212
* http://cameronspear.com/blog/bootstrap-dropdown-on-hover-plugin/
1313
*/
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) {
1915
// outside the scope of the jQuery plugin to
2016
// keep track of all dropdowns
2117
var $allDropdowns = $();
2218

2319
// if instantlyCloseOthers is true, then it will instantly
2420
// 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
2625

2726
// the element we really care about
2827
// is the dropdown-toggle's parent
2928
$allDropdowns = $allDropdowns.add(this.parent());
3029

31-
return this.each(function() {
30+
return this.each(function () {
3231
var $this = $(this),
3332
$parent = $this.parent(),
3433
defaults = {
@@ -39,58 +38,64 @@
3938
delay: $(this).data('delay'),
4039
instantlyCloseOthers: $(this).data('close-others')
4140
},
41+
showEvent = 'show.bs.dropdown',
42+
hideEvent = 'hide.bs.dropdown',
43+
// shownEvent = 'shown.bs.dropdown',
44+
// hiddenEvent = 'hidden.bs.dropdown',
4245
settings = $.extend(true, {}, defaults, options, data),
4346
timeout;
4447

45-
$parent.hover(function(event) {
48+
$parent.hover(function (event) {
4649
// so a neighbor can't open the dropdown
4750
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;
4954
}
5055

5156
if(settings.instantlyCloseOthers === true)
5257
$allDropdowns.removeClass('open');
5358

5459
window.clearTimeout(timeout);
5560
$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 () {
5964
$parent.removeClass('open');
60-
$parent.trigger('hide.bs.dropdown');
65+
$this.trigger(hideEvent);
6166
}, settings.delay);
6267
});
6368

6469
// this helps with button groups!
65-
$this.hover(function() {
70+
$this.hover(function () {
6671
if(settings.instantlyCloseOthers === true)
6772
$allDropdowns.removeClass('open');
6873

6974
window.clearTimeout(timeout);
7075
$parent.addClass('open');
71-
$parent.trigger($.Event('show.bs.dropdown'));
76+
$this.trigger(showEvent);
7277
});
7378

7479
// handle submenus
75-
$parent.find('.dropdown-submenu').each(function(){
80+
$parent.find('.dropdown-submenu').each(function (){
7681
var $this = $(this);
7782
var subTimeout;
78-
$this.hover(function() {
83+
$this.hover(function () {
7984
window.clearTimeout(subTimeout);
8085
$this.children('.dropdown-menu').show();
8186
// always close submenu siblings instantly
8287
$this.siblings().children('.dropdown-menu').hide();
83-
}, function() {
88+
}, function () {
8489
var $submenu = $this.children('.dropdown-menu');
85-
subTimeout = window.setTimeout(function() {
90+
subTimeout = window.setTimeout(function () {
8691
$submenu.hide();
8792
}, settings.delay);
8893
});
8994
});
9095
});
9196
};
9297

93-
$(document).ready(function() {
98+
$(document).ready(function () {
9499
// apply dropdownHover to all elements with the data-hover="dropdown" attribute
95100
$('[data-hover="dropdown"]').dropdownHover();
96101
});

Diff for: bootstrap-hover-dropdown.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)