This repository has been archived by the owner on Apr 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.motionnotion.js
465 lines (405 loc) · 12.7 KB
/
jquery.motionnotion.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
(function($) {
var options = {
suspendAnimationsOnAll: false
};
var engines = {
STANDARD: 'standard',
MOZILLA: 'mozilla',
WEBKIT: 'webkit',
MICROSOFT: 'microsoft',
OPERA: 'opera'
};
var checkOrder = [
engines.STANDARD,
engines.MOZILLA,
engines.WEBKIT,
engines.MICROSOFT,
engines.OPERA
];
var stylePrefixes = {
'standard': '',
'mozilla': 'Moz',
'webkit': 'Webkit',
'microsoft': 'ms',
'opera': 'O'
};
var eventPrefixes = {
'standard': '',
'mozilla': 'moz',
'webkit': 'webkit',
'microsoft': 'MS',
'opera': 'o'
};
var eventsCamelCased = {
'animationstart': 'AnimationStart',
'animationend': 'AnimationEnd',
'animationiteration': 'AnimationIteration'
};
//the name of the property by which we can fetch animation-name from
//various browsers. We can likely drop -ms- prefix for animations. More
//research, but as per this table http://caniuse.com/css-animation -ms-
//has never been required for animation support on any version of IE.
var animationNameStyleNames = {
'standard': 'animationName', //standard access for jQuery
'mozilla': '-moz-animation-name', //unsupported access for jQuery
'webkit': '-webkit-animation-name', //unsupported access for jQuery
'microsoft': '-ms-animation-name', //unsupported access for jQuery
'opera': '-o-animation-name' //unsupported access for jQuery
};
var transitionNames = {
'adding': 'mn-adding', //encompassing for all adds types
'removing': 'mn-removing', //encompassing all remove types
'appending': 'mn-appending',
'prepending': 'mn-prepending',
'aftering': 'mn-aftering',
'beforing': 'mn-beforing',
'emptying': 'mn-emptying',
'detaching': 'mn-detaching',
'hiding': 'mn-hiding',
'showing': 'mn-showing'
};
//Current test for animationName support:
//http://jsfiddle.net/39X9S/
var engineDetected = (function() {
var div = document.createElement('div');
var engineDetected = false;
for (var i = 0; i < checkOrder.length; i++) {
engineName = checkOrder[i];
var property = animationNameStyleNames[engineName];
if (div.style[property] !== undefined) {
engineDetected = engineName;
break;
}
}
return engineDetected;
})();
var animationNameStyleName = animationNameStyleNames[engineDetected];
/**
* A method for normalizing the event names depending on what browser
* you are using.
* @param {string} eventName W3C name of the event you will be normalizing.
* @return {string}
*/
var normalizeAnimationEventName = function(eventName) {
switch (engineDetected) {
case engines.MOZILLA:
case engines.WEBKIT:
case engines.MICROSOFT:
return eventPrefixes[engineDetected] +
eventsCamelCased[eventName];
break;
case engines.OPERA:
return eventPrefixes[engineDetected] + eventName;
break;
case engines.STANDARD:
default:
return eventName;
break;
}
return eventName;
};
/**
* Checks to see if an element's css contains a specific animation name.
* @param {jQuery} $el jQuery element that will be evaluated.
* @param {string} name Animation name is being queried for.
* @return {boolean}
*/
var hasAnimation = function($el, name) {
var names = getAnimationNames($el);
for (var i = 0; i < names.length; i++) {
if (names[i] == name) {
return true;
}
}
return false;
};
/**
* Gets all the animation names as an array.
* @param {jQuery} $el jQuery element that will be evaluated.
* @return {Array}
*/
var getAnimationNames = function($el) {
var commaSeperatedNames = $el.css(animationNameStyleName) || "";
if (commaSeperatedNames !== "" &&
commaSeperatedNames !== "none") {
return commaSeperatedNames.replace(/ /g, '').split(',');
} else {
return [];
}
};
/**
* Returns all newly discovered animation names, minus the transition
* animations names.
* @param {Array} prevNames Previously discovered animation names.
* @param {Array} currNames Currently discovered animation names.
* @return {Array}
*/
var getNewAnimationNames = function(prevNames, currNames) {
var transitionNamesArray = new Array;
for (var transition in transitionNames) {
transitionNamesArray.push(transitionNames[transition]);
}
//do not include transition animation name markers as discovered
prevNames = $(prevNames).not(transitionNamesArray).get();
currNames = $(currNames).not(transitionNamesArray).get();
//new animations are ones that were not there before.
return $(currNames).not(prevNames).get();
};
var asyncManipulation = function($el, transition, manipulationSuper, superScope, superArgs) {
manipulationSuper = manipulationSuper || function() {};
superArgs = superArgs || [];
superScope = superScope || $el;
var transitionOperation = $el.data('mnTransitionOperation') || null;
if (transitionOperation && transitionOperation.state() !== 'resolved') {
//finish the current manipulation & animation.
transitionOperation.resolve();
}
//apply super asynchronosly incase an animation takes place.
applyTransition(
$el,
transition,
manipulationSuper,
superScope,
superArgs
);
};
var syncManipulation = function($el, transition, manipulationSuper, superScope, superArgs) {
manipulationSuper = manipulationSuper || function() {};
superArgs = superArgs || [];
superScope = superScope || $el;
var transitionOperation = $el.data('mnTransitionOperation') || null;
if (transitionOperation && transitionOperation.state() !== 'resolved') {
//finish the current manipulation & animation.
transitionOperation.resolve();
}
//apply super right away (synchronosly)
manipulationSuper.apply(superScope, superArgs);
applyTransition(
$el,
transition
);
};
var applyTransition = function($el, transition, callback, scope, args) {
callback = callback || function() {};
scope = scope || $el;
args = args || [];
var transitionOperation = $.Deferred();
var prevNames = getAnimationNames($el);
$el.addClass(transitionNames[transition]);
if (hasAnimation($el, transitionNames[transition]) &&
!$el.data('mnSuspendAnimations') &&
!options.suspendAnimationsOnAll
) {
var newNames = getNewAnimationNames(prevNames, getAnimationNames($el));
var openAnimations = $el.data('mnOpenAnimations') || 0;
$el.data('mnOpenAnimations', openAnimations + newNames.length);
transitionOperation.done(function() {
completeTransition(
$el,
transition,
callback,
scope,
args
);
});
$el.data('mnTransitionOperation', transitionOperation);
$el.on(
normalizeAnimationEventName('animationend'), {
transitionOperation: transitionOperation
},
animationEndHandler
);
} else {
completeTransition(
$el,
transition,
callback,
scope,
args
);
}
};
var completeTransition = function($el, transition, callback, scope, args) {
$el.off(
normalizeAnimationEventName('animationend'),
animationEndHandler
);
$el.data('mnOpenAnimations', 0);
$el.removeClass(transitionNames[transition]);
callback.apply(scope, args);
$el.triggerHandler(transition + 'End');
};
var animationEndHandler = function(event) {
var data = event.data;
var $el = $(this);
var openAnimations = $el.data('mnOpenAnimations');
openAnimations--;
$el.data('mnOpenAnimations', openAnimations);
if (openAnimations == 0) {
data.transitionOperation.resolve();
}
};
//get all original methods, store them as super methods!
var removeSuper = $.fn.remove;
var appendSuper = $.fn.append;
var prependSuper = $.fn.prepend;
var beforeSuper = $.fn.before;
var afterSuper = $.fn.after;
var emptySuper = $.fn.empty;
var replaceWithSuper = $.fn.replaceWith;
var hideSuper = $.fn.hide;
var showSuper = $.fn.show;
/**
* New remove method.
*/
$.fn.remove = function(selector, keepData /*Internal Use Only*/ ) {
var els = selector ? jQuery.filter(selector, this) : this;
var completionArgs = [undefined, keepData];
for (var i = 0; i < els.length; i++) {
var $el = $(els[i]);
asyncManipulation($el, 'removing', removeSuper, $el, completionArgs);
}
return this;
};
/**
* New detach method.
*/
$.fn.detach = function(selector) {
var els = selector ? jQuery.filter(selector, this) : this;
for (var i = 0; i < els.length; i++) {
var $el = $(els[i]);
asyncManipulation($el, 'detaching', removeSuper, $el, [undefined, true /*keepData*/ ]);
}
return this;
};
/**
* New hide method.
*/
$.fn.hide = function() {
if (arguments.length > 0) {
//TODO: wrap callback function with an event dispatcher
hideSuper.apply(this, arguments);
} else {
for (var i = 0; i < this.length; i++) {
var $el = $(this[i]);
asyncManipulation($el, 'hiding', hideSuper, $el);
}
}
};
/**
* New show method.
*/
$.fn.show = function() {
if (arguments.length > 0) {
//TODO: wrap callback function with an event dispatcher
showSuper.apply(this, arguments);
} else {
for (var i = 0; i < this.length; i++) {
var $el = $(this[i]);
syncManipulation($el, 'showing', showSuper, $el);
}
}
};
/**
* New append mehtod
*/
$.fn.append = function() {
if (this.domManip.length == 2) {
return this.domManip(arguments, function(el) {
syncManipulation($(el), 'appending', appendSuper, $(this), [el]);
});
} else if (this.domManip.length == 3) {
return this.domManip(arguments, false, function(el) {
syncManipulation($(el), 'appending', appendSuper, $(this), [el]);
});
}
};
/**
* New prepend method.
*/
$.fn.prepend = function() {
if (this.domManip.length == 2) {
return this.domManip(arguments, function(el) {
syncManipulation($(el), 'prepending', prependSuper, $(this), [el]);
});
//for older versions of jQuery that take three arguments
} else if (this.domManip.length == 3) {
return this.domManip(arguments, false, function(el) {
syncManipulation($(el), 'prepending', prependSuper, $(this), [el]);
});
}
};
/**
* New after method.
*/
$.fn.after = function() {
if (this.domManip.length == 2) {
return this.domManip(arguments, function(el) {
syncManipulation($(el), 'aftering', afterSuper, $(this), [el]);
});
//for older versions of jQuery that take three arguments
} else if (this.domManip.length == 3) {
return this.domManip(arguments, false, function(el) {
syncManipulation($(el), 'aftering', afterSuper, $(this), [el]);
});
}
};
/**
* New before method.
*/
$.fn.before = function() {
if (this.domManip.length == 2) {
return this.domManip(arguments, function(el) {
syncManipulation($(el), 'beforing', beforeSuper, $(this), [el]);
});
//for older versions of jQuery that take three arguments
} else if (this.domManip.length == 3) {
return this.domManip(arguments, false, function(el) {
syncManipulation($(el), 'beforing', beforeSuper, $(this), [el]);
});
}
};
/**
* New empty method.
*/
$.fn.empty = function() {
for (var i = 0; i < this.length; i++) {
var $el = $(this[i]);
syncManipulation($el, 'emptying', emptySuper);
}
return this;
};
//prototype motionNotion
$.fn.motionNotion = function(method, param) {
switch (method) {
case 'suspendAnimations':
this.data('mnSuspendAnimations', param);
}
return this;
};
//global motionNotion
$.motionNotion = function(method, param) {
switch (method) {
case 'suspendAnimationsOnAll':
options.suspendAnimationsOnAll = param;
break;
case 'internals':
return {
'engines': engines,
'stylePrefixes': stylePrefixes,
'eventPrefixes': eventPrefixes,
'eventsCamelCased': eventsCamelCased,
'animationNameStyleNames': animationNameStyleNames,
'transitionNames': transitionNames,
'engineDetected': engineDetected,
'animationNameStyleName': animationNameStyleName,
'normalizeAnimationEventName': normalizeAnimationEventName,
'hasAnimation': hasAnimation,
'getAnimationNames': getAnimationNames,
'getNewAnimationNames': getNewAnimationNames
};
break;
}
return this;
};
})(jQuery);