VisRenderState.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. 'use strict';
  2. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  3. /**
  4. * Licensed Materials - Property of IBM
  5. * IBM Cognos Products: BI Cloud (C) Copyright IBM Corp. 2013, 2019
  6. * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. *
  8. * VisRenderState
  9. * The VisRenderState represents the current state of rendering for a visualization.
  10. * Rendering involves loading all the requirements to render, then calling the view's render method.
  11. *
  12. */
  13. define(['underscore'], function (_) {
  14. return function () {
  15. function VisRenderState() {
  16. _classCallCheck(this, VisRenderState);
  17. /**
  18. * _currentContext is a structure representing the current state of the render sequence.
  19. * Each member of the currentContext represents a 'step of the renderSequence which points
  20. * to various renderContexts that are responsible for those steps.
  21. */
  22. this._currentContext = {};
  23. /**
  24. * The render context pool holds the set of 'active renderContexts' that store portions
  25. * of the data needed to render a visualization. These are cleaned up when no longer
  26. * part of the active render context.
  27. *
  28. * The renderContextPool is keyed by id which is currently a serial number (_renderId) for ease of debuggins
  29. * (but could be any unique number).
  30. */
  31. this._renderContextPool = {};
  32. //Data Members...
  33. this.firstRenderComplete = false;
  34. this.lastSizeRendered = null;
  35. this._steps = [];
  36. }
  37. VisRenderState.prototype.remove = function remove() {
  38. this.resetRenderState();
  39. };
  40. VisRenderState.prototype.initCurrentContext = function initCurrentContext(stepName) {
  41. if (!this._currentContext.hasOwnProperty(this._stepToStepContext(stepName))) {
  42. this._steps.push(stepName);
  43. this._currentContext[this._stepToStepContext(stepName)] = null;
  44. }
  45. };
  46. /**
  47. * Given a step name (like 'visView', 'visControl', 'data') return the
  48. * current rendering context of that step.
  49. * @returns a RenderContext object that is responsible for that step in the render sequence.
  50. * (or that portion of the renderState's currentContext)
  51. */
  52. VisRenderState.prototype.getCurrentContext = function getCurrentContext(stepName) {
  53. return this._currentContext[this._stepToStepContext(stepName)];
  54. };
  55. // Main accessor
  56. // return elements of the renderStates' current context to callers.
  57. VisRenderState.prototype.getCurrentContextData = function getCurrentContextData(stepName) {
  58. return this._currentContext[this._stepToStepContext(stepName)] ? this._currentContext[this._stepToStepContext(stepName)][stepName] : null;
  59. };
  60. /**
  61. * Add a new render context which flows through the renderSequence collecting the parts of the render it is responsible for.
  62. * These parts are defined by the options.
  63. * If a renderContext completes and all parts are loaded, the visualization can render.
  64. *
  65. * @param options Information about what should be refreshed when running through the renderSequence.
  66. * (also is 'the steps this renderContext is responsible for')
  67. * @param {integer} options.renderId - the run id for this context
  68. * Examples:
  69. * a resize would use pass no options <= which denotes reRender without refreshing any data.
  70. * a filter change would use options { refresh: { data: true }},
  71. * a visualization type change where the renderer type stays the same would use { refresh: { visSpec: true } }
  72. * Initial render (or changing renderers) would pass { refreshAll: true } meaning all data needs to be loaded.
  73. *
  74. * @returns a new RenderContext.
  75. * @throws Exception when options.renderId is not defined
  76. */
  77. VisRenderState.prototype.addRenderContext = function addRenderContext(options) {
  78. if (_.isUndefined(options) || _.isUndefined(options.renderId)) {
  79. throw new Error('cannot define a render context: missing renderId');
  80. }
  81. var renderContext = {
  82. id: options.renderId
  83. };
  84. _.each(this._steps, function (step) {
  85. renderContext[step] = null;
  86. });
  87. if (_.keys(options).length > 1) {
  88. renderContext.extraInfo = options.extraInfo;
  89. if (this._currentContext.dataContext && options.extraInfo && options.extraInfo.sender === 'realtimeRefresh') {
  90. // If we are in realtime refresh mode, the data request might return nothing if the data didn't change.
  91. // We need to keep the data the same in case
  92. renderContext.previousData = this._currentContext.dataContext.data;
  93. }
  94. }
  95. renderContext.resizing = options && options.resizing ? true : false;
  96. this._renderContextPool[renderContext.id] = renderContext;
  97. if (_.keys(options).length > 1) {
  98. if (options.refresh && options.refresh.data && !this.readyToLoadData()) {
  99. options.refreshAll = true;
  100. }
  101. if (options.refresh) {
  102. options.refresh.render = true;
  103. } else {
  104. // For anything else, ensure to do the main render
  105. options.refresh = {
  106. render: true
  107. };
  108. }
  109. renderContext.refresh = options.refresh;
  110. renderContext.refreshAll = options.refreshAll;
  111. if (options.refreshAll) {
  112. //Initialize ALL of the _currentContext to point to this renderContext.
  113. this.reloadAllRenderSteps(); //Clear the loaded steps in the render sequence.
  114. _.each(_.keys(this._currentContext), function (key) {
  115. this._currentContext[key] = renderContext;
  116. }.bind(this));
  117. } else if (options.refresh) {
  118. //Initialize the portions of the _currentContext to be refreshed.
  119. _.each(_.keys(options.refresh), function (stepName) {
  120. this._currentContext[this._stepToStepContext(stepName)] = renderContext;
  121. }.bind(this));
  122. //If calling re-render, at a minumum, we need to ensure the view is loaded.
  123. if (!this._currentContext.visViewContext) {
  124. this._currentContext.visViewContext = renderContext;
  125. }
  126. }
  127. }
  128. return renderContext;
  129. };
  130. /**
  131. * @returns true when all steps of the renderStates' current context prior to loading data are complete (initialized).
  132. */
  133. VisRenderState.prototype.readyToLoadData = function readyToLoadData() {
  134. return !!(this.getCurrentContextData('visView') && this.getCurrentContextData('visControl'));
  135. };
  136. /**
  137. * @returns true when all steps of the renderStates' current context are complete (initialized).
  138. */
  139. VisRenderState.prototype.readyToRender = function readyToRender() {
  140. return !!(this.getCurrentContextData('visView') && this.getCurrentContextData('visControl') && this.getCurrentContextData('data'));
  141. };
  142. /**
  143. * Reset the current context and context Renderer pools back to their initial state.
  144. */
  145. VisRenderState.prototype.resetRenderState = function resetRenderState() {
  146. this.lastSizeRendered = null;
  147. this._clearCurrentContext();
  148. this.cleanRenderContextPool( /*removeAll*/true);
  149. };
  150. /**
  151. * @return true if 'thisRenderContext' is obsolete.
  152. * (ie: no longer being used by any step in the _current context.)
  153. */
  154. VisRenderState.prototype._renderContextUnused = function _renderContextUnused(thisRenderContext) {
  155. var thisRenderContextFound = _.find(_.keys(this._currentContext), function (step) {
  156. return this._currentContext[step] === thisRenderContext;
  157. }.bind(this));
  158. //Don't delete a render context if any step in the render sequence is still using it.
  159. if (thisRenderContextFound) {
  160. return false;
  161. }
  162. //If no steps use this context, still shouldn't delete a render context until it is complete (ie: view render has/has not been called)..
  163. return thisRenderContext.renderComplete ? true : false;
  164. };
  165. /**
  166. * Remove unused (or all) entries from the RenderContextPool.
  167. * @param removeAll - when true removes all entries...otherwise, checks if the element is used.
  168. */
  169. VisRenderState.prototype.cleanRenderContextPool = function cleanRenderContextPool(removeAll) {
  170. _.each(_.keys(this._renderContextPool), function (key) {
  171. var renderContext = this._renderContextPool[key];
  172. if (removeAll || this._renderContextUnused(renderContext)) {
  173. delete this._renderContextPool[key];
  174. }
  175. }.bind(this));
  176. };
  177. /**
  178. * Remove all backing data from the currentContext.
  179. */
  180. VisRenderState.prototype._clearCurrentContext = function _clearCurrentContext() {
  181. if (this.getCurrentContextData('visView')) {
  182. this.getCurrentContextData('visView').remove();
  183. }
  184. _.each(_.keys(this._currentContext), function (key) {
  185. this._currentContext[key] = null;
  186. }.bind(this));
  187. };
  188. /**
  189. * Force a reload of all rendering steps without removing the RenderInfo assignments.
  190. * This is done when the renderer changes...or for any reason a full render is being done.
  191. */
  192. VisRenderState.prototype.reloadAllRenderSteps = function reloadAllRenderSteps() {
  193. if (this.getCurrentContextData('visView')) {
  194. this.getCurrentContextData('visView').remove( /*finalRemove*/false);
  195. }
  196. _.each(_.keys(this._currentContext), function (stepContext) {
  197. //Set the data element within each context step
  198. //whose name matches the key without 'Context'
  199. //For example visViewContext.visView, visControlContext.visControl, dataContext.data.
  200. var step = this._stepContextToStep(stepContext);
  201. if (this._currentContext[stepContext] && this._currentContext[stepContext][step]) {
  202. this._currentContext[stepContext][step] = null;
  203. }
  204. }.bind(this));
  205. };
  206. /**
  207. * @returns The fully populated renderContext that is active for a particular render.
  208. * For example: a particular render might have "refresh: render" defined so other contexts would be null in the render context.
  209. * But there is an active data context from a previous render. This would populate that value and all previous render values.
  210. */
  211. VisRenderState.prototype.getFullRenderContext = function getFullRenderContext() {
  212. var fullRenderContext = {};
  213. for (var stepContext in this._currentContext) {
  214. //Set the data element within each context step whose name matches the stepContext name.
  215. var step = this._stepContextToStep(stepContext);
  216. fullRenderContext[step] = this._currentContext[stepContext] && this._currentContext[stepContext][step] || null;
  217. }
  218. return fullRenderContext;
  219. };
  220. //Helper function to convert a stepName (like 'data' into a stepContextName like 'dataContext')
  221. //TODO: Do we really have to make these different?
  222. VisRenderState.prototype._stepToStepContext = function _stepToStepContext(stepName) {
  223. return stepName + 'Context';
  224. };
  225. //Helper function to convert a contextName (like 'dataContext' into a stepName like 'data')
  226. //TODO: Do we really have to make these different?
  227. VisRenderState.prototype._stepContextToStep = function _stepContextToStep(contextName) {
  228. return contextName.substring(0, contextName.length - 7);
  229. };
  230. return VisRenderState;
  231. }();
  232. });
  233. //# sourceMappingURL=VisRenderState.js.map