|
30 | 30 | $parent = $this.parent(),
|
31 | 31 | defaults = {
|
32 | 32 | delay: 500,
|
| 33 | + hoverDelay: 0, |
33 | 34 | instantlyCloseOthers: true
|
34 | 35 | },
|
35 | 36 | data = {
|
36 | 37 | delay: $(this).data('delay'),
|
| 38 | + hoverDelay: $(this).data('hover-delay'), |
37 | 39 | instantlyCloseOthers: $(this).data('close-others')
|
38 | 40 | },
|
39 | 41 | showEvent = 'show.bs.dropdown',
|
40 | 42 | hideEvent = 'hide.bs.dropdown',
|
41 | 43 | // shownEvent = 'shown.bs.dropdown',
|
42 | 44 | // hiddenEvent = 'hidden.bs.dropdown',
|
43 | 45 | settings = $.extend(true, {}, defaults, options, data),
|
44 |
| - timeout; |
| 46 | + timeout, timeoutHover; |
45 | 47 |
|
46 | 48 | $parent.hover(function (event) {
|
47 | 49 | // so a neighbor can't open the dropdown
|
|
53 | 55 |
|
54 | 56 | openDropdown(event);
|
55 | 57 | }, function () {
|
| 58 | + // clear timer for hover event |
| 59 | + window.clearTimeout(timeoutHover) |
56 | 60 | timeout = window.setTimeout(function () {
|
57 | 61 | $parent.removeClass('open');
|
58 | 62 | $this.trigger(hideEvent);
|
|
90 | 94 | });
|
91 | 95 |
|
92 | 96 | function openDropdown(event) {
|
93 |
| - $allDropdowns.find(':focus').blur(); |
94 |
| - |
95 |
| - if(settings.instantlyCloseOthers === true) |
96 |
| - $allDropdowns.removeClass('open'); |
97 |
| - |
| 97 | + // clear dropdown timeout here so it doesnt close before it should |
98 | 98 | window.clearTimeout(timeout);
|
99 |
| - $parent.addClass('open'); |
100 |
| - $this.trigger(showEvent); |
| 99 | + // restart hover timer |
| 100 | + window.clearTimeout(timeoutHover); |
| 101 | + // delay for hover event. |
| 102 | + timeoutHover = window.setTimeout(function () { |
| 103 | + $allDropdowns.find(':focus').blur(); |
| 104 | + |
| 105 | + if(settings.instantlyCloseOthers === true) |
| 106 | + $allDropdowns.removeClass('open'); |
| 107 | + |
| 108 | + // clear timer for hover event |
| 109 | + window.clearTimeout(timeoutHover); |
| 110 | + $parent.addClass('open'); |
| 111 | + $this.trigger(showEvent); |
| 112 | + }, settings.hoverDelay); |
101 | 113 | }
|
102 | 114 | });
|
103 | 115 | };
|
|
0 commit comments