|
9 | 9 |
|
10 | 10 | (function( $, window, undefined ) { |
11 | 11 |
|
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. |
13 | 17 |
|
14 | 18 | 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 | + |
15 | 24 | 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 | + |
16 | 33 | 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 | + |
17 | 39 | $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 | + |
20 | 59 | if ( doneTimer ) { |
21 | 60 | clearTimeout( doneTimer ); |
22 | 61 | doneTimer = 0; |
23 | 62 | } |
24 | 63 |
|
| 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 | + |
25 | 68 | if ( $from ) { |
26 | 69 | $from.removeClass( $.mobile.activePageClass ); |
27 | 70 | } |
28 | 71 |
|
29 | 72 | $to.addClass( $.mobile.activePageClass ); |
30 | 73 |
|
| 74 | + // Now strip off the transition classes used to animate |
| 75 | + // the elements. |
| 76 | + |
31 | 77 | $both.removeClass( "out in reverse animate " + name ); |
32 | 78 |
|
33 | 79 | $to.parent().removeClass( viewportClass ); |
34 | 80 |
|
| 81 | + // Tell the caller of the transition handler that we're |
| 82 | + // all done. |
| 83 | + |
35 | 84 | deferred.resolve( name, reverse, $to, $from ); |
36 | 85 | }; |
37 | 86 |
|
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. |
42 | 89 |
|
43 | 90 | $to.transitionComplete( doneFunc ); |
44 | 91 |
|
| 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 | + |
45 | 96 | doneTimer = setTimeout(function() { |
46 | 97 | if( $.support.cssTransitions ) { |
47 | 98 | $to.unbind( "transitionend webkitTransitionEnd OTransitionEnd", doneFunc ); |
48 | 99 | } |
49 | 100 | 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. |
51 | 105 |
|
52 | 106 | $to.parent().addClass( viewportClass ); |
53 | 107 |
|
54 | 108 | if ( $from ) { |
55 | 109 | $from.addClass( name + " out" + reverseClass ); |
56 | 110 | } |
| 111 | + |
57 | 112 | $to.addClass( name + " in" + reverseClass ); |
58 | 113 |
|
| 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 | + |
59 | 118 | setTimeout(function(){ |
60 | 119 | $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. |
62 | 124 |
|
63 | 125 | return deferred.promise(); |
64 | 126 | } |
65 | 127 |
|
| 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 | + |
66 | 137 | // Make our transition handler public. |
67 | | -$.mobile.css3TransitionHandler = css3TransitionHandler; |
68 | 138 |
|
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 | + |
70 | 145 | if ( $.mobile.defaultTransitionHandler === $.mobile.noneTransitionHandler ) { |
71 | | - $.mobile.defaultTransitionHandler = css3TransitionHandler; |
| 146 | + $.mobile.defaultTransitionHandler = css3TransitionsHandler; |
72 | 147 | } |
73 | 148 |
|
74 | 149 | })( jQuery, this ); |
0 commit comments