Project

General

Profile

1
// Native Javascript for Bootstrap 3 | Alert
2
// by dnp_theme
3

    
4
(function(factory){
5

    
6
  // CommonJS/RequireJS and "native" compatibility
7
  if(typeof module !== "undefined" && typeof exports == "object") {
8
    // A commonJS/RequireJS environment
9
    if(typeof window != "undefined") {
10
      // Window and document exist, so return the factory's return value.
11
      module.exports = factory();
12
    } else {
13
      // Let the user give the factory a Window and Document.
14
      module.exports = factory;
15
    }
16
  } else {
17
    // Assume a traditional browser.
18
    window.Alert = factory();
19
  }
20

    
21
})(function(root){
22

    
23
  // ALERT DEFINITION
24
  // ===================
25
  var Alert = function( element ) {
26
    this.btn = typeof element === 'object' ? element : document.querySelector(element);
27
    this.alert = null;
28
    this.duration = 150; // default alert transition duration
29
    this.init();
30
  }
31

    
32
  // ALERT METHODS
33
  // ================
34
  Alert.prototype = {
35

    
36
    init : function() {
37
      this.actions();
38
      document.addEventListener('click', this.close, false); //delegate to all alerts, including those inserted later into the DOM
39
    },
40

    
41
    actions : function() {
42
      var self = this;
43

    
44
      this.close = function(e) {
45
        var target = e.target;
46
        self.btn = target.getAttribute('data-dismiss') === 'alert' && target.className === 'close' ? target : target.parentNode;
47
        self.alert = self.btn.parentNode;
48

    
49
        if ( self.alert !== null && self.btn.getAttribute('data-dismiss') === 'alert' && /in/.test(self.alert.className) ) {
50
          self.alert.className = self.alert.className.replace(' in','');
51
          setTimeout(function() {
52
            self.alert && self.alert.parentNode.removeChild(self.alert);
53
          }, self.duration);
54
        }
55

    
56
      }
57
    }
58
    }
59

    
60
  // ALERT DATA API
61
  // =================
62
    var Alerts = document.querySelectorAll('[data-dismiss="alert"]'), i = 0, all = Alerts.length;
63
  for (i;i<all;i++) {
64
    new Alert(Alerts[i]);
65
  }
66

    
67
  return Alert;
68

    
69
});
(2-2/17)