transitionbase.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. // Copyright 2011 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview An abstract base class for transitions. This is a simple
  16. * interface that allows for playing, pausing and stopping an animation. It adds
  17. * a simple event model, and animation status.
  18. */
  19. goog.provide('goog.fx.TransitionBase');
  20. goog.provide('goog.fx.TransitionBase.State');
  21. goog.require('goog.events.EventTarget');
  22. goog.require('goog.fx.Transition'); // Unreferenced: interface
  23. /**
  24. * Constructor for a transition object.
  25. *
  26. * @constructor
  27. * @struct
  28. * @implements {goog.fx.Transition}
  29. * @extends {goog.events.EventTarget}
  30. */
  31. goog.fx.TransitionBase = function() {
  32. goog.fx.TransitionBase.base(this, 'constructor');
  33. /**
  34. * The internal state of the animation.
  35. * @type {goog.fx.TransitionBase.State}
  36. * @private
  37. */
  38. this.state_ = goog.fx.TransitionBase.State.STOPPED;
  39. /**
  40. * Timestamp for when the animation was started.
  41. * @type {?number}
  42. * @protected
  43. */
  44. this.startTime = null;
  45. /**
  46. * Timestamp for when the animation finished or was stopped.
  47. * @type {?number}
  48. * @protected
  49. */
  50. this.endTime = null;
  51. };
  52. goog.inherits(goog.fx.TransitionBase, goog.events.EventTarget);
  53. /**
  54. * Enum for the possible states of an animation.
  55. * @enum {number}
  56. */
  57. goog.fx.TransitionBase.State = {
  58. STOPPED: 0,
  59. PAUSED: -1,
  60. PLAYING: 1
  61. };
  62. /**
  63. * Plays the animation.
  64. *
  65. * @param {boolean=} opt_restart Optional parameter to restart the animation.
  66. * @return {boolean} True iff the animation was started.
  67. * @override
  68. */
  69. goog.fx.TransitionBase.prototype.play = goog.abstractMethod;
  70. /**
  71. * Stops the animation.
  72. *
  73. * @param {boolean=} opt_gotoEnd Optional boolean parameter to go the the end of
  74. * the animation.
  75. * @override
  76. */
  77. goog.fx.TransitionBase.prototype.stop = goog.abstractMethod;
  78. /**
  79. * Pauses the animation.
  80. */
  81. goog.fx.TransitionBase.prototype.pause = goog.abstractMethod;
  82. /**
  83. * Returns the current state of the animation.
  84. * @return {goog.fx.TransitionBase.State} State of the animation.
  85. */
  86. goog.fx.TransitionBase.prototype.getStateInternal = function() {
  87. return this.state_;
  88. };
  89. /**
  90. * Sets the current state of the animation to playing.
  91. * @protected
  92. */
  93. goog.fx.TransitionBase.prototype.setStatePlaying = function() {
  94. this.state_ = goog.fx.TransitionBase.State.PLAYING;
  95. };
  96. /**
  97. * Sets the current state of the animation to paused.
  98. * @protected
  99. */
  100. goog.fx.TransitionBase.prototype.setStatePaused = function() {
  101. this.state_ = goog.fx.TransitionBase.State.PAUSED;
  102. };
  103. /**
  104. * Sets the current state of the animation to stopped.
  105. * @protected
  106. */
  107. goog.fx.TransitionBase.prototype.setStateStopped = function() {
  108. this.state_ = goog.fx.TransitionBase.State.STOPPED;
  109. };
  110. /**
  111. * @return {boolean} True iff the current state of the animation is playing.
  112. */
  113. goog.fx.TransitionBase.prototype.isPlaying = function() {
  114. return this.state_ == goog.fx.TransitionBase.State.PLAYING;
  115. };
  116. /**
  117. * @return {boolean} True iff the current state of the animation is paused.
  118. */
  119. goog.fx.TransitionBase.prototype.isPaused = function() {
  120. return this.state_ == goog.fx.TransitionBase.State.PAUSED;
  121. };
  122. /**
  123. * @return {boolean} True iff the current state of the animation is stopped.
  124. */
  125. goog.fx.TransitionBase.prototype.isStopped = function() {
  126. return this.state_ == goog.fx.TransitionBase.State.STOPPED;
  127. };
  128. /**
  129. * Dispatches the BEGIN event. Sub classes should override this instead
  130. * of listening to the event, and call this instead of dispatching the event.
  131. * @protected
  132. */
  133. goog.fx.TransitionBase.prototype.onBegin = function() {
  134. this.dispatchAnimationEvent(goog.fx.Transition.EventType.BEGIN);
  135. };
  136. /**
  137. * Dispatches the END event. Sub classes should override this instead
  138. * of listening to the event, and call this instead of dispatching the event.
  139. * @protected
  140. */
  141. goog.fx.TransitionBase.prototype.onEnd = function() {
  142. this.dispatchAnimationEvent(goog.fx.Transition.EventType.END);
  143. };
  144. /**
  145. * Dispatches the FINISH event. Sub classes should override this instead
  146. * of listening to the event, and call this instead of dispatching the event.
  147. * @protected
  148. */
  149. goog.fx.TransitionBase.prototype.onFinish = function() {
  150. this.dispatchAnimationEvent(goog.fx.Transition.EventType.FINISH);
  151. };
  152. /**
  153. * Dispatches the PAUSE event. Sub classes should override this instead
  154. * of listening to the event, and call this instead of dispatching the event.
  155. * @protected
  156. */
  157. goog.fx.TransitionBase.prototype.onPause = function() {
  158. this.dispatchAnimationEvent(goog.fx.Transition.EventType.PAUSE);
  159. };
  160. /**
  161. * Dispatches the PLAY event. Sub classes should override this instead
  162. * of listening to the event, and call this instead of dispatching the event.
  163. * @protected
  164. */
  165. goog.fx.TransitionBase.prototype.onPlay = function() {
  166. this.dispatchAnimationEvent(goog.fx.Transition.EventType.PLAY);
  167. };
  168. /**
  169. * Dispatches the RESUME event. Sub classes should override this instead
  170. * of listening to the event, and call this instead of dispatching the event.
  171. * @protected
  172. */
  173. goog.fx.TransitionBase.prototype.onResume = function() {
  174. this.dispatchAnimationEvent(goog.fx.Transition.EventType.RESUME);
  175. };
  176. /**
  177. * Dispatches the STOP event. Sub classes should override this instead
  178. * of listening to the event, and call this instead of dispatching the event.
  179. * @protected
  180. */
  181. goog.fx.TransitionBase.prototype.onStop = function() {
  182. this.dispatchAnimationEvent(goog.fx.Transition.EventType.STOP);
  183. };
  184. /**
  185. * Dispatches an event object for the current animation.
  186. * @param {string} type Event type that will be dispatched.
  187. * @protected
  188. */
  189. goog.fx.TransitionBase.prototype.dispatchAnimationEvent = function(type) {
  190. this.dispatchEvent(type);
  191. };