alert.js 2.18 KB
Newer Older
1
/* ========================================================================
Mark Otto's avatar
Mark Otto committed
2
 * Bootstrap: alert.js v3.1.1
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

fat's avatar
fat committed
21
22
  Alert.VERSION = '3.1.1'

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

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

fat's avatar
fat committed
32
    var $parent = $(selector)
33

fat's avatar
fat committed
34
    if (e) e.preventDefault()
35

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

40
    $parent.trigger(e = $.Event('close.bs.alert'))
41

42
    if (e.isDefaultPrevented()) return
43

44
    $parent.removeClass('in')
45

46
    function removeElement() {
Ross Nye's avatar
Ross Nye committed
47
48
      // detach from parent, fire event then clean up data
      $parent.detach().trigger('closed.bs.alert').remove()
49
50
    }

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


fat's avatar
fat committed
59
60
  // ALERT PLUGIN DEFINITION
  // =======================
61

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

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

72
73
74
  var old = $.fn.alert

  $.fn.alert             = Plugin
75
  $.fn.alert.Constructor = Alert
76
77


fat's avatar
fat committed
78
79
  // ALERT NO CONFLICT
  // =================
80
81
82
83
84
85
86

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


fat's avatar
fat committed
87
  // ALERT DATA-API
fat's avatar
fat committed
88
  // ==============
89

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

92
}(jQuery);