bootstrap-alert.js 2.46 KB
Newer Older
Jacob Thornton's avatar
Jacob Thornton committed
1
/* ==========================================================
Mark Otto's avatar
Mark Otto committed
2
 * bootstrap-alert.js v2.2.3
Jacob Thornton's avatar
Jacob Thornton committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 * http://twitter.github.com/bootstrap/javascript.html#alerts
 * ==========================================================
 * Copyright 2012 Twitter, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ========================================================== */


21
22
23
!function ($) {

  "use strict"; // jshint ;_;
Jacob Thornton's avatar
Jacob Thornton committed
24
25
26
27
28
29


 /* ALERT CLASS DEFINITION
  * ====================== */

  var dismiss = '[data-dismiss="alert"]'
30
    , Alert = function (el) {
Jacob Thornton's avatar
Jacob Thornton committed
31
32
33
        $(el).on('click', dismiss, this.close)
      }

34
35
36
37
  Alert.prototype.close = function (e) {
    var $this = $(this)
      , selector = $this.attr('data-target')
      , $parent
Jacob Thornton's avatar
Jacob Thornton committed
38

39
40
41
42
    if (!selector) {
      selector = $this.attr('href')
      selector = selector && selector.replace(/.*(?=#[^\s]*$)/, '') //strip for ie7
    }
Jacob Thornton's avatar
Jacob Thornton committed
43

44
    $parent = $(selector)
Jacob Thornton's avatar
Jacob Thornton committed
45

46
    e && e.preventDefault()
Jacob Thornton's avatar
Jacob Thornton committed
47

48
    $parent.length || ($parent = $this.hasClass('alert') ? $this : $this.parent())
49

50
    $parent.trigger(e = $.Event('close'))
51

52
    if (e.isDefaultPrevented()) return
Jacob Thornton's avatar
Jacob Thornton committed
53

54
    $parent.removeClass('in')
Jacob Thornton's avatar
Jacob Thornton committed
55

56
57
58
59
    function removeElement() {
      $parent
        .trigger('closed')
        .remove()
Jacob Thornton's avatar
Jacob Thornton committed
60
61
    }

62
63
64
    $.support.transition && $parent.hasClass('fade') ?
      $parent.on($.support.transition.end, removeElement) :
      removeElement()
Jacob Thornton's avatar
Jacob Thornton committed
65
66
67
68
69
70
  }


 /* ALERT PLUGIN DEFINITION
  * ======================= */

71
72
  var old = $.fn.alert

73
  $.fn.alert = function (option) {
Jacob Thornton's avatar
Jacob Thornton committed
74
75
76
77
78
79
80
81
82
83
84
    return this.each(function () {
      var $this = $(this)
        , data = $this.data('alert')
      if (!data) $this.data('alert', (data = new Alert(this)))
      if (typeof option == 'string') data[option].call($this)
    })
  }

  $.fn.alert.Constructor = Alert


85
86
87
88
89
90
91
92
93
 /* ALERT NO CONFLICT
  * ================= */

  $.fn.alert.noConflict = function () {
    $.fn.alert = old
    return this
  }


Jacob Thornton's avatar
Jacob Thornton committed
94
95
96
 /* ALERT DATA-API
  * ============== */

97
  $(document).on('click.alert.data-api', dismiss, Alert.prototype.close)
Jacob Thornton's avatar
Jacob Thornton committed
98

99
}(window.jQuery);