1 | /**
|
---|
2 | * --------------------------------------------------------------------------
|
---|
3 | * Bootstrap alert.js
|
---|
4 | * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
|
---|
5 | * --------------------------------------------------------------------------
|
---|
6 | */
|
---|
7 |
|
---|
8 | import BaseComponent from './base-component.js'
|
---|
9 | import EventHandler from './dom/event-handler.js'
|
---|
10 | import { enableDismissTrigger } from './util/component-functions.js'
|
---|
11 | import { defineJQueryPlugin } from './util/index.js'
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * Constants
|
---|
15 | */
|
---|
16 |
|
---|
17 | const NAME = 'alert'
|
---|
18 | const DATA_KEY = 'bs.alert'
|
---|
19 | const EVENT_KEY = `.${DATA_KEY}`
|
---|
20 |
|
---|
21 | const EVENT_CLOSE = `close${EVENT_KEY}`
|
---|
22 | const EVENT_CLOSED = `closed${EVENT_KEY}`
|
---|
23 | const CLASS_NAME_FADE = 'fade'
|
---|
24 | const CLASS_NAME_SHOW = 'show'
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Class definition
|
---|
28 | */
|
---|
29 |
|
---|
30 | class Alert extends BaseComponent {
|
---|
31 | // Getters
|
---|
32 | static get NAME() {
|
---|
33 | return NAME
|
---|
34 | }
|
---|
35 |
|
---|
36 | // Public
|
---|
37 | close() {
|
---|
38 | const closeEvent = EventHandler.trigger(this._element, EVENT_CLOSE)
|
---|
39 |
|
---|
40 | if (closeEvent.defaultPrevented) {
|
---|
41 | return
|
---|
42 | }
|
---|
43 |
|
---|
44 | this._element.classList.remove(CLASS_NAME_SHOW)
|
---|
45 |
|
---|
46 | const isAnimated = this._element.classList.contains(CLASS_NAME_FADE)
|
---|
47 | this._queueCallback(() => this._destroyElement(), this._element, isAnimated)
|
---|
48 | }
|
---|
49 |
|
---|
50 | // Private
|
---|
51 | _destroyElement() {
|
---|
52 | this._element.remove()
|
---|
53 | EventHandler.trigger(this._element, EVENT_CLOSED)
|
---|
54 | this.dispose()
|
---|
55 | }
|
---|
56 |
|
---|
57 | // Static
|
---|
58 | static jQueryInterface(config) {
|
---|
59 | return this.each(function () {
|
---|
60 | const data = Alert.getOrCreateInstance(this)
|
---|
61 |
|
---|
62 | if (typeof config !== 'string') {
|
---|
63 | return
|
---|
64 | }
|
---|
65 |
|
---|
66 | if (data[config] === undefined || config.startsWith('_') || config === 'constructor') {
|
---|
67 | throw new TypeError(`No method named "${config}"`)
|
---|
68 | }
|
---|
69 |
|
---|
70 | data[config](this)
|
---|
71 | })
|
---|
72 | }
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Data API implementation
|
---|
77 | */
|
---|
78 |
|
---|
79 | enableDismissTrigger(Alert, 'close')
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * jQuery
|
---|
83 | */
|
---|
84 |
|
---|
85 | defineJQueryPlugin(Alert)
|
---|
86 |
|
---|
87 | export default Alert
|
---|