bootstrap-alert.js 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* ==========================================================
  2. * bootstrap-alert.js v2.1.1
  3. * http://twitter.github.com/bootstrap/javascript.html#alerts
  4. * ==========================================================
  5. * Copyright 2012 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 ($) {
  20. "use strict"; // jshint ;_;
  21. /* ALERT CLASS DEFINITION
  22. * ====================== */
  23. var dismiss = '[data-dismiss="alert"]'
  24. , Alert = function (el) {
  25. $(el).on('click', dismiss, this.close)
  26. }
  27. Alert.prototype.close = function (e) {
  28. var $this = $(this)
  29. , selector = $this.attr('data-target')
  30. , $parent
  31. if (!selector) {
  32. selector = $this.attr('href')
  33. selector = selector && selector.replace(/.*(?=#[^\s]*$)/, '') //strip for ie7
  34. }
  35. $parent = $(selector)
  36. e && e.preventDefault()
  37. $parent.length || ($parent = $this.hasClass('alert') ? $this : $this.parent())
  38. $parent.trigger(e = $.Event('close'))
  39. if (e.isDefaultPrevented()) return
  40. $parent.removeClass('in')
  41. function removeElement() {
  42. $parent
  43. .trigger('closed')
  44. .remove()
  45. }
  46. $.support.transition && $parent.hasClass('fade') ?
  47. $parent.on($.support.transition.end, removeElement) :
  48. removeElement()
  49. }
  50. /* ALERT PLUGIN DEFINITION
  51. * ======================= */
  52. $.fn.alert = function (option) {
  53. return this.each(function () {
  54. var $this = $(this)
  55. , data = $this.data('alert')
  56. if (!data) $this.data('alert', (data = new Alert(this)))
  57. if (typeof option == 'string') data[option].call($this)
  58. })
  59. }
  60. $.fn.alert.Constructor = Alert
  61. /* ALERT DATA-API
  62. * ============== */
  63. $(function () {
  64. $('body').on('click.alert.data-api', dismiss, Alert.prototype.close)
  65. })
  66. }(window.jQuery);