alert.js 2.46 KB
Newer Older
Jacob Thornton's avatar
Jacob Thornton committed
1
/* ==========================================================
fat's avatar
fat committed
2
 * Bootstrap: alert.js v3.0.0
Jacob Thornton's avatar
Jacob Thornton committed
3
4
 * http://twitter.github.com/bootstrap/javascript.html#alerts
 * ==========================================================
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
18
19
 *
 * 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.
 * ========================================================== */

20

fat's avatar
fat committed
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

fat's avatar
fat committed
48
    $parent.trigger(e = $.Event('bs:alert:close'))
49

50
    if (e.isDefaultPrevented()) return
51

52
    $parent.removeClass('in')
53

54
    function removeElement() {
fat's avatar
fat committed
55
      $parent.trigger('bs-closed').remove()
56
57
    }

58
59
60
    $.support.transition && $parent.hasClass('fade') ?
      $parent.on($.support.transition.end, removeElement) :
      removeElement()
61
62
63
  }


fat's avatar
fat committed
64
65
  // ALERT PLUGIN DEFINITION
  // =======================
66

67
68
  var old = $.fn.alert

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

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

79
  $.fn.alert.Constructor = Alert
80
81


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

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


fat's avatar
fat committed
91
  // ALERT DATA-API
fat's avatar
fat committed
92
  // ==============
93

fat's avatar
fat committed
94
  $(document).on('click.bs-alert.bs-data-api', dismiss, Alert.prototype.close)
95

96
}(window.jQuery);