Browse code

Initial commit

Benjamin Roth authored on26/03/2015 16:52:11
Showing1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,202 @@
1
+(function() {
2
+  var vexFactory;
3
+
4
+  vexFactory = function($) {
5
+    var animationEndSupport, vex;
6
+    animationEndSupport = false;
7
+    $(function() {
8
+      var s;
9
+      s = (document.body || document.documentElement).style;
10
+      animationEndSupport = s.animation !== void 0 || s.WebkitAnimation !== void 0 || s.MozAnimation !== void 0 || s.MsAnimation !== void 0 || s.OAnimation !== void 0;
11
+      return $(window).bind('keyup.vex', function(event) {
12
+        if (event.keyCode === 27) {
13
+          return vex.closeByEscape();
14
+        }
15
+      });
16
+    });
17
+    return vex = {
18
+      globalID: 1,
19
+      animationEndEvent: 'animationend webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend',
20
+      baseClassNames: {
21
+        vex: 'vex',
22
+        content: 'vex-content',
23
+        overlay: 'vex-overlay',
24
+        close: 'vex-close',
25
+        closing: 'vex-closing',
26
+        open: 'vex-open'
27
+      },
28
+      defaultOptions: {
29
+        content: '',
30
+        showCloseButton: true,
31
+        escapeButtonCloses: true,
32
+        overlayClosesOnClick: true,
33
+        appendLocation: 'body',
34
+        className: '',
35
+        css: {},
36
+        overlayClassName: '',
37
+        overlayCSS: {},
38
+        contentClassName: '',
39
+        contentCSS: {},
40
+        closeClassName: '',
41
+        closeCSS: {},
42
+        preventBodyClass: false
43
+      },
44
+      open: function(options) {
45
+        options = $.extend({}, vex.defaultOptions, options);
46
+        options.id = vex.globalID;
47
+        vex.globalID += 1;
48
+        options.$vex = $('<div>').addClass(vex.baseClassNames.vex).addClass(options.className).css(options.css).data({
49
+          vex: options
50
+        });
51
+        options.$vexOverlay = $('<div>').addClass(vex.baseClassNames.overlay).addClass(options.overlayClassName).css(options.overlayCSS).data({
52
+          vex: options
53
+        });
54
+        if (options.overlayClosesOnClick) {
55
+          options.$vexOverlay.bind('click.vex', function(e) {
56
+            if (e.target !== this) {
57
+              return;
58
+            }
59
+            return vex.close($(this).data().vex.id);
60
+          });
61
+        }
62
+        options.$vex.append(options.$vexOverlay);
63
+        options.$vexContent = $('<div>').addClass(vex.baseClassNames.content).addClass(options.contentClassName).css(options.contentCSS).append(options.content).data({
64
+          vex: options
65
+        });
66
+        options.$vex.append(options.$vexContent);
67
+        if (options.showCloseButton) {
68
+          options.$closeButton = $('<div>').addClass(vex.baseClassNames.close).addClass(options.closeClassName).css(options.closeCSS).data({
69
+            vex: options
70
+          }).bind('click.vex', function() {
71
+            return vex.close($(this).data().vex.id);
72
+          });
73
+          options.$vexContent.append(options.$closeButton);
74
+        }
75
+        $(options.appendLocation).append(options.$vex);
76
+        if (!options.preventBodyClass){
77
+          vex.setupBodyClassName(options.$vex);
78
+		}
79
+        if (options.afterOpen) {
80
+          options.afterOpen(options.$vexContent, options);
81
+        }
82
+        setTimeout((function() {
83
+          return options.$vexContent.trigger('vexOpen', options);
84
+        }), 0);
85
+        return options.$vexContent;
86
+      },
87
+      getAllVexes: function() {
88
+        return $("." + vex.baseClassNames.vex + ":not(\"." + vex.baseClassNames.closing + "\") ." + vex.baseClassNames.content);
89
+      },
90
+		getAllBCVexes: function() {
91
+			var BCVexes = [];
92
+			vex.getAllVexes().each(function(idx, item) {
93
+				if (!$(this).data().vex.preventBodyClass) {
94
+					BCVexes.push($(this));
95
+				}
96
+			});
97
+			return BCVexes;
98
+		},
99
+      getVexByID: function(id) {
100
+        return vex.getAllVexes().filter(function() {
101
+          return $(this).data().vex.id === id;
102
+        });
103
+      },
104
+      close: function(id) {
105
+        var $lastVex;
106
+        if (!id) {
107
+          $lastVex = vex.getAllVexes().last();
108
+          if (!$lastVex.length) {
109
+            return false;
110
+          }
111
+          id = $lastVex.data().vex.id;
112
+        }
113
+        return vex.closeByID(id);
114
+      },
115
+      closeAll: function() {
116
+        var ids;
117
+        ids = vex.getAllVexes().map(function() {
118
+          return $(this).data().vex.id;
119
+        }).toArray();
120
+        if (!(ids != null ? ids.length : void 0)) {
121
+          return false;
122
+        }
123
+        $.each(ids.reverse(), function(index, id) {
124
+          return vex.closeByID(id);
125
+        });
126
+        return true;
127
+      },
128
+      closeByID: function(id) {
129
+        var $vex, $vexContent, beforeClose, close, options;
130
+        $vexContent = vex.getVexByID(id);
131
+        if (!$vexContent.length) {
132
+          return;
133
+        }
134
+        $vex = $vexContent.data().vex.$vex;
135
+        options = $.extend({}, $vexContent.data().vex);
136
+        beforeClose = function() {
137
+          if (options.beforeClose) {
138
+            return options.beforeClose($vexContent, options);
139
+          }
140
+        };
141
+        close = function() {
142
+          $vexContent.trigger('vexClose', options);
143
+          $vex.remove();
144
+          $('body').trigger('vexAfterClose', options);
145
+          if (options.afterClose) {
146
+            return options.afterClose($vexContent, options);
147
+          }
148
+        };
149
+        if (animationEndSupport) {
150
+          beforeClose();
151
+          $vex.unbind(vex.animationEndEvent).bind(vex.animationEndEvent, function() {
152
+            return close();
153
+          }).addClass(vex.baseClassNames.closing);
154
+        } else {
155
+          beforeClose();
156
+          close();
157
+        }
158
+        return true;
159
+      },
160
+      closeByEscape: function() {
161
+        var $lastVex, id, ids;
162
+        ids = vex.getAllVexes().map(function() {
163
+          return $(this).data().vex.id;
164
+        }).toArray();
165
+        if (!(ids != null ? ids.length : void 0)) {
166
+          return false;
167
+        }
168
+        id = Math.max.apply(Math, ids);
169
+        $lastVex = vex.getVexByID(id);
170
+        if ($lastVex.data().vex.escapeButtonCloses !== true) {
171
+          return false;
172
+        }
173
+        return vex.closeByID(id);
174
+      },
175
+      setupBodyClassName: function($vex) {
176
+        return $('body').bind('vexOpen.vex', function() {
177
+          return $('body').addClass(vex.baseClassNames.open);
178
+        }).bind('vexAfterClose.vex', function() {
179
+          if (!vex.getAllBCVexes().length) {
180
+            return $('body').removeClass(vex.baseClassNames.open);
181
+          }
182
+        });
183
+      },
184
+      hideLoading: function() {
185
+        return $('.vex-loading-spinner').remove();
186
+      },
187
+      showLoading: function() {
188
+        vex.hideLoading();
189
+        return $('body').append("<div class=\"vex-loading-spinner " + vex.defaultOptions.className + "\"></div>");
190
+      }
191
+    };
192
+  };
193
+
194
+  if (typeof define === 'function' && define.amd) {
195
+    define(['jquery'], vexFactory);
196
+  } else if (typeof exports === 'object') {
197
+    module.exports = vexFactory(require('jquery'));
198
+  } else {
199
+    window.vex = vexFactory(jQuery);
200
+  }
201
+
202
+}).call(this);