alert.js 2.06 KB
Newer Older
1
/* ========================================================================
Mark Otto's avatar
Mark Otto committed
2
 * Bootstrap: alert.js v3.1.0
3
 * http://getbootstrap.com/javascript/#alerts
4
 * ========================================================================
fat's avatar
fat committed
5
 * Copyright 2013 Twitter, Inc.
6
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
7
 * ======================================================================== */
Jacob Thornton's avatar
Jacob Thornton committed
8

9

XhmikosR's avatar
XhmikosR committed
10
+function ($) { 'use strict';
11

fat's avatar
fat committed
12
13
  // ALERT CLASS DEFINITION
  // ======================
14

15
  var dismiss = '[data-dismiss="alert"]'
fat's avatar
fat committed
16
17
18
  var Alert   = function (el) {
    $(el).on('click', dismiss, this.close)
  }
19

20
  Alert.prototype.close = function (e) {
fat's avatar
fat committed
21
22
    var $this    = $(this)
    var selector = $this.attr('data-target')
Jacob Thornton's avatar
Jacob Thornton committed
23

24
25
    if (!selector) {
      selector = $this.attr('href')
fat's avatar
fat committed
26
      selector = selector && selector.replace(/.*(?=#[^\s]*$)/, '') // strip for ie7
27
    }
Jonathan Ingram's avatar
Jonathan Ingram committed
28

fat's avatar
fat committed
29
    var $parent = $(selector)
30

fat's avatar
fat committed
31
    if (e) e.preventDefault()
32

fat's avatar
fat committed
33
34
35
    if (!$parent.length) {
      $parent = $this.hasClass('alert') ? $this : $this.parent()
    }
36

37
    $parent.trigger(e = $.Event('close.bs.alert'))
38

39
    if (e.isDefaultPrevented()) return
40

41
    $parent.removeClass('in')
42

43
    function removeElement() {
44
      $parent.trigger('closed.bs.alert').remove()
45
46
    }

47
    $.support.transition && $parent.hasClass('fade') ?
48
49
50
      $parent
        .one($.support.transition.end, removeElement)
        .emulateTransitionEnd(150) :
51
      removeElement()
52
53
54
  }


fat's avatar
fat committed
55
56
  // ALERT PLUGIN DEFINITION
  // =======================
57

58
59
  var old = $.fn.alert

60
  $.fn.alert = function (option) {
61
62
    return this.each(function () {
      var $this = $(this)
63
      var data  = $this.data('bs.alert')
fat's avatar
fat committed
64

65
      if (!data) $this.data('bs.alert', (data = new Alert(this)))
66
      if (typeof option == 'string') data[option].call($this)
67
68
69
    })
  }

70
  $.fn.alert.Constructor = Alert
71
72


fat's avatar
fat committed
73
74
  // ALERT NO CONFLICT
  // =================
75
76
77
78
79
80
81

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


fat's avatar
fat committed
82
  // ALERT DATA-API
fat's avatar
fat committed
83
  // ==============
84

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

87
}(jQuery);