alert.js 3.88 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
46
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
47

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

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

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

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

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

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

Johann-S's avatar
Johann-S committed
69
    if (customEvent.defaultPrevented) {
Johann-S's avatar
Johann-S committed
70
      return
71
72
    }

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

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

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

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

Johann-S's avatar
Johann-S committed
87
    if (selector) {
88
      parent = SelectorEngine.find(selector)[0]
fat's avatar
fat committed
89
90
    }

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

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

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

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

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

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

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

Johann-S's avatar
Johann-S committed
117
  _destroyElement(element) {
Johann-S's avatar
Johann-S committed
118
119
    EventHandler.trigger(element, Event.CLOSED)
    element.parentNode.removeChild(element)
Johann-S's avatar
Johann-S committed
120
  }
121

Johann-S's avatar
Johann-S committed
122
  // Static
fat's avatar
fat committed
123

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

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

Johann-S's avatar
Johann-S committed
133
134
135
136
137
138
      if (config === 'close') {
        data[config](this)
      }
    })
  }
}
fat's avatar
fat committed
139

Johann-S's avatar
Johann-S committed
140
141
142
143
144
/**
 * ------------------------------------------------------------------------
 * Data Api implementation
 * ------------------------------------------------------------------------
 */
145
EventHandler.on(document, Event.CLICK_DATA_API, Selector.DISMISS, Alert._handleDismiss(new Alert()))
fat's avatar
fat committed
146

Johann-S's avatar
Johann-S committed
147
148
149
150
/**
 * ------------------------------------------------------------------------
 * jQuery
 * ------------------------------------------------------------------------
151
 * add .alert to jQuery only if jQuery is present
Johann-S's avatar
Johann-S committed
152
 */
fat's avatar
fat committed
153

154
155
156
157
158
159
160
161
if (typeof window.$ !== 'undefined' || typeof window.jQuery !== 'undefined') {
  const $ = window.$ || window.jQuery
  $.fn[NAME]             = Alert._jQueryInterface
  $.fn[NAME].Constructor = Alert
  $.fn[NAME].noConflict  = () => {
    $.fn[NAME] = JQUERY_NO_CONFLICT
    return Alert._jQueryInterface
  }
Johann-S's avatar
Johann-S committed
162
}
fat's avatar
fat committed
163

164
export default Alert