Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
/**
2
 * --------------------------------------------------------------------------
3
 * Bootstrap (v4.6.2): toast.js
4
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/main/LICENSE)
5
 * --------------------------------------------------------------------------
6
 */
7
 
8
import $ from 'jquery'
9
import Util from './util'
10
 
11
/**
12
 * Constants
13
 */
14
 
15
const NAME = 'toast'
16
const VERSION = '4.6.2'
17
const DATA_KEY = 'bs.toast'
18
const EVENT_KEY = `.${DATA_KEY}`
19
const JQUERY_NO_CONFLICT = $.fn[NAME]
20
 
21
const CLASS_NAME_FADE = 'fade'
22
const CLASS_NAME_HIDE = 'hide'
23
const CLASS_NAME_SHOW = 'show'
24
const CLASS_NAME_SHOWING = 'showing'
25
 
26
const EVENT_CLICK_DISMISS = `click.dismiss${EVENT_KEY}`
27
const EVENT_HIDE = `hide${EVENT_KEY}`
28
const EVENT_HIDDEN = `hidden${EVENT_KEY}`
29
const EVENT_SHOW = `show${EVENT_KEY}`
30
const EVENT_SHOWN = `shown${EVENT_KEY}`
31
 
32
const SELECTOR_DATA_DISMISS = '[data-dismiss="toast"]'
33
 
34
const Default = {
35
  animation: true,
36
  autohide: true,
37
  delay: 500
38
}
39
 
40
const DefaultType = {
41
  animation: 'boolean',
42
  autohide: 'boolean',
43
  delay: 'number'
44
}
45
 
46
/**
47
 * Class definition
48
 */
49
 
50
class Toast {
51
  constructor(element, config) {
52
    this._element = element
53
    this._config = this._getConfig(config)
54
    this._timeout = null
55
    this._setListeners()
56
  }
57
 
58
  // Getters
59
  static get VERSION() {
60
    return VERSION
61
  }
62
 
63
  static get DefaultType() {
64
    return DefaultType
65
  }
66
 
67
  static get Default() {
68
    return Default
69
  }
70
 
71
  // Public
72
  show() {
73
    const showEvent = $.Event(EVENT_SHOW)
74
 
75
    $(this._element).trigger(showEvent)
76
    if (showEvent.isDefaultPrevented()) {
77
      return
78
    }
79
 
80
    this._clearTimeout()
81
 
82
    if (this._config.animation) {
83
      this._element.classList.add(CLASS_NAME_FADE)
84
    }
85
 
86
    const complete = () => {
87
      this._element.classList.remove(CLASS_NAME_SHOWING)
88
      this._element.classList.add(CLASS_NAME_SHOW)
89
 
90
      $(this._element).trigger(EVENT_SHOWN)
91
 
92
      if (this._config.autohide) {
93
        this._timeout = setTimeout(() => {
94
          this.hide()
95
        }, this._config.delay)
96
      }
97
    }
98
 
99
    this._element.classList.remove(CLASS_NAME_HIDE)
100
    Util.reflow(this._element)
101
    this._element.classList.add(CLASS_NAME_SHOWING)
102
    if (this._config.animation) {
103
      const transitionDuration = Util.getTransitionDurationFromElement(this._element)
104
 
105
      $(this._element)
106
        .one(Util.TRANSITION_END, complete)
107
        .emulateTransitionEnd(transitionDuration)
108
    } else {
109
      complete()
110
    }
111
  }
112
 
113
  hide() {
114
    if (!this._element.classList.contains(CLASS_NAME_SHOW)) {
115
      return
116
    }
117
 
118
    const hideEvent = $.Event(EVENT_HIDE)
119
 
120
    $(this._element).trigger(hideEvent)
121
    if (hideEvent.isDefaultPrevented()) {
122
      return
123
    }
124
 
125
    this._close()
126
  }
127
 
128
  dispose() {
129
    this._clearTimeout()
130
 
131
    if (this._element.classList.contains(CLASS_NAME_SHOW)) {
132
      this._element.classList.remove(CLASS_NAME_SHOW)
133
    }
134
 
135
    $(this._element).off(EVENT_CLICK_DISMISS)
136
 
137
    $.removeData(this._element, DATA_KEY)
138
    this._element = null
139
    this._config = null
140
  }
141
 
142
  // Private
143
  _getConfig(config) {
144
    config = {
145
      ...Default,
146
      ...$(this._element).data(),
147
      ...(typeof config === 'object' && config ? config : {})
148
    }
149
 
150
    Util.typeCheckConfig(
151
      NAME,
152
      config,
153
      this.constructor.DefaultType
154
    )
155
 
156
    return config
157
  }
158
 
159
  _setListeners() {
160
    $(this._element).on(EVENT_CLICK_DISMISS, SELECTOR_DATA_DISMISS, () => this.hide())
161
  }
162
 
163
  _close() {
164
    const complete = () => {
165
      this._element.classList.add(CLASS_NAME_HIDE)
166
      $(this._element).trigger(EVENT_HIDDEN)
167
    }
168
 
169
    this._element.classList.remove(CLASS_NAME_SHOW)
170
    if (this._config.animation) {
171
      const transitionDuration = Util.getTransitionDurationFromElement(this._element)
172
 
173
      $(this._element)
174
        .one(Util.TRANSITION_END, complete)
175
        .emulateTransitionEnd(transitionDuration)
176
    } else {
177
      complete()
178
    }
179
  }
180
 
181
  _clearTimeout() {
182
    clearTimeout(this._timeout)
183
    this._timeout = null
184
  }
185
 
186
  // Static
187
  static _jQueryInterface(config) {
188
    return this.each(function () {
189
      const $element = $(this)
190
      let data = $element.data(DATA_KEY)
191
      const _config = typeof config === 'object' && config
192
 
193
      if (!data) {
194
        data = new Toast(this, _config)
195
        $element.data(DATA_KEY, data)
196
      }
197
 
198
      if (typeof config === 'string') {
199
        if (typeof data[config] === 'undefined') {
200
          throw new TypeError(`No method named "${config}"`)
201
        }
202
 
203
        data[config](this)
204
      }
205
    })
206
  }
207
}
208
 
209
/**
210
 * jQuery
211
 */
212
 
213
$.fn[NAME] = Toast._jQueryInterface
214
$.fn[NAME].Constructor = Toast
215
$.fn[NAME].noConflict = () => {
216
  $.fn[NAME] = JQUERY_NO_CONFLICT
217
  return Toast._jQueryInterface
218
}
219
 
220
export default Toast