/jquery/source/js/rotator.js

https://github.com/kolyuchiy/gtv-ui-lib · JavaScript · 192 lines · 99 code · 31 blank · 62 comment · 9 complexity · 4d5d949c056bc5a7b2dfe79f9422bd8f MD5 · raw file

  1. // Copyright 2010 Google Inc. 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 Classes for RotatorControl
  16. *
  17. */
  18. var gtv = gtv || {
  19. jq: {}
  20. };
  21. /**
  22. * RotatorControl class. Rotator control displays a single item of a list at
  23. * a time, rotating new items into view when user presses up/down.
  24. * @param {gtv.jq.CreationParams} createParams
  25. * @constructor
  26. */
  27. gtv.jq.RotatorControl = function(createParams) {
  28. this.params_ = new gtv.jq.CreationParams(createParams);
  29. };
  30. /**
  31. * Parent element containing the control elements.
  32. * @type {jQuery.Element}
  33. * @private
  34. */
  35. gtv.jq.RotatorControl.prototype.container_ = null;
  36. /**
  37. * Holds the params the control was created with.
  38. * @type {CreationParams}
  39. * @private
  40. */
  41. gtv.jq.RotatorControl.prototype.params_ = null;
  42. /**
  43. * Holds the params for showing the control.
  44. * @type {ShowParams}
  45. * @private
  46. */
  47. gtv.jq.RotatorControl.prototype.showParams_ = null;
  48. /**
  49. * Key controller behavior zone for this control.
  50. * @type {KeyBehaviorZone}
  51. * @private
  52. */
  53. gtv.jq.RotatorControl.prototype.behaviorZone_ = null;
  54. /**
  55. * Removes the control from its container and from the key controller.
  56. */
  57. gtv.jq.RotatorControl.prototype.deleteControl = function() {
  58. var rotatorControl = this;
  59. rotatorControl.keyController.removeBehaviorZone(rotatorControl.behaviorZone_);
  60. rotatorControl.container_.remove();
  61. };
  62. /**
  63. * Creates a new RotatorControl with the specified items and adds it to a
  64. * container on the page.
  65. * @param {gtv.jq.ShowParams} showParams Params for creating the control.
  66. * @return {boolean} true on success
  67. */
  68. gtv.jq.RotatorControl.prototype.showControl = function(showParams) {
  69. var rotatorControl = this;
  70. rotatorControl.showParams_ = new gtv.jq.ShowParams(showParams);
  71. if (!rotatorControl.showParams_.contents.items) {
  72. throw new Error('RotatorControl requires items array');
  73. }
  74. rotatorControl.container_ = $('<div></div>')
  75. .attr('id', rotatorControl.params_.containerId)
  76. .addClass('rotator-div');
  77. rotatorControl.showParams_.topParent.append(rotatorControl.container_);
  78. for (var i = 0; i < rotatorControl.showParams_.contents.items.length; i++) {
  79. var itemRow = $('<div></div>').addClass('rotator-row');
  80. rotatorControl.container_.append(itemRow);
  81. var itemDiv = $('<div></div>')
  82. .addClass('rotator-item-div')
  83. .append(rotatorControl.showParams_.contents.items[i]);
  84. rotatorControl.showParams_.contents.items[i].data('index', i);
  85. rotatorControl.showParams_.contents.items[i].addClass(
  86. 'rotator-item ' + rotatorControl.params_.styles.item);
  87. itemRow.append(itemDiv);
  88. }
  89. var navSelectors = {
  90. item: '.rotator-item',
  91. itemParent: '.rotator-item-div',
  92. itemRow: '.rotator-row'
  93. };
  94. // Configure gtv.jq.KeyActions object.
  95. var actions = {
  96. scrollIntoView: function(selectedItem, newItem, getFinishCallback) {
  97. rotatorControl.showItem_(selectedItem, newItem, getFinishCallback);
  98. }
  99. };
  100. rotatorControl.behaviorZone_ =
  101. new gtv.jq.KeyBehaviorZone({
  102. containerSelector: '#' + rotatorControl.params_.containerId,
  103. actions: actions,
  104. navSelectors: navSelectors,
  105. selectHidden: true
  106. });
  107. rotatorControl.params_.keyController.addBehaviorZone(
  108. rotatorControl.behaviorZone_, true, rotatorControl.params_.layerNames);
  109. };
  110. /**
  111. * Display the selected item, animating "away" the currently selected item and
  112. * then animating the new item into view.
  113. * @param {jQuery.Element} selectedItem The currently selected item.
  114. * @param {jQuery.Element} newItem Newly selected item to scroll into view).
  115. * @param {SynchronizedCallback.callback} getFinishCallback Callback generator
  116. * that returns a callback to make when movement animations are complete.
  117. * @private
  118. */
  119. gtv.jq.RotatorControl.prototype.showItem_ = function(selectedItem,
  120. newItem,
  121. getFinishCallback) {
  122. var rotatorControl = this;
  123. if (selectedItem) {
  124. if (selectedItem.get(0) == newItem.get(0)) {
  125. return;
  126. }
  127. var finishCallback = getFinishCallback();
  128. var itemHeight = selectedItem.height();
  129. selectedItem.css({
  130. '-webkit-animation-name': 'rotateaway'
  131. });
  132. selectedItem.get(0).addEventListener('webkitAnimationEnd', rotateAway);
  133. function rotateAway(event) {
  134. selectedItem.get(0).removeEventListener('webkitAnimationEnd', rotateAway);
  135. selectedItem.css({
  136. '-webkit-animation-name': '',
  137. display: 'none'
  138. });
  139. newItem.css({
  140. '-webkit-animation-name': 'rotatein',
  141. display: 'block'
  142. });
  143. newItem.get(0).addEventListener('webkitAnimationEnd', rotateIn);
  144. function rotateIn(event) {
  145. newItem.get(0).removeEventListener('webkitAnimationEnd', rotateIn);
  146. newItem.css({
  147. '-webkit-animation-name': ''
  148. });
  149. if (rotatorControl.params_.choiceCallback) {
  150. rotatorControl.params_.choiceCallback(newItem);
  151. }
  152. finishCallback();
  153. }
  154. }
  155. } else {
  156. newItem.css('display', 'block');
  157. if (rotatorControl.params_.choiceCallback) {
  158. rotatorControl.params_.choiceCallback(newItem);
  159. }
  160. }
  161. };