alert.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* ========================================================================
  2. * Bootstrap: alert.js v3.3.4
  3. * http://getbootstrap.com/javascript/#alerts
  4. * ========================================================================
  5. * Copyright 2011-2015 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.3.4'
  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. var $parent = $(selector)
  26. if (e) e.preventDefault()
  27. if (!$parent.length) {
  28. $parent = $this.closest('.alert')
  29. }
  30. $parent.trigger(e = $.Event('close.bs.alert'))
  31. if (e.isDefaultPrevented()) return
  32. $parent.removeClass('in')
  33. function removeElement() {
  34. // detach from parent, fire event then clean up data
  35. $parent.detach().trigger('closed.bs.alert').remove()
  36. }
  37. $.support.transition && $parent.hasClass('fade') ?
  38. $parent
  39. .one('bsTransitionEnd', removeElement)
  40. .emulateTransitionEnd(Alert.TRANSITION_DURATION) :
  41. removeElement()
  42. }
  43. // ALERT PLUGIN DEFINITION
  44. // =======================
  45. function Plugin(option) {
  46. return this.each(function () {
  47. var $this = $(this)
  48. var data = $this.data('bs.alert')
  49. if (!data) $this.data('bs.alert', (data = new Alert(this)))
  50. if (typeof option == 'string') data[option].call($this)
  51. })
  52. }
  53. var old = $.fn.alert
  54. $.fn.alert = Plugin
  55. $.fn.alert.Constructor = Alert
  56. // ALERT NO CONFLICT
  57. // =================
  58. $.fn.alert.noConflict = function () {
  59. $.fn.alert = old
  60. return this
  61. }
  62. // ALERT DATA-API
  63. // ==============
  64. $(document).on('click.bs.alert.data-api', dismiss, Alert.prototype.close)
  65. }(jQuery);