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
 * ========================================================================
5
 * Copyright 2011-2014 Twitter, Inc.
6
 * Licensed under MIT (https://github.com/twbs/bootstrap/blob/master/LICENSE)
7
 * ======================================================================== */
Jacob Thornton's avatar
Jacob Thornton committed
8

9

Zlatan Vasović's avatar
Zlatan Vasović committed
10
11
+function ($) {
  'use strict';
12

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

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

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

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

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

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

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

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

40
    if (e.isDefaultPrevented()) return
41

42
    $parent.removeClass('in')
43

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

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


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

59
60
  var old = $.fn.alert

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

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

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


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

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


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

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

88
}(jQuery);