PickTree.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. *+------------------------------------------------------------------------+
  3. *| Licensed Materials - Property of IBM
  4. *| BI and PM: prmt
  5. *| (C) Copyright IBM Corp. 2002, 2011
  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 PickTree prompt control
  14. This control primary purpose is to support SAP hierarchy variables
  15. @private
  16. @class
  17. */
  18. cognos.Prompt.Control.PickTree = cognos.Prompt.Control.f_extend({
  19. f_initialize: function( v_oProps ) {
  20. this._type_ = "cognos.Prompt.Control.PickTree";
  21. this.f_parent( v_oProps ); // call parent's initialize()
  22. var v_oPropsComponent = Object.f_extend( {}, v_oProps );
  23. v_oPropsComponent["@required"] = true;
  24. v_oPropsComponent["@hideAdornments"] = true;
  25. v_oPropsComponent.m_bIsAComponent = true;
  26. v_oPropsComponent.m_oOwner = this;
  27. v_oPropsComponent._id_ = this.f_getId();
  28. this.m_oTreeArguments = v_oPropsComponent;
  29. this.f_initCompleted();
  30. // initialize adornment image
  31. this.m_adornmentImg = 'spacer.gif';
  32. },
  33. //validate the input into the control
  34. checkData: function()
  35. {
  36. this.m_bValid = this.m_oTree.m_bValid;
  37. this.f_parent(); // call parent's checkData using saved reference.
  38. return this.m_bValid;
  39. },
  40. clearValues: function() {
  41. this.f_parent();
  42. this.f_clear();
  43. },
  44. f_drawCompleted: function()
  45. {
  46. this.m_oTree.f_drawCompleted();
  47. var v_oPickList = $(this.f_getId("PRMT_PT_HIERARCHIES_"));
  48. this.m_oPickList = v_oPickList;
  49. PRMTUtils.f_addEvent(v_oPickList, "change", this.f_onChangeTree.bind(this) );
  50. }
  51. });
  52. /**
  53. * Return the value of the control, which in this case is returning the tree selection
  54. */
  55. cognos.Prompt.Control.PickTree.prototype.f_getPV = function()
  56. {
  57. var v_oTree = this.m_oTree;
  58. var v_aPV = [];
  59. if ( v_oTree )
  60. {
  61. v_aPV = v_oTree.f_getPV();
  62. }
  63. return v_aPV;
  64. };
  65. /**
  66. * Draws the layout
  67. * <td><table><tr>
  68. * <td>Adornment</td>
  69. * <td>
  70. * <div>pickList</div>
  71. * <div>Tree</div>
  72. *
  73. * @private
  74. * @param {C_PromptElement} v_el Container.
  75. * @return {void}
  76. */
  77. cognos.Prompt.Control.PickTree.prototype.f_drawLayout = function( v_el )
  78. {
  79. var v_tdLayout = $CE( "td", {}, v_el );
  80. var v_tbl = $CE( "table", {"border": K_PRMT_DEBUG_TABLEBORDER, "cellPadding":0, "cellSpacing":0, "role":K_PRMT_ARIA_ROLE_PRESENTATION}, v_tdLayout );
  81. if ( this[K_PRMT_sATTR_DISABLED] )
  82. {
  83. v_tbl.f_setProperty( "disabled", "disabled" );
  84. }
  85. var v_tbd = $CE( "tbody", {}, v_tbl );
  86. var v_tr = $CE( "tr", {}, v_tbd );
  87. var v_td = $CE( "td", {"vAlign":"top", "class": "clsControlLabel pc"}, v_tr );
  88. if ( this.isRequired() && !this["@hideAdornments"] )
  89. {
  90. var v_imgReq = $CE( "img", {
  91. "src": this.m_sSkin + "/prompting/images/icon_required.gif",
  92. "class": "clsErrorRequired",
  93. "vAlign": "top",
  94. "height": 10,
  95. "width": 10,
  96. "border": 0,
  97. "alt": ""
  98. }, v_td);
  99. }
  100. var v_tdControl = $CE( "td", {}, v_tr );
  101. this.f_drawPickList( v_tdControl );
  102. v_tr = $CE( "tr", {}, v_tbd );
  103. v_td = $CE( "td", {"vAlign":"top", "class": "clsControlLabel pc"}, v_tr );
  104. $CE("img", {"src": this.m_sSkin + "/prompting/images/spacer.gif", "width": 10, "height": 1, "border": 0, "alt": ""}, v_td);
  105. this.m_elPrompt = this.f_drawTree( v_tr );
  106. return (this.m_elPrompt);
  107. }
  108. /**
  109. * Draws the drop down to pick the hierarchy
  110. *
  111. * @private
  112. * @param {C_PromptElement} v_el Container.
  113. * @return {void}
  114. */
  115. cognos.Prompt.Control.PickTree.prototype.f_drawPickList = function( v_el )
  116. {
  117. var v_oContainer = $CE("div", {"id": this.f_getId("PRMT_PT_HIERARCHIES_BOX_")}, v_el);
  118. var v_oPickList = $CE("select",{"id": this.f_getId("PRMT_PT_HIERARCHIES_"), "class":"clsSelectControl pv"},v_oContainer);
  119. var v_firstOption = this["@columnName"] && this["@columnName"].length > 0 ? this["@columnName"]:this["@parameter"];
  120. // draw select options
  121. var v_choices = this.f_getChildByTagName( "selectHierarchyChoice" );
  122. var v_selectedHierarchy = ((v_choices && v_choices.c && v_choices.c.length > 0)? v_choices.c[0] : null);
  123. var v_options = this.f_getChildByTagName( "selectHierarchies" );
  124. var v_optionEl;
  125. // add the top two lines
  126. v_optionEl = $CE("option", {}, v_oPickList);
  127. v_optionEl.f_appendText( G_IsBidiEnabled && this["@contentTextDirection"] ? PRMT_BidiUtils.enforceBidiDirection(v_firstOption, this["@contentTextDirection"]) : v_firstOption );
  128. v_optionEl = $CE("option", {}, v_oPickList);
  129. v_optionEl.f_appendText("--------------------------------------------");
  130. if ( v_options && v_options.c && v_options.c.length > 0)
  131. {
  132. for (var i = 0; i < v_options.c.length; i++ )
  133. {
  134. var v_option = v_options.c[i];
  135. var v_optionAttr = {"value": v_option[K_PRMT_sATTR_USE_VALUE], "dv":v_option[K_PRMT_sATTR_DISPLAY_VALUE]};
  136. if (v_selectedHierarchy && v_option && v_option[K_PRMT_sATTR_USE_VALUE] == v_selectedHierarchy[K_PRMT_sATTR_USE_VALUE]) {
  137. v_optionAttr["selected"] = "true"
  138. }
  139. v_optionEl = $CE("option", v_optionAttr, v_oPickList);
  140. v_optionEl.f_appendText( (G_IsBidiEnabled && this["@contentTextDirection"] ? PRMT_BidiUtils.enforceBidiDirection(v_option[K_PRMT_sATTR_DISPLAY_VALUE], this["@contentTextDirection"]) : v_option[K_PRMT_sATTR_DISPLAY_VALUE]) );
  141. }
  142. }
  143. return v_oContainer;
  144. };
  145. /**
  146. * Draw the tree part of the control leveraging already existing Tree control
  147. */
  148. cognos.Prompt.Control.PickTree.prototype.f_drawTree = function( v_el )
  149. {
  150. var v_oContainer = $CE("div", {"id": this.f_getId("PRMT_PT_TREE_BOX_")}, v_el);
  151. var v_tbl = $CE( "table", {"border": K_PRMT_DEBUG_TABLEBORDER, "cellPadding":0, "cellSpacing":0, "role":K_PRMT_ARIA_ROLE_PRESENTATION}, v_oContainer );
  152. var v_tbd = $CE( "tbody", {}, v_tbl );
  153. var v_tr = $CE( "tr", {}, v_tbd );
  154. this.f_cleanTreeArguments();
  155. var v_oTree = new cognos.Prompt.Control.Tree( this.m_oTreeArguments );
  156. v_oTree.f_drawLayout(v_tr);
  157. this.m_oTree = v_oTree;
  158. };
  159. /**
  160. * Clean the hierarchy list and choice from the tree arguments
  161. */
  162. cognos.Prompt.Control.PickTree.prototype.f_cleanTreeArguments = function( ) {
  163. // rm "selectHierarchies" and "selectHierarchyChoice", if selectOptions is missing or empty set canExpand to false;
  164. var v_treeArguments = this.m_oTreeArguments;
  165. if ( v_treeArguments.c && v_treeArguments.c.length )
  166. {
  167. for ( var i = 0, j = 0; i < v_treeArguments.c.length; i++ )
  168. {
  169. if ( v_treeArguments.c[i].n == "selectHierarchies" || v_treeArguments.c[i].n == "selectHierarchyChoice") {
  170. v_treeArguments.c.splice(i,1);
  171. i--;
  172. }
  173. }
  174. }
  175. };
  176. /**
  177. * Get the selected hierarchy from the hierarchy pick list
  178. */
  179. cognos.Prompt.Control.PickTree.prototype.f_getPickListSelection = function()
  180. {
  181. var result = null;
  182. var v_oPickList = this.m_oPickList;
  183. if (v_oPickList.options && v_oPickList.options.length > 0) {
  184. var v_oSelOption = v_oPickList.options[v_oPickList.selectedIndex];
  185. if (v_oSelOption) {
  186. result = v_oSelOption.getAttribute('value');
  187. if (result === null)
  188. {
  189. result = v_oSelOption.value;
  190. }
  191. // for embedded IE 9 "feature"
  192. if (!result && v_oSelOption.attributes && v_oSelOption.attributes['value']) {
  193. result = v_oSelOption.attributes['value'].value;
  194. }
  195. }
  196. }
  197. return result;
  198. };
  199. /**
  200. * Get the hierarchy options in XML format, like the example below:
  201. * <selectHierarchies>
  202. * <selectHierarchy useValue="[P].[D].[H1]" displayValue=" Hierarchy 1"/>
  203. * <selectHierarchy useValue="[P].[D].[H2]" displayValue=" Hierarchy 2"/>
  204. * <selectHierarchy useValue="[P].[D].[H3]" displayValue=" Hierarchy 3"/>
  205. * </selectHierarchies>
  206. */
  207. cognos.Prompt.Control.PickTree.prototype.f_getXMLHierachyValues = function()
  208. {
  209. var v_aOptionXml = ['<selectHierarchy', ' useValue="',K_PRMT_sEMPTY,'" displayValue="',K_PRMT_sEMPTY,'" />'];
  210. var result = "<selectHierarchies>";
  211. var v_oPickList = this.m_oPickList;
  212. if (v_oPickList.options && v_oPickList.options.length > 0) {
  213. //var v_oSelOption = v_oPickList.options[v_oPickList.selectedIndex];
  214. var v_option = null;
  215. var v_oPickListOptions = v_oPickList.options;
  216. // skip the first two options (header and separator)
  217. for ( var i = 2; i < v_oPickListOptions.length; i++ )
  218. {
  219. v_option = v_oPickListOptions[i];
  220. v_aOptionXml[2] = v_option.getAttribute('value');
  221. // for embedded IE 9 "feature"
  222. if (!v_aOptionXml[2] && v_option.attributes && v_option.attributes['value']) {
  223. v_aOptionXml[2] = v_option.attributes['value'].value;
  224. }
  225. v_aOptionXml[4] = v_option.getAttribute(K_PRMT_sDV);
  226. result += v_aOptionXml.join(K_PRMT_sEMPTY);
  227. }
  228. }
  229. result += "</selectHierarchies>";
  230. return result;
  231. };
  232. /**
  233. * Send a forward request to Report Server indicating that it is a reprompt and
  234. * the chosen hierarchy node will be included as a form-field variable:
  235. * - name : "_swt_"+ promptName + "_hierarchy";
  236. * - value: useValue for the selected hierarchy
  237. * and the list of hierarchies, so RSVP doesn't need to do another metadata request
  238. * to re-fill the hierarchies list.
  239. * - hierachies_name : "_swt_"+ promptName + "_hierarchies";
  240. * - value : the list of hierarchies in XML format (like the intermediateXML)
  241. */
  242. cognos.Prompt.Control.PickTree.prototype.f_onChangeTree = function()
  243. {
  244. var v_idName = this["@parameter"];
  245. var isNameUsedAsId = false;
  246. if (!v_idName || (v_idName.trim && v_idName.trim().length < 1)) {
  247. v_idName = ( this["@name"] ? this["@name"] : "");
  248. isNameUsedAsId = true;
  249. }
  250. var v_sName = "_swt_"+ v_idName + "_hierarchy";
  251. var v_sValue = this.f_getPickListSelection();
  252. // clear current tree selection if any
  253. this.m_oTree.clearValues();
  254. if ( v_sValue )
  255. {
  256. var oCV = this.f_getCV();
  257. var v_sSubmitType = K_ACTION_REPROMPT;
  258. var v_sHierarchiesName = "_swt_"+ v_idName + "_hierarchies";
  259. if (oCV && typeof oCV.submitPromptValues == K_PRMT_sFUNCTION && typeof ViewerDispatcherEntry == K_PRMT_sFUNCTION){
  260. var oReq = new ViewerDispatcherEntry(oCV);
  261. oReq.addFormField("ui.action", K_ACTION_FORWARD);
  262. oReq.addFormField(v_sName, v_sValue);
  263. oReq.addFormField(v_sHierarchiesName, this.f_getXMLHierachyValues());
  264. if (isNameUsedAsId) {
  265. oReq.addFormField("_promptIdBasedNames",true);
  266. }
  267. oReq.addFormField("_promptControl", K_ACTION_REPROMPT);
  268. oCV.submitPromptValues(oReq);
  269. } else {
  270. SetPromptMethod(K_ACTION_FORWARD);
  271. if (document.forms[0]) {
  272. var frm = document.forms[0];
  273. genHiddenInputHTML( frm.name, v_sName, v_sValue );
  274. genHiddenInputHTML( frm.name, v_sHierarchiesName, this.f_getXMLHierachyValues());
  275. if (isNameUsedAsId) {
  276. genHiddenInputHTML( frm.name,"_promptIdBasedNames",true);
  277. }
  278. }
  279. SetPromptControl( K_ACTION_REPROMPT );
  280. }
  281. }
  282. };