Skip to content

Commit d8c2f97

Browse files
committed
Merge pull request #164 from mdeboer/master
Trigger event on dom object when activated
2 parents 859ac32 + b0a44f0 commit d8c2f97

File tree

5 files changed

+213
-91
lines changed

5 files changed

+213
-91
lines changed

dist/wow.js

Lines changed: 136 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(function() {
22
var MutationObserver, Util, WeakMap, getComputedStyle, getComputedStyleRX,
3-
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
4-
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
3+
bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
4+
indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
55

66
Util = (function() {
77
function Util() {}
@@ -21,6 +21,39 @@
2121
return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(agent);
2222
};
2323

24+
Util.prototype.createEvent = function(event, bubble, cancel, detail) {
25+
var customEvent;
26+
if (bubble == null) {
27+
bubble = false;
28+
}
29+
if (cancel == null) {
30+
cancel = false;
31+
}
32+
if (detail == null) {
33+
detail = null;
34+
}
35+
if (document.createEvent != null) {
36+
customEvent = document.createEvent('CustomEvent');
37+
customEvent.initCustomEvent(event, bubble, cancel, detail);
38+
} else if (document.createEventObject != null) {
39+
customEvent = document.createEventObject();
40+
customEvent.eventType = event;
41+
} else {
42+
customEvent.eventName = event;
43+
}
44+
return customEvent;
45+
};
46+
47+
Util.prototype.emitEvent = function(elem, event) {
48+
if (elem.dispatchEvent != null) {
49+
return elem.dispatchEvent(event);
50+
} else if (event in (elem != null)) {
51+
return elem[event]();
52+
} else if (("on" + event) in (elem != null)) {
53+
return elem["on" + event]();
54+
}
55+
};
56+
2457
Util.prototype.addEvent = function(elem, event, fn) {
2558
if (elem.addEventListener != null) {
2659
return elem.addEventListener(event, fn, false);
@@ -60,21 +93,21 @@
6093
}
6194

6295
WeakMap.prototype.get = function(key) {
63-
var i, item, _i, _len, _ref;
64-
_ref = this.keys;
65-
for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {
66-
item = _ref[i];
96+
var i, item, j, len, ref;
97+
ref = this.keys;
98+
for (i = j = 0, len = ref.length; j < len; i = ++j) {
99+
item = ref[i];
67100
if (item === key) {
68101
return this.values[i];
69102
}
70103
}
71104
};
72105

73106
WeakMap.prototype.set = function(key, value) {
74-
var i, item, _i, _len, _ref;
75-
_ref = this.keys;
76-
for (i = _i = 0, _len = _ref.length; _i < _len; i = ++_i) {
77-
item = _ref[i];
107+
var i, item, j, len, ref;
108+
ref = this.keys;
109+
for (i = j = 0, len = ref.length; j < len; i = ++j) {
110+
item = ref[i];
78111
if (item === key) {
79112
this.values[i] = value;
80113
return;
@@ -108,7 +141,7 @@
108141

109142
getComputedStyle = this.getComputedStyle || function(el, pseudo) {
110143
this.getPropertyValue = function(prop) {
111-
var _ref;
144+
var ref;
112145
if (prop === 'float') {
113146
prop = 'styleFloat';
114147
}
@@ -117,7 +150,7 @@
117150
return _char.toUpperCase();
118151
});
119152
}
120-
return ((_ref = el.currentStyle) != null ? _ref[prop] : void 0) || null;
153+
return ((ref = el.currentStyle) != null ? ref[prop] : void 0) || null;
121154
};
122155
return this;
123156
};
@@ -138,18 +171,19 @@
138171
if (options == null) {
139172
options = {};
140173
}
141-
this.scrollCallback = __bind(this.scrollCallback, this);
142-
this.scrollHandler = __bind(this.scrollHandler, this);
143-
this.start = __bind(this.start, this);
174+
this.scrollCallback = bind(this.scrollCallback, this);
175+
this.scrollHandler = bind(this.scrollHandler, this);
176+
this.start = bind(this.start, this);
144177
this.scrolled = true;
145178
this.config = this.util().extend(options, this.defaults);
146179
this.animationNameCache = new WeakMap();
180+
this.wowEvent = this.util().createEvent(this.config.boxClass);
147181
}
148182

149183
WOW.prototype.init = function() {
150-
var _ref;
184+
var ref;
151185
this.element = window.document.documentElement;
152-
if ((_ref = document.readyState) === "interactive" || _ref === "complete") {
186+
if ((ref = document.readyState) === "interactive" || ref === "complete") {
153187
this.start();
154188
} else {
155189
this.util().addEvent(document, 'DOMContentLoaded', this.start);
@@ -158,35 +192,35 @@
158192
};
159193

160194
WOW.prototype.start = function() {
161-
var box, _i, _len, _ref;
195+
var box, j, len, ref;
162196
this.stopped = false;
163197
this.boxes = (function() {
164-
var _i, _len, _ref, _results;
165-
_ref = this.element.querySelectorAll("." + this.config.boxClass);
166-
_results = [];
167-
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
168-
box = _ref[_i];
169-
_results.push(box);
198+
var j, len, ref, results;
199+
ref = this.element.querySelectorAll("." + this.config.boxClass);
200+
results = [];
201+
for (j = 0, len = ref.length; j < len; j++) {
202+
box = ref[j];
203+
results.push(box);
170204
}
171-
return _results;
205+
return results;
172206
}).call(this);
173207
this.all = (function() {
174-
var _i, _len, _ref, _results;
175-
_ref = this.boxes;
176-
_results = [];
177-
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
178-
box = _ref[_i];
179-
_results.push(box);
208+
var j, len, ref, results;
209+
ref = this.boxes;
210+
results = [];
211+
for (j = 0, len = ref.length; j < len; j++) {
212+
box = ref[j];
213+
results.push(box);
180214
}
181-
return _results;
215+
return results;
182216
}).call(this);
183217
if (this.boxes.length) {
184218
if (this.disabled()) {
185219
this.resetStyle();
186220
} else {
187-
_ref = this.boxes;
188-
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
189-
box = _ref[_i];
221+
ref = this.boxes;
222+
for (j = 0, len = ref.length; j < len; j++) {
223+
box = ref[j];
190224
this.applyStyle(box, true);
191225
}
192226
}
@@ -199,22 +233,22 @@
199233
if (this.config.live) {
200234
return new MutationObserver((function(_this) {
201235
return function(records) {
202-
var node, record, _j, _len1, _results;
203-
_results = [];
204-
for (_j = 0, _len1 = records.length; _j < _len1; _j++) {
205-
record = records[_j];
206-
_results.push((function() {
207-
var _k, _len2, _ref1, _results1;
208-
_ref1 = record.addedNodes || [];
209-
_results1 = [];
210-
for (_k = 0, _len2 = _ref1.length; _k < _len2; _k++) {
211-
node = _ref1[_k];
212-
_results1.push(this.doSync(node));
236+
var k, len1, node, record, results;
237+
results = [];
238+
for (k = 0, len1 = records.length; k < len1; k++) {
239+
record = records[k];
240+
results.push((function() {
241+
var l, len2, ref1, results1;
242+
ref1 = record.addedNodes || [];
243+
results1 = [];
244+
for (l = 0, len2 = ref1.length; l < len2; l++) {
245+
node = ref1[l];
246+
results1.push(this.doSync(node));
213247
}
214-
return _results1;
248+
return results1;
215249
}).call(_this));
216250
}
217-
return _results;
251+
return results;
218252
};
219253
})(this)).observe(document.body, {
220254
childList: true,
@@ -239,40 +273,46 @@
239273
};
240274

241275
WOW.prototype.doSync = function(element) {
242-
var box, _i, _len, _ref, _results;
276+
var box, j, len, ref, results;
243277
if (element == null) {
244278
element = this.element;
245279
}
246280
if (element.nodeType !== 1) {
247281
return;
248282
}
249283
element = element.parentNode || element;
250-
_ref = element.querySelectorAll("." + this.config.boxClass);
251-
_results = [];
252-
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
253-
box = _ref[_i];
254-
if (__indexOf.call(this.all, box) < 0) {
284+
ref = element.querySelectorAll("." + this.config.boxClass);
285+
results = [];
286+
for (j = 0, len = ref.length; j < len; j++) {
287+
box = ref[j];
288+
if (indexOf.call(this.all, box) < 0) {
255289
this.boxes.push(box);
256290
this.all.push(box);
257291
if (this.stopped || this.disabled()) {
258292
this.resetStyle();
259293
} else {
260294
this.applyStyle(box, true);
261295
}
262-
_results.push(this.scrolled = true);
296+
results.push(this.scrolled = true);
263297
} else {
264-
_results.push(void 0);
298+
results.push(void 0);
265299
}
266300
}
267-
return _results;
301+
return results;
268302
};
269303

270304
WOW.prototype.show = function(box) {
271305
this.applyStyle(box);
272-
box.className = "" + box.className + " " + this.config.animateClass;
306+
box.className = box.className + " " + this.config.animateClass;
273307
if (this.config.callback != null) {
274-
return this.config.callback(box);
308+
this.config.callback(box);
275309
}
310+
this.util().emitEvent(box, this.wowEvent);
311+
this.util().addEvent(box, 'animationend', this.resetAnimation);
312+
this.util().addEvent(box, 'oanimationend', this.resetAnimation);
313+
this.util().addEvent(box, 'webkitAnimationEnd', this.resetAnimation);
314+
this.util().addEvent(box, 'MSAnimationEnd', this.resetAnimation);
315+
return box;
276316
};
277317

278318
WOW.prototype.applyStyle = function(box, hidden) {
@@ -300,14 +340,22 @@
300340
})();
301341

302342
WOW.prototype.resetStyle = function() {
303-
var box, _i, _len, _ref, _results;
304-
_ref = this.boxes;
305-
_results = [];
306-
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
307-
box = _ref[_i];
308-
_results.push(box.style.visibility = 'visible');
343+
var box, j, len, ref, results;
344+
ref = this.boxes;
345+
results = [];
346+
for (j = 0, len = ref.length; j < len; j++) {
347+
box = ref[j];
348+
results.push(box.style.visibility = 'visible');
349+
}
350+
return results;
351+
};
352+
353+
WOW.prototype.resetAnimation = function(event) {
354+
var target;
355+
if (event.type.toLowerCase().indexOf('animationend') >= 0) {
356+
target = event.target || event.srcElement;
357+
return target.className = target.className.replace(config.animateClass, '').trim();
309358
}
310-
return _results;
311359
};
312360

313361
WOW.prototype.customStyle = function(box, hidden, duration, delay, iteration) {
@@ -339,32 +387,32 @@
339387
WOW.prototype.vendors = ["moz", "webkit"];
340388

341389
WOW.prototype.vendorSet = function(elem, properties) {
342-
var name, value, vendor, _results;
343-
_results = [];
390+
var name, results, value, vendor;
391+
results = [];
344392
for (name in properties) {
345393
value = properties[name];
346394
elem["" + name] = value;
347-
_results.push((function() {
348-
var _i, _len, _ref, _results1;
349-
_ref = this.vendors;
350-
_results1 = [];
351-
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
352-
vendor = _ref[_i];
353-
_results1.push(elem["" + vendor + (name.charAt(0).toUpperCase()) + (name.substr(1))] = value);
395+
results.push((function() {
396+
var j, len, ref, results1;
397+
ref = this.vendors;
398+
results1 = [];
399+
for (j = 0, len = ref.length; j < len; j++) {
400+
vendor = ref[j];
401+
results1.push(elem["" + vendor + (name.charAt(0).toUpperCase()) + (name.substr(1))] = value);
354402
}
355-
return _results1;
403+
return results1;
356404
}).call(this));
357405
}
358-
return _results;
406+
return results;
359407
};
360408

361409
WOW.prototype.vendorCSS = function(elem, property) {
362-
var result, style, vendor, _i, _len, _ref;
410+
var j, len, ref, result, style, vendor;
363411
style = getComputedStyle(elem);
364412
result = style.getPropertyCSSValue(property);
365-
_ref = this.vendors;
366-
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
367-
vendor = _ref[_i];
413+
ref = this.vendors;
414+
for (j = 0, len = ref.length; j < len; j++) {
415+
vendor = ref[j];
368416
result = result || style.getPropertyCSSValue("-" + vendor + "-" + property);
369417
}
370418
return result;
@@ -401,21 +449,21 @@
401449
if (this.scrolled) {
402450
this.scrolled = false;
403451
this.boxes = (function() {
404-
var _i, _len, _ref, _results;
405-
_ref = this.boxes;
406-
_results = [];
407-
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
408-
box = _ref[_i];
452+
var j, len, ref, results;
453+
ref = this.boxes;
454+
results = [];
455+
for (j = 0, len = ref.length; j < len; j++) {
456+
box = ref[j];
409457
if (!(box)) {
410458
continue;
411459
}
412460
if (this.isVisible(box)) {
413461
this.show(box);
414462
continue;
415463
}
416-
_results.push(box);
464+
results.push(box);
417465
}
418-
return _results;
466+
return results;
419467
}).call(this);
420468
if (!(this.boxes.length || this.config.live)) {
421469
return this.stop();

0 commit comments

Comments
 (0)