alert.js 4.09 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.1.3): 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.1.3'
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
  const JQUERY_NO_CONFLICT  = $.fn[NAME]
fat's avatar
fat committed
24

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

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

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

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

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

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

    static get VERSION() {
      return VERSION
    }

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

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

66
      const customEvent = this._triggerCloseEvent(rootElement)
fat's avatar
fat committed
67

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

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

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

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

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

86
      if (selector) {
87
        parent = document.querySelector(selector)
88
      }
fat's avatar
fat committed
89

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

      return parent
fat's avatar
fat committed
95
96
    }

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

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

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

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

112
113
      const transitionDuration = Util.getTransitionDurationFromElement(element)

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

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

XhmikosR's avatar
XhmikosR committed
126
    // Static
fat's avatar
fat committed
127

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

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

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

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

150
        alertInstance.close(this)
fat's avatar
fat committed
151
      }
152
    }
fat's avatar
fat committed
153
154
  }

155
156
157
158
159
  /**
   * ------------------------------------------------------------------------
   * Data Api implementation
   * ------------------------------------------------------------------------
   */
fat's avatar
fat committed
160

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

167
168
169
170
171
  /**
   * ------------------------------------------------------------------------
   * jQuery
   * ------------------------------------------------------------------------
   */
fat's avatar
fat committed
172

173
174
  $.fn[NAME]             = Alert._jQueryInterface
  $.fn[NAME].Constructor = Alert
XhmikosR's avatar
XhmikosR committed
175
  $.fn[NAME].noConflict  = () => {
fat's avatar
fat committed
176
    $.fn[NAME] = JQUERY_NO_CONFLICT
177
178
    return Alert._jQueryInterface
  }
fat's avatar
fat committed
179

180
  return Alert
181
})($)
fat's avatar
fat committed
182

183
export default Alert