diff --git a/actionview/CHANGELOG.md b/actionview/CHANGELOG.md index b1ef9625b6d5660425916735a932a121bb254700..ecbc3fa5ecdd3a7f3194a1537e6342474d63361d 100644 --- a/actionview/CHANGELOG.md +++ b/actionview/CHANGELOG.md @@ -1,3 +1,9 @@ +* Allow programmatic click events to trigger Rails UJS click handlers. + Programmatic click events (eg. ones generated by `Rails.fire(link, "click")`) don't specify a button. These events were being incorrectly stopped by code meant to ignore scroll wheel and right clicks introduced in #34573. + + *Sudara Williams* + + ## Rails 5.2.3 (March 27, 2019) ## * Prevent non-primary mouse keys from triggering Rails UJS click handlers. diff --git a/actionview/app/assets/javascripts/rails-ujs/features/remote.coffee b/actionview/app/assets/javascripts/rails-ujs/features/remote.coffee index a5b61220bba9b25a94589969f7360b49531fa87f..d1aeef56c7383de62be6c22f3cd5f37c0628dc70 100644 --- a/actionview/app/assets/javascripts/rails-ujs/features/remote.coffee +++ b/actionview/app/assets/javascripts/rails-ujs/features/remote.coffee @@ -88,6 +88,6 @@ Rails.preventInsignificantClick = (e) -> data = link.getAttribute('data-params') metaClick = e.metaKey or e.ctrlKey insignificantMetaClick = metaClick and method is 'GET' and not data - primaryMouseKey = e.button is 0 - e.stopImmediatePropagation() if not primaryMouseKey or insignificantMetaClick + nonPrimaryMouseClick = e.button? and e.button isnt 0 + e.stopImmediatePropagation() if nonPrimaryMouseClick or insignificantMetaClick diff --git a/actionview/test/ujs/public/test/data-remote.js b/actionview/test/ujs/public/test/data-remote.js index 55d39b0a520aee75e71e740191113a3ccb7cfebc..f5215b175db3da983871d56e092cd35e0705d141 100644 --- a/actionview/test/ujs/public/test/data-remote.js +++ b/actionview/test/ujs/public/test/data-remote.js @@ -82,6 +82,20 @@ asyncTest('right/mouse-wheel-clicking on a link does not fire ajaxyness', 0, fun setTimeout(function() { start() }, 13) }) +asyncTest('clicking on a link via a non-mouse Event (such as from js) works', 1, function() { + var link = $('a[data-remote]') + + link + .removeAttr('data-params') + .bindNative('ajax:beforeSend', function() { + ok(true, 'ajax should be triggered') + }) + + Rails.fire(link[0], 'click') + + setTimeout(function() { start() }, 13) +}) + asyncTest('ctrl-clicking on a link still fires ajax for non-GET links and for links with "data-params"', 2, function() { var link = $('a[data-remote]')