HiddenIframeDispatcherEntry.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. *+------------------------------------------------------------------------+
  3. *| Licensed Materials - Property of IBM
  4. *| IBM Cognos Products: Viewer
  5. *| (C) Copyright IBM Corp. 2013
  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. * This class is used to make requests in a hidden iframe
  14. */
  15. HiddenIframeDispatcherEntry.IFRAME_ID_PREFIX = "viewerHiddenRequest";
  16. HiddenIframeDispatcherEntry.FORM_NAME = "viewerHiddenFormRequest";
  17. function HiddenIframeDispatcherEntry(oCV) {
  18. HiddenIframeDispatcherEntry.baseConstructor.call(this, oCV);
  19. if (oCV) {
  20. HiddenIframeDispatcherEntry.prototype.setDefaultFormFields.call(this);
  21. this.setRequestHandler(new RequestHandler(oCV));
  22. this.setWorkingDialog(oCV.getWorkingDialog());
  23. this.setRequestIndicator(oCV.getRequestIndicator());
  24. this.m_httpRequestConfig = oCV.getConfig() && oCV.getConfig().getHttpRequestConfig() ? oCV.getConfig().getHttpRequestConfig() : null;
  25. this.setIframeId(HiddenIframeDispatcherEntry.IFRAME_ID_PREFIX + oCV.getId());
  26. this.originalGetViewerConfiguration = null;
  27. }
  28. }
  29. HiddenIframeDispatcherEntry.prototype = new DispatcherEntry();
  30. HiddenIframeDispatcherEntry.baseConstructor = DispatcherEntry;
  31. HiddenIframeDispatcherEntry.prototype.setDefaultFormFields = function() {
  32. var oCV = this.getViewer();
  33. var sCAF = oCV.getCAFContext();
  34. this.addDefinedNonNullFormField("ui.cafcontextid", sCAF);
  35. };
  36. HiddenIframeDispatcherEntry.prototype.sendRequest = function() {
  37. this._createHiddenIframe();
  38. var form = this._createForm();
  39. this._setupCallbacks();
  40. this.onPreHttpRequest(this.getRequest());
  41. form.submit();
  42. };
  43. /**
  44. * Do any cleanup or callbacks once the iframe is finished running the request
  45. */
  46. HiddenIframeDispatcherEntry.prototype._iframeRequestComplete = function() {
  47. window.getViewerConfiguration = this.originalGetViewerConfiguration;
  48. this.onPostHttpRequest();
  49. this.onEntryComplete();
  50. };
  51. /**
  52. * Using our public callback mechanism setup callbacks for the hidden iframes
  53. */
  54. HiddenIframeDispatcherEntry.prototype._setupCallbacks = function() {
  55. // Save the original getViewerConfiguration method if we have one
  56. this.originalGetViewerConfiguration = window.getViewerConfiguration;
  57. // We only need to setup these callbacks if we're using Ajax otherwise
  58. // the iframes onload callback will be triggered and we'll get the status there
  59. if (this.getFormField("cv.useAjax") != "false") {
  60. var hiddenIframeDispatcherEntry = this;
  61. var requestIndicator = this.getRequestHandler().getRequestIndicator();
  62. var workingDialog = this.getRequestHandler().getWorkingDialog();
  63. window.getViewerConfiguration = function() {
  64. var configObj = {
  65. "httpRequestCallbacks" : {
  66. "reportStatus" : {
  67. "complete" : function() { hiddenIframeDispatcherEntry.onComplete() },
  68. "working" : function() { hiddenIframeDispatcherEntry.onWorking() },
  69. "prompting" : function() { hiddenIframeDispatcherEntry.onPrompting() }
  70. }
  71. }
  72. };
  73. return configObj;
  74. };
  75. }
  76. };
  77. HiddenIframeDispatcherEntry.prototype.setIframeId = function(id) {
  78. this._iframeId = id;
  79. };
  80. HiddenIframeDispatcherEntry.prototype.getIframeId = function() {
  81. return this._iframeId;
  82. };
  83. /**
  84. * Creates the form that will POST the request to the hidden iframe
  85. */
  86. HiddenIframeDispatcherEntry.prototype._createForm = function(params) {
  87. var oCV = this.getViewer();
  88. var formId = HiddenIframeDispatcherEntry.FORM_NAME + oCV.getId();
  89. var requestForm = document.getElementById(formId);
  90. if (requestForm) {
  91. requestForm.parentNode.removeChild(requestForm);
  92. requestForm = null;
  93. }
  94. var sDispatcherURI = location.protocol + '//' + location.host + oCV.m_sGateway;
  95. requestForm = document.createElement("form");
  96. requestForm.setAttribute("method","post");
  97. requestForm.setAttribute("action", sDispatcherURI);
  98. requestForm.setAttribute("target", this.getIframeId());
  99. requestForm.setAttribute("id", formId);
  100. requestForm.style.display = "none";
  101. var formFields = this.getRequest().getFormFields();
  102. var formFieldNames = formFields.keys();
  103. for (var index = 0; index < formFieldNames.length; index++) {
  104. requestForm.appendChild(createHiddenFormField(formFieldNames[index], formFields.get(formFieldNames[index])));
  105. }
  106. document.body.appendChild(requestForm);
  107. return requestForm;
  108. };
  109. /**
  110. * Creates the hidden iframe that will be used for the request
  111. */
  112. HiddenIframeDispatcherEntry.prototype._createHiddenIframe = function() {
  113. var oCV = this.getViewer();
  114. var iframeId = this.getIframeId();
  115. var iframeElem = document.getElementById(iframeId);
  116. if (iframeElem) {
  117. iframeElem.parentNode.parentNode.removeChild(iframeElem.parentNode);
  118. }
  119. // There's a bug in IE where you can't post to an iframe if it's created dynamically,
  120. // however if you append an iframe into a div using innerHTML then it work.
  121. var div = document.createElement("div");
  122. div.style.position = "absolute";
  123. div.style.left="0px";
  124. div.style.top="0px";
  125. div.style.display = "none";
  126. document.body.appendChild(div);
  127. div.innerHTML = "<iframe frameborder=\"0\" id=\"" + iframeId + "\" name=\"" + iframeId + "\"></iframe>";
  128. iframeElem = document.getElementById(iframeId);
  129. // only set the onload after it's appended to the DOM or it will get triggered right away in certain browsers
  130. var thisObj = this;
  131. var func = function() {HiddenIframeDispatcherEntry.handleIframeLoad(thisObj);};
  132. if(iframeElem.attachEvent) {
  133. iframeElem.attachEvent("onload", func);
  134. }
  135. else {
  136. iframeElem.addEventListener("load", func, true);
  137. }
  138. };
  139. /**
  140. * Hides the iframe. This gets called when we got a fault that we
  141. * showed to the user and they hit the Ok button in the fault dialog.
  142. */
  143. HiddenIframeDispatcherEntry.hideIframe = function(cvId) {
  144. var iframeElement = document.getElementById(HiddenIframeDispatcherEntry.IFRAME_ID_PREFIX + cvId);
  145. if (iframeElement) {
  146. iframeElement.parentNode.style.display = "none";
  147. }
  148. };
  149. HiddenIframeDispatcherEntry.showIframeContentsInWindow = function(cvId) {
  150. var iframeElement = document.getElementById(HiddenIframeDispatcherEntry.IFRAME_ID_PREFIX + cvId);
  151. if (!iframeElement) {
  152. return;
  153. }
  154. var html = iframeElement.contentWindow.document.getElementsByTagName('html')[0].innerHTML;
  155. var htmlWindow = window.open("","",'height=400,width=500');
  156. if(htmlWindow) {
  157. htmlWindow.document.write("<html>" + html + "</html>");
  158. }
  159. };
  160. /**
  161. * Gets called when the iframe is loaded. Status can be complete, working, fault, ...
  162. */
  163. HiddenIframeDispatcherEntry.handleIframeLoad = function(dispatcherEntry) {
  164. if (!dispatcherEntry) {
  165. return;
  166. }
  167. var iframeElement = document.getElementById(dispatcherEntry.getIframeId());
  168. if (!iframeElement) {
  169. return;
  170. }
  171. var oCV = iframeElement.contentWindow.window.gaRV_INSTANCES ? iframeElement.contentWindow.window.gaRV_INSTANCES[0] : null;
  172. var status = oCV ? oCV.getStatus() : null;
  173. if (status == "complete") {
  174. dispatcherEntry.onComplete();
  175. }
  176. if (status == "working") {
  177. dispatcherEntry.onWorking();
  178. }
  179. if (status == "prompting") {
  180. dispatcherEntry.onPrompting();
  181. }
  182. if (!oCV || status == "fault" || status == "") {
  183. dispatcherEntry.onFault();
  184. }
  185. };
  186. HiddenIframeDispatcherEntry.prototype.onFault = function() {
  187. this._iframeRequestComplete();
  188. HiddenIframeDispatcherEntry.showIframeContentsInWindow(this.getViewer().getId());
  189. };
  190. HiddenIframeDispatcherEntry.prototype.onPrompting = function() {
  191. this._iframeRequestComplete();
  192. if (this.m_httpRequestConfig) {
  193. var callback = this.m_httpRequestConfig.getReportStatusCallback("prompting");
  194. if (typeof callback == "function") {
  195. callback();
  196. }
  197. }
  198. HiddenIframeDispatcherEntry.showIframeContentsInWindow(this.getViewer().getId());
  199. };
  200. HiddenIframeDispatcherEntry.prototype.onComplete = function() {
  201. this._iframeRequestComplete();
  202. if (this.m_httpRequestConfig) {
  203. var callback = this.m_httpRequestConfig.getReportStatusCallback("complete");
  204. if (typeof callback == "function") {
  205. callback();
  206. }
  207. }
  208. var iframeElement = document.getElementById(this.getIframeId());
  209. // We don't want the iframe to ever release the conversation, so unhook the leavingRV method.
  210. if (typeof iframeElement.contentWindow.detachLeavingRV == "function") {
  211. iframeElement.contentWindow.detachLeavingRV();
  212. }
  213. var divContainer = iframeElement.parentNode;
  214. divContainer.style.display = "none";
  215. if (this.getCallbacks() && this.getCallbacks()["complete"]) {
  216. HiddenIframeDispatcherEntry.executeCallback(this.getCallbacks()["complete"]);
  217. }
  218. };
  219. HiddenIframeDispatcherEntry.prototype.cancelRequest = function(forceSynchronous) {
  220. this._iframeRequestComplete();
  221. // guard against sending multiple cancel requests
  222. if (!this.m_bCancelCalled) {
  223. this.m_bCancelCalled = true;
  224. var iframeElement = document.getElementById(this.getIframeId());
  225. if (!iframeElement) {
  226. return;
  227. }
  228. var oCV = iframeElement.contentWindow[getCognosViewerObjectString(this.getViewer().getId())];
  229. if (oCV) {
  230. oCV.cancel();
  231. }
  232. }
  233. };
  234. HiddenIframeDispatcherEntry.executeCallback = function(callback) {
  235. if (callback) {
  236. var callbackFunc = GUtil.generateCallback(callback.method, callback.params, callback.object);
  237. callbackFunc();
  238. }
  239. };
  240. HiddenIframeDispatcherEntry.getIframe = function(cvId) {
  241. var iframe = document.getElementById(HiddenIframeDispatcherEntry.IFRAME_ID_PREFIX + cvId);
  242. return iframe;
  243. };