bootstrap-alert.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /* ==========================================================
  2. * bootstrap-alert.js v2.0.2
  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"
  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 = {
  28. constructor: Alert
  29. , close: function ( e ) {
  30. var $this = $(this)
  31. , selector = $this.attr('data-target')
  32. , $parent
  33. if (!selector) {
  34. selector = $this.attr('href')
  35. selector = selector && selector.replace(/.*(?=#[^\s]*$)/, '') //strip for ie7
  36. }
  37. $parent = $(selector)
  38. $parent.trigger('close')
  39. e && e.preventDefault()
  40. $parent.length || ($parent = $this.hasClass('alert') ? $this : $this.parent())
  41. $parent
  42. .trigger('close')
  43. .removeClass('in')
  44. function removeElement() {
  45. $parent
  46. .trigger('closed')
  47. .remove()
  48. }
  49. $.support.transition && $parent.hasClass('fade') ?
  50. $parent.on($.support.transition.end, removeElement) :
  51. removeElement()
  52. }
  53. }
  54. /* ALERT PLUGIN DEFINITION
  55. * ======================= */
  56. $.fn.alert = function ( option ) {
  57. return this.each(function () {
  58. var $this = $(this)
  59. , data = $this.data('alert')
  60. if (!data) $this.data('alert', (data = new Alert(this)))
  61. if (typeof option == 'string') data[option].call($this)
  62. })
  63. }
  64. $.fn.alert.Constructor = Alert
  65. /* ALERT DATA-API
  66. * ============== */
  67. $(function () {
  68. $('body').on('click.alert.data-api', dismiss, Alert.prototype.close)
  69. })
  70. }( window.jQuery );