alert.js 3.92 KB
Newer Older
fat's avatar
fat committed
1
2
/**
 * --------------------------------------------------------------------------
XhmikosR's avatar
XhmikosR committed
3
 * Bootstrap (v4.3.1): alert.js
fat's avatar
fat committed
4
5
6
7
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
 * --------------------------------------------------------------------------
 */

Johann-S's avatar
Johann-S committed
8
9
10
import Data from './dom/data'
import EventHandler from './dom/eventHandler'
import SelectorEngine from './dom/selectorEngine'
11
12
import Util from './util'

Johann-S's avatar
Johann-S committed
13
14
15
16
17
18
19
/**
 * ------------------------------------------------------------------------
 * Constants
 * ------------------------------------------------------------------------
 */

const NAME                = 'alert'
XhmikosR's avatar
XhmikosR committed
20
const VERSION             = '4.3.1'
Johann-S's avatar
Johann-S committed
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
const DATA_KEY            = 'bs.alert'
const EVENT_KEY           = `.${DATA_KEY}`
const DATA_API_KEY        = '.data-api'

const Selector = {
  DISMISS : '[data-dismiss="alert"]'
}

const Event = {
  CLOSE          : `close${EVENT_KEY}`,
  CLOSED         : `closed${EVENT_KEY}`,
  CLICK_DATA_API : `click${EVENT_KEY}${DATA_API_KEY}`
}

const ClassName = {
  ALERT : 'alert',
  FADE  : 'fade',
  SHOW  : 'show'
}

/**
 * ------------------------------------------------------------------------
 * Class Definition
 * ------------------------------------------------------------------------
 */
fat's avatar
fat committed
46

Johann-S's avatar
Johann-S committed
47
48
49
class Alert {
  constructor(element) {
    this._element = element
50
  }
fat's avatar
fat committed
51

Johann-S's avatar
Johann-S committed
52
53
54
55
  // Getters

  static get VERSION() {
    return VERSION
56
  }
fat's avatar
fat committed
57

Johann-S's avatar
Johann-S committed
58
  // Public
fat's avatar
fat committed
59

Johann-S's avatar
Johann-S committed
60
61
62
63
  close(element) {
    let rootElement = this._element
    if (element) {
      rootElement = this._getRootElement(element)
fat's avatar
fat committed
64
65
    }

Johann-S's avatar
Johann-S committed
66
    const customEvent = this._triggerCloseEvent(rootElement)
67

68
    if (customEvent === null || customEvent.defaultPrevented) {
Johann-S's avatar
Johann-S committed
69
      return
70
71
    }

Johann-S's avatar
Johann-S committed
72
73
    this._removeElement(rootElement)
  }
74

Johann-S's avatar
Johann-S committed
75
  dispose() {
Johann-S's avatar
Johann-S committed
76
    Data.removeData(this._element, DATA_KEY)
Johann-S's avatar
Johann-S committed
77
78
    this._element = null
  }
fat's avatar
fat committed
79

Johann-S's avatar
Johann-S committed
80
  // Private
fat's avatar
fat committed
81

Johann-S's avatar
Johann-S committed
82
83
84
  _getRootElement(element) {
    const selector = Util.getSelectorFromElement(element)
    let parent     = false
fat's avatar
fat committed
85

Johann-S's avatar
Johann-S committed
86
    if (selector) {
87
      parent = SelectorEngine.findOne(selector)
fat's avatar
fat committed
88
89
    }

Johann-S's avatar
Johann-S committed
90
    if (!parent) {
Johann-S's avatar
Johann-S committed
91
      parent = SelectorEngine.closest(element, `.${ClassName.ALERT}`)
fat's avatar
fat committed
92
93
    }

Johann-S's avatar
Johann-S committed
94
95
    return parent
  }
fat's avatar
fat committed
96

Johann-S's avatar
Johann-S committed
97
  _triggerCloseEvent(element) {
Johann-S's avatar
Johann-S committed
98
    return EventHandler.trigger(element, Event.CLOSE)
Johann-S's avatar
Johann-S committed
99
  }
fat's avatar
fat committed
100

Johann-S's avatar
Johann-S committed
101
  _removeElement(element) {
Johann-S's avatar
Johann-S committed
102
    element.classList.remove(ClassName.SHOW)
Jacob Thornton's avatar
Jacob Thornton committed
103

Johann-S's avatar
Johann-S committed
104
    if (!element.classList.contains(ClassName.FADE)) {
Johann-S's avatar
Johann-S committed
105
106
      this._destroyElement(element)
      return
fat's avatar
fat committed
107
108
    }

Johann-S's avatar
Johann-S committed
109
    const transitionDuration = Util.getTransitionDurationFromElement(element)
fat's avatar
fat committed
110

Johann-S's avatar
Johann-S committed
111
112
113
    EventHandler
      .one(element, Util.TRANSITION_END, (event) => this._destroyElement(element, event))
    Util.emulateTransitionEnd(element, transitionDuration)
Johann-S's avatar
Johann-S committed
114
  }
fat's avatar
fat committed
115

Johann-S's avatar
Johann-S committed
116
  _destroyElement(element) {
Johann-S's avatar
Johann-S committed
117
118
    EventHandler.trigger(element, Event.CLOSED)
    element.parentNode.removeChild(element)
Johann-S's avatar
Johann-S committed
119
  }
120

Johann-S's avatar
Johann-S committed
121
  // Static
fat's avatar
fat committed
122

Johann-S's avatar
Johann-S committed
123
124
  static _jQueryInterface(config) {
    return this.each(function () {
Johann-S's avatar
Johann-S committed
125
      let data = Data.getData(this, DATA_KEY)
fat's avatar
fat committed
126

Johann-S's avatar
Johann-S committed
127
128
      if (!data) {
        data = new Alert(this)
Johann-S's avatar
Johann-S committed
129
        Data.setData(this, DATA_KEY, data)
Johann-S's avatar
Johann-S committed
130
      }
fat's avatar
fat committed
131

Johann-S's avatar
Johann-S committed
132
133
134
135
136
137
      if (config === 'close') {
        data[config](this)
      }
    })
  }
}
fat's avatar
fat committed
138

Johann-S's avatar
Johann-S committed
139
140
141
142
143
/**
 * ------------------------------------------------------------------------
 * Data Api implementation
 * ------------------------------------------------------------------------
 */
144
EventHandler.on(document, Event.CLICK_DATA_API, Selector.DISMISS, Alert._handleDismiss(new Alert()))
fat's avatar
fat committed
145

Johann-S's avatar
Johann-S committed
146
147
148
149
/**
 * ------------------------------------------------------------------------
 * jQuery
 * ------------------------------------------------------------------------
150
 * add .alert to jQuery only if jQuery is present
Johann-S's avatar
Johann-S committed
151
 */
fat's avatar
fat committed
152

153
if (typeof window.$ !== 'undefined' || typeof window.jQuery !== 'undefined') {
154
155
156
157
158
  const $                  = window.$ || window.jQuery
  const JQUERY_NO_CONFLICT = $.fn[NAME]
  $.fn[NAME]               = Alert._jQueryInterface
  $.fn[NAME].Constructor   = Alert
  $.fn[NAME].noConflict    = () => {
159
160
161
    $.fn[NAME] = JQUERY_NO_CONFLICT
    return Alert._jQueryInterface
  }
Johann-S's avatar
Johann-S committed
162
}
fat's avatar
fat committed
163

164
export default Alert