/* IBM Confidential OCO Source Materials IBM Cognos Products: rs (C) Copyright IBM Corp. 2018, 2020 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. */ define( ['jquery','text!bi/authoring/res/DatasetList.xml'], function($, datasetList) { var getOpenerLaunchParameters = function( v_bParent ) { // This code gets the rsLaunchParameters module that was loaded by the opener window. // It handles the case where it was loaded either directly by the opener or by the opener's parent. // When RS launches a report in another window, the opener is RS but the module was loaded by the parent bi code // However, the bi code can also launch another window (prompting when processing the getParameters plugin in the bi code) // In this case, the opener is the window that loaded the module. var v_oOpenerLaunchParameters; try { v_oOpenerLaunchParameters = v_bParent ? window.opener.parent.require('bi/authoring/utils/pat/rsLaunchParameters') : window.opener.require('bi/authoring/utils/pat/rsLaunchParameters'); } catch (e) { } return v_oOpenerLaunchParameters; }; // Create the service var rsLaunchParameters = { m_oParameterMap: {}, m_oTemplates: {DatasetList: datasetList} }; rsLaunchParameters.GetTemplate = function( v_sTemplate ) { return this.m_oTemplates[v_sTemplate]; }; /** * Store data. * @param v_oData The data to be stored * @param v_bJSONEncode JSON encode any objects prior to storing * @param v_bParent When true, indicates that the key should be for the opener parent window. * @return The key to be used to retrieve the data, undefined if something went wrong. */ rsLaunchParameters.Store = function( v_oData, v_bJSONEncode, v_bUseParent ) { var v_sKey = (v_bUseParent ? '-' : '' ) + Date.now().toString(); if (v_bJSONEncode) { var v_oAdd = new Map(); var v_aRemove = []; Object.keys(v_oData).forEach( function(key) { if (typeof v_oData[key] == 'object' && v_oData[key] != undefined && !(typeof v_oData[key].toString == 'function' && v_oData[key].toString().indexOf('Window') >= 0)) { // We are dealing with an object property that is not a window, JSON encode it // Use v_oAdd and v_aRemove to avoid modifying v_oData wile iterating over it's properties v_oAdd.set( 'JSON_' + key, JSON.stringify(v_oData[key]) ); v_aRemove.push( key ); } }); // Remove properties that where JSON encoded for (var idx = 0; idx < v_aRemove.length; ++idx) { delete v_oData[v_aRemove[idx]]; } // Add JSON encoded properties v_oAdd.forEach( function(value, key) { v_oData[key] = value; }); } this.m_oParameterMap[v_sKey] = v_oData; return v_sKey; }; /** * Create a wrapper where the Store method registers fact that when retrieving data, * the parent of the opener should be used instead of the opener itself. * @return An alternate version of Store that indicates parent should be used to retrieve. */ rsLaunchParameters.UseParent = function() { return { Store: function( v_oData, v_bJSONEncode ) { return this.Store( v_oData, v_bJSONEncode, true ); }.bind(this), GetTemplate: this.GetTemplate.bind(this) }; }; /** * Retrieve data. The data is removed from the service so calling this method a second time * with the same key will return undefined. * @param v_sKey The key to the data to be retrieved (see Store()). * @return The data associated with v_sKey or undefined. */ rsLaunchParameters.Retrieve = function( v_sKey ) { var v_oReturn; if (v_sKey) { var v_oOpenerLaunchParameters = getOpenerLaunchParameters(v_sKey.indexOf('-') == 0); if (v_oOpenerLaunchParameters) { v_oReturn = v_oOpenerLaunchParameters.m_oParameterMap[v_sKey]; if (v_oReturn) { // Temporarily remove any property that we believe is not extendable by jquery // We know for a fact that jquery 3.3 does not handle window objects var v_oNonExtendable = {}; for (var p in v_oReturn) { if (v_oReturn.hasOwnProperty( p )) { if (v_oReturn[p] && typeof v_oReturn[p].toString == 'function' && v_oReturn[p].toString().indexOf('Window') >= 0) { v_oNonExtendable[p] = v_oReturn[p]; delete v_oReturn[p]; } } } try { v_oReturn = $.extend( true, {}, v_oReturn ); } catch (e) { console.log('rsLaunchParameters.Retrieve failed to extend launch parameters'); } // Restore non-extendable properties for (var p in v_oNonExtendable) { v_oReturn[p] = v_oNonExtendable[p]; } // Parse any properties that were JSON encoded var v_oParsed = {}; for (var p in v_oReturn) { if (p.indexOf('JSON_') == 0 && v_oReturn.hasOwnProperty( p )) { var v_sP = p.slice(5); v_oParsed[v_sP] = JSON.parse(v_oReturn[p]); } } // Replace JSON encoded properties with their parsed version for (var p in v_oParsed) { if (v_oParsed.hasOwnProperty( p )) { v_oReturn[p] = v_oParsed[p]; delete v_oReturn['JSON_' + p]; } } } delete v_oOpenerLaunchParameters.m_oParameterMap[v_sKey]; } } return v_oReturn; }; return rsLaunchParameters; });