CMultipleTextPicker.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. CMultipleTextPicker.js
  14. This script is used to provide interactivity for the
  15. multiple select textBox prompt component. This component is
  16. a combination of the date picker and a list box.
  17. */
  18. //Constructor to create a checkboxlist component
  19. // oTextPicker: the text picker control
  20. // oLstChoices: the name of the select form control
  21. // oSubmit: the form control to submit selections to the server
  22. // bRequired: is this a required field true/false
  23. // sSubmitType: submit this as a standard form, or as XML
  24. // oErrorFeedback: object used to provide validation feedback
  25. // oSizer: image object used to control the minimum size of the list
  26. // oInsertButton: the insert button
  27. // oRemoveButton: the remove button
  28. var CMultipleTextPicker_INSTANCES = new Array();
  29. function CMultipleTextPicker(oTextPicker, oLstChoices, oSubmit, bRequired, sSubmitType, oErrorFeedback, oSizer, oInsertButton, oRemoveButton, sCVId)
  30. {
  31. this.setCVId(sCVId);
  32. CMultipleTextPicker_INSTANCES[CMultipleTextPicker_INSTANCES.length] = this;
  33. this.m_oTextPicker = oTextPicker;
  34. this.m_oLstChoices = oLstChoices;
  35. this.m_oSubmit = oSubmit;
  36. this.m_bRequired = bRequired;
  37. this.m_bValid = false;
  38. this.m_sSubmitType = sSubmitType;
  39. this.m_oErrorFeedback = oErrorFeedback;
  40. if (oInsertButton)
  41. {
  42. this.m_oInsertButton = oInsertButton;
  43. }
  44. if (oRemoveButton)
  45. {
  46. this.m_oRemoveButton = oRemoveButton;
  47. }
  48. this.checkData();
  49. //resize the list if necessary
  50. if(oSizer)
  51. {
  52. this.m_oSizer = oSizer;
  53. this.m_oLstChoices.style.width='auto';
  54. setTimeout("resizeCMultipleTextPickerINSTANCES()",1);
  55. }
  56. //check the states of the insert and remove buttons
  57. this.checkInsertRemove();
  58. }
  59. CMultipleTextPicker.prototype = new CPromptControl();
  60. //select all items
  61. function CMultipleTextPicker_selectAll()
  62. {
  63. for (var i=0; i < this.m_oLstChoices.options.length; i++)
  64. {
  65. this.m_oLstChoices.options[i].selected = true;
  66. }
  67. //check the states of the insert and remove buttons
  68. this.checkInsertRemove();
  69. }
  70. //remove selection from all items
  71. function CMultipleTextPicker_deSelectAll()
  72. {
  73. for (var i=0; i < this.m_oLstChoices.options.length; i++)
  74. {
  75. this.m_oLstChoices.options[i].selected = false;
  76. }
  77. //check the states of the insert and remove buttons
  78. this.checkInsertRemove();
  79. }
  80. //insert values from the picker control
  81. function CMultipleTextPicker_insert()
  82. {
  83. var sInsertText = this.m_oTextPicker.sGetValue();
  84. var sDisplayValue = this.m_oTextPicker.sGetFormatValue();
  85. if (sInsertText)
  86. {
  87. //determine whether this is a single value or multiple values
  88. var rMultiline = /[\n\f\r]+/;
  89. var arInsertText = sInsertText.replace(/[\n\f\r\s]+$/,'').split(rMultiline);
  90. if (arInsertText.length > 1)
  91. {
  92. for (var i=0; i < arInsertText.length; i++)
  93. {
  94. var sFormatValue = getFormatByDataType(arInsertText[i], this.m_oTextPicker.m_sDataType, this.m_oTextPicker.m_bIsCurrency);
  95. this.m_oLstChoices.options[this.m_oLstChoices.options.length] = new Option(sFormatValue, arInsertText[i], false, false);
  96. }
  97. }
  98. else
  99. {
  100. this.m_oLstChoices.options[this.m_oLstChoices.options.length] = new Option(sDisplayValue, sInsertText, false, false);
  101. }
  102. //clear the choices from the text box
  103. this.m_oTextPicker.clear();
  104. }
  105. this.checkData();
  106. setTimeout("resizeCMultipleTextPickerINSTANCES()",1);
  107. //check the states of the insert and remove buttons
  108. this.checkInsertRemove();
  109. }
  110. //remove selections from the list of choices
  111. function CMultipleTextPicker_remove()
  112. {
  113. this.removeSelectedChoices();
  114. setTimeout("resizeCMultipleTextPickerINSTANCES()",1);
  115. //check the states of the insert and remove buttons
  116. this.checkInsertRemove();
  117. }
  118. CMultipleTextPicker.prototype = new CPromptControl();
  119. //perform any special processing for the server.
  120. //this function will wrap all list items in XML.
  121. //This is required for cases where the unselected items need
  122. //to be submitted too.
  123. function CMultipleTextPicker_preProcess()
  124. {
  125. var i=0;
  126. if (this.m_sSubmitType == K_PRMT_sXML)
  127. {
  128. //convert the data to XML and submit
  129. var sURLValues = K_PRMT_sEMPTY;
  130. if (this.m_oLstChoices.options.length > 0)
  131. {
  132. for(i = 0; i < this.m_oLstChoices.options.length; i ++)
  133. {
  134. sURLValues += '<selectOption';
  135. sURLValues += ' displayValue="' + sXmlEncode(this.m_oLstChoices.options[i].text) +'"';
  136. sURLValues += ' useValue="' + sXmlEncode(this.m_oLstChoices.options[i].value) + '"';
  137. if (this.m_oLstChoices.options[i].selected == true)
  138. {
  139. sURLValues += ' selected="true" />';
  140. }
  141. else
  142. {
  143. sURLValues += ' selected="false" />';
  144. }
  145. }
  146. }
  147. addSelectChoices(this.m_oSubmit, sURLValues);
  148. }
  149. else
  150. {
  151. //select all the choices so they can be submitted with the form
  152. for(i = 0; i < this.m_oLstChoices.options.length; i ++)
  153. {
  154. this.m_oLstChoices.options[i].selected = true;
  155. }
  156. }
  157. }
  158. //return true unless this is a required prompt
  159. //and there are no values
  160. function CMultipleTextPicker_checkData()
  161. {
  162. if ((this.m_bRequired == true) && (this.m_oLstChoices.options.length === 0))
  163. {
  164. this.m_bValid = false;
  165. this.checkFail();
  166. return false;
  167. }
  168. else
  169. {
  170. this.m_bValid = true;
  171. this.checkPass();
  172. return true;
  173. }
  174. }
  175. //make sure that choices are always visible and set a minimum size
  176. //resize the list to fit the data if required
  177. //if there are no items, prevent the list from
  178. //shrinking too small
  179. //allow the list to expand to fit the biggest item
  180. function CMultipleTextPicker_resizeList()
  181. {
  182. if (this.m_oErrorFeedback)
  183. {
  184. if(this.m_oLstChoices.style.tableLayout!='fixed')
  185. {
  186. this.m_oLstChoices.style.width='auto';
  187. }
  188. if (this.m_oLstChoices.offsetWidth < 200)
  189. {
  190. this.m_oLstChoices.style.width='200px';
  191. }
  192. this.m_oErrorFeedback.style.width = this.m_oLstChoices.offsetWidth + 'px';
  193. }
  194. }
  195. //enable and disable the insert and remove buttons
  196. //based on what the user has selected
  197. function CMultipleTextPicker_checkInsertRemove()
  198. {
  199. if (this.m_oInsertButton)
  200. {
  201. if ((this.m_oTextPicker.getValid() == true) && this.m_oTextPicker.hasValue())
  202. {
  203. this.m_oInsertButton.disabled = false;
  204. }
  205. else
  206. {
  207. this.m_oInsertButton.disabled = true;
  208. this.m_oInsertButton.className = "clsInsertRemoveButton";
  209. }
  210. }
  211. if (this.m_oRemoveButton)
  212. {
  213. if (this.m_oLstChoices.selectedIndex == -1)
  214. {
  215. this.m_oRemoveButton.disabled = true;
  216. this.m_oRemoveButton.className = "clsInsertRemoveButton";
  217. }
  218. else
  219. {
  220. this.m_oRemoveButton.disabled = false;
  221. }
  222. }
  223. }
  224. //add a value to the list via javascript
  225. function CMultipleTextPicker_add(sDisplayValue, sInsertText)
  226. {
  227. this.m_oLstChoices.options[this.m_oLstChoices.options.length] = new Option(sDisplayValue, sInsertText, false, false);
  228. this.checkData();
  229. setTimeout("resizeCMultipleTextPickerINSTANCES()",1);
  230. //check the states of the insert and remove buttons
  231. this.checkInsertRemove();
  232. }
  233. //add an item to the list without any checking by the control
  234. //this method provides an efficient way to add items, but
  235. //the update() method should be called to clean up the control
  236. //when finished adding
  237. function CMultipleTextPicker_addNoUpdate(sDisplayValue, sInsertText, sel)
  238. {
  239. sDisplayValue = sDecodeU003( sDisplayValue );
  240. sInsertText = sDecodeU003( sInsertText );
  241. if (sel == true) {
  242. this.m_oLstChoices.options[this.m_oLstChoices.options.length] = new Option(sDisplayValue, sInsertText, true, true);
  243. }
  244. else {
  245. this.m_oLstChoices.options[this.m_oLstChoices.options.length] = new Option(sDisplayValue, sInsertText, false, false);
  246. }
  247. }
  248. //clean up the control
  249. function CMultipleTextPicker_update()
  250. {
  251. this.checkData();
  252. setTimeout("resizeCMultipleTextPickerINSTANCES()",1);
  253. //check the states of the insert and remove buttons
  254. this.checkInsertRemove();
  255. }
  256. //Prototypes to assign methods to new instances of the object
  257. CMultipleTextPicker.prototype.selectAll = CMultipleTextPicker_selectAll;
  258. CMultipleTextPicker.prototype.deSelectAll = CMultipleTextPicker_deSelectAll;
  259. CMultipleTextPicker.prototype.insert = CMultipleTextPicker_insert;
  260. CMultipleTextPicker.prototype.remove = CMultipleTextPicker_remove;
  261. CMultipleTextPicker.prototype.preProcess = CMultipleTextPicker_preProcess;
  262. CMultipleTextPicker.prototype.checkData = CMultipleTextPicker_checkData;
  263. CMultipleTextPicker.prototype.resizeList = CMultipleTextPicker_resizeList;
  264. CMultipleTextPicker.prototype.checkInsertRemove = CMultipleTextPicker_checkInsertRemove;
  265. CMultipleTextPicker.prototype.add = CMultipleTextPicker_add;
  266. CMultipleTextPicker.prototype.addNoUpdate = CMultipleTextPicker_addNoUpdate;
  267. CMultipleTextPicker.prototype.update = CMultipleTextPicker_update;
  268. function resizeCMultipleTextPickerINSTANCES() {
  269. if (typeof CMultipleTextPicker_INSTANCES == K_PRMT_sOBJECT && CMultipleTextPicker_INSTANCES.length) {
  270. for (var i = 0; i < CMultipleTextPicker_INSTANCES.length; i++) {
  271. CMultipleTextPicker_INSTANCES[i].resizeList();
  272. }
  273. }
  274. }