From ad3a47759e67a411f3534309cdd704f12f6930a7 Mon Sep 17 00:00:00 2001 From: Guillermo Iguaran Date: Fri, 25 Nov 2016 10:27:07 -0500 Subject: Add rails-ujs to Action View --- .../app/assets/javascripts/utils/event.coffee | 40 ++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 actionview/app/assets/javascripts/utils/event.coffee (limited to 'actionview/app/assets/javascripts/utils/event.coffee') diff --git a/actionview/app/assets/javascripts/utils/event.coffee b/actionview/app/assets/javascripts/utils/event.coffee new file mode 100644 index 0000000000..049b2a3ecd --- /dev/null +++ b/actionview/app/assets/javascripts/utils/event.coffee @@ -0,0 +1,40 @@ +#= require ./dom + +{ matches } = Rails + +# Polyfill for CustomEvent in IE9+ +# https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent#Polyfill +CustomEvent = window.CustomEvent + +if typeof CustomEvent is 'function' + CustomEvent = (event, params) -> + evt = document.createEvent('CustomEvent') + evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail) + evt + CustomEvent.prototype = window.Event.prototype + +# Triggers an custom event on an element and returns false if the event result is false +fire = Rails.fire = (obj, name, data) -> + event = new CustomEvent( + name, + bubbles: true, + cancelable: true, + detail: data, + ) + obj.dispatchEvent(event) + !event.defaultPrevented + +# Helper function, needed to provide consistent behavior in IE +Rails.stopEverything = (e) -> + fire(e.target, 'ujs:everythingStopped') + e.preventDefault() + e.stopPropagation() + e.stopImmediatePropagation() + +Rails.delegate = (element, selector, eventType, handler) -> + element.addEventListener eventType, (e) -> + target = e.target + target = target.parentNode until not (target instanceof Element) or matches(target, selector) + if target instanceof Element and handler.call(target, e) == false + e.preventDefault() + e.stopPropagation() -- cgit v1.2.3