Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

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