• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

twbs / bootstrap / 15237876905

25 May 2025 12:29PM CUT coverage: 96.07%. Remained the same
15237876905

push

github

web-flow
Examples: remove unused `myChart` variable (#41494)

666 of 726 branches covered (91.74%)

Branch coverage included in aggregate %.

2023 of 2073 relevant lines covered (97.59%)

231.53 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.3
/js/src/toast.js
1
/**
2
 * --------------------------------------------------------------------------
3
 * Bootstrap toast.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, reflow } from './util/index.js'
12

13
/**
14
 * Constants
15
 */
16

17
const NAME = 'toast'
1✔
18
const DATA_KEY = 'bs.toast'
1✔
19
const EVENT_KEY = `.${DATA_KEY}`
1✔
20

21
const EVENT_MOUSEOVER = `mouseover${EVENT_KEY}`
1✔
22
const EVENT_MOUSEOUT = `mouseout${EVENT_KEY}`
1✔
23
const EVENT_FOCUSIN = `focusin${EVENT_KEY}`
1✔
24
const EVENT_FOCUSOUT = `focusout${EVENT_KEY}`
1✔
25
const EVENT_HIDE = `hide${EVENT_KEY}`
1✔
26
const EVENT_HIDDEN = `hidden${EVENT_KEY}`
1✔
27
const EVENT_SHOW = `show${EVENT_KEY}`
1✔
28
const EVENT_SHOWN = `shown${EVENT_KEY}`
1✔
29

30
const CLASS_NAME_FADE = 'fade'
1✔
31
const CLASS_NAME_HIDE = 'hide' // @deprecated - kept here only for backwards compatibility
1✔
32
const CLASS_NAME_SHOW = 'show'
1✔
33
const CLASS_NAME_SHOWING = 'showing'
1✔
34

35
const DefaultType = {
1✔
36
  animation: 'boolean',
37
  autohide: 'boolean',
38
  delay: 'number'
39
}
40

41
const Default = {
1✔
42
  animation: true,
43
  autohide: true,
44
  delay: 5000
45
}
46

47
/**
48
 * Class definition
49
 */
50

51
class Toast extends BaseComponent {
52
  constructor(element, config) {
53
    super(element, config)
28✔
54

55
    this._timeout = null
28✔
56
    this._hasMouseInteraction = false
28✔
57
    this._hasKeyboardInteraction = false
28✔
58
    this._setListeners()
28✔
59
  }
60

61
  // Getters
62
  static get Default() {
63
    return Default
29✔
64
  }
65

66
  static get DefaultType() {
67
    return DefaultType
29✔
68
  }
69

70
  static get NAME() {
71
    return NAME
60✔
72
  }
73

74
  // Public
75
  show() {
76
    const showEvent = EventHandler.trigger(this._element, EVENT_SHOW)
15✔
77

78
    if (showEvent.defaultPrevented) {
15✔
79
      return
1✔
80
    }
81

82
    this._clearTimeout()
14✔
83

84
    if (this._config.animation) {
14✔
85
      this._element.classList.add(CLASS_NAME_FADE)
11✔
86
    }
87

88
    const complete = () => {
14✔
89
      this._element.classList.remove(CLASS_NAME_SHOWING)
14✔
90
      EventHandler.trigger(this._element, EVENT_SHOWN)
14✔
91

92
      this._maybeScheduleHide()
14✔
93
    }
94

95
    this._element.classList.remove(CLASS_NAME_HIDE) // @deprecated
14✔
96
    reflow(this._element)
14✔
97
    this._element.classList.add(CLASS_NAME_SHOW, CLASS_NAME_SHOWING)
14✔
98

99
    this._queueCallback(complete, this._element, this._config.animation)
14✔
100
  }
101

102
  hide() {
103
    if (!this.isShown()) {
11✔
104
      return
1✔
105
    }
106

107
    const hideEvent = EventHandler.trigger(this._element, EVENT_HIDE)
10✔
108

109
    if (hideEvent.defaultPrevented) {
10✔
110
      return
2✔
111
    }
112

113
    const complete = () => {
8✔
114
      this._element.classList.add(CLASS_NAME_HIDE) // @deprecated
8✔
115
      this._element.classList.remove(CLASS_NAME_SHOWING, CLASS_NAME_SHOW)
8✔
116
      EventHandler.trigger(this._element, EVENT_HIDDEN)
8✔
117
    }
118

119
    this._element.classList.add(CLASS_NAME_SHOWING)
8✔
120
    this._queueCallback(complete, this._element, this._config.animation)
8✔
121
  }
122

123
  dispose() {
124
    this._clearTimeout()
2✔
125

126
    if (this.isShown()) {
2✔
127
      this._element.classList.remove(CLASS_NAME_SHOW)
1✔
128
    }
129

130
    super.dispose()
2✔
131
  }
132

133
  isShown() {
134
    return this._element.classList.contains(CLASS_NAME_SHOW)
13✔
135
  }
136

137
  // Private
138
  _maybeScheduleHide() {
139
    if (!this._config.autohide) {
20✔
140
      return
4✔
141
    }
142

143
    if (this._hasMouseInteraction || this._hasKeyboardInteraction) {
16✔
144
      return
3✔
145
    }
146

147
    this._timeout = setTimeout(() => {
13✔
148
      this.hide()
7✔
149
    }, this._config.delay)
150
  }
151

152
  _onInteraction(event, isInteracting) {
153
    switch (event.type) {
14✔
154
      case 'mouseover':
22!
155
      case 'mouseout': {
156
        this._hasMouseInteraction = isInteracting
6✔
157
        break
6✔
158
      }
159

160
      case 'focusin':
161
      case 'focusout': {
162
        this._hasKeyboardInteraction = isInteracting
8✔
163
        break
8✔
164
      }
165

166
      default: {
167
        break
×
168
      }
169
    }
170

171
    if (isInteracting) {
14✔
172
      this._clearTimeout()
8✔
173
      return
8✔
174
    }
175

176
    const nextElement = event.relatedTarget
6✔
177
    if (this._element === nextElement || this._element.contains(nextElement)) {
6!
178
      return
×
179
    }
180

181
    this._maybeScheduleHide()
6✔
182
  }
183

184
  _setListeners() {
185
    EventHandler.on(this._element, EVENT_MOUSEOVER, event => this._onInteraction(event, true))
28✔
186
    EventHandler.on(this._element, EVENT_MOUSEOUT, event => this._onInteraction(event, false))
28✔
187
    EventHandler.on(this._element, EVENT_FOCUSIN, event => this._onInteraction(event, true))
28✔
188
    EventHandler.on(this._element, EVENT_FOCUSOUT, event => this._onInteraction(event, false))
28✔
189
  }
190

191
  _clearTimeout() {
192
    clearTimeout(this._timeout)
24✔
193
    this._timeout = null
24✔
194
  }
195

196
  // Static
197
  static jQueryInterface(config) {
198
    return this.each(function () {
4✔
199
      const data = Toast.getOrCreateInstance(this, config)
4✔
200

201
      if (typeof config === 'string') {
4✔
202
        if (typeof data[config] === 'undefined') {
2✔
203
          throw new TypeError(`No method named "${config}"`)
1✔
204
        }
205

206
        data[config](this)
1✔
207
      }
208
    })
209
  }
210
}
211

212
/**
213
 * Data API implementation
214
 */
215

216
enableDismissTrigger(Toast)
1✔
217

218
/**
219
 * jQuery
220
 */
221

222
defineJQueryPlugin(Toast)
1✔
223

224
export default Toast
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc