source: trip-planner-front/node_modules/bootstrap/js/src/toast.js@ 6a3a178

Last change on this file since 6a3a178 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 5.8 KB
Line 
1/**
2 * --------------------------------------------------------------------------
3 * Bootstrap (v5.1.3): toast.js
4 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
5 * --------------------------------------------------------------------------
6 */
7
8import {
9 defineJQueryPlugin,
10 reflow,
11 typeCheckConfig
12} from './util/index'
13import EventHandler from './dom/event-handler'
14import Manipulator from './dom/manipulator'
15import BaseComponent from './base-component'
16import { enableDismissTrigger } from './util/component-functions'
17
18/**
19 * ------------------------------------------------------------------------
20 * Constants
21 * ------------------------------------------------------------------------
22 */
23
24const NAME = 'toast'
25const DATA_KEY = 'bs.toast'
26const EVENT_KEY = `.${DATA_KEY}`
27
28const EVENT_MOUSEOVER = `mouseover${EVENT_KEY}`
29const EVENT_MOUSEOUT = `mouseout${EVENT_KEY}`
30const EVENT_FOCUSIN = `focusin${EVENT_KEY}`
31const EVENT_FOCUSOUT = `focusout${EVENT_KEY}`
32const EVENT_HIDE = `hide${EVENT_KEY}`
33const EVENT_HIDDEN = `hidden${EVENT_KEY}`
34const EVENT_SHOW = `show${EVENT_KEY}`
35const EVENT_SHOWN = `shown${EVENT_KEY}`
36
37const CLASS_NAME_FADE = 'fade'
38const CLASS_NAME_HIDE = 'hide' // @deprecated - kept here only for backwards compatibility
39const CLASS_NAME_SHOW = 'show'
40const CLASS_NAME_SHOWING = 'showing'
41
42const DefaultType = {
43 animation: 'boolean',
44 autohide: 'boolean',
45 delay: 'number'
46}
47
48const Default = {
49 animation: true,
50 autohide: true,
51 delay: 5000
52}
53
54/**
55 * ------------------------------------------------------------------------
56 * Class Definition
57 * ------------------------------------------------------------------------
58 */
59
60class Toast extends BaseComponent {
61 constructor(element, config) {
62 super(element)
63
64 this._config = this._getConfig(config)
65 this._timeout = null
66 this._hasMouseInteraction = false
67 this._hasKeyboardInteraction = false
68 this._setListeners()
69 }
70
71 // Getters
72
73 static get DefaultType() {
74 return DefaultType
75 }
76
77 static get Default() {
78 return Default
79 }
80
81 static get NAME() {
82 return NAME
83 }
84
85 // Public
86
87 show() {
88 const showEvent = EventHandler.trigger(this._element, EVENT_SHOW)
89
90 if (showEvent.defaultPrevented) {
91 return
92 }
93
94 this._clearTimeout()
95
96 if (this._config.animation) {
97 this._element.classList.add(CLASS_NAME_FADE)
98 }
99
100 const complete = () => {
101 this._element.classList.remove(CLASS_NAME_SHOWING)
102 EventHandler.trigger(this._element, EVENT_SHOWN)
103
104 this._maybeScheduleHide()
105 }
106
107 this._element.classList.remove(CLASS_NAME_HIDE) // @deprecated
108 reflow(this._element)
109 this._element.classList.add(CLASS_NAME_SHOW)
110 this._element.classList.add(CLASS_NAME_SHOWING)
111
112 this._queueCallback(complete, this._element, this._config.animation)
113 }
114
115 hide() {
116 if (!this._element.classList.contains(CLASS_NAME_SHOW)) {
117 return
118 }
119
120 const hideEvent = EventHandler.trigger(this._element, EVENT_HIDE)
121
122 if (hideEvent.defaultPrevented) {
123 return
124 }
125
126 const complete = () => {
127 this._element.classList.add(CLASS_NAME_HIDE) // @deprecated
128 this._element.classList.remove(CLASS_NAME_SHOWING)
129 this._element.classList.remove(CLASS_NAME_SHOW)
130 EventHandler.trigger(this._element, EVENT_HIDDEN)
131 }
132
133 this._element.classList.add(CLASS_NAME_SHOWING)
134 this._queueCallback(complete, this._element, this._config.animation)
135 }
136
137 dispose() {
138 this._clearTimeout()
139
140 if (this._element.classList.contains(CLASS_NAME_SHOW)) {
141 this._element.classList.remove(CLASS_NAME_SHOW)
142 }
143
144 super.dispose()
145 }
146
147 // Private
148
149 _getConfig(config) {
150 config = {
151 ...Default,
152 ...Manipulator.getDataAttributes(this._element),
153 ...(typeof config === 'object' && config ? config : {})
154 }
155
156 typeCheckConfig(NAME, config, this.constructor.DefaultType)
157
158 return config
159 }
160
161 _maybeScheduleHide() {
162 if (!this._config.autohide) {
163 return
164 }
165
166 if (this._hasMouseInteraction || this._hasKeyboardInteraction) {
167 return
168 }
169
170 this._timeout = setTimeout(() => {
171 this.hide()
172 }, this._config.delay)
173 }
174
175 _onInteraction(event, isInteracting) {
176 switch (event.type) {
177 case 'mouseover':
178 case 'mouseout':
179 this._hasMouseInteraction = isInteracting
180 break
181 case 'focusin':
182 case 'focusout':
183 this._hasKeyboardInteraction = isInteracting
184 break
185 default:
186 break
187 }
188
189 if (isInteracting) {
190 this._clearTimeout()
191 return
192 }
193
194 const nextElement = event.relatedTarget
195 if (this._element === nextElement || this._element.contains(nextElement)) {
196 return
197 }
198
199 this._maybeScheduleHide()
200 }
201
202 _setListeners() {
203 EventHandler.on(this._element, EVENT_MOUSEOVER, event => this._onInteraction(event, true))
204 EventHandler.on(this._element, EVENT_MOUSEOUT, event => this._onInteraction(event, false))
205 EventHandler.on(this._element, EVENT_FOCUSIN, event => this._onInteraction(event, true))
206 EventHandler.on(this._element, EVENT_FOCUSOUT, event => this._onInteraction(event, false))
207 }
208
209 _clearTimeout() {
210 clearTimeout(this._timeout)
211 this._timeout = null
212 }
213
214 // Static
215
216 static jQueryInterface(config) {
217 return this.each(function () {
218 const data = Toast.getOrCreateInstance(this, config)
219
220 if (typeof config === 'string') {
221 if (typeof data[config] === 'undefined') {
222 throw new TypeError(`No method named "${config}"`)
223 }
224
225 data[config](this)
226 }
227 })
228 }
229}
230
231enableDismissTrigger(Toast)
232
233/**
234 * ------------------------------------------------------------------------
235 * jQuery
236 * ------------------------------------------------------------------------
237 * add .Toast to jQuery only if jQuery is present
238 */
239
240defineJQueryPlugin(Toast)
241
242export default Toast
Note: See TracBrowser for help on using the repository browser.