alert.js 2.34 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

Katie Zhu's avatar
Katie Zhu committed
10
11
12
13
14
(function (o_o) {
  typeof define  === 'function' && define.amd ? define(['jquery'], o_o) :
  typeof exports === 'object' ? o_o(require('jquery')) : o_o(this.jQuery)
})(function ($) {

Zlatan Vasović's avatar
Zlatan Vasović committed
15
  'use strict';
16

fat's avatar
fat committed
17
18
  // ALERT CLASS DEFINITION
  // ======================
19

20
  var dismiss = '[data-dismiss="alert"]'
fat's avatar
fat committed
21
22
23
  var Alert   = function (el) {
    $(el).on('click', dismiss, this.close)
  }
24

fat's avatar
fat committed
25
26
  Alert.VERSION = '3.1.1'

27
  Alert.prototype.close = function (e) {
fat's avatar
fat committed
28
29
    var $this    = $(this)
    var selector = $this.attr('data-target')
Jacob Thornton's avatar
Jacob Thornton committed
30

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

fat's avatar
fat committed
36
    var $parent = $(selector)
37

fat's avatar
fat committed
38
    if (e) e.preventDefault()
39

fat's avatar
fat committed
40
41
42
    if (!$parent.length) {
      $parent = $this.hasClass('alert') ? $this : $this.parent()
    }
43

44
    $parent.trigger(e = $.Event('close.bs.alert'))
45

46
    if (e.isDefaultPrevented()) return
47

48
    $parent.removeClass('in')
49

50
    function removeElement() {
Ross Nye's avatar
Ross Nye committed
51
52
      // detach from parent, fire event then clean up data
      $parent.detach().trigger('closed.bs.alert').remove()
53
54
    }

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


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

66
  function Plugin(option) {
67
68
    return this.each(function () {
      var $this = $(this)
69
      var data  = $this.data('bs.alert')
fat's avatar
fat committed
70

71
      if (!data) $this.data('bs.alert', (data = new Alert(this)))
72
      if (typeof option == 'string') data[option].call($this)
73
74
75
    })
  }

76
77
78
  var old = $.fn.alert

  $.fn.alert             = Plugin
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

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

Katie Zhu's avatar
Katie Zhu committed
96
});