alert.js 4.06 KB
Newer Older
1
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-beta): alert.js
fat's avatar
fat committed
7
8
9
10
 * 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
  const NAME                = 'alert'
Mark Otto's avatar
Mark Otto committed
21
  const VERSION             = '4.0.0-beta'
22
  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
  const ClassName = {
Starsam80's avatar
Starsam80 committed
39
40
41
    ALERT : 'alert',
    FADE  : 'fade',
    SHOW  : 'show'
42
  }
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
      const rootElement = this._getRootElement(element)
      const 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
    _getRootElement(element) {
89
90
      const selector = Util.getSelectorFromElement(element)
      let parent     = false
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
    _triggerCloseEvent(element) {
104
      const closeEvent = $.Event(Event.CLOSE)
Jacob Thornton's avatar
Jacob Thornton committed
105

106
107
      $(element).trigger(closeEvent)
      return closeEvent
fat's avatar
fat committed
108
109
    }

110
    _removeElement(element) {
Starsam80's avatar
Starsam80 committed
111
      $(element).removeClass(ClassName.SHOW)
fat's avatar
fat committed
112

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

119
      $(element)
120
        .one(Util.TRANSITION_END, (event) => this._destroyElement(element, event))
121
122
        .emulateTransitionEnd(TRANSITION_DURATION)
    }
fat's avatar
fat committed
123

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


132
    // static
fat's avatar
fat committed
133

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

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

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

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

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

fat's avatar
fat committed
160
161
162
  }


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

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


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

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

189
  return Alert
fat's avatar
fat committed
190

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

193
export default Alert