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

Forward originalEvent to hide.bs.dropdown handlers #11373

Closed
wants to merge 4 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
16 changes: 10 additions & 6 deletions dist/js/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -710,22 +710,24 @@ if (typeof jQuery === "undefined") { throw new Error("Bootstrap requires jQuery"

var $parent = getParent($this)
var isActive = $parent.hasClass('open')
var evProps
if (e) evProps = { originalEvent: e }

clearMenus()
clearMenus(e)

if (!isActive) {
if ('ontouchstart' in document.documentElement && !$parent.closest('.navbar-nav').length) {
// if mobile we we use a backdrop because click events don't delegate
$('<div class="dropdown-backdrop"/>').insertAfter($(this)).on('click', clearMenus)
}

$parent.trigger(e = $.Event('show.bs.dropdown'))
$parent.trigger(e = $.Event('show.bs.dropdown', evProps))

if (e.isDefaultPrevented()) return

$parent
.toggleClass('open')
.trigger('shown.bs.dropdown')
.trigger('shown.bs.dropdown', evProps)

$this.focus()
}
Expand Down Expand Up @@ -764,14 +766,16 @@ if (typeof jQuery === "undefined") { throw new Error("Bootstrap requires jQuery"
$items.eq(index).focus()
}

function clearMenus() {
function clearMenus(ev) {
var evProps
if (ev) evProps = { originalEvent: ev }
$(backdrop).remove()
$(toggle).each(function (e) {
var $parent = getParent($(this))
if (!$parent.hasClass('open')) return
$parent.trigger(e = $.Event('hide.bs.dropdown'))
$parent.trigger(e = $.Event('hide.bs.dropdown', evProps))
if (e.isDefaultPrevented()) return
$parent.removeClass('open').trigger('hidden.bs.dropdown')
$parent.removeClass('open').trigger('hidden.bs.dropdown', evProps)
})
}

Expand Down
11 changes: 9 additions & 2 deletions javascript.html
Original file line number Diff line number Diff line change
Expand Up @@ -522,9 +522,16 @@ <h3>Events</h3>
</tbody>
</table>
</div><!-- ./bs-table-responsive -->

{% highlight js %}
$('#myDropdown').on('show.bs.dropdown', function () {
// do something…
$('#myDropdown').on('hide.bs.dropdown', function (ev) {
// If not triggered programmatically, all those events will have a `originalEvent`
// additional key containing the event at the source of the trigger.
//
// Example:
if (ev.originalEvent && $(ev.originalEvent.target).is('[contenteditable]')) {
ev.preventDefault()
}
})
{% endhighlight %}
</div>
Expand Down
16 changes: 10 additions & 6 deletions js/dropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,24 @@

var $parent = getParent($this)
var isActive = $parent.hasClass('open')
var evProps
if (e) evProps = { originalEvent: e }

clearMenus()
clearMenus(e)

if (!isActive) {
if ('ontouchstart' in document.documentElement && !$parent.closest('.navbar-nav').length) {
// if mobile we we use a backdrop because click events don't delegate
$('<div class="dropdown-backdrop"/>').insertAfter($(this)).on('click', clearMenus)
}

$parent.trigger(e = $.Event('show.bs.dropdown'))
$parent.trigger(e = $.Event('show.bs.dropdown', evProps))

if (e.isDefaultPrevented()) return

$parent
.toggleClass('open')
.trigger('shown.bs.dropdown')
.trigger('shown.bs.dropdown', evProps)

$this.focus()
}
Expand Down Expand Up @@ -90,14 +92,16 @@
$items.eq(index).focus()
}

function clearMenus() {
function clearMenus(ev) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Presumably the call to clearMenus() within toggle() should pass along its event object?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct me if I'm wrong but toggle is supposed to be called programmatically so I don't see what event could be bound to it !?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

toggle is bound to the dropdown's click event, and is invoked when you click a dropdown to show/hide its menu.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad ! I applied the same principle to all custom events triggered by dropdown. I also used sourceEvent instead of originalEvent in order to avoid to hoist the jQuery semantic on originalEvent. If you prefer another name than sourceEvent just tell me.

var evProps
if (ev) evProps = { originalEvent: ev }
$(backdrop).remove()
$(toggle).each(function (e) {
var $parent = getParent($(this))
if (!$parent.hasClass('open')) return
$parent.trigger(e = $.Event('hide.bs.dropdown'))
$parent.trigger(e = $.Event('hide.bs.dropdown', evProps))
if (e.isDefaultPrevented()) return
$parent.removeClass('open').trigger('hidden.bs.dropdown')
$parent.removeClass('open').trigger('hidden.bs.dropdown', evProps)
})
}

Expand Down