/* *+------------------------------------------------------------------------+ *| Licensed Materials - Property of IBM *| BI and PM: prmt *| (C) Copyright IBM Corp. 2002, 2016 *| *| US Government Users Restricted Rights - Use, duplication or *| disclosure restricted by GSA ADP Schedule Contract with IBM Corp. *| *+------------------------------------------------------------------------+ */ /** This script is used to provide interactivity for the SelectFile prompt control @private @class */ cognos.Prompt.Control.SelectFile = cognos.Prompt.Control.f_extend({ f_initialize: function( v_oProps ) { this._type_ = "cognos.Prompt.Control.SelectFile"; this.f_parent( v_oProps ); // call parent's initialize() this.f_initCompleted(); // initialize adornment image this.m_adornmentImg = 'spacer.gif'; }, //validate the input into the control checkData: function() { // We keep a reference to the parent function // Calling this.m_oFrom functions seems to have a side effect on this.f_parent(). var v_fnParent = this.f_parent; this.m_bValid = false; // it's valid when the response from upload returns valid, the response should be as follows // $FILE_NAME.... var v_aMatchResult = null; if ( this.getValue() && this.m_sProcessResponse && this.m_sProcessResponse.length > 0 ) { v_aMatchResult = this.decodeHTML(this.m_sProcessResponse).match(/^.*[^<]*]+>[^<]*([^>]+) 0) { // this.getValue() should match v_aMatchResult[1] this.m_bValid = true; } else { //console.log("no match in checkData"); } } this.f_parent = v_fnParent; // call parent's checkData using saved reference. this.f_parent(); // call parent's checkData using saved reference. return this.m_bValid; }, clearValues: function() { this.f_parent(); this.f_clear(); }, f_drawCompleted: function() { var v_oIframeContainer = $(this.f_getId("PRMT_SF_CONTAINER_")); var v_oIFrame = new PRMTFormIframe(v_oIframeContainer, this.f_getId("iframe_"), this.getWebContentRoot() + '/prompting/selectfileBlank.html'); if (v_oIFrame !== null) { var v_oDoc = v_oIFrame.doc; if (v_oDoc) { PRMTUtils.f_addEvent(v_oIFrame, "load", this.f_fillIframe.bind(this) ); } } } }); /** * Fill the content of the iframe * * @private * @return {void} */ cognos.Prompt.Control.SelectFile.prototype.f_fillIframe = function() { var iframeId = this.f_getId("iframe_"); var iframeDoc = (window.ie ? window[iframeId].document : document.getElementById(iframeId).contentDocument); if (iframeDoc) { var guiDir = PRMT_BidiUtils.lookupDirection($( this.f_getId(this.m_sDivPrefix) )); if (guiDir) { iframeDoc.body.dir = guiDir; } var outerDiv = iframeDoc.getElementById('outerdivSF'); // get doc lang var mainDocLang = document.documentElement.getAttribute("lang"); iframeDoc.documentElement.setAttribute("lang",mainDocLang); // set the Global Report Style CSS for the iframe body background var cssRef = iframeDoc.createElement("link"); cssRef.setAttribute("rel", "stylesheet"); cssRef.setAttribute("type", "text/css"); var docHead = iframeDoc.getElementsByTagName("head")[0]; cssRef.setAttribute("href", this.f_findGlobalReportStyle()); docHead.appendChild(cssRef); // add the prompt css cssRef = cssRef.cloneNode(true); cssRef.setAttribute("href", this.m_sSkin + '/prompting/promptCommon.css'); docHead.appendChild(cssRef); // add bux css if exists if (this.f_isBUX()) { var buxCss = this.f_findBuxStyle(); if (buxCss) { cssRef = cssRef.cloneNode(true); cssRef.setAttribute("href", buxCss); docHead.appendChild(cssRef); iframeDoc.body.className = "icdUI"; } } if (outerDiv) { outerDiv.innerHTML = this.f_uploadPageContent(); this.m_elPrompt = iframeDoc.getElementById(this.f_getId(K_PRMT_SF_INPUT_PREFIX)); if (this.m_elPrompt) { PRMTUtils.f_addEvent(this.m_elPrompt, "change", this.f_selectFileChange.bind(this) ); } } } }; /** * Find the GlobalReportStyle used in the page * * @private * @return {String} */ cognos.Prompt.Control.SelectFile.prototype.f_findGlobalReportStyle = function() { var result = this.getWebContentRoot() + '/schemas/GlobalReportStyles_10.css'; var links = document.getElementsByTagName("link"); var link = null; var globalStyleRE = new RegExp(/GlobalReportStyles/); for (i=0; i < links.length; i++) { link = links[i]; if (link.type == "text/css" && link.href.match(globalStyleRE)) { result = link.href; } } return result; }; /** * Find the GlobalReportStyle used in the page * * @private * @return {String} */ cognos.Prompt.Control.SelectFile.prototype.f_findBuxStyle = function() { // http://localhost/ibmCognos/webcontent/rv/bux.css var result = null; var links = document.getElementsByTagName("link"); var link = null; var buxStyleRE = new RegExp(/rv\/bux\.css/); for (i=0; i < links.length; i++) { link = links[i]; if (link.type == "text/css" && link.href.match(buxStyleRE)) { result = link.href; } } return result; }; cognos.Prompt.Control.SelectFile.prototype.f_getPV = function() { return this.getValue(); }; /** * Draws the container for the iframe, the iframe is rendered in the drawComplete method * * @private * @param {C_PromptElement} v_el Container. * @return {void} */ cognos.Prompt.Control.SelectFile.prototype.f_drawInput = function( v_el ) { var v_oContainer = $CE("div", {"id": this.f_getId("PRMT_SF_CONTAINER_"), "class":"clsPromptComponent"}, v_el); v_oContainer.f_appendText(K_PRMT_sEMPTY); return v_oContainer; }; /** * Override superclass(Control) because for SelectFile the adornment should be render * in the iframe. It just prepare the image that will used as adorment. * * @private * @param {HTMLelement} v_el Container for this control. * @param {boolean} [v_bSkipRequired] Skips rendering of the required flag. Used when controls are set as children of another control (like in ranges). The parent control will handle the 'Required' UI. * @return {C_PromptElement} */ cognos.Prompt.Control.SelectFile.prototype.f_drawAdornments = function(v_el, v_bSkipRequired) { // draw an empty cell, the real rendering happens inside the iframe var v_td0 = $CE( "td", {"vAlign": "top", "width": "1px"}, v_el ); var v_img = $CE( "img", { "src": this.m_sSkin + "/prompting/images/spacer.gif", "height": 1, "width": 1, "border": 0, "alt": "" }, v_td0); if ( this.isRequired() && !v_bSkipRequired ) { this.m_adornmentImg = 'icon_required.gif'; } return v_td0; } /** * HTML form with a File input element to be rendered in the visible iframe * */ cognos.Prompt.Control.SelectFile.prototype.f_uploadPageContent = function() { var inputFileId = this.f_getId(K_PRMT_SF_INPUT_PREFIX); var webContentURL = getWebContentURI(); var iconSrc = (this.isRequired() && (!(this["@hideAdornments"] || this.m_bSkipAdornments)) ? 'icon_required.gif' : 'spacer.gif'); var requiredImgHTML = ''; var direction = PRMT_BidiUtils.lookupDirection($( this.f_getId(this.m_sDivPrefix) )); var result = '
' + '
' + '

' + PMT_SF_INTROTEXT + '

' + '

' + PMT_SF_TIP + ''+ (G_IsBidiEnabled ? PRMT_BidiUtils.formatFilePath(this["@defaultPath"]) : this["@defaultPath"]) + '

' + ' ' + '
' + requiredImgHTML + ' ' + '
' + ' 0 ) { result += ' defaultValue="' + this["@defaultPath"] + '" '; } if (G_IsBidiEnabled) { result += 'onchange="PRMT_BidiUtils.onFileChange(this, \'fake' + inputFileId + '\', \'FILEPATH\')" onkeydown="return PRMT_BidiUtils.focusElement(\'fake' + inputFileId + '\');"'; result += 'onfocus="PRMT_BidiUtils.onFileFocus(this, \'fake' + inputFileId + '\', \'FILEPATH\')" onmousedown="return PRMT_BidiUtils.focusElement(\'fake' + inputFileId + '\');"'; } result +='> '; if (G_IsBidiEnabled) { result += ' ' + '
'; return result; }; /** * Build the visible iframe to contain the form with the File input element */ function PRMTFormIframe(parentElement, iframeId, src) { var iframe = document.createElement('iframe'); iframe.setAttribute("id", iframeId); iframe.src = src; iframe.width = "720px"; iframe.height = "150"; iframe.scrolling = "no"; iframe.setAttribute("title","blank"); iframe.setAttribute("frameBorder","0"); iframe.setAttribute("noresize","noresize"); parentElement.appendChild(iframe); // Depending on browser platform get the iframe's document, this is only // available if the iframe has already been appended to an element which // has been added to the document if(iframe.contentDocument) { // Firefox, Opera iframe.doc = iframe.contentDocument; } else if(iframe.contentWindow) { // Internet Explorer iframe.doc = iframe.contentWindow.document; } else if(iframe.document) { // Others? iframe.doc = iframe.document; } // Return the iframe, now with an extra property iframe.doc containing the // iframe's document return iframe; } /** * Event handler for change the file in the file input control. * It prepares the form and submit it to the metadata service * */ cognos.Prompt.Control.SelectFile.prototype.f_selectFileChange = function() { var inputElem = this.m_elPrompt; var v_sFileName = inputElem.value; var v_aNameSegments = v_sFileName.split("."); var v_fileExtension = v_aNameSegments[v_aNameSegments.length - 1].toLowerCase(); var v_sPersonalDataFormat=""; if ( v_fileExtension == "xml" ) { v_sPersonalDataFormat="XML"; } else if ( v_fileExtension == "csv" ) { v_sPersonalDataFormat="CSV"; } else if ( v_fileExtension == "xls" ) { v_sPersonalDataFormat="Excel2003"; } else if ( v_fileExtension == "xlsx" ) { v_sPersonalDataFormat="Excel2003"; } var v_oForm = inputElem.form; var v_sInSpec = ''; v_sInSpec += "" + sXmlEncode(this["@baseModelSearchPath"]) + "" ; v_sInSpec += '' ; v_sInSpec += "" + sXmlEncode(this["@parameter"]) + "" ; v_sInSpec += "" + sXmlEncode(v_sFileName) + "" ; v_oForm.inSpec.value = v_sInSpec + ""; this.m_sFolderName =""; v_oForm.action = this.getGateway() + '/metadataUIService?pid=pdm_process&c=processPersonalData'; var v_sIFrameId = this.f_CreateUploadIFrame(); this.m_sUploadFrameId = v_sIFrameId; v_oForm.setAttribute('target', v_sIFrameId); v_oForm.submit(); }; cognos.Prompt.Control.SelectFile.prototype.f_CreateUploadIFrame = function () { var v_sIFrameId = 'IFrame' + Math.floor(Math.random() * 11111); var v_oEle = document.createElement('DIV'); v_oEle.innerHTML = ''; document.body.appendChild(v_oEle); var iFrame = document.getElementById(v_sIFrameId); PRMTUtils.f_addEvent(iFrame, "load", this.f_UploadCallback.bind(this) ); return v_sIFrameId; }; cognos.Prompt.Control.SelectFile.prototype.f_UploadCallback = function (v_sIFrameId) { // complete the upload v_sIFrameId = this.m_sUploadFrameId; var v_oIFrameEle = document.getElementById(v_sIFrameId); var v_oDoc = window.frames[v_sIFrameId].document; if (v_oIFrameEle.contentDocument) { v_oDoc = v_oIFrameEle.contentDocument; } else if (v_oIFrameEle.contentWindow) { v_oDoc = v_oIFrameEle.contentWindow.document; } if (v_oDoc.location.href == "about:blank") { return; } this.f_CompleteUpload(v_oDoc); }; /*Helper function to convert HTML entities into actual tags*/ cognos.Prompt.Control.SelectFile.prototype.decodeHTML = function(htmlText) { var doDecode = function(htmlText) { var txt = document.createElement("textarea"); txt.innerHTML = htmlText; return txt.value; }; try { return doDecode(htmlText); } catch (e) { try { return doDecode(htmlText.replace(//g, ">")); } catch (e) { return htmlText; } } }; cognos.Prompt.Control.SelectFile.prototype.f_CompleteUpload = function (v_oUploadResponseXML) { var xmlDoc = null; var v_sXMLString = null; if (v_oUploadResponseXML.implementation && v_oUploadResponseXML.implementation.createDocument) { //For Firefox & Safari try { var v_oSerializer = new XMLSerializer(); if (window.XML) { xmlDoc = XML(v_oSerializer.serializeToString(v_oUploadResponseXML)); v_sXMLString = xmlDoc.toXMLString(); } else // Safari { v_sXMLString = v_oSerializer.serializeToString(v_oUploadResponseXML); } } catch (e) { var sError = v_oUploadResponseXML.body.textContent; var n1 = sError.indexOf("Mandatory parameter \""); var n2 = sError.indexOf("\" has not been specified"); if (n1>=0 && n2>=0 && n2>n1) { alert(sError.substring(n1+21,n2)); } else { alert("Error 1! "+sError); } return; } } else if ("ActiveXObject" in window) { //for IE if (null != v_oUploadResponseXML.XMLDocument) { xmlDoc = v_oUploadResponseXML.XMLDocument; // var v_sXMLString = v_oUploadResponseXML.XMLDocument.xml; v_sXMLString = xmlDoc.xml; } else { var sContent = v_oUploadResponseXML.body.innerText; if (sContent.match(/^.*\r?\n?.*\r?\n?.*=0 && n2>=0 && n2>n1) { alert(sContent.substring(n1+21,n2)); } else { alert("Error 2! "+sContent); } return; } } } this.m_sProcessResponse = v_sXMLString.replace(/.*/g,"").replace(/<\/output>/g,""); // there should be no "output" element anymore but I'm going to leave this here just to be safe... this.checkData(); }; cognos.Prompt.Control.SelectFile.prototype.getValue = function() { return this.m_elPrompt.value; };