2
* # Semantic - Transition
3
* http://github.com/jlukic/semantic-ui/
6
* Copyright 2014 Contributors
7
* Released under the MIT license
8
* http://opensource.org/licenses/MIT
12
;(function ( $, window, document, undefined ) {
14
$.fn.transition = function() {
16
$allModules = $(this),
17
moduleSelector = $allModules.selector || '',
19
time = new Date().getTime(),
22
moduleArguments = arguments,
23
query = moduleArguments[0],
24
queryArguments = [].slice.call(arguments, 1),
25
methodInvoked = (typeof query === 'string'),
27
requestAnimationFrame = window.requestAnimationFrame
28
|| window.mozRequestAnimationFrame
29
|| window.webkitRequestAnimationFrame
30
|| window.msRequestAnimationFrame
31
|| function(callback) { setTimeout(callback, 0); },
58
initialize: function() {
60
settings = module.get.settings.apply(element, moduleArguments);
61
module.verbose('Converted arguments into settings object', settings);
64
error = settings.error;
65
className = settings.className;
66
namespace = settings.namespace;
67
metadata = settings.metadata;
68
moduleNamespace = 'module-' + namespace;
70
animationEnd = module.get.animationEvent();
71
animationName = module.get.animationName();
73
instance = $module.data(moduleNamespace) || module;
76
methodInvoked = module.invoke(query);
78
// no internal method was found matching query or query not made
79
if(methodInvoked === false) {
85
instantiate: function() {
86
module.verbose('Storing instance of module', module);
88
.data(moduleNamespace, instance)
93
module.verbose('Destroying previous module for', element);
95
.removeData(moduleNamespace)
100
module.verbose('Refreshing display type on next animation');
101
delete instance.displayType;
104
forceRepaint: function() {
105
module.verbose('Forcing element repaint');
107
$parentElement = $module.parent(),
108
$nextElement = $module.next()
110
if($nextElement.size() === 0) {
111
$module.detach().appendTo($parentElement);
114
$module.detach().insertBefore($nextElement);
118
repaint: function() {
119
module.verbose('Repainting element');
121
fakeAssignment = element.offsetWidth
125
animate: function(overrideSettings) {
126
settings = overrideSettings || settings;
127
if(!module.is.supported()) {
128
module.error(error.support);
131
module.debug('Preparing animation', settings.animation);
132
if(module.is.animating() && settings.queue) {
133
if(!settings.allowRepeats && module.has.direction() && module.is.occuring() && instance.queuing !== true) {
134
module.error(error.repeated);
137
module.queue(settings.animation);
141
if(module.can.animate) {
142
module.set.animating(settings.animation);
145
module.error(error.noAnimation, settings.animation);
150
module.debug('Resetting animation to beginning conditions');
151
$module.off(animationEnd);
152
module.restore.conditions();
154
module.remove.animating();
157
queue: function(animation) {
158
module.debug('Queueing animation of', animation);
159
instance.queuing = true;
161
.one(animationEnd, function() {
162
instance.queuing = false;
164
module.animate.apply(this, settings);
169
complete: function () {
170
module.verbose('CSS animation complete', settings.animation);
171
if(!module.is.looping()) {
172
if( module.is.outward() ) {
173
module.verbose('Animation is outward, hiding element');
174
module.restore.conditions();
175
module.remove.display();
177
$.proxy(settings.onHide, this)();
179
else if( module.is.inward() ) {
180
module.verbose('Animation is outward, showing element');
181
module.restore.conditions();
183
$.proxy(settings.onShow, this)();
186
module.restore.conditions();
188
module.remove.duration();
189
module.remove.animating();
191
$.proxy(settings.complete, this)();
195
direction: function(animation) {
196
animation = animation || settings.animation;
197
if( animation.search(className.inward) !== -1 || animation.search(className.outward) !== -1) {
198
module.debug('Direction already set in animation');
207
animating: function(animation) {
208
animation = animation || settings.animation;
209
module.save.conditions();
210
if(module.can.transition() && !module.has.direction()) {
211
module.set.direction();
213
module.remove.hidden();
214
module.set.display();
216
.addClass(className.animating)
217
.addClass(className.transition)
219
.one(animationEnd, module.complete)
221
module.set.duration(settings.duration);
222
module.debug('Starting tween', settings.animation, $module.attr('class'));
225
display: function() {
227
displayType = module.get.displayType()
229
if(displayType !== 'block' && displayType !== 'none') {
230
module.verbose('Setting final visibility to', displayType);
239
direction: function() {
240
if($module.is(':visible')) {
241
module.debug('Automatically determining the direction of animation', 'Outward');
243
.removeClass(className.inward)
244
.addClass(className.outward)
248
module.debug('Automatically determining the direction of animation', 'Inward');
250
.removeClass(className.outward)
251
.addClass(className.inward)
256
looping: function() {
257
module.debug('Transition set to loop');
259
.addClass(className.looping)
263
duration: function(duration) {
264
duration = duration || settings.duration;
265
duration = (typeof duration == 'number')
269
module.verbose('Setting animation duration', duration);
272
'-webkit-animation-duration': duration,
273
'-moz-animation-duration': duration,
274
'-ms-animation-duration': duration,
275
'-o-animation-duration': duration,
276
'animation-duration': duration
283
.addClass(className.transition)
284
.addClass(className.hidden)
288
visible: function() {
290
.addClass(className.transition)
291
.addClass(className.visible)
297
displayType: function(displayType) {
298
instance.displayType = displayType;
300
transitionExists: function(animation, exists) {
301
$.fn.transition.exists[animation] = exists;
302
module.verbose('Saving existence of transition', animation, exists);
304
conditions: function() {
306
className : $module.attr('class'),
307
style : $module.attr('style')
309
module.verbose('Saving original attributes', instance.cache);
314
conditions: function() {
315
if(instance.cache === undefined) {
318
if(instance.cache.className) {
319
$module.attr('class', instance.cache.className);
322
$module.removeAttr('class');
324
if(instance.cache.style) {
325
$module.attr('style', instance.cache.style);
328
if(module.get.displayType() === 'block') {
329
$module.removeAttr('style');
332
if(module.is.looping()) {
333
module.remove.looping();
335
module.verbose('Restoring original attributes', instance.cache);
341
animating: function() {
342
$module.removeClass(className.animating);
345
display: function() {
346
if(instance.displayType !== undefined) {
347
$module.css('display', '');
351
duration: function() {
354
'-webkit-animation-duration' : '',
355
'-moz-animation-duration' : '',
356
'-ms-animation-duration' : '',
357
'-o-animation-duration' : '',
358
'animation-duration' : ''
364
$module.removeClass(className.hidden);
367
visible: function() {
368
$module.removeClass(className.visible);
371
looping: function() {
372
module.debug('Transitions are no longer looping');
374
.removeClass(className.looping)
376
module.forceRepaint();
383
settings: function(animation, duration, complete) {
384
// single settings object
385
if(typeof animation == 'object') {
386
return $.extend(true, {}, $.fn.transition.settings, animation);
388
// all arguments provided
389
else if(typeof complete == 'function') {
390
return $.extend({}, $.fn.transition.settings, {
391
animation : animation,
396
// only duration provided
397
else if(typeof duration == 'string' || typeof duration == 'number') {
398
return $.extend({}, $.fn.transition.settings, {
399
animation : animation,
403
// duration is actually settings object
404
else if(typeof duration == 'object') {
405
return $.extend({}, $.fn.transition.settings, duration, {
406
animation : animation
409
// duration is actually callback
410
else if(typeof duration == 'function') {
411
return $.extend({}, $.fn.transition.settings, {
412
animation : animation,
416
// only animation provided
418
return $.extend({}, $.fn.transition.settings, {
419
animation : animation
422
return $.fn.transition.settings;
425
displayType: function() {
426
if(instance.displayType === undefined) {
427
// create fake element to determine display state
428
module.can.transition();
430
return instance.displayType;
433
transitionExists: function(animation) {
434
return $.fn.transition.exists[animation];
437
animationName: function() {
439
element = document.createElement('div'),
441
'animation' :'animationName',
442
'OAnimation' :'oAnimationName',
443
'MozAnimation' :'mozAnimationName',
444
'WebkitAnimation' :'webkitAnimationName'
448
for(animation in animations){
449
if( element.style[animation] !== undefined ){
450
module.verbose('Determined animation vendor name property', animations[animation]);
451
return animations[animation];
457
animationEvent: function() {
459
element = document.createElement('div'),
461
'animation' :'animationend',
462
'OAnimation' :'oAnimationEnd',
463
'MozAnimation' :'animationend',
464
'WebkitAnimation' :'webkitAnimationEnd'
468
for(animation in animations){
469
if( element.style[animation] !== undefined ){
470
module.verbose('Determined animation vendor end event', animations[animation]);
471
return animations[animation];
480
animate: function() {
481
if($module.css(settings.animation) !== 'none') {
482
module.debug('CSS definition found', $module.css(settings.animation));
486
module.debug('Unable to find css definition', $module.attr('class'));
490
transition: function() {
492
elementClass = $module.attr('class'),
493
animation = settings.animation,
494
transitionExists = module.get.transitionExists(settings.animation),
500
if( transitionExists === undefined || instance.displayType === undefined) {
501
module.verbose('Determining whether animation exists');
502
$clone = $('<div>').addClass( elementClass ).appendTo($('body'));
503
currentAnimation = $clone
504
.removeClass(className.inward)
505
.removeClass(className.outward)
506
.addClass(className.animating)
507
.addClass(className.transition)
512
.addClass(className.inward)
516
.attr('class', elementClass)
520
module.verbose('Determining final display state', displayType);
521
if(currentAnimation != inAnimation) {
522
module.debug('Transition exists for animation', animation);
523
transitionExists = true;
526
module.debug('Static animation found', animation, displayType);
527
transitionExists = false;
530
module.save.displayType(displayType);
531
module.save.transitionExists(animation, transitionExists);
533
return transitionExists;
538
animating: function() {
539
return $module.hasClass(className.animating);
542
return $module.hasClass(className.inward);
544
outward: function() {
545
return $module.hasClass(className.outward);
547
looping: function() {
548
return $module.hasClass(className.looping);
550
occuring: function(animation) {
551
animation = animation || settings.animation;
552
return ( $module.hasClass(animation) );
554
visible: function() {
555
return $module.is(':visible');
557
supported: function() {
558
return(animationName !== false && animationEnd !== false);
563
module.verbose('Hiding element');
564
module.remove.visible();
569
show: function(display) {
570
module.verbose('Showing element', display);
571
module.remove.hidden();
572
module.set.visible();
577
module.verbose('Starting animation');
578
$module.removeClass(className.disabled);
582
module.debug('Stopping animation');
583
$module.addClass(className.disabled);
587
module.debug('Toggling play status');
588
$module.toggleClass(className.disabled);
591
setting: function(name, value) {
592
if( $.isPlainObject(name) ) {
593
$.extend(true, settings, name);
595
else if(value !== undefined) {
596
settings[name] = value;
599
return settings[name];
602
internal: function(name, value) {
603
if( $.isPlainObject(name) ) {
604
$.extend(true, module, name);
606
else if(value !== undefined) {
607
module[name] = value;
615
if(settings.performance) {
616
module.performance.log(arguments);
619
module.debug = Function.prototype.bind.call(console.info, console, settings.name + ':');
620
module.debug.apply(console, arguments);
624
verbose: function() {
625
if(settings.verbose && settings.debug) {
626
if(settings.performance) {
627
module.performance.log(arguments);
630
module.verbose = Function.prototype.bind.call(console.info, console, settings.name + ':');
631
module.verbose.apply(console, arguments);
636
module.error = Function.prototype.bind.call(console.error, console, settings.name + ':');
637
module.error.apply(console, arguments);
640
log: function(message) {
646
if(settings.performance) {
647
currentTime = new Date().getTime();
648
previousTime = time || currentTime;
649
executionTime = currentTime - previousTime;
654
'Arguments' : [].slice.call(message, 1) || '',
655
'Execution Time' : executionTime
658
clearTimeout(module.performance.timer);
659
module.performance.timer = setTimeout(module.performance.display, 100);
661
display: function() {
663
title = settings.name + ':',
667
clearTimeout(module.performance.timer);
668
$.each(performance, function(index, data) {
669
totalTime += data['Execution Time'];
671
title += ' ' + totalTime + 'ms';
673
title += ' \'' + moduleSelector + '\'';
675
if($allModules.size() > 1) {
676
title += ' ' + '(' + $allModules.size() + ')';
678
if( (console.group !== undefined || console.table !== undefined) && performance.length > 0) {
679
console.groupCollapsed(title);
681
console.table(performance);
684
$.each(performance, function(index, data) {
685
console.log(data['Name'] + ': ' + data['Execution Time']+'ms');
693
invoke: function(query, passedArguments, context) {
700
passedArguments = passedArguments || queryArguments;
701
context = element || context;
702
if(typeof query == 'string' && object !== undefined) {
703
query = query.split(/[\. ]/);
704
maxDepth = query.length - 1;
705
$.each(query, function(depth, value) {
706
var camelCaseValue = (depth != maxDepth)
707
? value + query[depth + 1].charAt(0).toUpperCase() + query[depth + 1].slice(1)
710
if( $.isPlainObject( object[camelCaseValue] ) && (depth != maxDepth) ) {
711
object = object[camelCaseValue];
713
else if( object[camelCaseValue] !== undefined ) {
714
found = object[camelCaseValue];
717
else if( $.isPlainObject( object[value] ) && (depth != maxDepth) ) {
718
object = object[value];
720
else if( object[value] !== undefined ) {
721
found = object[value];
729
if ( $.isFunction( found ) ) {
730
response = found.apply(context, passedArguments);
732
else if(found !== undefined) {
735
if($.isArray(returnedValue)) {
736
returnedValue.push(response);
738
else if(returnedValue !== undefined) {
739
returnedValue = [returnedValue, response];
741
else if(response !== undefined) {
742
returnedValue = response;
744
return found || false;
750
return (returnedValue !== undefined)
756
$.fn.transition.exists = {};
758
$.fn.transition.settings = {
763
// debug content outputted to console
766
// verbose debug output
769
// performance data output
773
namespace : 'transition',
775
// animation complete event
776
complete : function() {},
777
onShow : function() {},
778
onHide : function() {},
780
// whether animation can occur twice in a row
781
allowRepeats : false,
783
// animation duration
787
// new animations will occur after previous ones
791
animating : 'animating',
792
disabled : 'disabled',
798
transition : 'ui transition',
804
noAnimation : 'There is no css animation matching the one you specified.',
805
repeated : 'That animation is already occurring, cancelling repeated animation',
806
method : 'The method you called is not defined',
807
support : 'This browser does not support CSS animations'
813
})( jQuery, window , document );