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
This commit is contained in:
parent
5e4b70461d
commit
41e3bbdee1
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue