Skip to content

Commit 0e1d92f

Browse files
committed
Added lots of comments to explain what is going on. Added a couple of properties to the handler itself so our timer intervals can be changed from the outside.
1 parent a87bc52 commit 0e1d92f

File tree

1 file changed

+87
-12
lines changed

1 file changed

+87
-12
lines changed

js/jquery.mobile.transition.js

Lines changed: 87 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,66 +9,141 @@
99

1010
(function( $, window, undefined ) {
1111

12-
function css3TransitionHandler( name, reverse, $to, $from ) {
12+
function css3TransitionsHandler( name, reverse, $to, $from ) {
13+
14+
// All transition handlers are required to return a
15+
// deferred promise object so the caller can tell when the
16+
// transition is complete.
1317

1418
var deferred = new $.Deferred(),
19+
20+
// Are we being asked to run the transition in reverse?
21+
// if so, we'll need to make sure we place the "reverse" class
22+
// on our "to" and "from" elements.
23+
1524
reverseClass = reverse ? " reverse" : "",
25+
26+
// Some transitions require extra styling on the parent of
27+
// the elements being transitioned. We place a generic class
28+
// on the parent that someone can use as a styling hook for
29+
// when any transition is running, and another class that ends
30+
// with the name of the transition so that transitions can enable
31+
// properties specifically for the transition.
32+
1633
viewportClass = "ui-mobile-viewport-transitioning viewport-" + name,
34+
35+
// We'll be manipulating both the "to" and "from" elements in
36+
// the same way, several times, so we use a cached collection
37+
// of both elements.
38+
1739
$both = $to.add( $from ),
18-
doneTimer = 0;
19-
var doneFunc = function() {
40+
41+
// Typically, we listen for the completion of the transition to
42+
// the "to" element, but in some cases, the "to" element is never
43+
// animated, so no transition complete is ever recieved. We need
44+
// to set a timer to catch this case so we don't end up waiting
45+
// for this notification that will never come.
46+
47+
doneTimer = 0,
48+
49+
// When the transition completes, we need to do some clean-up
50+
// of the CSS classes we placed on the "to" and "from" elements.
51+
52+
doneFunc = function() {
53+
// In most cases, this function will have been fired
54+
// off by the event that signals the completion of the
55+
// transition to the "to" element. We need to make sure
56+
// we clear the doneTimer so that it doesn't fire this
57+
// function a 2nd time.
58+
2059
if ( doneTimer ) {
2160
clearTimeout( doneTimer );
2261
doneTimer = 0;
2362
}
2463

64+
// Only one page can ever have the activePageClass on it,
65+
// so remove it from the "from" element and then place it
66+
// on the "to" element.
67+
2568
if ( $from ) {
2669
$from.removeClass( $.mobile.activePageClass );
2770
}
2871

2972
$to.addClass( $.mobile.activePageClass );
3073

74+
// Now strip off the transition classes used to animate
75+
// the elements.
76+
3177
$both.removeClass( "out in reverse animate " + name );
3278

3379
$to.parent().removeClass( viewportClass );
3480

81+
// Tell the caller of the transition handler that we're
82+
// all done.
83+
3584
deferred.resolve( name, reverse, $to, $from );
3685
};
3786

38-
// Set up a callback on the to element so we know when it
39-
// is done transitioning. Some transitions don't actually
40-
// animate the to element, so we also fire a timer to manually
41-
// trigger our done callback .
87+
// Set up a "transitionend" callback on the "to" element so we know when it
88+
// is done transitioning.
4289

4390
$to.transitionComplete( doneFunc );
4491

92+
// Some page-transitions don't actually trigger any CSS3 transitions on the
93+
// "to" element, so we fire off a timer to manually trigger our done callback
94+
// if we haven't recieved a "transitionend" notification within the alotted time.
95+
4596
doneTimer = setTimeout(function() {
4697
if( $.support.cssTransitions ) {
4798
$to.unbind( "transitionend webkitTransitionEnd OTransitionEnd", doneFunc );
4899
}
49100
doneFunc();
50-
}, 1000);
101+
}, css3TransitionsHandler.transitionThreshold );
102+
103+
// Add the animation classes that set up the transition interval and initial
104+
// values for the properties that will be transitioned.
51105

52106
$to.parent().addClass( viewportClass );
53107

54108
if ( $from ) {
55109
$from.addClass( name + " out" + reverseClass );
56110
}
111+
57112
$to.addClass( name + " in" + reverseClass );
58113

114+
// Fire off a timer that will add the "animate" class which triggers the CSS
115+
// rules for the "to" and "from" elements that specify new CSS property values
116+
// that will kick-off the transitions.
117+
59118
setTimeout(function(){
60119
$both.addClass("animate");
61-
}, 25);
120+
}, css3TransitionsHandler.animateClassInterval );
121+
122+
// After we've set up and started the transitions, return a promise to the
123+
// caller so they can tell when our async transition is done.
62124

63125
return deferred.promise();
64126
}
65127

128+
// msecs tow wait before placing the "animate" class on the "to" and "from" element
129+
// to kick-off any transitions.
130+
131+
css3TransitionsHandler.animateClassInterval = 25;
132+
133+
// msecs to wait for a "transitionend" event before manually firing off the done callback.
134+
135+
css3TransitionsHandler.transitionThreshold = 1000;
136+
66137
// Make our transition handler public.
67-
$.mobile.css3TransitionHandler = css3TransitionHandler;
68138

69-
// If the default transition handler is the 'none' handler, replace it with our handler.
139+
$.mobile.css3TransitionsHandler = css3TransitionsHandler;
140+
141+
// We want to avoid the situation where we accidentally override a default
142+
// handler specified by the developer, so we only replace the default if it
143+
// is currently the defaultTransitionHandler().
144+
70145
if ( $.mobile.defaultTransitionHandler === $.mobile.noneTransitionHandler ) {
71-
$.mobile.defaultTransitionHandler = css3TransitionHandler;
146+
$.mobile.defaultTransitionHandler = css3TransitionsHandler;
72147
}
73148

74149
})( jQuery, this );

0 commit comments

Comments
 (0)