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

Johann-S's avatar
Johann-S committed
8
9
10
import Data from './dom/data'
import EventHandler from './dom/eventHandler'
import SelectorEngine from './dom/selectorEngine'
11
12
import Util from './util'

Johann-S's avatar
Johann-S committed
13
14
15
16
17
18
19
/**
 * ------------------------------------------------------------------------
 * Constants
 * ------------------------------------------------------------------------
 */

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

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
46

Johann-S's avatar
Johann-S committed
47
48
49
class Alert {
  constructor(element) {
    this._element = element
50
51
52
    if (this._element) {
      Data.setData(element, DATA_KEY, this)
    }
53
  }
fat's avatar
fat committed
54

Johann-S's avatar
Johann-S committed
55
56
57
58
  // Getters

  static get VERSION() {
    return VERSION
59
  }
fat's avatar
fat committed
60

Johann-S's avatar
Johann-S committed
61
  // Public
fat's avatar
fat committed
62

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

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

71
    if (customEvent === null || customEvent.defaultPrevented) {
Johann-S's avatar
Johann-S committed
72
      return
73
74
    }

Johann-S's avatar
Johann-S committed
75
76
    this._removeElement(rootElement)
  }
77

Johann-S's avatar
Johann-S committed
78
  dispose() {
Johann-S's avatar
Johann-S committed
79
    Data.removeData(this._element, DATA_KEY)
Johann-S's avatar
Johann-S committed
80
81
    this._element = null
  }
fat's avatar
fat committed
82

Johann-S's avatar
Johann-S committed
83
  // Private
fat's avatar
fat committed
84

Johann-S's avatar
Johann-S committed
85
86
87
  _getRootElement(element) {
    const selector = Util.getSelectorFromElement(element)
    let parent     = false
fat's avatar
fat committed
88

Johann-S's avatar
Johann-S committed
89
    if (selector) {
90
      parent = SelectorEngine.findOne(selector)
fat's avatar
fat committed
91
92
    }

Johann-S's avatar
Johann-S committed
93
    if (!parent) {
Johann-S's avatar
Johann-S committed
94
      parent = SelectorEngine.closest(element, `.${ClassName.ALERT}`)
fat's avatar
fat committed
95
96
    }

Johann-S's avatar
Johann-S committed
97
98
    return parent
  }
fat's avatar
fat committed
99

Johann-S's avatar
Johann-S committed
100
  _triggerCloseEvent(element) {
Johann-S's avatar
Johann-S committed
101
    return EventHandler.trigger(element, Event.CLOSE)
Johann-S's avatar
Johann-S committed
102
  }
fat's avatar
fat committed
103

Johann-S's avatar
Johann-S committed
104
  _removeElement(element) {
Johann-S's avatar
Johann-S committed
105
    element.classList.remove(ClassName.SHOW)
Jacob Thornton's avatar
Jacob Thornton committed
106

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

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

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

Johann-S's avatar
Johann-S committed
119
  _destroyElement(element) {
120
121
122
123
    if (element.parentNode) {
      element.parentNode.removeChild(element)
    }

Johann-S's avatar
Johann-S committed
124
    EventHandler.trigger(element, Event.CLOSED)
Johann-S's avatar
Johann-S committed
125
  }
126

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

Johann-S's avatar
Johann-S committed
129
130
  static _jQueryInterface(config) {
    return this.each(function () {
Johann-S's avatar
Johann-S committed
131
      let data = Data.getData(this, DATA_KEY)
fat's avatar
fat committed
132

Johann-S's avatar
Johann-S committed
133
134
135
      if (!data) {
        data = new Alert(this)
      }
fat's avatar
fat committed
136

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

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

      alertInstance.close(this)
    }
  }

153
154
155
  static _getInstance(element) {
    return Data.getData(element, DATA_KEY)
  }
Johann-S's avatar
Johann-S committed
156
}
fat's avatar
fat committed
157

Johann-S's avatar
Johann-S committed
158
159
160
161
162
/**
 * ------------------------------------------------------------------------
 * Data Api implementation
 * ------------------------------------------------------------------------
 */
163
164
EventHandler
  .on(document, Event.CLICK_DATA_API, Selector.DISMISS, Alert._handleDismiss(new Alert()))
fat's avatar
fat committed
165

Johann-S's avatar
Johann-S committed
166
167
168
169
/**
 * ------------------------------------------------------------------------
 * jQuery
 * ------------------------------------------------------------------------
170
 * add .alert to jQuery only if jQuery is present
Johann-S's avatar
Johann-S committed
171
 */
fat's avatar
fat committed
172

173
174
const $ = Util.jQuery
if (typeof $ !== 'undefined') {
175
176
177
178
  const JQUERY_NO_CONFLICT = $.fn[NAME]
  $.fn[NAME]               = Alert._jQueryInterface
  $.fn[NAME].Constructor   = Alert
  $.fn[NAME].noConflict    = () => {
179
180
181
    $.fn[NAME] = JQUERY_NO_CONFLICT
    return Alert._jQueryInterface
  }
Johann-S's avatar
Johann-S committed
182
}
fat's avatar
fat committed
183

184
export default Alert