alert.js 3.79 KB
Newer Older
1
import $ from 'jquery'
2
3
import Util from './util'

fat's avatar
fat committed
4
5
/**
 * --------------------------------------------------------------------------
Mark Otto's avatar
Mark Otto committed
6
 * Bootstrap (v4.1.3): alert.js
fat's avatar
fat committed
7
8
9
10
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
 * --------------------------------------------------------------------------
 */

Johann-S's avatar
Johann-S committed
11
12
13
14
15
16
17
18
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
/**
 * ------------------------------------------------------------------------
 * Constants
 * ------------------------------------------------------------------------
 */

const NAME                = 'alert'
const VERSION             = '4.1.3'
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
115
116
    $(element)
      .one(Util.TRANSITION_END, (event) => this._destroyElement(element, event))
      .emulateTransitionEnd(transitionDuration)
  }
fat's avatar
fat committed
117

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

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

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

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

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

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

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

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

Johann-S's avatar
Johann-S committed
160
161
162
163
164
$(document).on(
  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
170
/**
 * ------------------------------------------------------------------------
 * jQuery
 * ------------------------------------------------------------------------
 */
fat's avatar
fat committed
171

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

179
export default Alert