SelectFile.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. *+------------------------------------------------------------------------+
  3. *| Licensed Materials - Property of IBM
  4. *| BI and PM: prmt
  5. *| (C) Copyright IBM Corp. 2002, 2016
  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. This script is used to provide interactivity for the SelectFile prompt control
  14. @private
  15. @class
  16. */
  17. cognos.Prompt.Control.SelectFile = cognos.Prompt.Control.f_extend({
  18. f_initialize: function( v_oProps ) {
  19. this._type_ = "cognos.Prompt.Control.SelectFile";
  20. this.f_parent( v_oProps ); // call parent's initialize()
  21. this.f_initCompleted();
  22. // initialize adornment image
  23. this.m_adornmentImg = 'spacer.gif';
  24. },
  25. //validate the input into the control
  26. checkData: function()
  27. {
  28. // We keep a reference to the parent function
  29. // Calling this.m_oFrom functions seems to have a side effect on this.f_parent().
  30. var v_fnParent = this.f_parent;
  31. this.m_bValid = false;
  32. // it's valid when the response from upload returns valid, the response should be as follows
  33. // <pdSpec> <dataSet type="XML"> <name>$FILE_NAME</name>....
  34. var v_aMatchResult = null;
  35. if ( this.getValue() && this.m_sProcessResponse && this.m_sProcessResponse.length > 0 ) {
  36. v_aMatchResult = this.decodeHTML(this.m_sProcessResponse).match(/^.*<pdSpec>[^<]*<dataSet[^>]+>[^<]*<name>([^>]+)</);
  37. if (v_aMatchResult !== null && v_aMatchResult.length > 0) {
  38. // this.getValue() should match v_aMatchResult[1]
  39. this.m_bValid = true;
  40. } else {
  41. //console.log("no match in checkData");
  42. }
  43. }
  44. this.f_parent = v_fnParent; // call parent's checkData using saved reference.
  45. this.f_parent(); // call parent's checkData using saved reference.
  46. return this.m_bValid;
  47. },
  48. clearValues: function() {
  49. this.f_parent();
  50. this.f_clear();
  51. },
  52. f_drawCompleted: function()
  53. {
  54. var v_oIframeContainer = $(this.f_getId("PRMT_SF_CONTAINER_"));
  55. var v_oIFrame = new PRMTFormIframe(v_oIframeContainer, this.f_getId("iframe_"), this.getWebContentRoot() + '/prompting/selectfileBlank.html');
  56. if (v_oIFrame !== null) {
  57. var v_oDoc = v_oIFrame.doc;
  58. if (v_oDoc) {
  59. PRMTUtils.f_addEvent(v_oIFrame, "load", this.f_fillIframe.bind(this) );
  60. }
  61. }
  62. }
  63. });
  64. /**
  65. * Fill the content of the iframe
  66. *
  67. * @private
  68. * @return {void}
  69. */
  70. cognos.Prompt.Control.SelectFile.prototype.f_fillIframe = function()
  71. {
  72. var iframeId = this.f_getId("iframe_");
  73. var iframeDoc = (window.ie ? window[iframeId].document : document.getElementById(iframeId).contentDocument);
  74. if (iframeDoc) {
  75. var guiDir = PRMT_BidiUtils.lookupDirection($( this.f_getId(this.m_sDivPrefix) ));
  76. if (guiDir) {
  77. iframeDoc.body.dir = guiDir;
  78. }
  79. var outerDiv = iframeDoc.getElementById('outerdivSF');
  80. // get doc lang
  81. var mainDocLang = document.documentElement.getAttribute("lang");
  82. iframeDoc.documentElement.setAttribute("lang",mainDocLang);
  83. // set the Global Report Style CSS for the iframe body background
  84. var cssRef = iframeDoc.createElement("link");
  85. cssRef.setAttribute("rel", "stylesheet");
  86. cssRef.setAttribute("type", "text/css");
  87. var docHead = iframeDoc.getElementsByTagName("head")[0];
  88. cssRef.setAttribute("href", this.f_findGlobalReportStyle());
  89. docHead.appendChild(cssRef);
  90. // add the prompt css
  91. cssRef = cssRef.cloneNode(true);
  92. cssRef.setAttribute("href", this.m_sSkin + '/prompting/promptCommon.css');
  93. docHead.appendChild(cssRef);
  94. // add bux css if exists
  95. if (this.f_isBUX()) {
  96. var buxCss = this.f_findBuxStyle();
  97. if (buxCss) {
  98. cssRef = cssRef.cloneNode(true);
  99. cssRef.setAttribute("href", buxCss);
  100. docHead.appendChild(cssRef);
  101. iframeDoc.body.className = "icdUI";
  102. }
  103. }
  104. if (outerDiv) {
  105. outerDiv.innerHTML = this.f_uploadPageContent();
  106. this.m_elPrompt = iframeDoc.getElementById(this.f_getId(K_PRMT_SF_INPUT_PREFIX));
  107. if (this.m_elPrompt)
  108. {
  109. PRMTUtils.f_addEvent(this.m_elPrompt, "change", this.f_selectFileChange.bind(this) );
  110. }
  111. }
  112. }
  113. };
  114. /**
  115. * Find the GlobalReportStyle used in the page
  116. *
  117. * @private
  118. * @return {String}
  119. */
  120. cognos.Prompt.Control.SelectFile.prototype.f_findGlobalReportStyle = function()
  121. {
  122. var result = this.getWebContentRoot() + '/schemas/GlobalReportStyles_10.css';
  123. var links = document.getElementsByTagName("link");
  124. var link = null;
  125. var globalStyleRE = new RegExp(/GlobalReportStyles/);
  126. for (i=0; i < links.length; i++)
  127. {
  128. link = links[i];
  129. if (link.type == "text/css" && link.href.match(globalStyleRE)) {
  130. result = link.href;
  131. }
  132. }
  133. return result;
  134. };
  135. /**
  136. * Find the GlobalReportStyle used in the page
  137. *
  138. * @private
  139. * @return {String}
  140. */
  141. cognos.Prompt.Control.SelectFile.prototype.f_findBuxStyle = function()
  142. {
  143. // http://localhost/ibmCognos/webcontent/rv/bux.css
  144. var result = null;
  145. var links = document.getElementsByTagName("link");
  146. var link = null;
  147. var buxStyleRE = new RegExp(/rv\/bux\.css/);
  148. for (i=0; i < links.length; i++)
  149. {
  150. link = links[i];
  151. if (link.type == "text/css" && link.href.match(buxStyleRE)) {
  152. result = link.href;
  153. }
  154. }
  155. return result;
  156. };
  157. cognos.Prompt.Control.SelectFile.prototype.f_getPV = function()
  158. {
  159. return this.getValue();
  160. };
  161. /**
  162. * Draws the container for the iframe, the iframe is rendered in the drawComplete method
  163. *
  164. * @private
  165. * @param {C_PromptElement} v_el Container.
  166. * @return {void}
  167. */
  168. cognos.Prompt.Control.SelectFile.prototype.f_drawInput = function( v_el )
  169. {
  170. var v_oContainer = $CE("div", {"id": this.f_getId("PRMT_SF_CONTAINER_"), "class":"clsPromptComponent"}, v_el);
  171. v_oContainer.f_appendText(K_PRMT_sEMPTY);
  172. return v_oContainer;
  173. };
  174. /**
  175. * Override superclass(Control) because for SelectFile the adornment should be render
  176. * in the iframe. It just prepare the image that will used as adorment.
  177. *
  178. * @private
  179. * @param {HTMLelement} v_el Container for this control.
  180. * @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.
  181. * @return {C_PromptElement}
  182. */
  183. cognos.Prompt.Control.SelectFile.prototype.f_drawAdornments = function(v_el, v_bSkipRequired) {
  184. // draw an empty cell, the real rendering happens inside the iframe
  185. var v_td0 = $CE( "td", {"vAlign": "top", "width": "1px"}, v_el );
  186. var v_img = $CE( "img", {
  187. "src": this.m_sSkin + "/prompting/images/spacer.gif",
  188. "height": 1,
  189. "width": 1,
  190. "border": 0,
  191. "alt": ""
  192. }, v_td0);
  193. if ( this.isRequired() && !v_bSkipRequired )
  194. {
  195. this.m_adornmentImg = 'icon_required.gif';
  196. }
  197. return v_td0;
  198. }
  199. /**
  200. * HTML form with a File input element to be rendered in the visible iframe
  201. *
  202. */
  203. cognos.Prompt.Control.SelectFile.prototype.f_uploadPageContent = function() {
  204. var inputFileId = this.f_getId(K_PRMT_SF_INPUT_PREFIX);
  205. var webContentURL = getWebContentURI();
  206. var iconSrc = (this.isRequired() && (!(this["@hideAdornments"] || this.m_bSkipAdornments)) ? 'icon_required.gif' : 'spacer.gif');
  207. var requiredImgHTML = '<img class="clsErrorRequired" height="10" border="0" width="10" valign="top" alt="" src="' + webContentURL + '/prompting/reportskin/prompting/images/' + iconSrc + '" />';
  208. var direction = PRMT_BidiUtils.lookupDirection($( this.f_getId(this.m_sDivPrefix) ));
  209. var result = ' <div> ' +
  210. ' <form action="" enctype="multipart/form-data" method="post"> ' +
  211. ' <p class="clsDialogIntroduction">' + PMT_SF_INTROTEXT + '</p>' +
  212. ' <p class="clsDialogIntroduction">' + PMT_SF_TIP + '<span class="clsControlLabel" >'+ (G_IsBidiEnabled ? PRMT_BidiUtils.formatFilePath(this["@defaultPath"]) : this["@defaultPath"]) + '</span></p>' +
  213. ' <table cellspacing="0" cellpadding="0" border="0"><tbody><tr>' +
  214. ' <td class="clsControlLabel" valign="top">' + requiredImgHTML +
  215. ' <label for="'+ inputFileId + '">' + PMT_SF_LABEL + '</label> ' +
  216. ' </td></tr></tbody></table>' +
  217. ' <input id="'+ inputFileId +'" type="file" style="' + (G_IsBidiEnabled ? 'position:absolute' : 'width:580px') + '" name="pdsContent" size="80" accept="*.csv;*.xml;*.xls"';
  218. if (this["@defaultPath"] && this["@defaultPath"].length >0 ) {
  219. result += ' defaultValue="' + this["@defaultPath"] + '" ';
  220. }
  221. if (G_IsBidiEnabled)
  222. {
  223. result += 'onchange="PRMT_BidiUtils.onFileChange(this, \'fake' + inputFileId + '\', \'FILEPATH\')" onkeydown="return PRMT_BidiUtils.focusElement(\'fake' + inputFileId + '\');"';
  224. result += 'onfocus="PRMT_BidiUtils.onFileFocus(this, \'fake' + inputFileId + '\', \'FILEPATH\')" onmousedown="return PRMT_BidiUtils.focusElement(\'fake' + inputFileId + '\');"';
  225. }
  226. result +='> ';
  227. if (G_IsBidiEnabled)
  228. {
  229. result += '<input id="fake'+ inputFileId +'" type="text" dir="ltr" style="position:relative;z-index:2;background-color:#C0C0C0" name="fakePdsContent" size="80"';
  230. result += 'onkeydown="return PRMT_BidiUtils.onFakeKeyDown(event, \'' + inputFileId + '\');" oncopy="PRMT_BidiUtils.processCopy(this);" ';
  231. result += 'onpaste="return false;" oncut="return false;"';
  232. result +='> ';
  233. }
  234. result += '<input type="hidden" name="inSpec" id="inSpec"/> ' +
  235. ' </form> ' +
  236. ' </div> ';
  237. return result;
  238. };
  239. /**
  240. * Build the visible iframe to contain the form with the File input element
  241. */
  242. function PRMTFormIframe(parentElement, iframeId, src) {
  243. var iframe = document.createElement('iframe');
  244. iframe.setAttribute("id", iframeId);
  245. iframe.src = src;
  246. iframe.width = "720px";
  247. iframe.height = "150";
  248. iframe.scrolling = "no";
  249. iframe.setAttribute("title","blank");
  250. iframe.setAttribute("frameBorder","0");
  251. iframe.setAttribute("noresize","noresize");
  252. parentElement.appendChild(iframe);
  253. // Depending on browser platform get the iframe's document, this is only
  254. // available if the iframe has already been appended to an element which
  255. // has been added to the document
  256. if(iframe.contentDocument) {
  257. // Firefox, Opera
  258. iframe.doc = iframe.contentDocument;
  259. } else if(iframe.contentWindow) {
  260. // Internet Explorer
  261. iframe.doc = iframe.contentWindow.document;
  262. } else if(iframe.document) {
  263. // Others?
  264. iframe.doc = iframe.document;
  265. }
  266. // Return the iframe, now with an extra property iframe.doc containing the
  267. // iframe's document
  268. return iframe;
  269. }
  270. /**
  271. * Event handler for change the file in the file input control.
  272. * It prepares the form and submit it to the metadata service
  273. *
  274. */
  275. cognos.Prompt.Control.SelectFile.prototype.f_selectFileChange = function()
  276. {
  277. var inputElem = this.m_elPrompt;
  278. var v_sFileName = inputElem.value;
  279. var v_aNameSegments = v_sFileName.split(".");
  280. var v_fileExtension = v_aNameSegments[v_aNameSegments.length - 1].toLowerCase();
  281. var v_sPersonalDataFormat="";
  282. if ( v_fileExtension == "xml" ) {
  283. v_sPersonalDataFormat="XML";
  284. } else if ( v_fileExtension == "csv" ) {
  285. v_sPersonalDataFormat="CSV";
  286. } else if ( v_fileExtension == "xls" ) {
  287. v_sPersonalDataFormat="Excel2003";
  288. } else if ( v_fileExtension == "xlsx" ) {
  289. v_sPersonalDataFormat="Excel2003";
  290. }
  291. var v_oForm = inputElem.form;
  292. var v_sInSpec = '<input>';
  293. v_sInSpec += "<baseModelSearchPath>" + sXmlEncode(this["@baseModelSearchPath"]) + "</baseModelSearchPath>" ;
  294. v_sInSpec += '<dataSet type="' + v_sPersonalDataFormat + '">' ;
  295. v_sInSpec += "<name>" + sXmlEncode(this["@parameter"]) + "</name>" ;
  296. v_sInSpec += "<sourcePath>" + sXmlEncode(v_sFileName) + "</sourcePath>" ;
  297. v_oForm.inSpec.value = v_sInSpec + "</dataSet></input>";
  298. this.m_sFolderName ="";
  299. v_oForm.action = this.getGateway() + '/metadataUIService?pid=pdm_process&c=processPersonalData';
  300. var v_sIFrameId = this.f_CreateUploadIFrame();
  301. this.m_sUploadFrameId = v_sIFrameId;
  302. v_oForm.setAttribute('target', v_sIFrameId);
  303. v_oForm.submit();
  304. };
  305. cognos.Prompt.Control.SelectFile.prototype.f_CreateUploadIFrame = function ()
  306. {
  307. var v_sIFrameId = 'IFrame' + Math.floor(Math.random() * 11111);
  308. var v_oEle = document.createElement('DIV');
  309. v_oEle.innerHTML = '<iframe style="display:none" src="about:blank" content="text/xml" id="'+v_sIFrameId+'" name="'+ v_sIFrameId + '"></iframe>';
  310. document.body.appendChild(v_oEle);
  311. var iFrame = document.getElementById(v_sIFrameId);
  312. PRMTUtils.f_addEvent(iFrame, "load", this.f_UploadCallback.bind(this) );
  313. return v_sIFrameId;
  314. };
  315. cognos.Prompt.Control.SelectFile.prototype.f_UploadCallback = function (v_sIFrameId)
  316. {
  317. // complete the upload
  318. v_sIFrameId = this.m_sUploadFrameId;
  319. var v_oIFrameEle = document.getElementById(v_sIFrameId);
  320. var v_oDoc = window.frames[v_sIFrameId].document;
  321. if (v_oIFrameEle.contentDocument)
  322. {
  323. v_oDoc = v_oIFrameEle.contentDocument;
  324. }
  325. else if (v_oIFrameEle.contentWindow)
  326. {
  327. v_oDoc = v_oIFrameEle.contentWindow.document;
  328. }
  329. if (v_oDoc.location.href == "about:blank")
  330. {
  331. return;
  332. }
  333. this.f_CompleteUpload(v_oDoc);
  334. };
  335. /*Helper function to convert HTML entities into actual tags*/
  336. cognos.Prompt.Control.SelectFile.prototype.decodeHTML = function(htmlText) {
  337. var doDecode = function(htmlText) {
  338. var txt = document.createElement("textarea");
  339. txt.innerHTML = htmlText;
  340. return txt.value;
  341. };
  342. try {
  343. return doDecode(htmlText);
  344. } catch (e) {
  345. try {
  346. return doDecode(htmlText.replace(/</g, "&lt;").replace(/>/g, "&gt;"));
  347. } catch (e) {
  348. return htmlText;
  349. }
  350. }
  351. };
  352. cognos.Prompt.Control.SelectFile.prototype.f_CompleteUpload = function (v_oUploadResponseXML) {
  353. var xmlDoc = null;
  354. var v_sXMLString = null;
  355. if (v_oUploadResponseXML.implementation && v_oUploadResponseXML.implementation.createDocument)
  356. {
  357. //For Firefox & Safari
  358. try
  359. {
  360. var v_oSerializer = new XMLSerializer();
  361. if (window.XML) {
  362. xmlDoc = XML(v_oSerializer.serializeToString(v_oUploadResponseXML));
  363. v_sXMLString = xmlDoc.toXMLString();
  364. } else // Safari
  365. {
  366. v_sXMLString = v_oSerializer.serializeToString(v_oUploadResponseXML);
  367. }
  368. }
  369. catch (e)
  370. {
  371. var sError = v_oUploadResponseXML.body.textContent;
  372. var n1 = sError.indexOf("Mandatory parameter \"");
  373. var n2 = sError.indexOf("\" has not been specified");
  374. if (n1>=0 && n2>=0 && n2>n1)
  375. {
  376. alert(sError.substring(n1+21,n2));
  377. } else {
  378. alert("Error 1! "+sError);
  379. }
  380. return;
  381. }
  382. }
  383. else if ("ActiveXObject" in window)
  384. {
  385. //for IE
  386. if (null != v_oUploadResponseXML.XMLDocument)
  387. {
  388. xmlDoc = v_oUploadResponseXML.XMLDocument;
  389. // var v_sXMLString = v_oUploadResponseXML.XMLDocument.xml;
  390. v_sXMLString = xmlDoc.xml;
  391. } else {
  392. var sContent = v_oUploadResponseXML.body.innerText;
  393. if (sContent.match(/^.*\r?\n?.*<pdSpec>\r?\n?.*<dataSet/)) {
  394. v_sXMLString = sContent;
  395. } else {
  396. var n1 = sContent.indexOf("Mandatory parameter \"");
  397. var n2 = sContent.indexOf("\" has not been specified");
  398. if (n1>=0 && n2>=0 && n2>n1)
  399. {
  400. alert(sContent.substring(n1+21,n2));
  401. } else {
  402. alert("Error 2! "+sContent);
  403. }
  404. return;
  405. }
  406. }
  407. }
  408. this.m_sProcessResponse = v_sXMLString.replace(/.*<output>/g,"").replace(/<\/output>/g,""); // there should be no "output" element anymore but I'm going to leave this here just to be safe...
  409. this.checkData();
  410. };
  411. cognos.Prompt.Control.SelectFile.prototype.getValue = function()
  412. {
  413. return this.m_elPrompt.value;
  414. };