eventHandler.js 2.68 KB
Newer Older
Johann-S's avatar
Johann-S committed
1
2
/**
 * --------------------------------------------------------------------------
Johann-S's avatar
Johann-S committed
3
 * Bootstrap (v4.0.0-beta): dom/eventHandler.js
Johann-S's avatar
Johann-S committed
4
5
6
7
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
 * --------------------------------------------------------------------------
 */

8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// defaultPrevented is broken in IE.
// https://connect.microsoft.com/IE/feedback/details/790389/event-defaultprevented-returns-false-after-preventdefault-was-called
const workingDefaultPrevented = (() => {
  const e = document.createEvent('CustomEvent')
  e.initEvent('Bootstrap', true, true)
  e.preventDefault()
  return e.defaultPrevented
})()

// CustomEvent polyfill for IE (see: https://mzl.la/2v76Zvn)
if (typeof window.CustomEvent !== 'function') {
  window.CustomEvent = (event, params) => {
    params = params || {
      bubbles: false,
      cancelable: false
    }
    const evt = document.createEvent('CustomEvent')
    evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail)
    if (!workingDefaultPrevented) {
      const origPreventDefault = Event.prototype.preventDefault
      evt.preventDefault = () => {
        if (!evt.cancelable) {
          return
        }

        origPreventDefault.call(evt)
        Object.defineProperty(evt, 'defaultPrevented', {
          get() {
            return true
          },
          configurable: true
        })
      }
    }
    return evt
  }

  window.CustomEvent.prototype = window.Event.prototype
}

// Event constructor shim
if (!window.Event || typeof window.Event !== 'function') {
  const origEvent = window.Event
  window.Event = (inType, params) => {
    params = params || {}
    const e = document.createEvent('Event')
    e.initEvent(inType, Boolean(params.bubbles), Boolean(params.cancelable))
    return e
  }
  window.Event.prototype = origEvent.prototype
}

Johann-S's avatar
Johann-S committed
60
const EventHandler = {
Johann-S's avatar
Johann-S committed
61
  on(element, event, handler) {
Johann-S's avatar
Johann-S committed
62
    if (typeof event !== 'string' || typeof element === 'undefined') {
Johann-S's avatar
Johann-S committed
63
64
65
66
67
68
69
70
71
72
73
      return
    }
    element.addEventListener(event, handler, false)
  },

  one(element, event, handler) {
    const complete = () => {
      /* eslint func-style: off */
      handler()
      element.removeEventListener(event, complete, false)
    }
Johann-S's avatar
Johann-S committed
74
    EventHandler.on(element, event, complete)
Johann-S's avatar
Johann-S committed
75
76
77
  },

  trigger(element, event) {
Johann-S's avatar
Johann-S committed
78
79
    if (typeof event !== 'string' || typeof element === 'undefined') {
      return null
Johann-S's avatar
Johann-S committed
80
81
82
83
84
85
    }

    const eventToDispatch = new CustomEvent(event, {
      bubbles: true,
      cancelable: true
    })
86
87
88

    // Add a function 'isDefaultPrevented'
    eventToDispatch.isDefaultPrevented = () => eventToDispatch.defaultPrevented
Johann-S's avatar
Johann-S committed
89
    element.dispatchEvent(eventToDispatch)
Johann-S's avatar
Johann-S committed
90
91

    return eventToDispatch
Johann-S's avatar
Johann-S committed
92
93
94
  }
}

Johann-S's avatar
Johann-S committed
95
export default EventHandler