alert.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* ========================================================================
  2. * Bootstrap: alert.js v3.4.1
  3. * https://getbootstrap.com/docs/3.4/javascript/#alerts
  4. * ========================================================================
  5. * Copyright 2011-2019 Twitter, Inc.
  6. * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
  7. * ======================================================================== */
  8. +function ($) {
  9. 'use strict';
  10. // ALERT CLASS DEFINITION
  11. // ======================
  12. var dismiss = '[data-dismiss="alert"]'
  13. var Alert = function (el) {
  14. $(el).on('click', dismiss, this.close)
  15. }
  16. Alert.VERSION = '3.4.1'
  17. Alert.TRANSITION_DURATION = 150
  18. Alert.prototype.close = function (e) {
  19. var $this = $(this)
  20. var selector = $this.attr('data-target')
  21. if (!selector) {
  22. selector = $this.attr('href')
  23. selector = selector && selector.replace(/.*(?=#[^\s]*$)/, '') // strip for ie7
  24. }
  25. selector = selector === '#' ? [] : selector
  26. var $parent = $(document).find(selector)
  27. if (e) e.preventDefault()
  28. if (!$parent.length) {
  29. $parent = $this.closest('.alert')
  30. }
  31. $parent.trigger(e = $.Event('close.bs.alert'))
  32. if (e.isDefaultPrevented()) return
  33. $parent.removeClass('in')
  34. function removeElement() {
  35. // detach from parent, fire event then clean up data
  36. $parent.detach().trigger('closed.bs.alert').remove()
  37. }
  38. $.support.transition && $parent.hasClass('fade') ?
  39. $parent
  40. .one('bsTransitionEnd', removeElement)
  41. .emulateTransitionEnd(Alert.TRANSITION_DURATION) :
  42. removeElement()
  43. }
  44. // ALERT PLUGIN DEFINITION
  45. // =======================
  46. function Plugin(option) {
  47. return this.each(function () {
  48. var $this = $(this)
  49. var data = $this.data('bs.alert')
  50. if (!data) $this.data('bs.alert', (data = new Alert(this)))
  51. if (typeof option == 'string') data[option].call($this)
  52. })
  53. }
  54. var old = $.fn.alert
  55. $.fn.alert = Plugin
  56. $.fn.alert.Constructor = Alert
  57. // ALERT NO CONFLICT
  58. // =================
  59. $.fn.alert.noConflict = function () {
  60. $.fn.alert = old
  61. return this
  62. }
  63. // ALERT DATA-API
  64. // ==============
  65. $(document).on('click.bs.alert.data-api', dismiss, Alert.prototype.close)
  66. }(jQuery);