Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider clearTimeout if mouseEnter parent and take submenu hover events also into consideration #65

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 60 additions & 13 deletions bootstrap-hover-dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,47 +31,86 @@
var $this = $(this),
$parent = $this.parent(),
defaults = {
delay: 500,
delayClose: 1000,
delayOpen: 300,
delaySwitch: 100,
instantlyCloseOthers: true
},
data = {
delay: $(this).data('delay'),
delayClose: $(this).data('delay-close'),
delayOpen: $(this).data('delay-open'),
delaySwitch: $(this).data('delay-switch'),
instantlyCloseOthers: $(this).data('close-others')
},
showEvent = 'show.bs.dropdown',
hideEvent = 'hide.bs.dropdown',
// shownEvent = 'shown.bs.dropdown',
// hiddenEvent = 'hidden.bs.dropdown',
settings = $.extend(true, {}, defaults, options, data),
timeout;
timeoutOpen,
timeoutClose;


$parent.hover(function (event) {

// so a neighbor can't open the dropdown
if(!$parent.hasClass('open') && !$this.is(event.target)) {
// FIX: see https://github.com/CWSpear/bootstrap-hover-dropdown/issues/55
if($parent.hasClass('open') && !$this.is(event.target)) {
// stop this event, stop executing any code
// in this callback but continue to propagate
return true;
}

openDropdown(event);
var isChildMenu = $parent.parents('.dropdown-menu').length,
siblingIsOpen = $parent.siblings().hasClass('open');

window.clearTimeout(timeoutClose);

timeoutOpen = window.setTimeout(function(){
openDropdown(event);
}, siblingIsOpen || isChildMenu ? settings.delaySwitch : settings.delayOpen);



}, function () {
timeout = window.setTimeout(function () {
clearTimeout(timeoutOpen);

var isChildMenu = $parent.parents('.dropdown-menu').length;

timeoutClose = window.setTimeout(function () {
$parent.removeClass('open');
$this.trigger(hideEvent);
}, settings.delay);
}, timeoutOpen && !isChildMenu ? settings.delayClose : settings.delaySwitch);
});

// clear timeout if hovering submenu
$allDropdowns.find('.dropdown-menu').hover(function(){
window.clearTimeout(timeoutClose);
}, function(){

var isChildMenu = $(this).parents('.dropdown-menu').length;

if(isChildMenu){
return true;
}

timeoutClose = window.setTimeout(function () {
$parent.removeClass('open');
$this.trigger(hideEvent);
}, timeoutOpen > 0 ? settings.delayClose : 0);
});


// this helps with button groups!
$this.hover(function (event) {
// this helps prevent a double event from firing.
// see https://github.com/CWSpear/bootstrap-hover-dropdown/issues/55
if(!$parent.hasClass('open') && !$parent.is(event.target)) {
if($parent.hasClass('open') && !$parent.is(event.target)) {
// stop this event, stop executing any code
// in this callback but continue to propagate
return true;
}

openDropdown(event);
});

// handle submenus
Expand All @@ -87,17 +126,25 @@
var $submenu = $this.children('.dropdown-menu');
subTimeout = window.setTimeout(function () {
$submenu.hide();
}, settings.delay);
}, timeoutOpen > 0 ? settings.delayClose : 0);
});
});

function openDropdown(event) {
$allDropdowns.find(':focus').blur();

if(settings.instantlyCloseOthers === true)
$allDropdowns.removeClass('open');
if(settings.instantlyCloseOthers === true){

// not the first level
if($this.parents('.dropdown-menu').length && $this.siblings().parent().hasClass('open')){
$this.siblings().parent().removeClass('open');
}else{
$this.parent('li').siblings().removeClass('open');
}
}


window.clearTimeout(timeout);
window.clearTimeout(timeoutClose);
$parent.addClass('open');
$this.trigger(showEvent);
}
Expand Down
14 changes: 1 addition & 13 deletions bootstrap-hover-dropdown.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.