ViewerDispatcher.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. *+------------------------------------------------------------------------+
  3. *| Licensed Materials - Property of IBM
  4. *| IBM Cognos Products: Viewer
  5. *| (C) Copyright IBM Corp. 2001, 2012
  6. *|
  7. *| US Government Users Restricted Rights - Use, duplication or
  8. *| disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  9. *|
  10. *+------------------------------------------------------------------------+
  11. */
  12. /*
  13. *******************************************************************************
  14. *** View DispatcherEntry.js for information on the dispatcher entry classes ***
  15. *******************************************************************************
  16. */
  17. function ViewerDispatcher()
  18. {
  19. this.m_activeRequest = null;
  20. this.m_requestQueue = [];
  21. this.m_bUsePageRequest = false;
  22. }
  23. ViewerDispatcher.prototype.getActiveRequest = function() {
  24. return this.m_activeRequest;
  25. };
  26. ViewerDispatcher.prototype.setUsePageRequest = function(bUsePageRequest)
  27. {
  28. this.m_bUsePageRequest = bUsePageRequest;
  29. };
  30. ViewerDispatcher.prototype.getUsePageRequest = function() {
  31. return this.m_bUsePageRequest;
  32. };
  33. ViewerDispatcher.prototype.dispatchRequest = function(dispatcherEntry)
  34. {
  35. if (this.m_activeRequest==null) {
  36. this.startRequest(dispatcherEntry);
  37. } else if (dispatcherEntry.canBeQueued()==true) {
  38. this.m_requestQueue.push(dispatcherEntry);
  39. } else if (window.cognosViewerDebug && console && console.warn) {
  40. console.warn("Warning! Dropped a dispatcher entry!");
  41. }
  42. };
  43. ViewerDispatcher.prototype.startRequest = function(dispatcherEntry)
  44. {
  45. this.m_activeRequest=dispatcherEntry;
  46. if (dispatcherEntry!=null) {
  47. dispatcherEntry.setUsePageRequest(this.m_bUsePageRequest);
  48. dispatcherEntry.sendRequest();
  49. }
  50. };
  51. /*
  52. * Will cancel the request(s) matching the provided key
  53. */
  54. ViewerDispatcher.prototype.cancelRequest = function(key) {
  55. for (var i=0; i < this.m_requestQueue.length; i++) {
  56. var request = this.m_requestQueue[i];
  57. if (request.getKey() === key) {
  58. // we don't want the onEntryComplete callback to be called since it'll fire off the next request in the queue
  59. request.setCallbacks( { "onEntryComplete" : null } );
  60. request.cancelRequest(false);
  61. this.m_requestQueue.splice(i, 1);
  62. // Since we removed an item from the array back up i or we might hit an out of bounds
  63. i--;
  64. }
  65. }
  66. if (this.m_activeRequest && this.m_activeRequest.getKey() === key) {
  67. this.m_activeRequest.setCallbacks( { "onEntryComplete" : null } );
  68. this.m_activeRequest.cancelRequest(false);
  69. // If we're canceling the active request, let the queue know so that it start running
  70. // any other requests in the queue
  71. this.requestComplete();
  72. }
  73. };
  74. ViewerDispatcher.prototype.possibleUnloadEvent = function()
  75. {
  76. if (this.m_activeRequest) {
  77. this.m_activeRequest.possibleUnloadEvent();
  78. }
  79. };
  80. ViewerDispatcher.prototype.requestComplete = function(dispatcherEntry)
  81. {
  82. this.startRequest(this.nextRequest());
  83. };
  84. ViewerDispatcher.prototype.nextRequest = function()
  85. {
  86. //Get the next request to run:
  87. //If there are queued requests, check if its necessary to run them all.
  88. //NOTE: For now, just look at the duplicate requests at the head of the queue.
  89. var requestEntry=null;
  90. if (this.m_requestQueue.length>0) {
  91. requestEntry = this.m_requestQueue.shift();
  92. if (requestEntry.getKey() != null) {
  93. while(this.m_requestQueue.length > 0 &&
  94. this.m_requestQueue[0].getKey() == requestEntry.getKey()) {
  95. requestEntry = this.m_requestQueue.shift();
  96. }
  97. }
  98. }
  99. return requestEntry;
  100. };
  101. ViewerDispatcher.prototype.queueIsEmpty = function()
  102. {
  103. return (this.m_requestQueue.length==0);
  104. };