// Licensed Materials - Property of IBM // // IBM Cognos Products: cpscrn // // (C) Copyright IBM Corp. 2013, 2014 // // US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. // // (function() { // define namespace if (!window.com_ibm_cognos_cps){ com_ibm_cognos_cps = {} } if (com_ibm_cognos_cps.ApplicationContext){ // Stop if the ApplicationContext is already defined. return; } var _F_Array = { indexOf: function(array, value, begin, strict) { for (var i = +begin || 0, l = array.length; i < l; i++) { if (array[i] === value || strict && array[i] == value) { return i; } } return -1; }, unique: function (array, strict) { var a = [], i, l = array.length; for (i = 0; i < l; i++) { if (this.indexOf(a, array[i], 0, strict) < 0) { a.push(array[i]); } } return a; }, forEach: function(array, func, thisArg) { if (typeof func == "function") { var i, v, l = array.length; if (thisArg === undefined) { for (i = 0; i < l; i++) { func(array[i]); } } else { for (i = 0; i < l; i++) { func.call(thisArg, array[i]); } } } }, insert: function(array, i, obj) { if (i !== undefined && obj !==undefined) { array.splice(i, 0, obj); } }, remove: function(array, obj) { if (obj !== undefined) { var i = this.indexOf(array, obj); if (i >= 0) { array.splice(i, 1); return obj; } } return null; }, removeAt: function(array, i) { if (i !== undefined) { array.splice(i, 1); } } }; // Helper object used to create the xmlHttpRequest based on the current browser var CPSConfig = { browser: 'unknown', browserVersion: 'unknown', OS: 'unknown', xmlHttpDefault: null, emptyFunction:function(){}, initialize: function() { this.browser = this.lookup(this.browsers).toLowerCase() || 'unknown'; this.browserVersion = this.parseVersion(navigator.userAgent) || this.parseVersion(navigator.appVersion) || 'unknown'; this.OS = this.lookup(this.systems) || 'unknown'; this.xmlHttpDefault = this.findXMLHttpActiveXVersion(); }, lookup: function(data) { var i, l = data.length; for (i = 0; i < l; i++) { this.versionKey = data[i].partialKey || data[i].identity; var agent = data[i].agent; if (agent) { if (agent.indexOf(data[i].key) != -1) return data[i].identity; } else if (data[i].prop) return data[i].identity; } }, parseVersion: function(s) { var index = s.indexOf(this.versionKey); if (index == -1) return; return parseFloat(s.substring(index + this.versionKey.length + 1).replace(/[^\d\.\-\+]/g, '_')); //sanitize before parse to avoid XSS vulnerability! }, findXMLHttpActiveXVersion: function() { if (window.ActiveXObject) { var i, l = this.xmlHttpVersions.length; for (i = 0; i < l; i++) { try { // Try and create the ActiveXObject for Internet Explorer, if it doesn't work, try again. var xmlhttp = new ActiveXObject(this.xmlHttpVersions[i]); if (xmlhttp) return this.xmlHttpVersions[i]; } catch (e) { // this ActiveX is not there, continue with next one in list } } } return null; }, browsers: [ { agent: navigator.userAgent, key: 'MSIE', identity: 'Explorer', partialKey: 'MSIE' }, { agent: navigator.userAgent, key: 'Firefox', identity: 'Firefox' }, { agent: navigator.userAgent, key: 'Gecko', identity: 'Mozilla', partialKey: 'rv' }, { agent: navigator.userAgent, key: 'Mozilla', identity: 'Netscape', partialKey: 'Mozilla' }, { agent: navigator.userAgent, key: 'Netscape', identity: 'Netscape' }, { prop: window.opera, identity: 'Opera' }, { agent: navigator.vendor, key: 'Apple', identity: 'Safari' } ], systems: [ { agent: navigator.platform, key: 'Win', identity: 'Windows' }, { agent: navigator.platform, key: 'Mac', identity: 'Mac' }, { agent: navigator.platform, key: 'Linux', identity: 'Linux' } ], xmlHttpVersions: [ 'Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.3.0', 'Msxml2.XMLHTTP', 'Microsoft.XMLHTTP' ] } CPSConfig.initialize(); /* Helper DOM utils. */ com_ibm_cognos_cps._F_DOM = { addEventListener: function (node, type, callback, capture) { type = type.toLowerCase(); if (node.addEventListener) { node.addEventListener(type, callback, capture); } else if (node.attachEvent) { node.attachEvent('on' + type, callback); } else { node['on' + type] = callback; } }, selectNodes: function(node, xpath) { if (document.all) { var arr = new Array(); var nodes = node.selectNodes(xpath); var i, l = nodes.length; for (i=0; i -1) { if (CPSWebApplication.eventHandler[event]) { _F_Array.insert(CPSWebApplication.eventHandler[event], 0, handler); } else { CPSWebApplication.eventHandler[event] = new Array(handler); } } else { throw "This application does not listen for event: " + event; } }, publishEvent: function(event, payload) { if(_F_Array.indexOf(this.events.publish, event) > -1) { var handlerArray = CPSWebApplication.eventHandler[event] if (handlerArray) { for (var i = 0; i < handlerArray.length; i++) { var handler = handlerArray[i]; if (handler) { handler(payload); } } } else { throw "No listeners present for event: " + event; } } else { throw "This application does not publish event: " + event; } }, /* * makes an absolute url from a base and a url which maybe absolute or relative. */ makeAbsolute: function(base, url){ if(url.indexOf(base) !== 0){ var index = base.indexOf('://'); if (index !== -1){ index = base.indexOf('/', index + 3); if (index !== -1){ return base.substring(0, index) + (url.charAt(0) === '/' ? '' : '/' ) + url; } } } return url; }, /* * Return a URI which will be proxied by the portal, so as to avoid cross site request issues */ getProxiedURI:function(url){ var proxyTemplate = this.getProperty('proxyTemplate'); // no proxying in CC if (proxyTemplate && !('cognos' == this.getProperty('portalAgent'))){ return this._cpsresourceurl(this.getProperty('proxyTemplate'), url); } return url; }, /* * Save the setting in the given form as portlet settings. */ saveSettings: function(form){ var saveForm = document[this.getProperty('scope')+'_editSaveForm']; if (saveForm){ for (var i = 0 ; i < form.elements.length; i++){ if (form.elements[i].type != 'button'){ var input = document.createElement("input"); input.name = "p_" + form.elements[i].name; input.type = form.elements[i].type; input.value = form.elements[i].value; input.checked = form.elements[i].checked; saveForm.appendChild(input); } } saveForm.submit(); } }, /* * Reset the settings of a portlet. */ resetSettings: function(form){ if (this.getProperty('editResetTemplate')) { document.location.href = this.getProperty('editResetTemplate'); } }, /* * Cancel the editing of the setting and go back to view the app */ cancelSettings: function(){ if (this.getProperty('editCancelTemplate')) { document.location.href = this.getProperty('editCancelTemplate'); } }, getPortalAgent: function(){ return this.getProperty('portalAgent'); }, /* * Helper function that returns a url that will store the query to the nav state when accessed */ _getActionURI:function(query){ var actionTemplate = this.getProperty('actionTemplate'); if (actionTemplate){ return this._cpsactionurl(this.getProperty('actionTemplate'), query); } return null; }, /* * This will log into the gateway, then call the callback. * It may be called by many different portlets simultaneously */ login : function(callback){ var passport = this.getProperty('camPassport'); if (passport) { if (CPSWebApplication.loadState == 'unloaded') { CPSWebApplication.loadState = 'loading'; if (callback){ CPSWebApplication.pendingCallbacks.push(callback); } var div = document.createElement("div"); div.style.display='none'; var fr = document.createElement("iframe"); fr.src = this.gateway + "?b_action=xts.run&m=/cps4/common/sso.xts&m_passportID=" + encodeURIComponent(passport); fr.onload = window.parent.CPSWebApplication.postLogin(); div.appendChild(fr); document.body.appendChild(div); } else if (CPSWebApplication.loadState == 'loading' && callback) { CPSWebApplication.pendingCallbacks.push(callback); } else if (CPSWebApplication.loadState == 'loaded' && callback) { callback(); } } else if(callback){ callback(); } }, /* * helper function for getProxiedURI(), */ _cpsresourceurl: function(resourceTemplate, target){ var begin = resourceTemplate.indexOf('_cpsresourceurl'); if (begin != -1){ var endMarker='cpsendmarker_'; var end = resourceTemplate.indexOf(endMarker, begin); if (end != -1){ var marker = resourceTemplate.substring(begin, end + endMarker.length); var decodeCount = 0; var decodedMarker = marker; while(decodedMarker != '_cpsresourceurl:cpsendmarker_' && decodeCount ++ < 10){ decodedMarker = decodeURIComponent(decodedMarker); } if (decodeCount < 10){ var encodedTarget = target; for (i =0; i < decodeCount ; i++){ encodedTarget = encodeURIComponent(encodedTarget); } target = resourceTemplate.replace(marker, encodedTarget); } } } // Plumtree portletIds.. so we can synchronize passports. if (window.cognosPortletIds){ if(target.indexOf('?') != -1) target += '&'; else target += '?'; target += 'cognosPortletIds='+encodeURIComponent(cognosPortletIds.join('_')); } return target; }, /* * helper function for _getActionURI() */ _cpsactionurl: function(resourceTemplate, target){ var begin = resourceTemplate.indexOf('_cpsparamsurl'); if (begin != -1){ var endMarker='cpsendmarker_'; var end = resourceTemplate.indexOf(endMarker, begin); if (end != -1){ var marker = resourceTemplate.substring(begin, end + endMarker.length); var decodeCount = 0; var decodedMarker = marker; while(decodedMarker != '_cpsparamsurl=cpsendmarker_' && decodeCount ++ < 10){ decodedMarker = decodeURIComponent(decodedMarker); } if (decodeCount < 10){ var encodedTarget = target; for (i =0; i < decodeCount ; i++){ encodedTarget = encodeURIComponent(encodedTarget); } target = resourceTemplate.replace(marker, encodedTarget); } } } // Plumtree portletIds.. so we can synchronize passports. if (window.cognosPortletIds){ if(target.indexOf('?') != -1) target += '&'; else target += '?'; target += 'cognosPortletIds='+encodeURIComponent(cognosPortletIds.join('_')); } return target; }, /* * returns an XHR request in a cross-browser fashion */ _getXHR: function(options){ var obj; // plumtree xhr (knows how to handle timeouts ..) if (window.CustomPTHttpRequest !== undefined){ obj = new CustomPTHttpRequest(); } if (CPSConfig.xmlHttpDefault != null) { obj = new ActiveXObject(CPSConfig.xmlHttpDefault); } // Well if there is no ActiveXObject available it must be firefox, opera, or something else if (typeof XMLHttpRequest != 'undefined') { obj = new XMLHttpRequest(); } if (!obj){ throw 'No XMLHttpRequest object is available'; } if (options){ obj.onreadystatechange = function() { try { if (obj.readyState == 4) { if (obj.status >= 200 && obj.status < 300) { if (typeof options.onSuccess == "function") { options.onSuccess(obj); } } else { if (typeof options.onFailure == "function") { options.onFailure(obj); } } // fix for IE memory leak _self.transport.onreadystatechange = CPSConfig.emptyFunction; } } catch (e) { try { if (typeof options.onException == "function") { if (this.status !== 0) { options.onException(e); } } } catch (e1) { console.log('e1:' + e1); } } }; // of function } return obj; }, /* * returns a function that will be called with the object as its scope */ _hitch:function(obj, func){ return function(){ func.apply(obj); }; }, /* * This can be called to load a library in such a way that it will only be loaded once on the page. */ loadLibrary: function(href, callback, conditionFunction) { if (conditionFunction && !conditionFunction()){ if (callback){ callback(false); } return false; } if (!window.CognosCPSLoadedLibMap){ window.CognosCPSLoadedLibMap = {}; } if ( !window.CognosCPSLoadedLibMap[href]) { CognosCPSLoadedLibMap[href] = true; var callbacks = []; CPSWebApplication.loadingCallbacks[href] = callbacks; callbacks.push(callback); var head = document.getElementsByTagName('HEAD').item(0); var node = document.createElement('SCRIPT'); var _self = this; node.onload = node.onreadystatechange = function() { if (this.readyState && this.readyState != 'loaded' && this.readyState != 'complete') { return; } var callbacks = CPSWebApplication.loadingCallbacks[href]; for (var i = 0 ; i < callbacks.length; i++){ callbacks[i](true); } delete CPSWebApplication.loadingCallbacks[href]; }; node.type = 'text/javascript'; node.src = href; head.appendChild(node); return true; } if (callback){ var callbacks = CPSWebApplication.loadingCallbacks[href]; if (callbacks){ callbacks.push(callback); }else { callback(); } } return false; } } /* * helper function for login() */ CPSWebApplication.postLogin = function() { CPSWebApplication.loadState = 'loaded'; for (var i = 0; i < CPSWebApplication.pendingCallbacks.length; i++) { CPSWebApplication.pendingCallbacks[i](); } } CPSWebApplication.loadState = 'unloaded'; CPSWebApplication.pendingCallbacks = []; CPSWebApplication.loadingCallbacks = {}; })(); window.CognosConnect = new CPSWebApplication();