alert.js 4.25 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
11
12
13
14
import {
  jQuery as $,
  TRANSITION_END,
  emulateTransitionEnd,
  getSelectorFromElement,
  getTransitionDurationFromElement
} from './util/index'
Johann-S's avatar
Johann-S committed
15
16
17
import Data from './dom/data'
import EventHandler from './dom/eventHandler'
import SelectorEngine from './dom/selectorEngine'
18

Johann-S's avatar
Johann-S committed
19
20
21
22
23
24
25
/**
 * ------------------------------------------------------------------------
 * Constants
 * ------------------------------------------------------------------------
 */

const NAME                = 'alert'
XhmikosR's avatar
XhmikosR committed
26
const VERSION             = '4.3.1'
Johann-S's avatar
Johann-S committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
52

Johann-S's avatar
Johann-S committed
53
54
55
class Alert {
  constructor(element) {
    this._element = element
56
57
58
    if (this._element) {
      Data.setData(element, DATA_KEY, this)
    }
59
  }
fat's avatar
fat committed
60

Johann-S's avatar
Johann-S committed
61
62
63
64
  // Getters

  static get VERSION() {
    return VERSION
65
  }
fat's avatar
fat committed
66

Johann-S's avatar
Johann-S committed
67
  // Public
fat's avatar
fat committed
68

Johann-S's avatar
Johann-S committed
69
70
71
72
  close(element) {
    let rootElement = this._element
    if (element) {
      rootElement = this._getRootElement(element)
fat's avatar
fat committed
73
74
    }

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

77
    if (customEvent === null || customEvent.defaultPrevented) {
Johann-S's avatar
Johann-S committed
78
      return
79
80
    }

Johann-S's avatar
Johann-S committed
81
82
    this._removeElement(rootElement)
  }
83

Johann-S's avatar
Johann-S committed
84
  dispose() {
Johann-S's avatar
Johann-S committed
85
    Data.removeData(this._element, DATA_KEY)
Johann-S's avatar
Johann-S committed
86
87
    this._element = null
  }
fat's avatar
fat committed
88

Johann-S's avatar
Johann-S committed
89
  // Private
fat's avatar
fat committed
90

Johann-S's avatar
Johann-S committed
91
  _getRootElement(element) {
92
    const selector = getSelectorFromElement(element)
Johann-S's avatar
Johann-S committed
93
    let parent     = false
fat's avatar
fat committed
94

Johann-S's avatar
Johann-S committed
95
    if (selector) {
96
      parent = SelectorEngine.findOne(selector)
fat's avatar
fat committed
97
98
    }

Johann-S's avatar
Johann-S committed
99
    if (!parent) {
Johann-S's avatar
Johann-S committed
100
      parent = SelectorEngine.closest(element, `.${ClassName.ALERT}`)
fat's avatar
fat committed
101
102
    }

Johann-S's avatar
Johann-S committed
103
104
    return parent
  }
fat's avatar
fat committed
105

Johann-S's avatar
Johann-S committed
106
  _triggerCloseEvent(element) {
Johann-S's avatar
Johann-S committed
107
    return EventHandler.trigger(element, Event.CLOSE)
Johann-S's avatar
Johann-S committed
108
  }
fat's avatar
fat committed
109

Johann-S's avatar
Johann-S committed
110
  _removeElement(element) {
Johann-S's avatar
Johann-S committed
111
    element.classList.remove(ClassName.SHOW)
Jacob Thornton's avatar
Jacob Thornton committed
112

Johann-S's avatar
Johann-S committed
113
    if (!element.classList.contains(ClassName.FADE)) {
Johann-S's avatar
Johann-S committed
114
115
      this._destroyElement(element)
      return
fat's avatar
fat committed
116
117
    }

118
    const transitionDuration = getTransitionDurationFromElement(element)
fat's avatar
fat committed
119

Johann-S's avatar
Johann-S committed
120
    EventHandler
121
122
      .one(element, TRANSITION_END, (event) => this._destroyElement(element, event))
    emulateTransitionEnd(element, transitionDuration)
Johann-S's avatar
Johann-S committed
123
  }
fat's avatar
fat committed
124

Johann-S's avatar
Johann-S committed
125
  _destroyElement(element) {
126
127
128
129
    if (element.parentNode) {
      element.parentNode.removeChild(element)
    }

Johann-S's avatar
Johann-S committed
130
    EventHandler.trigger(element, Event.CLOSED)
Johann-S's avatar
Johann-S committed
131
  }
132

Johann-S's avatar
Johann-S committed
133
  // Static
fat's avatar
fat committed
134

Johann-S's avatar
Johann-S committed
135
136
  static _jQueryInterface(config) {
    return this.each(function () {
Johann-S's avatar
Johann-S committed
137
      let data = Data.getData(this, DATA_KEY)
fat's avatar
fat committed
138

Johann-S's avatar
Johann-S committed
139
140
141
      if (!data) {
        data = new Alert(this)
      }
fat's avatar
fat committed
142

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

Johann-S's avatar
Johann-S committed
149
150
151
152
153
154
155
156
157
158
  static _handleDismiss(alertInstance) {
    return function (event) {
      if (event) {
        event.preventDefault()
      }

      alertInstance.close(this)
    }
  }

159
160
161
  static _getInstance(element) {
    return Data.getData(element, DATA_KEY)
  }
Johann-S's avatar
Johann-S committed
162
}
fat's avatar
fat committed
163

Johann-S's avatar
Johann-S committed
164
165
166
167
168
/**
 * ------------------------------------------------------------------------
 * Data Api implementation
 * ------------------------------------------------------------------------
 */
169
170
EventHandler
  .on(document, Event.CLICK_DATA_API, Selector.DISMISS, Alert._handleDismiss(new Alert()))
fat's avatar
fat committed
171

Johann-S's avatar
Johann-S committed
172
173
174
175
/**
 * ------------------------------------------------------------------------
 * jQuery
 * ------------------------------------------------------------------------
176
 * add .alert to jQuery only if jQuery is present
Johann-S's avatar
Johann-S committed
177
 */
fat's avatar
fat committed
178

179
if (typeof $ !== 'undefined') {
180
181
182
183
  const JQUERY_NO_CONFLICT = $.fn[NAME]
  $.fn[NAME]               = Alert._jQueryInterface
  $.fn[NAME].Constructor   = Alert
  $.fn[NAME].noConflict    = () => {
184
185
186
    $.fn[NAME] = JQUERY_NO_CONFLICT
    return Alert._jQueryInterface
  }
Johann-S's avatar
Johann-S committed
187
}
fat's avatar
fat committed
188

189
export default Alert