alert.js 3.79 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)
 * --------------------------------------------------------------------------
 */

8
9
10
import $ from 'jquery'
import Util from './util'

Johann-S's avatar
Johann-S committed
11
12
13
14
15
16
17
/**
 * ------------------------------------------------------------------------
 * Constants
 * ------------------------------------------------------------------------
 */

const NAME                = 'alert'
XhmikosR's avatar
XhmikosR committed
18
const VERSION             = '4.3.1'
Johann-S's avatar
Johann-S committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const DATA_KEY            = 'bs.alert'
const EVENT_KEY           = `.${DATA_KEY}`
const DATA_API_KEY        = '.data-api'
const JQUERY_NO_CONFLICT  = $.fn[NAME]

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
45

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

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

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

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

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

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

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

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

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

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

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

Johann-S's avatar
Johann-S committed
85
86
    if (selector) {
      parent = document.querySelector(selector)
fat's avatar
fat committed
87
88
    }

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

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

Johann-S's avatar
Johann-S committed
96
97
  _triggerCloseEvent(element) {
    const closeEvent = $.Event(Event.CLOSE)
98

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

Johann-S's avatar
Johann-S committed
103
104
  _removeElement(element) {
    $(element).removeClass(ClassName.SHOW)
Jacob Thornton's avatar
Jacob Thornton committed
105

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

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

Johann-S's avatar
Johann-S committed
113
114
    $(element)
      .one(Util.TRANSITION_END, (event) => this._destroyElement(element, event))
Johann-S's avatar
Johann-S committed
115
116

    Util.emulateTransitionEnd(transitionDuration)
Johann-S's avatar
Johann-S committed
117
  }
fat's avatar
fat committed
118

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

Johann-S's avatar
Johann-S committed
126
  // Static
fat's avatar
fat committed
127

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

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

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

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

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

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

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

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

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

180
export default Alert