alert.js 4.05 KB
Newer Older
1
import $ from 'jquery'
2
3
import Util from './util'

fat's avatar
fat committed
4
5
/**
 * --------------------------------------------------------------------------
Mark Otto's avatar
Mark Otto committed
6
 * Bootstrap (v4.0.0): alert.js
fat's avatar
fat committed
7
8
9
10
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
 * --------------------------------------------------------------------------
 */

11
const Alert = (($) => {
12
13
14
15
16
  /**
   * ------------------------------------------------------------------------
   * Constants
   * ------------------------------------------------------------------------
   */
fat's avatar
fat committed
17

18
  const NAME                = 'alert'
Mark Otto's avatar
Mark Otto committed
19
  const VERSION             = '4.0.0'
20
  const DATA_KEY            = 'bs.alert'
fat's avatar
fat committed
21
22
  const EVENT_KEY           = `.${DATA_KEY}`
  const DATA_API_KEY        = '.data-api'
23
24
  const JQUERY_NO_CONFLICT  = $.fn[NAME]
  const TRANSITION_DURATION = 150
fat's avatar
fat committed
25

26
27
28
  const Selector = {
    DISMISS : '[data-dismiss="alert"]'
  }
fat's avatar
fat committed
29

30
  const Event = {
fat's avatar
fat committed
31
32
33
    CLOSE          : `close${EVENT_KEY}`,
    CLOSED         : `closed${EVENT_KEY}`,
    CLICK_DATA_API : `click${EVENT_KEY}${DATA_API_KEY}`
34
  }
fat's avatar
fat committed
35

36
  const ClassName = {
Starsam80's avatar
Starsam80 committed
37
38
39
    ALERT : 'alert',
    FADE  : 'fade',
    SHOW  : 'show'
40
  }
fat's avatar
fat committed
41

42
43
44
45
46
  /**
   * ------------------------------------------------------------------------
   * Class Definition
   * ------------------------------------------------------------------------
   */
fat's avatar
fat committed
47

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

XhmikosR's avatar
XhmikosR committed
53
    // Getters
54
55
56
57
58

    static get VERSION() {
      return VERSION
    }

XhmikosR's avatar
XhmikosR committed
59
    // Public
60
61

    close(element) {
fat's avatar
fat committed
62
      element = element || this._element
fat's avatar
fat committed
63

64
65
      const rootElement = this._getRootElement(element)
      const customEvent = this._triggerCloseEvent(rootElement)
fat's avatar
fat committed
66

67
68
69
      if (customEvent.isDefaultPrevented()) {
        return
      }
fat's avatar
fat committed
70

71
      this._removeElement(rootElement)
fat's avatar
fat committed
72
73
    }

fat's avatar
fat committed
74
75
76
77
78
    dispose() {
      $.removeData(this._element, DATA_KEY)
      this._element = null
    }

XhmikosR's avatar
XhmikosR committed
79
    // Private
fat's avatar
fat committed
80

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

85
86
87
      if (selector) {
        parent = $(selector)[0]
      }
fat's avatar
fat committed
88

89
      if (!parent) {
fat's avatar
fat committed
90
        parent = $(element).closest(`.${ClassName.ALERT}`)[0]
91
92
93
      }

      return parent
fat's avatar
fat committed
94
95
    }

96
    _triggerCloseEvent(element) {
97
      const closeEvent = $.Event(Event.CLOSE)
Jacob Thornton's avatar
Jacob Thornton committed
98

99
100
      $(element).trigger(closeEvent)
      return closeEvent
fat's avatar
fat committed
101
102
    }

103
    _removeElement(element) {
Starsam80's avatar
Starsam80 committed
104
      $(element).removeClass(ClassName.SHOW)
fat's avatar
fat committed
105

106
107
108
109
110
      if (!Util.supportsTransitionEnd() ||
          !$(element).hasClass(ClassName.FADE)) {
        this._destroyElement(element)
        return
      }
fat's avatar
fat committed
111

112
      $(element)
113
        .one(Util.TRANSITION_END, (event) => this._destroyElement(element, event))
114
115
        .emulateTransitionEnd(TRANSITION_DURATION)
    }
fat's avatar
fat committed
116

117
118
119
120
121
    _destroyElement(element) {
      $(element)
        .detach()
        .trigger(Event.CLOSED)
        .remove()
fat's avatar
fat committed
122
123
    }

XhmikosR's avatar
XhmikosR committed
124
    // Static
fat's avatar
fat committed
125

126
127
    static _jQueryInterface(config) {
      return this.each(function () {
128
129
        const $element = $(this)
        let data       = $element.data(DATA_KEY)
fat's avatar
fat committed
130

131
132
133
134
        if (!data) {
          data = new Alert(this)
          $element.data(DATA_KEY, data)
        }
fat's avatar
fat committed
135

136
137
138
139
140
        if (config === 'close') {
          data[config](this)
        }
      })
    }
fat's avatar
fat committed
141

142
143
144
145
146
    static _handleDismiss(alertInstance) {
      return function (event) {
        if (event) {
          event.preventDefault()
        }
fat's avatar
fat committed
147

148
        alertInstance.close(this)
fat's avatar
fat committed
149
      }
150
    }
fat's avatar
fat committed
151
152
  }

153
154
155
156
157
  /**
   * ------------------------------------------------------------------------
   * Data Api implementation
   * ------------------------------------------------------------------------
   */
fat's avatar
fat committed
158

159
  $(document).on(
fat's avatar
fat committed
160
    Event.CLICK_DATA_API,
161
162
163
    Selector.DISMISS,
    Alert._handleDismiss(new Alert())
  )
fat's avatar
fat committed
164

165
166
167
168
169
  /**
   * ------------------------------------------------------------------------
   * jQuery
   * ------------------------------------------------------------------------
   */
fat's avatar
fat committed
170

171
172
173
  $.fn[NAME]             = Alert._jQueryInterface
  $.fn[NAME].Constructor = Alert
  $.fn[NAME].noConflict  = function () {
fat's avatar
fat committed
174
    $.fn[NAME] = JQUERY_NO_CONFLICT
175
176
    return Alert._jQueryInterface
  }
fat's avatar
fat committed
177

178
  return Alert
179
})($)
fat's avatar
fat committed
180

181
export default Alert