alert.js 2.52 KB
Newer Older
1
/* ========================================================================
2
 * Bootstrap: alert.js v3.0.1
3
 * http://getbootstrap.com/javascript/#alerts
4
 * ========================================================================
fat's avatar
fat committed
5
 * Copyright 2013 Twitter, Inc.
Jacob Thornton's avatar
Jacob Thornton committed
6
7
8
9
10
11
12
13
14
15
16
17
 *
 * 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.
18
 * ======================================================================== */
Jacob Thornton's avatar
Jacob Thornton committed
19

20

21
+function ($) { "use strict";
22

fat's avatar
fat committed
23
24
  // ALERT CLASS DEFINITION
  // ======================
25

26
  var dismiss = '[data-dismiss="alert"]'
fat's avatar
fat committed
27
28
29
  var Alert   = function (el) {
    $(el).on('click', dismiss, this.close)
  }
30

31
  Alert.prototype.close = function (e) {
fat's avatar
fat committed
32
33
    var $this    = $(this)
    var selector = $this.attr('data-target')
Jacob Thornton's avatar
Jacob Thornton committed
34

35
36
    if (!selector) {
      selector = $this.attr('href')
fat's avatar
fat committed
37
      selector = selector && selector.replace(/.*(?=#[^\s]*$)/, '') // strip for ie7
38
    }
Jonathan Ingram's avatar
Jonathan Ingram committed
39

fat's avatar
fat committed
40
    var $parent = $(selector)
41

fat's avatar
fat committed
42
    if (e) e.preventDefault()
43

fat's avatar
fat committed
44
45
46
    if (!$parent.length) {
      $parent = $this.hasClass('alert') ? $this : $this.parent()
    }
47

48
    $parent.trigger(e = $.Event('close.bs.alert'))
49

50
    if (e.isDefaultPrevented()) return
51

52
    $parent.removeClass('in')
53

54
    function removeElement() {
55
      $parent.trigger('closed.bs.alert').remove()
56
57
    }

58
    $.support.transition && $parent.hasClass('fade') ?
59
60
61
      $parent
        .one($.support.transition.end, removeElement)
        .emulateTransitionEnd(150) :
62
      removeElement()
63
64
65
  }


fat's avatar
fat committed
66
67
  // ALERT PLUGIN DEFINITION
  // =======================
68

69
70
  var old = $.fn.alert

71
  $.fn.alert = function (option) {
72
73
    return this.each(function () {
      var $this = $(this)
74
      var data  = $this.data('bs.alert')
fat's avatar
fat committed
75

76
      if (!data) $this.data('bs.alert', (data = new Alert(this)))
77
      if (typeof option == 'string') data[option].call($this)
78
79
80
    })
  }

81
  $.fn.alert.Constructor = Alert
82
83


fat's avatar
fat committed
84
85
  // ALERT NO CONFLICT
  // =================
86
87
88
89
90
91
92

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


fat's avatar
fat committed
93
  // ALERT DATA-API
fat's avatar
fat committed
94
  // ==============
95

96
  $(document).on('click.bs.alert.data-api', dismiss, Alert.prototype.close)
97

98
}(jQuery);