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

Enable nested dropdowns #131

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
59 changes: 34 additions & 25 deletions jquery.dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ if (jQuery) (function ($) {
} else {
if (trigger !== object.target && $(object.target).hasClass('jq-dropdown-ignore')) return;
}
hide();
hide(event);

if (isOpen || trigger.hasClass('jq-dropdown-disabled')) return;

Expand All @@ -63,7 +63,7 @@ if (jQuery) (function ($) {

// Trigger the show callback
jqDropdown
.trigger('show', {
.triggerHandler('show', {
jqDropdown: jqDropdown,
trigger: trigger
});
Expand All @@ -75,50 +75,59 @@ if (jQuery) (function ($) {
// In some cases we don't hide them
var targetGroup = event ? $(event.target).parents().addBack() : null;

// Are we clicking anywhere in a jq-dropdown?
if (targetGroup && targetGroup.is('.jq-dropdown')) {
// Is it a jq-dropdown menu?
if (targetGroup.is('.jq-dropdown-menu')) {
// Did we click on an option? If so close it.
if (!targetGroup.is('A')) return;
} else {
// Nope, it's a panel. Leave it open.
return;
}
// hide other dropdowns, but not the one we've clicked within
var toHide = $(document).find('.jq-dropdown:visible').not(
$(event.target).parents('.jq-dropdown'));

if (targetGroup && targetGroup.is('.jq-dropdown') &&
targetGroup.is('.jq-dropdown-menu') &&
targetGroup.is('A')) {
// Clicked an option within the current dropdown
// If this is a nested dropdown, make sure to keep the parent dropdown open
toHide = $(event.target).closest('.jq-dropdown:visible')
}

// Trigger the event early, so that it might be prevented on the visible popups
var hideEvent = jQuery.Event("hide");

$(document).find('.jq-dropdown:visible').each(function () {

toHide.each(function () {
var jqDropdown = $(this);
jqDropdown
.hide()
.removeData('jq-dropdown-trigger')
.trigger('hide', { jqDropdown: jqDropdown });
.triggerHandler('hide', { jqDropdown: jqDropdown });
});

if(!hideEvent.isDefaultPrevented()) {
// Hide any jq-dropdown that may be showing
$(document).find('.jq-dropdown:visible').each(function () {
toHide.each(function () {
// Remove all jq-dropdown-open classes
var jqDropdown = $(this);
var trigger = jqDropdown.data('jq-dropdown-trigger');
trigger.removeClass('jq-dropdown-open');
jqDropdown
.hide()
.removeData('jq-dropdown-trigger')
.trigger('hide', { jqDropdown: jqDropdown });
.triggerHandler('hide', { jqDropdown: jqDropdown });
});

// Remove all jq-dropdown-open classes
$(document).find('.jq-dropdown-open').removeClass('jq-dropdown-open');
}
}

function position() {
$('.jq-dropdown:visible').each(function() {
positionDropdown($(this));
});
}

function positionDropdown(jqDropdown) {

var trigger = jqDropdown.data('jq-dropdown-trigger');
if (!trigger) {
return;
}

var jqDropdown = $('.jq-dropdown:visible').eq(0),
trigger = jqDropdown.data('jq-dropdown-trigger'),
hOffset = trigger ? parseInt(trigger.attr('data-horizontal-offset') || 0, 10) : null,
vOffset = trigger ? parseInt(trigger.attr('data-vertical-offset') || 0, 10) : null;
var hOffset = parseInt(trigger.attr('data-horizontal-offset') || 0, 10),
vOffset = parseInt(trigger.attr('data-vertical-offset') || 0, 10);

if (jqDropdown.length === 0 || !trigger) return;

Expand All @@ -144,4 +153,4 @@ if (jQuery) (function ($) {
$(document).on('click.jq-dropdown', hide);
$(window).on('resize', position);

})(jQuery);
})(jQuery);