eventHandler.js 1.03 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)
 * --------------------------------------------------------------------------
 */

Johann-S's avatar
Johann-S committed
8
const EventHandler = {
Johann-S's avatar
Johann-S committed
9
  on(element, event, handler) {
Johann-S's avatar
Johann-S committed
10
    if (typeof event !== 'string' || typeof element === 'undefined') {
Johann-S's avatar
Johann-S committed
11
12
13
14
15
16
17
18
19
20
21
      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
22
    EventHandler.on(element, event, complete)
Johann-S's avatar
Johann-S committed
23
24
25
  },

  trigger(element, event) {
Johann-S's avatar
Johann-S committed
26
27
    if (typeof event !== 'string' || typeof element === 'undefined') {
      return null
Johann-S's avatar
Johann-S committed
28
29
30
31
32
33
34
    }

    const eventToDispatch = new CustomEvent(event, {
      bubbles: true,
      cancelable: true
    })
    element.dispatchEvent(eventToDispatch)
Johann-S's avatar
Johann-S committed
35
36

    return eventToDispatch
Johann-S's avatar
Johann-S committed
37
38
39
  }
}

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