NavigationController.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. 'use strict';
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: Storytelling (C) Copyright IBM Corp. 2014, 2019
  5. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  6. */
  7. define(['baglass/core-client/js/core-client/ui/core/Class'], function (Class) {
  8. var NavigationController = Class.extend({
  9. /**
  10. * @param {Object} options A normal object of properties
  11. * @param {Object} options.transitionController An instance of a class whose base class is a TransitionController
  12. * @param {Object} options.sceneLoader An instance of the SceneLoader class
  13. * @param {Object} options.logger The logging class
  14. * @param {Object} options.sceneLayoutApi The SceneLayout API
  15. *
  16. * @see TransitionController.js
  17. * @see SceneLoader.js
  18. * @see SceneLayout.js
  19. */
  20. init: function init(options) {
  21. NavigationController.inherited('init', this, arguments);
  22. this._transitionController = options.transitionController;
  23. this._sceneLoader = options.sceneLoader;
  24. this.logger = options.logger;
  25. this.sceneLayoutApi = options.sceneLayoutApi; // Required
  26. this.inProgress = false;
  27. },
  28. /**
  29. * @param {Object} options
  30. * @param {Number} options.index (Deprecated) This value is still used by StoryPaneController but should be avoided for new uses
  31. * @param {String} options.modelId This is the layout model id and should be used over an index value
  32. * @param {Function} [options.onTargetSelected] An optional function that, when provided, will trigger a 'navigation:started' event
  33. * @param {Function} [options.refreshData] An optional parameter that, when provided, will trigger a refresh of all the story data widgets
  34. */
  35. jumpTo: function jumpTo(options) {
  36. var _this = this;
  37. if (this.inProgress) {
  38. return Promise.reject();
  39. }
  40. this.inProgress = true;
  41. // TODO: Update StoryPaneController to use sceneIds instead of index values
  42. var targetSceneByIndex = this.sceneLayoutApi.getSceneByIndex(options.index);
  43. var targetScene = targetSceneByIndex || this.sceneLayoutApi.getSceneById(options.modelId);
  44. var currentScene = this.sceneLayoutApi.getCurrentScene() || targetScene;
  45. var onDeck = this.sceneLayoutApi.getAdjacentScenes(targetScene).concat([targetScene]);
  46. var offDeck = this.sceneLayoutApi.getAdjacentScenes(currentScene).concat([currentScene]);
  47. return this._sceneLoader.waitForSceneToLoad(targetScene, options).then(function () {
  48. // start pre-loading of the next scenes.
  49. _this._sceneLoader.startBackgroundLoad(_this._generateShiftedSceneArray(targetScene), options);
  50. _this._transitionController.deckScenes(onDeck);
  51. _this._updateSelection(currentScene, targetScene);
  52. if (options && options.onTargetSelected) {
  53. options.onTargetSelected({
  54. scene: targetScene,
  55. play: options.play
  56. });
  57. }
  58. return _this._transitionController.jumpTo(currentScene, targetScene);
  59. }).then(function (result) {
  60. _this._transitionController.undeckScenes(offDeck);
  61. _this.inProgress = false;
  62. return {
  63. scene: targetScene,
  64. overview: result.overview
  65. };
  66. }).catch(function (error) {
  67. _this.logger.error('NavigationController.jumpTo()', error);
  68. _this.inProgress = false;
  69. return Promise.reject();
  70. });
  71. },
  72. nextPage: function nextPage(options) {
  73. var _this2 = this;
  74. if (this.inProgress) {
  75. return Promise.reject();
  76. }
  77. this.inProgress = true;
  78. var targetScene = this.sceneLayoutApi.getSceneByIndex(options.to) || this.sceneLayoutApi.getNextScene();
  79. var currentScene = this.sceneLayoutApi.getCurrentScene() || targetScene;
  80. var onDeck = this.sceneLayoutApi.getAdjacentScenes(targetScene).concat([targetScene]);
  81. var offDeck = this.sceneLayoutApi.getAdjacentScenes(currentScene).concat([currentScene]);
  82. return this._sceneLoader.waitForSceneToLoad(targetScene, options).then(function () {
  83. if (options.refreshData) {
  84. _this2._sceneLoader.startBackgroundLoad(_this2._generateShiftedSceneArray(targetScene), options);
  85. }
  86. _this2._transitionController.deckScenes(onDeck);
  87. _this2._updateSelection(currentScene, targetScene);
  88. if (options && options.onTargetSelected) {
  89. options.onTargetSelected({ scene: targetScene });
  90. }
  91. return _this2._transitionController.forward(currentScene, targetScene);
  92. }).then(function (result) {
  93. _this2._transitionController.undeckScenes(offDeck);
  94. _this2.inProgress = false;
  95. return {
  96. scene: targetScene,
  97. overview: result.overview
  98. };
  99. }).catch(function (error) {
  100. _this2.logger.error('NavigationController.nextPage()', error);
  101. _this2.inProgress = false;
  102. return Promise.reject();
  103. });
  104. },
  105. previousPage: function previousPage(options) {
  106. var _this3 = this;
  107. if (this.inProgress) {
  108. return Promise.reject();
  109. }
  110. this.inProgress = true;
  111. var targetScene = this.sceneLayoutApi.getSceneByIndex(options.to) || this.sceneLayoutApi.getPreviousScene();
  112. var currentScene = this.sceneLayoutApi.getCurrentScene() || targetScene;
  113. var onDeck = this.sceneLayoutApi.getAdjacentScenes(targetScene).concat([targetScene]);
  114. var offDeck = this.sceneLayoutApi.getAdjacentScenes(currentScene).concat([currentScene]);
  115. return this._sceneLoader.waitForSceneToLoad(targetScene, options).then(function () {
  116. if (options.refreshData) {
  117. _this3._sceneLoader.startBackgroundLoad(_this3._generateShiftedSceneArray(targetScene), options);
  118. }
  119. _this3._transitionController.deckScenes(onDeck);
  120. _this3._updateSelection(currentScene, targetScene);
  121. if (options && options.onTargetSelected) {
  122. options.onTargetSelected({ scene: targetScene });
  123. }
  124. return _this3._transitionController.backward(currentScene, targetScene);
  125. }).then(function (result) {
  126. _this3._transitionController.undeckScenes(offDeck);
  127. _this3.inProgress = false;
  128. return {
  129. scene: targetScene,
  130. overview: result.overview
  131. };
  132. }).catch(function (error) {
  133. _this3.logger.error('NavigationController.previousPage()', error);
  134. _this3.inProgress = false;
  135. return Promise.reject();
  136. });
  137. },
  138. /**
  139. * FIXME: This method should probably be within SceneLayout.js / part of the SceneLayout API
  140. */
  141. _updateSelection: function _updateSelection(from, to) {
  142. from.$el.removeClass('selected');
  143. to.$el.addClass('selected');
  144. },
  145. /**
  146. * Create a new ordered array of all scenes in sequential order starting from the provided scene
  147. * Example:
  148. * array = [1, 2, 3, 4, 5, 6]
  149. * currentIndex = 2 (value of 3)
  150. * newArray = [3, 4, 5, 6, 1]
  151. *
  152. * @param {Object} scene The scene to use as the starting scene of the new array
  153. */
  154. _generateShiftedSceneArray: function _generateShiftedSceneArray(scene) {
  155. var _inRange = function _inRange(value, array) {
  156. return value >= 0 && value < array.length;
  157. };
  158. var firstScene = this.sceneLayoutApi.getNextScene(scene);
  159. var scenes = this.sceneLayoutApi.getScenes();
  160. var index = this.sceneLayoutApi.getSceneIndex(firstScene);
  161. index = _inRange(index, scenes) ? index : 0;
  162. var orderedScenes = [];
  163. for (var i = 0; i < scenes.length; i++) {
  164. var _scene = scenes[(i + index) % scenes.length];
  165. orderedScenes.push(_scene);
  166. }
  167. if (orderedScenes[orderedScenes.length - 1].id === scene.id) {
  168. orderedScenes.pop();
  169. }
  170. return orderedScenes;
  171. },
  172. /**
  173. * Used when a story type (like Slideshow) has multiples transition types
  174. */
  175. setTransitionController: function setTransitionController(transitionController) {
  176. delete this._transitionController;
  177. this._transitionController = transitionController;
  178. }
  179. });
  180. return NavigationController;
  181. });
  182. //# sourceMappingURL=NavigationController.js.map