alert.js 3.96 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
  }
fat's avatar
fat committed
51

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

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

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

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

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

68
    if (customEvent === null || customEvent.defaultPrevented) {
Johann-S's avatar
Johann-S committed
69
      return
70
71
    }

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

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

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

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

Johann-S's avatar
Johann-S committed
86
    if (selector) {
87
      parent = SelectorEngine.findOne(selector)
fat's avatar
fat committed
88
89
    }

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

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

Johann-S's avatar
Johann-S committed
97
  _triggerCloseEvent(element) {
Johann-S's avatar
Johann-S committed
98
    return EventHandler.trigger(element, Event.CLOSE)
Johann-S's avatar
Johann-S committed
99
  }
fat's avatar
fat committed
100

Johann-S's avatar
Johann-S committed
101
  _removeElement(element) {
Johann-S's avatar
Johann-S committed
102
    element.classList.remove(ClassName.SHOW)
Jacob Thornton's avatar
Jacob Thornton committed
103

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

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

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

Johann-S's avatar
Johann-S committed
116
  _destroyElement(element) {
117
118
119
120
    if (element.parentNode) {
      element.parentNode.removeChild(element)
    }

Johann-S's avatar
Johann-S committed
121
    EventHandler.trigger(element, Event.CLOSED)
Johann-S's avatar
Johann-S committed
122
  }
123

Johann-S's avatar
Johann-S committed
124
  // Static
fat's avatar
fat committed
125

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

Johann-S's avatar
Johann-S committed
130
131
      if (!data) {
        data = new Alert(this)
Johann-S's avatar
Johann-S committed
132
        Data.setData(this, DATA_KEY, data)
Johann-S's avatar
Johann-S committed
133
      }
fat's avatar
fat committed
134

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

  static _getInstance(element) {
    return Data.getData(element, DATA_KEY)
  }
Johann-S's avatar
Johann-S committed
144
}
fat's avatar
fat committed
145

Johann-S's avatar
Johann-S committed
146
147
148
149
150
/**
 * ------------------------------------------------------------------------
 * Data Api implementation
 * ------------------------------------------------------------------------
 */
151
EventHandler.on(document, Event.CLICK_DATA_API, Selector.DISMISS, Alert._handleDismiss(new Alert()))
fat's avatar
fat committed
152

Johann-S's avatar
Johann-S committed
153
154
155
156
/**
 * ------------------------------------------------------------------------
 * jQuery
 * ------------------------------------------------------------------------
157
 * add .alert to jQuery only if jQuery is present
Johann-S's avatar
Johann-S committed
158
 */
fat's avatar
fat committed
159

160
161
const $ = Util.jQuery
if (typeof $ !== 'undefined') {
162
163
164
165
  const JQUERY_NO_CONFLICT = $.fn[NAME]
  $.fn[NAME]               = Alert._jQueryInterface
  $.fn[NAME].Constructor   = Alert
  $.fn[NAME].noConflict    = () => {
166
167
168
    $.fn[NAME] = JQUERY_NO_CONFLICT
    return Alert._jQueryInterface
  }
Johann-S's avatar
Johann-S committed
169
}
fat's avatar
fat committed
170

171
export default Alert