Version 3.17.2
Show:

File: app/js/app-extensions/app-transitions.js

  1. /**
  2. `Y.App` extension that provides view transitions in browsers which support
  3. native CSS3 transitions.
  4.  
  5. @module app
  6. @submodule app-transitions
  7. @since 3.5.0
  8. **/
  9.  
  10. /**
  11. `Y.App` extension that provides view transitions in browsers which support
  12. native CSS3 transitions.
  13.  
  14. View transitions provide an nice way to move from one "page" to the next that is
  15. both pleasant to the user and helps to communicate a hierarchy between sections
  16. of an application.
  17.  
  18. When the `"app-transitions"` module is used, it will automatically mix itself
  19. into `Y.App` and transition between `activeView` changes using the following
  20. effects:
  21.  
  22. - **`fade`**: Cross-fades between the old an new active views.
  23.  
  24. - **`slideLeft`**: The old and new active views are positioned next to each
  25. other and both slide to the left.
  26.  
  27. - **`slideRight`**: The old and new active views are positioned next to each
  28. other and both slide to the right.
  29.  
  30. **Note:** Transitions are an opt-in feature and are enabled via an app's
  31. `transitions` attribute.
  32.  
  33. @class App.Transitions
  34. @uses App.TransitionsNative
  35. @extensionfor App
  36. @since 3.5.0
  37. **/
  38. function AppTransitions() {}
  39.  
  40. AppTransitions.ATTRS = {
  41. /**
  42. Whether or not this application should use view transitions, and if so then
  43. which ones or `true` for the defaults which are specified by the
  44. `transitions` prototype property.
  45.  
  46. **Note:** Transitions are an opt-in feature and will only be used in
  47. browsers which support native CSS3 transitions.
  48.  
  49. @attribute transitions
  50. @type Boolean|Object
  51. @default false
  52. @since 3.5.0
  53. **/
  54. transitions: {
  55. setter: '_setTransitions',
  56. value : false
  57. }
  58. };
  59.  
  60. /**
  61. Collect of transitions -> fx.
  62.  
  63. A transition (e.g. "fade") is a simple name given to a configuration of fx to
  64. apply, consisting of `viewIn` and `viewOut` properties who's values are names of
  65. fx registered on `Y.Transition.fx`.
  66.  
  67. By default transitions: `fade`, `slideLeft`, and `slideRight` have fx defined.
  68.  
  69. @property FX
  70. @type Object
  71. @static
  72. @since 3.5.0
  73. **/
  74. AppTransitions.FX = {
  75. fade: {
  76. viewIn : 'app:fadeIn',
  77. viewOut: 'app:fadeOut'
  78. },
  79.  
  80. slideLeft: {
  81. viewIn : 'app:slideLeft',
  82. viewOut: 'app:slideLeft'
  83. },
  84.  
  85. slideRight: {
  86. viewIn : 'app:slideRight',
  87. viewOut: 'app:slideRight'
  88. }
  89. };
  90.  
  91. AppTransitions.prototype = {
  92. // -- Public Properties ----------------------------------------------------
  93.  
  94. /**
  95. Default transitions to use when the `activeView` changes.
  96.  
  97. The following are types of changes for which transitions can be defined that
  98. correspond to the relationship between the new and previous `activeView`:
  99.  
  100. * `navigate`: The default transition to use when changing the `activeView`
  101. of the application.
  102.  
  103. * `toChild`: The transition to use when the new `activeView` is configured
  104. as a child of the previously active view via its `parent` property as
  105. defined in this app's `views`.
  106.  
  107. * `toParent`: The transition to use when the new `activeView` is
  108. configured as the `parent` of the previously active view as defined in
  109. this app's `views`.
  110.  
  111. **Note:** Transitions are an opt-in feature and will only be used in
  112. browsers which support native CSS3 transitions.
  113.  
  114. @property transitions
  115. @type Object
  116. @default
  117. {
  118. navigate: 'fade',
  119. toChild : 'slideLeft',
  120. toParent: 'slideRight'
  121. }
  122. @since 3.5.0
  123. **/
  124. transitions: {
  125. navigate: 'fade',
  126. toChild : 'slideLeft',
  127. toParent: 'slideRight'
  128. },
  129.  
  130. // -- Public Methods -------------------------------------------------------
  131.  
  132. /**
  133. Sets which view is active/visible for the application. This will set the
  134. app's `activeView` attribute to the specified `view`.
  135.  
  136. The `view` will be "attached" to this app, meaning it will be both rendered
  137. into this app's `viewContainer` node and all of its events will bubble to
  138. the app. The previous `activeView` will be "detached" from this app.
  139.  
  140. When a string-name is provided for a view which has been registered on this
  141. app's `views` object, the referenced metadata will be used and the
  142. `activeView` will be set to either a preserved view instance, or a new
  143. instance of the registered view will be created using the specified `config`
  144. object passed-into this method.
  145.  
  146. A callback function can be specified as either the third or fourth argument,
  147. and this function will be called after the new `view` becomes the
  148. `activeView`, is rendered to the `viewContainer`, and is ready to use.
  149.  
  150. @example
  151. var app = new Y.App({
  152. views: {
  153. usersView: {
  154. // Imagine that `Y.UsersView` has been defined.
  155. type: Y.UsersView
  156. }
  157. },
  158.  
  159. transitions: true,
  160. users : new Y.ModelList()
  161. });
  162.  
  163. app.route('/users/', function () {
  164. this.showView('usersView', {users: this.get('users')});
  165. });
  166.  
  167. app.render();
  168. app.navigate('/uses/');
  169. // => Creates a new `Y.UsersView` and transitions to it.
  170.  
  171. @method showView
  172. @param {String|View} view The name of a view defined in the `views` object,
  173. or a view instance which should become this app's `activeView`.
  174. @param {Object} [config] Optional configuration to use when creating a new
  175. view instance. This config object can also be used to update an existing
  176. or preserved view's attributes when `options.update` is `true`.
  177. @param {Object} [options] Optional object containing any of the following
  178. properties:
  179. @param {Function} [options.callback] Optional callback function to call
  180. after new `activeView` is ready to use, the function will be passed:
  181. @param {View} options.callback.view A reference to the new
  182. `activeView`.
  183. @param {Boolean} [options.prepend=false] Whether the `view` should be
  184. prepended instead of appended to the `viewContainer`.
  185. @param {Boolean} [options.render] Whether the `view` should be rendered.
  186. **Note:** If no value is specified, a view instance will only be
  187. rendered if it's newly created by this method.
  188. @param {Boolean|String} [options.transition] Optional transition override.
  189. A transition can be specified which will override the default, or
  190. `false` for no transition.
  191. @param {Boolean} [options.update=false] Whether an existing view should
  192. have its attributes updated by passing the `config` object to its
  193. `setAttrs()` method. **Note:** This option does not have an effect if
  194. the `view` instance is created as a result of calling this method.
  195. @param {Function} [callback] Optional callback Function to call after the
  196. new `activeView` is ready to use. **Note:** this will override
  197. `options.callback` and it can be specified as either the third or fourth
  198. argument. The function will be passed the following:
  199. @param {View} callback.view A reference to the new `activeView`.
  200. @chainable
  201. @since 3.5.0
  202. **/
  203. // Does not override `showView()` but does use `options.transitions`.
  204.  
  205. // -- Protected Methods ----------------------------------------------------
  206.  
  207. /**
  208. Setter for `transitions` attribute.
  209.  
  210. When specified as `true`, the defaults will be use as specified by the
  211. `transitions` prototype property.
  212.  
  213. @method _setTransitions
  214. @param {Boolean|Object} transitions The new `transitions` attribute value.
  215. @return {Mixed} The processed value which represents the new state.
  216. @protected
  217. @see App.Base.showView()
  218. @since 3.5.0
  219. **/
  220. _setTransitions: function (transitions) {
  221. var defTransitions = this.transitions;
  222.  
  223. if (transitions && transitions === true) {
  224. return Y.merge(defTransitions);
  225. }
  226.  
  227. return transitions;
  228. }
  229. };
  230.  
  231. // -- Namespace ----------------------------------------------------------------
  232. Y.App.Transitions = AppTransitions;
  233. Y.Base.mix(Y.App, [AppTransitions]);
  234.  
  235. Y.mix(Y.App.CLASS_NAMES, {
  236. transitioning: Y.ClassNameManager.getClassName('app', 'transitioning')
  237. });
  238.