rsLaunchParameters.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. IBM Confidential
  3. OCO Source Materials
  4. IBM Cognos Products: rs
  5. (C) Copyright IBM Corp. 2018, 2020
  6. The source code for this program is not published or otherwise divested of its trade secrets, irrespective of what has been deposited with the U.S. Copyright Office.
  7. */
  8. define( ['jquery','text!bi/authoring/res/DatasetList.xml'], function($, datasetList) {
  9. var getOpenerLaunchParameters = function( v_bParent ) {
  10. // This code gets the rsLaunchParameters module that was loaded by the opener window.
  11. // It handles the case where it was loaded either directly by the opener or by the opener's parent.
  12. // When RS launches a report in another window, the opener is RS but the module was loaded by the parent bi code
  13. // However, the bi code can also launch another window (prompting when processing the getParameters plugin in the bi code)
  14. // In this case, the opener is the window that loaded the module.
  15. var v_oOpenerLaunchParameters;
  16. try {
  17. v_oOpenerLaunchParameters = v_bParent ?
  18. window.opener.parent.require('bi/authoring/utils/pat/rsLaunchParameters')
  19. :
  20. window.opener.require('bi/authoring/utils/pat/rsLaunchParameters');
  21. }
  22. catch (e) {
  23. }
  24. return v_oOpenerLaunchParameters;
  25. };
  26. // Create the service
  27. var rsLaunchParameters = {
  28. m_oParameterMap: {},
  29. m_oTemplates: {DatasetList: datasetList}
  30. };
  31. rsLaunchParameters.GetTemplate = function( v_sTemplate )
  32. {
  33. return this.m_oTemplates[v_sTemplate];
  34. };
  35. /**
  36. * Store data.
  37. * @param v_oData The data to be stored
  38. * @param v_bJSONEncode JSON encode any objects prior to storing
  39. * @param v_bParent When true, indicates that the key should be for the opener parent window.
  40. * @return The key to be used to retrieve the data, undefined if something went wrong.
  41. */
  42. rsLaunchParameters.Store = function( v_oData, v_bJSONEncode, v_bUseParent )
  43. {
  44. var v_sKey = (v_bUseParent ? '-' : '' ) + Date.now().toString();
  45. if (v_bJSONEncode)
  46. {
  47. var v_oAdd = new Map();
  48. var v_aRemove = [];
  49. Object.keys(v_oData).forEach( function(key) {
  50. if (typeof v_oData[key] == 'object' && v_oData[key] != undefined && !(typeof v_oData[key].toString == 'function' && v_oData[key].toString().indexOf('Window') >= 0))
  51. {
  52. // We are dealing with an object property that is not a window, JSON encode it
  53. // Use v_oAdd and v_aRemove to avoid modifying v_oData wile iterating over it's properties
  54. v_oAdd.set( 'JSON_' + key, JSON.stringify(v_oData[key]) );
  55. v_aRemove.push( key );
  56. }
  57. });
  58. // Remove properties that where JSON encoded
  59. for (var idx = 0; idx < v_aRemove.length; ++idx)
  60. {
  61. delete v_oData[v_aRemove[idx]];
  62. }
  63. // Add JSON encoded properties
  64. v_oAdd.forEach( function(value, key) {
  65. v_oData[key] = value;
  66. });
  67. }
  68. this.m_oParameterMap[v_sKey] = v_oData;
  69. return v_sKey;
  70. };
  71. /**
  72. * Create a wrapper where the Store method registers fact that when retrieving data,
  73. * the parent of the opener should be used instead of the opener itself.
  74. * @return An alternate version of Store that indicates parent should be used to retrieve.
  75. */
  76. rsLaunchParameters.UseParent = function()
  77. {
  78. return {
  79. Store: function( v_oData, v_bJSONEncode ) {
  80. return this.Store( v_oData, v_bJSONEncode, true );
  81. }.bind(this),
  82. GetTemplate: this.GetTemplate.bind(this)
  83. };
  84. };
  85. /**
  86. * Retrieve data. The data is removed from the service so calling this method a second time
  87. * with the same key will return undefined.
  88. * @param v_sKey The key to the data to be retrieved (see Store()).
  89. * @return The data associated with v_sKey or undefined.
  90. */
  91. rsLaunchParameters.Retrieve = function( v_sKey )
  92. {
  93. var v_oReturn;
  94. if (v_sKey)
  95. {
  96. var v_oOpenerLaunchParameters = getOpenerLaunchParameters(v_sKey.indexOf('-') == 0);
  97. if (v_oOpenerLaunchParameters)
  98. {
  99. v_oReturn = v_oOpenerLaunchParameters.m_oParameterMap[v_sKey];
  100. if (v_oReturn)
  101. {
  102. // Temporarily remove any property that we believe is not extendable by jquery
  103. // We know for a fact that jquery 3.3 does not handle window objects
  104. var v_oNonExtendable = {};
  105. for (var p in v_oReturn)
  106. {
  107. if (v_oReturn.hasOwnProperty( p ))
  108. {
  109. if (v_oReturn[p] && typeof v_oReturn[p].toString == 'function' && v_oReturn[p].toString().indexOf('Window') >= 0)
  110. {
  111. v_oNonExtendable[p] = v_oReturn[p];
  112. delete v_oReturn[p];
  113. }
  114. }
  115. }
  116. try {
  117. v_oReturn = $.extend( true, {}, v_oReturn );
  118. }
  119. catch (e) {
  120. console.log('rsLaunchParameters.Retrieve failed to extend launch parameters');
  121. }
  122. // Restore non-extendable properties
  123. for (var p in v_oNonExtendable)
  124. {
  125. v_oReturn[p] = v_oNonExtendable[p];
  126. }
  127. // Parse any properties that were JSON encoded
  128. var v_oParsed = {};
  129. for (var p in v_oReturn)
  130. {
  131. if (p.indexOf('JSON_') == 0 && v_oReturn.hasOwnProperty( p ))
  132. {
  133. var v_sP = p.slice(5);
  134. v_oParsed[v_sP] = JSON.parse(v_oReturn[p]);
  135. }
  136. }
  137. // Replace JSON encoded properties with their parsed version
  138. for (var p in v_oParsed)
  139. {
  140. if (v_oParsed.hasOwnProperty( p ))
  141. {
  142. v_oReturn[p] = v_oParsed[p];
  143. delete v_oReturn['JSON_' + p];
  144. }
  145. }
  146. }
  147. delete v_oOpenerLaunchParameters.m_oParameterMap[v_sKey];
  148. }
  149. }
  150. return v_oReturn;
  151. };
  152. return rsLaunchParameters;
  153. });