alert.js 2.2 KB

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