alert.js 4.02 KB
Newer Older
1
2
3
import Util from './util'


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

fat's avatar
fat committed
11
const Alert = (($) => {
fat's avatar
fat committed
12
13


14
15
16
17
18
  /**
   * ------------------------------------------------------------------------
   * Constants
   * ------------------------------------------------------------------------
   */
fat's avatar
fat committed
19

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

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

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

38
39
40
41
42
  const ClassName = {
    ALERT : 'alert',
    FADE  : 'fade',
    IN    : 'in'
  }
fat's avatar
fat committed
43
44


45
46
47
48
49
  /**
   * ------------------------------------------------------------------------
   * Class Definition
   * ------------------------------------------------------------------------
   */
fat's avatar
fat committed
50

51
  class Alert {
fat's avatar
fat committed
52

53
    constructor(element) {
fat's avatar
fat committed
54
      this._element = element
fat's avatar
fat committed
55
56
57
    }


58
59
60
61
62
63
64
    // getters

    static get VERSION() {
      return VERSION
    }


65
66
67
    // public

    close(element) {
fat's avatar
fat committed
68
      element = element || this._element
fat's avatar
fat committed
69

70
71
      let rootElement = this._getRootElement(element)
      let customEvent = this._triggerCloseEvent(rootElement)
fat's avatar
fat committed
72

73
74
75
      if (customEvent.isDefaultPrevented()) {
        return
      }
fat's avatar
fat committed
76

77
      this._removeElement(rootElement)
fat's avatar
fat committed
78
79
    }

fat's avatar
fat committed
80
81
82
83
84
    dispose() {
      $.removeData(this._element, DATA_KEY)
      this._element = null
    }

fat's avatar
fat committed
85

86
    // private
fat's avatar
fat committed
87

88
89
90
    _getRootElement(element) {
      let parent   = false
      let selector = Util.getSelectorFromElement(element)
fat's avatar
fat committed
91

92
93
94
      if (selector) {
        parent = $(selector)[0]
      }
fat's avatar
fat committed
95

96
      if (!parent) {
fat's avatar
fat committed
97
        parent = $(element).closest(`.${ClassName.ALERT}`)[0]
98
99
100
      }

      return parent
fat's avatar
fat committed
101
102
    }

103
104
105
106
    _triggerCloseEvent(element) {
      var closeEvent = $.Event(Event.CLOSE)
      $(element).trigger(closeEvent)
      return closeEvent
fat's avatar
fat committed
107
108
    }

109
110
    _removeElement(element) {
      $(element).removeClass(ClassName.IN)
fat's avatar
fat committed
111

112
113
114
115
116
      if (!Util.supportsTransitionEnd() ||
          !$(element).hasClass(ClassName.FADE)) {
        this._destroyElement(element)
        return
      }
fat's avatar
fat committed
117

118
      $(element)
Jacob Thornton's avatar
Jacob Thornton committed
119
        .one(Util.TRANSITION_END, $.proxy(this._destroyElement, this, element))
120
121
        .emulateTransitionEnd(TRANSITION_DURATION)
    }
fat's avatar
fat committed
122

123
124
125
126
127
    _destroyElement(element) {
      $(element)
        .detach()
        .trigger(Event.CLOSED)
        .remove()
fat's avatar
fat committed
128
129
130
    }


131
    // static
fat's avatar
fat committed
132

133
134
135
136
    static _jQueryInterface(config) {
      return this.each(function () {
        let $element = $(this)
        let data     = $element.data(DATA_KEY)
fat's avatar
fat committed
137

138
139
140
141
        if (!data) {
          data = new Alert(this)
          $element.data(DATA_KEY, data)
        }
fat's avatar
fat committed
142

143
144
145
146
147
        if (config === 'close') {
          data[config](this)
        }
      })
    }
fat's avatar
fat committed
148

149
150
151
152
153
    static _handleDismiss(alertInstance) {
      return function (event) {
        if (event) {
          event.preventDefault()
        }
fat's avatar
fat committed
154

155
        alertInstance.close(this)
fat's avatar
fat committed
156
      }
157
158
    }

fat's avatar
fat committed
159
160
161
  }


162
163
164
165
166
  /**
   * ------------------------------------------------------------------------
   * Data Api implementation
   * ------------------------------------------------------------------------
   */
fat's avatar
fat committed
167

168
  $(document).on(
fat's avatar
fat committed
169
    Event.CLICK_DATA_API,
170
171
172
    Selector.DISMISS,
    Alert._handleDismiss(new Alert())
  )
fat's avatar
fat committed
173
174


175
176
177
178
179
  /**
   * ------------------------------------------------------------------------
   * jQuery
   * ------------------------------------------------------------------------
   */
fat's avatar
fat committed
180

181
182
183
  $.fn[NAME]             = Alert._jQueryInterface
  $.fn[NAME].Constructor = Alert
  $.fn[NAME].noConflict  = function () {
fat's avatar
fat committed
184
    $.fn[NAME] = JQUERY_NO_CONFLICT
185
186
    return Alert._jQueryInterface
  }
fat's avatar
fat committed
187

188
  return Alert
fat's avatar
fat committed
189

fat's avatar
fat committed
190
})(jQuery)
fat's avatar
fat committed
191

192
export default Alert