PrintAction.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. * Used to print the report in HTML from a hidden iframe.
  14. */
  15. function PrintAction() {
  16. this._pageNumber = null;
  17. this._pageCount = null;
  18. }
  19. PrintAction.prototype = new CognosViewerAction();
  20. PrintAction.ERROR_CODE_INVALID_INT = "Print-001";
  21. PrintAction.ERROR_CODE_REPORT_NOT_COMPLETE = "Print-002";
  22. PrintAction.ERROR_CODE_INVALID_PAGE_RANGE = "Print-003";
  23. PrintAction.prototype.setRequestParms = function(params) {
  24. if (params) {
  25. if (params.pageNumber) {
  26. this._pageNumber = params.pageNumber;
  27. }
  28. if (params.pageCount) {
  29. this._pageCount = params.pageCount;
  30. }
  31. }
  32. };
  33. PrintAction.prototype.execute = function() {
  34. var oCV = this.getCognosViewer();
  35. var pageInfo = this.getCognosViewer().getPageInfo();
  36. // The error are part of a PUBLIC API, do not change
  37. if (this._pageNumber && !this.isPositiveInt(this._pageNumber)) {
  38. return this.buildActionResponseObject("error", PrintAction.ERROR_CODE_INVALID_INT, RV_RES.IDS_JS_ERROR_INVALID_INT);
  39. }
  40. else if (this._pageCount && !this.isPositiveInt(this._pageCount)) {
  41. return this.buildActionResponseObject("error", PrintAction.ERROR_CODE_INVALID_INT, RV_RES.IDS_JS_ERROR_INVALID_INT);
  42. }
  43. else if (oCV.getStatus() != "complete") {
  44. return this.buildActionResponseObject("error", PrintAction.ERROR_CODE_REPORT_NOT_COMPLETE, RV_RES.IDS_JS_ERROR_REPORT_NOT_COMPLETE);
  45. }
  46. else if (pageInfo && pageInfo.pageCount && this._pageNumber > pageInfo.pageCount) {
  47. return this.buildActionResponseObject("error", PrintAction.ERROR_CODE_INVALID_PAGE_RANGE, RV_RES.IDS_JS_ERROR_INVALID_PAGE_RANGE);
  48. }
  49. var envParams = oCV.envParams;
  50. var pageNumber = this._pageNumber > 0 ? this._pageNumber : "1";
  51. var pageCount = this._pageCount ? this._pageCount : "0"; // 0 means print all remaining pages
  52. var request = new HiddenIframeDispatcherEntry(oCV);
  53. request.addFormField("ui.action", "reportAction");
  54. request.addFormField("cv.responseFormat", "print");
  55. request.addFormField("ui.conversation", oCV.getConversation());
  56. request.addFormField("m_tracking", oCV.getTracking());
  57. request.addFormField("cv.header", "false");
  58. request.addFormField("cv.toolbar", "false");
  59. request.addFormField("cv.id", oCV.getId());
  60. request.addFormField("cv.useAjax", "false");
  61. request.addFormField("generic.anyURI.http://developer.cognos.com/ceba/constants/runOptionEnum#pageNumber", pageNumber);
  62. request.addFormField("generic.anyURI.http://developer.cognos.com/ceba/constants/runOptionEnum#pageCount", pageCount);
  63. request.setCallbacks({
  64. "complete" : { "object" : this, "params" : [], "method" : this.printIframe }
  65. });
  66. oCV.dispatchRequest(request);
  67. };
  68. /**
  69. * Call the javascript command to print the iframe
  70. */
  71. PrintAction.prototype.printIframe = function() {
  72. var iframe = HiddenIframeDispatcherEntry.getIframe(this.getCognosViewer().getId());
  73. if (iframe) {
  74. if(isIE()) {
  75. iframe.contentWindow.document.execCommand("print", true, null);
  76. } else {
  77. iframe.focus();
  78. iframe.contentWindow.print();
  79. }
  80. }
  81. };