prompt.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. *+------------------------------------------------------------------------+
  3. *| Licensed Materials - Property of IBM
  4. *| BI and PM: prmt
  5. *| (C) Copyright IBM Corp. 2002, 2011, 2020
  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. Should be accessed through <tt>cognos.Report.prompt</tt>.
  14. @fileOverview Managing Prompts.
  15. @namespace cognos.Prompt
  16. @class Manager class for prompts.
  17. */
  18. cognos.Prompt = function()
  19. {
  20. this.m_aControls = [];
  21. this.m_aParameters = [];
  22. };
  23. /**
  24. Adds a prompt control to the manager.
  25. @private
  26. @param {object} v_oProps properties of the object
  27. @param {object} v_oExtra Extra properties to add to the object, like <i>CVId</i>.
  28. @return {cognos.Prompt.Control} Newly created object.
  29. */
  30. cognos.Prompt.prototype.F_Add = function( v_oProps, v_oExtra )
  31. {
  32. Object.f_extend( v_oProps, v_oExtra );
  33. var v_o = this.f_create( v_oProps );
  34. if ( v_o )
  35. {
  36. v_o.f_setPromptManager(this);
  37. this.m_aControls.push( v_o );
  38. window[ v_o.f_getId("p_") ] = v_o;
  39. var cv = v_o.f_getCV();
  40. if (cv)
  41. {
  42. if (typeof cv.setHasPrompts == "function")
  43. {
  44. cv.setHasPrompts(true);
  45. }
  46. var v_aPP = cv.preProcessControlArray;
  47. if ( v_aPP )
  48. {
  49. v_aPP.push( v_o );
  50. }
  51. }
  52. }
  53. return ( v_o );
  54. };
  55. /**
  56. Adds a prompt control to the manager.
  57. @private
  58. @param {object} v_oProps properties of the object
  59. @param {object} v_oExtra Extra properties to add to the object, like <i>CVId</i>.
  60. @return {cognos.Prompt.Control} Newly created object.
  61. */
  62. cognos.Prompt.prototype.f_create = function( v_oProps )
  63. {
  64. var v_o = null;
  65. if ( v_oProps )
  66. {
  67. var isHTML5 = this.isHTML5(v_oProps);
  68. if ( v_oProps["@range"] )
  69. {
  70. v_o = new cognos.Prompt.Control.Range( v_oProps, this );
  71. } else if ( v_oProps["@parameterizedTree"] && ( v_oProps["@parameterizedTree"] == true )) {
  72. v_o = new cognos.Prompt.Control.PickTree( v_oProps );
  73. } else {
  74. switch( v_oProps.n )
  75. {
  76. case "textBox": v_o = new cognos.Prompt.Control.Text( v_oProps ); break;
  77. case "selectFile": v_o = new cognos.Prompt.Control.SelectFile( v_oProps ); break;
  78. case "selectValue": v_o = new cognos.Prompt.Control.SelectValue( v_oProps ); break;
  79. case "selectInterval": v_o = new cognos.Prompt.Control.Interval( v_oProps, this ); break;
  80. case "selectDate":
  81. v_o = ((isHTML5 && this.browserSupportsHTML5Input(v_oProps.n))? new cognos.Prompt.Control.SelectHTML5Date( v_oProps ) : new cognos.Prompt.Control.Date( v_oProps ));
  82. break;
  83. case "selectTime":
  84. v_o = ((isHTML5 && this.browserSupportsHTML5Input(v_oProps.n))? new cognos.Prompt.Control.SelectHTML5Time( v_oProps ) : new cognos.Prompt.Control.Time( v_oProps ));
  85. break;
  86. case "selectDateTime":
  87. v_o = ((isHTML5 && this.browserSupportsHTML5Input(v_oProps.n))? new cognos.Prompt.Control.SelectHTML5DateTime( v_oProps ) : new cognos.Prompt.Control.DateTime( v_oProps ));
  88. break;
  89. case "selectWithTree": v_o = new cognos.Prompt.Control.Tree( v_oProps ); break;
  90. case "selectWithSearch": v_o = new cognos.Prompt.Control.Search( v_oProps ); break;
  91. }
  92. }
  93. }
  94. return ( v_o );
  95. };
  96. cognos.Prompt.prototype.browserSupportsHTML5Input = function(sInputType)
  97. {
  98. var bIsSupported = true;
  99. var inputEle = document.createElement("input");
  100. switch(sInputType)
  101. {
  102. case "selectDate":
  103. inputEle.setAttribute("type", "date");
  104. bIsSupported = inputEle.type !== "text";
  105. break;
  106. case "selectTime":
  107. inputEle.setAttribute("type", "time");
  108. bIsSupported = inputEle.type !== "text";
  109. break;
  110. case "selectDateTime":
  111. /*PRMT doesn't use the HTML5 datetime or datetime-local input type, instead we generate a composite control composed of HTML5 date and time controls, so we can support a datetime control as long as both the individual HTML5 date and time input types are supported.*/
  112. inputEle.setAttribute("type", "date");
  113. bIsSupported = inputEle.type !== "text";
  114. inputEle.setAttribute("type", "time");
  115. bIsSupported = (inputEle.type !== "text") && bIsSupported;
  116. break;
  117. }
  118. return bIsSupported;
  119. };
  120. cognos.Prompt.prototype.isHTML5 = function( v_oProps )
  121. {
  122. var result = false;
  123. if ((typeof G_PRMT_HTML5PromptControls) != K_PRMT_sUNDEFINED && G_PRMT_HTML5PromptControls) {
  124. result = true;
  125. }
  126. else {
  127. var reHTML5 = new RegExp("^_html5", "im");
  128. if ( v_oProps["@name"] && ( v_oProps["@name"].match(reHTML5) ) ) {
  129. result = true;
  130. }
  131. }
  132. return result;
  133. }
  134. /**
  135. Returns the control object associated to a name.
  136. @param {String} sName Prompt Control ID. It could be the <tt>name</tt> property set for the control in Report Studio.
  137. @return {cognos.Prompt.Control} Object associated to sName. Return <tt>null</tt> if it doesn't exists.
  138. */
  139. cognos.Prompt.prototype.getControlByName = function(sName)
  140. {
  141. return this.getPromptControl(sName);
  142. };
  143. /**
  144. Returns an array of all controls associated to this report.
  145. @return {cognos.Prompt.Control[]}
  146. */
  147. cognos.Prompt.prototype.getControls = function() { return this.m_aControls; };
  148. /**
  149. Returns the parameter object associated to a name.
  150. @param {String} sName Parameter Name.
  151. @return {cognos.Prompt.Parameter} Parameter associated to sName. Creates a new Parameter is it doesn't exists.
  152. */
  153. cognos.Prompt.prototype.getParameterByName = function(sName)
  154. {
  155. if ( sName instanceof cognos.Prompt.Parameter )
  156. {
  157. return sName;
  158. }
  159. for (var v_idx = 0; v_idx < this.m_aParameters.length; v_idx++)
  160. {
  161. if ( this.m_aParameters[v_idx].getName() == sName )
  162. {
  163. return (this.m_aParameters[v_idx]);
  164. }
  165. }
  166. var v_oParameter = new cognos.Prompt.Parameter(sName);
  167. this.m_aParameters.push(v_oParameter);
  168. return v_oParameter;
  169. };
  170. /**
  171. Returns an array of all parameters associated to this report.
  172. @return {cognos.Prompt.Parameter[]}
  173. */
  174. cognos.Prompt.prototype.getParameters = function() { return this.m_aParameters; };
  175. /**
  176. Returns the object associated to an ID.
  177. @private
  178. @param {string} sID Prompt Control ID. It could be the <tt>name</tt> property set for the control in Report Studio.
  179. @return {cognos.Prompt.Control} Object associated to sID. Return <tt>null</tt> if it doesn't exists.
  180. */
  181. cognos.Prompt.prototype.getPromptControl = function( sID )
  182. {
  183. var v_aPrompts = this.m_aControls;
  184. var v_oPrompt = null;
  185. for (var v_idx = 0; v_idx < v_aPrompts.length; v_idx++)
  186. {
  187. var v_oTemp = v_aPrompts[ v_idx ];
  188. var v_sID = v_oTemp.f_getId();
  189. if ( v_sID == sID ) {
  190. v_oPrompt = v_oTemp;
  191. break;
  192. }
  193. }
  194. // didn't find using _id_, try with @id
  195. if ( v_oPrompt === null )
  196. {
  197. for (v_idx = 0; v_idx < v_aPrompts.length; v_idx++)
  198. {
  199. var v_oTemp = v_aPrompts[ v_idx ];
  200. var v_sID = v_oTemp["@id"];
  201. if ( v_sID == sID ) {
  202. v_oPrompt = v_oTemp;
  203. break;
  204. }
  205. }
  206. }
  207. // try with @name
  208. if ( v_oPrompt === null )
  209. {
  210. for (v_idx = 0; v_idx < v_aPrompts.length; v_idx++)
  211. {
  212. var v_oTemp = v_aPrompts[ v_idx ];
  213. var v_sID = v_oTemp["@name"];
  214. if ( v_sID == sID ) {
  215. v_oPrompt = v_oTemp;
  216. break;
  217. }
  218. }
  219. }
  220. return v_oPrompt;
  221. };
  222. /**
  223. Return <tt>true</tt> if parameter sParameterName is satisfied.
  224. @private
  225. @param {String} [sParameterName] Parameter to validate. If omitted, returns true if <i>ALL</i> parameters are valid.
  226. @return {boolean}
  227. */
  228. cognos.Prompt.prototype.isValid = function( sParameterName )
  229. {
  230. var v_bValid = true;
  231. for ( var v_idx = 0; v_bValid && v_idx < this.m_aParameters.length; v_idx++ ) {
  232. v_bValid &= this.m_aParameters[ v_idx ].isValid();
  233. }
  234. return v_bValid;
  235. };
  236. /**
  237. Associate a custom control to a parameter.
  238. ** <u>For future use</u> - It's only here to show our intention to support this kind of action - <i>Still need to be worked out.</i>
  239. @param {String|cognos.Prompt.Parameter} parameter Parameter or Parameter Name to associate with the control.
  240. @param {cognos.Prompt.Control} control The control.
  241. @return {void}
  242. @example var <tt>ctrl</tt> = new CustomMapControl(); // based on a cognos.Prompt.Control
  243. <br/>var <tt>prmtr</tt> = cognos.Prompt.getParameterByName('Country');
  244. <br/>cognos.Prompt.registerControl( <tt>prmtr</tt>, <tt>ctrl</tt> );
  245. */
  246. cognos.Prompt.prototype.registerControl = function(parameter, control)
  247. {
  248. var v_oParameter = this.getParameterByName( parameter );
  249. if ( v_oParameter )
  250. {
  251. v_oParameter.f_addControl( control );
  252. }
  253. this.m_aControls.push( control );
  254. };
  255. /**
  256. Returns XML representation for parameter names and values.
  257. @private
  258. @return {string} XML string.
  259. */
  260. cognos.Prompt.prototype.toXML = function( )
  261. {
  262. var v_aXML = [];
  263. for ( var v_idx = 0; v_idx < this.m_aParameters.length; v_idx++ )
  264. {
  265. var sName = this.m_aParameters[v_idx].getName();
  266. v_aXML.push('<param name="');
  267. v_aXML.push( sName.f_xmlSafe() );
  268. v_aXML.push('">');
  269. v_aXML.push( this.m_aParameters[v_idx].getXML() );
  270. v_aXML.push('</param>');
  271. }
  272. return ( v_aXML.join(K_PRMT_sEMPTY) );
  273. };
  274. var C_PromptManager = cognos.Prompt; // Keep old reference for backward compatibility with custom scripts.