diff options
author | Javan Makhmali <javan@javan.us> | 2018-01-01 13:02:07 -0500 |
---|---|---|
committer | Javan Makhmali <javan@javan.us> | 2018-01-01 13:02:07 -0500 |
commit | 41e3bbdee1a33bd4a8f5c54a808b31ebb598898c (patch) | |
tree | f290f9fc0707a275ed5a76d947b037490a1ee4e1 /actionview/app/assets | |
parent | 5e4b70461dfd869c7d96b2528e666a9dd8e29183 (diff) | |
download | rails-41e3bbdee1a33bd4a8f5c54a808b31ebb598898c.tar.gz rails-41e3bbdee1a33bd4a8f5c54a808b31ebb598898c.tar.bz2 rails-41e3bbdee1a33bd4a8f5c54a808b31ebb598898c.zip |
Improve `preventDefault` fix for rails-ujs
Improves 049a3374aa85f33091f0e7cba8635edd4b4786bd:
* Attempt native `preventDefault()` before stepping in
* Fix that calling `preventDefault()` more than once would throw an error
* Fix that non-cancelable events could be canceled
Diffstat (limited to 'actionview/app/assets')
-rw-r--r-- | actionview/app/assets/javascripts/rails-ujs/utils/event.coffee | 17 |
1 files changed, 11 insertions, 6 deletions
diff --git a/actionview/app/assets/javascripts/rails-ujs/utils/event.coffee b/actionview/app/assets/javascripts/rails-ujs/utils/event.coffee index c8fbc64b51..0cb8ce6ba4 100644 --- a/actionview/app/assets/javascripts/rails-ujs/utils/event.coffee +++ b/actionview/app/assets/javascripts/rails-ujs/utils/event.coffee @@ -7,17 +7,22 @@ CustomEvent = window.CustomEvent if typeof CustomEvent isnt 'function' - CustomEvent = (event, params) -> + window.CustomEvent = (event, params) -> evt = document.createEvent('CustomEvent') evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail) - # IE does not set `defaultPrevented` when `preventDefault()` is called on CustomEvents - # http://stackoverflow.com/questions/23349191/event-preventdefault-is-not-working-in-ie-11-for-custom-events - evt.preventDefault = -> - Object.defineProperty this, 'defaultPrevented', get: -> - true evt + CustomEvent.prototype = window.Event.prototype + # Fix setting `defaultPrevented` when `preventDefault()` is called + # http://stackoverflow.com/questions/23349191/event-preventdefault-is-not-working-in-ie-11-for-custom-events + { preventDefault } = CustomEvent.prototype + CustomEvent.prototype.preventDefault = -> + result = preventDefault.call(this) + if @cancelable and not @defaultPrevented + Object.defineProperty(this, 'defaultPrevented', get: -> true) + result + # Triggers a custom event on an element and returns false if the event result is false # obj:: # a native DOM element |