alert.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* ========================================================================
  2. * Bootstrap: alert.js v3.0.3
  3. * http://getbootstrap.com/javascript/#alerts
  4. * ========================================================================
  5. * Copyright 2013 Twitter, Inc.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * ======================================================================== */
  19. +function ($) { "use strict";
  20. // ALERT CLASS DEFINITION
  21. // ======================
  22. var dismiss = '[data-dismiss="alert"]'
  23. var Alert = function (el) {
  24. $(el).on('click', dismiss, this.close)
  25. }
  26. Alert.prototype.close = function (e) {
  27. var $this = $(this)
  28. var selector = $this.attr('data-target')
  29. if (!selector) {
  30. selector = $this.attr('href')
  31. selector = selector && selector.replace(/.*(?=#[^\s]*$)/, '') // strip for ie7
  32. }
  33. var $parent = $(selector)
  34. if (e) e.preventDefault()
  35. if (!$parent.length) {
  36. $parent = $this.hasClass('alert') ? $this : $this.parent()
  37. }
  38. $parent.trigger(e = $.Event('close.bs.alert'))
  39. if (e.isDefaultPrevented()) return
  40. $parent.removeClass('in')
  41. function removeElement() {
  42. $parent.trigger('closed.bs.alert').remove()
  43. }
  44. $.support.transition && $parent.hasClass('fade') ?
  45. $parent
  46. .one($.support.transition.end, removeElement)
  47. .emulateTransitionEnd(150) :
  48. removeElement()
  49. }
  50. // ALERT PLUGIN DEFINITION
  51. // =======================
  52. var old = $.fn.alert
  53. $.fn.alert = function (option) {
  54. return this.each(function () {
  55. var $this = $(this)
  56. var data = $this.data('bs.alert')
  57. if (!data) $this.data('bs.alert', (data = new Alert(this)))
  58. if (typeof option == 'string') data[option].call($this)
  59. })
  60. }
  61. $.fn.alert.Constructor = Alert
  62. // ALERT NO CONFLICT
  63. // =================
  64. $.fn.alert.noConflict = function () {
  65. $.fn.alert = old
  66. return this
  67. }
  68. // ALERT DATA-API
  69. // ==============
  70. $(document).on('click.bs.alert.data-api', dismiss, Alert.prototype.close)
  71. }(jQuery);