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,190 @@
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
+      },
43
+      open: function(options) {
44
+        options = $.extend({}, vex.defaultOptions, options);
45
+        options.id = vex.globalID;
46
+        vex.globalID += 1;
47
+        options.$vex = $('<div>').addClass(vex.baseClassNames.vex).addClass(options.className).css(options.css).data({
48
+          vex: options
49
+        });
50
+        options.$vexOverlay = $('<div>').addClass(vex.baseClassNames.overlay).addClass(options.overlayClassName).css(options.overlayCSS).data({
51
+          vex: options
52
+        });
53
+        if (options.overlayClosesOnClick) {
54
+          options.$vexOverlay.bind('click.vex', function(e) {
55
+            if (e.target !== this) {
56
+              return;
57
+            }
58
+            return vex.close($(this).data().vex.id);
59
+          });
60
+        }
61
+        options.$vex.append(options.$vexOverlay);
62
+        options.$vexContent = $('<div>').addClass(vex.baseClassNames.content).addClass(options.contentClassName).css(options.contentCSS).append(options.content).data({
63
+          vex: options
64
+        });
65
+        options.$vex.append(options.$vexContent);
66
+        if (options.showCloseButton) {
67
+          options.$closeButton = $('<div>').addClass(vex.baseClassNames.close).addClass(options.closeClassName).css(options.closeCSS).data({
68
+            vex: options
69
+          }).bind('click.vex', function() {
70
+            return vex.close($(this).data().vex.id);
71
+          });
72
+          options.$vexContent.append(options.$closeButton);
73
+        }
74
+        $(options.appendLocation).append(options.$vex);
75
+        vex.setupBodyClassName(options.$vex);
76
+        if (options.afterOpen) {
77
+          options.afterOpen(options.$vexContent, options);
78
+        }
79
+        setTimeout((function() {
80
+          return options.$vexContent.trigger('vexOpen', options);
81
+        }), 0);
82
+        return options.$vexContent;
83
+      },
84
+      getAllVexes: function() {
85
+        return $("." + vex.baseClassNames.vex + ":not(\"." + vex.baseClassNames.closing + "\") ." + vex.baseClassNames.content);
86
+      },
87
+      getVexByID: function(id) {
88
+        return vex.getAllVexes().filter(function() {
89
+          return $(this).data().vex.id === id;
90
+        });
91
+      },
92
+      close: function(id) {
93
+        var $lastVex;
94
+        if (!id) {
95
+          $lastVex = vex.getAllVexes().last();
96
+          if (!$lastVex.length) {
97
+            return false;
98
+          }
99
+          id = $lastVex.data().vex.id;
100
+        }
101
+        return vex.closeByID(id);
102
+      },
103
+      closeAll: function() {
104
+        var ids;
105
+        ids = vex.getAllVexes().map(function() {
106
+          return $(this).data().vex.id;
107
+        }).toArray();
108
+        if (!(ids != null ? ids.length : void 0)) {
109
+          return false;
110
+        }
111
+        $.each(ids.reverse(), function(index, id) {
112
+          return vex.closeByID(id);
113
+        });
114
+        return true;
115
+      },
116
+      closeByID: function(id) {
117
+        var $vex, $vexContent, beforeClose, close, options;
118
+        $vexContent = vex.getVexByID(id);
119
+        if (!$vexContent.length) {
120
+          return;
121
+        }
122
+        $vex = $vexContent.data().vex.$vex;
123
+        options = $.extend({}, $vexContent.data().vex);
124
+        beforeClose = function() {
125
+          if (options.beforeClose) {
126
+            return options.beforeClose($vexContent, options);
127
+          }
128
+        };
129
+        close = function() {
130
+          $vexContent.trigger('vexClose', options);
131
+          $vex.remove();
132
+          $('body').trigger('vexAfterClose', options);
133
+          if (options.afterClose) {
134
+            return options.afterClose($vexContent, options);
135
+          }
136
+        };
137
+        if (animationEndSupport) {
138
+          beforeClose();
139
+          $vex.unbind(vex.animationEndEvent).bind(vex.animationEndEvent, function() {
140
+            return close();
141
+          }).addClass(vex.baseClassNames.closing);
142
+        } else {
143
+          beforeClose();
144
+          close();
145
+        }
146
+        return true;
147
+      },
148
+      closeByEscape: function() {
149
+        var $lastVex, id, ids;
150
+        ids = vex.getAllVexes().map(function() {
151
+          return $(this).data().vex.id;
152
+        }).toArray();
153
+        if (!(ids != null ? ids.length : void 0)) {
154
+          return false;
155
+        }
156
+        id = Math.max.apply(Math, ids);
157
+        $lastVex = vex.getVexByID(id);
158
+        if ($lastVex.data().vex.escapeButtonCloses !== true) {
159
+          return false;
160
+        }
161
+        return vex.closeByID(id);
162
+      },
163
+      setupBodyClassName: function($vex) {
164
+        return $('body').bind('vexOpen.vex', function() {
165
+          return $('body').addClass(vex.baseClassNames.open);
166
+        }).bind('vexAfterClose.vex', function() {
167
+          if (!vex.getAllVexes().length) {
168
+            return $('body').removeClass(vex.baseClassNames.open);
169
+          }
170
+        });
171
+      },
172
+      hideLoading: function() {
173
+        return $('.vex-loading-spinner').remove();
174
+      },
175
+      showLoading: function() {
176
+        vex.hideLoading();
177
+        return $('body').append("<div class=\"vex-loading-spinner " + vex.defaultOptions.className + "\"></div>");
178
+      }
179
+    };
180
+  };
181
+
182
+  if (typeof define === 'function' && define.amd) {
183
+    define(['jquery'], vexFactory);
184
+  } else if (typeof exports === 'object') {
185
+    module.exports = vexFactory(require('jquery'));
186
+  } else {
187
+    window.vex = vexFactory(jQuery);
188
+  }
189
+
190
+}).call(this);