dynDropDown.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. // Licensed Materials - Property of IBM
  2. //
  3. // IBM Cognos Products: ps
  4. //
  5. // (C) Copyright IBM Corp. 2005, 2011
  6. //
  7. // US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  8. // Copyright (C) 2008 Cognos ULC, an IBM Company. All rights reserved.
  9. // Cognos and the Cognos logo are trademarks of Cognos ULC, (formerly Cognos Incorporated).
  10. // a set of global javascript variables that get sets when the edit field takes focus or the user click on
  11. // the arrow to open the dropdown
  12. var g_dynamicDD_controlName = null;
  13. var g_dynamicDD_array = null;
  14. var g_dynamicDD_dropdownDiv = null;
  15. var g_dynamicDD_editField = null;
  16. var g_dynamicDD_callbackFunc = null;
  17. var g_dynamicDD_openedBy = null;
  18. var g_dynamicDD_filterList = null;
  19. var g_dynamicDD_currentValue = null;
  20. var regEx = new RegExp(String.fromCharCode(160),"g");
  21. var IDX_LABEL = 0;
  22. var IDX_VALUE = 1;
  23. // handle all keyboard events for the drop down list
  24. function handleSelectKey(evt) {
  25. var keyCode = evt ? evt.keyCode : window.event.keyCode;
  26. // Enter and Esc
  27. if ((keyCode == 27) || (keyCode == 13)) {
  28. closeDropDown();
  29. g_dynamicDD_editField.focus();
  30. }
  31. }
  32. // handle all keyboard events for the edit field list
  33. function handleEditFieldKey(evt) {
  34. }
  35. // handle all keyboard events for the edit field list
  36. function handleEditFieldKey(evt) {
  37. var keyCode = evt ? evt.keyCode : window.event.keyCode;
  38. if (keyCode == 40) {
  39. // if the dropdown is closed, then simply change the current value to the next in the list
  40. if (g_dynamicDD_dropdownDiv && g_dynamicDD_dropdownDiv.style.display == 'none') {
  41. moveThroughArray(1);
  42. } else {
  43. moveThroughDropDown(1);
  44. }
  45. } else if (keyCode == 38) { // up arrow
  46. // if the dropdown is closed, then simply change the current value to the next in the list
  47. if (g_dynamicDD_dropdownDiv && g_dynamicDD_dropdownDiv.style.display == 'none') {
  48. moveThroughArray(-1);
  49. } else {
  50. moveThroughDropDown(-1);
  51. }
  52. } else if ((keyCode == 27) || (keyCode == 13)) { // Enter and Esc
  53. closeDropDown();
  54. } else if ((keyCode == 8) || (keyCode >= 32)) { // all other keys
  55. if (g_dynamicDD_editField.value != '') {
  56. // only do something if the value of the edit field has changed
  57. if (g_dynamicDD_currentValue != g_dynamicDD_editField.value) {
  58. g_dynamicDD_currentValue = g_dynamicDD_editField.value;
  59. if (g_dynamicDD_dropdownDiv && g_dynamicDD_dropdownDiv.style.display == 'none' && g_dynamicDD_filterList == 'true') {
  60. doDropDown('typing');
  61. } else if (g_dynamicDD_filterList == 'true' && g_dynamicDD_openedBy == 'typing') {
  62. populateDropDown();
  63. }
  64. }
  65. if (document.getElementById('select_' + g_dynamicDD_controlName) && document.getElementById('select_' + g_dynamicDD_controlName).options.length == 0) {
  66. closeDropDown();
  67. }
  68. document.getElementById(g_dynamicDD_controlName).value = g_dynamicDD_editField.value;
  69. g_dynamicDD_currentValue = g_dynamicDD_editField.value;
  70. }
  71. else {
  72. closeDropDown();
  73. }
  74. handleEditFieldChange();
  75. }
  76. }
  77. // Change the edit and hidden field to a value in the array using a delta from the current selection
  78. function moveThroughArray(delta) {
  79. var found = false;
  80. for (var i = 0; i < g_dynamicDD_array.length; i++) {
  81. if (g_dynamicDD_editField.value == g_dynamicDD_array[i][IDX_LABEL]) {
  82. found = true;
  83. setEditAndHiddenFromArray(i+delta);
  84. break;
  85. }
  86. }
  87. if (!found) {
  88. setEditAndHiddenFromArray(0);
  89. }
  90. handleEditFieldChange();
  91. }
  92. function moveThroughDropDown(delta) {
  93. var selectControl = document.getElementById('select_' + g_dynamicDD_controlName);
  94. if (!selectControl) {
  95. return;
  96. }
  97. var found = false;
  98. for (var i = 0; i < selectControl.options.length; i++) {
  99. if (selectControl[i].selected) {
  100. changeDropDownSelection(i, i+delta);
  101. found = true;
  102. break;
  103. }
  104. }
  105. if (!found && selectControl.options.length > 0) {
  106. selectControl.selectedIndex = 0;
  107. setEditAndHiddenFromDropDown(0);
  108. }
  109. selectControl.focus();
  110. }
  111. function changeDropDownSelection(fromIndex, toIndex) {
  112. var selectControl = document.getElementById('select_' + g_dynamicDD_controlName);
  113. if (selectControl.options.length > toIndex && toIndex >= 0) {
  114. selectControl.selectedIndex = toIndex;
  115. setEditAndHiddenFromDropDown(toIndex);
  116. }
  117. }
  118. // Sets a bunch of global vars. Should be called everytime one of the controls gets focus or
  119. // when the user hits the open dropdown button
  120. function setGlobals(name, array, validateCallback, filterList) {
  121. if (g_dynamicDD_controlName != name) {
  122. if (g_dynamicDD_controlName != null) {
  123. closeDropDown();
  124. }
  125. g_dynamicDD_controlName = name;
  126. g_currentSelectedIndex = null;
  127. g_dynamicDD_array = array;
  128. g_dynamicDD_dropdownDiv = document.getElementById('div_'+name);
  129. g_dynamicDD_editField = document.getElementById('editField_'+name);
  130. g_dynamicDD_callbackFunc = validateCallback;
  131. g_dynamicDD_filterList = filterList;
  132. g_dynamicDD_editField.onkeyup = handleEditFieldKey;
  133. g_dynamicDD_openedBy = null;
  134. g_dynamicDD_currentValue = g_dynamicDD_editField.value;
  135. }
  136. }
  137. // handles the logic for opening the drop down
  138. function doDropDown(calledBy) {
  139. if (g_dynamicDD_dropdownDiv && g_dynamicDD_dropdownDiv.style.display == 'none') {
  140. g_dynamicDD_openedBy = calledBy;
  141. populateDropDown();
  142. // if this was called by typing in the edit field,
  143. // then only open the drop down if it's not empty
  144. if (calledBy != 'typing' || document.getElementById('select_' + g_dynamicDD_controlName).options.length > 0) {
  145. openDropDown(calledBy=='downArrow');
  146. }
  147. }
  148. else {
  149. closeDropDown();
  150. g_dynamicDD_openedBy = null;
  151. }
  152. }
  153. // close the drop down
  154. function closeDropDown() {
  155. if (g_dynamicDD_dropdownDiv)
  156. {
  157. g_dynamicDD_dropdownDiv.style.display='none';
  158. }
  159. }
  160. // open the drop down
  161. function openDropDown(setFocus) {
  162. if (g_dynamicDD_dropdownDiv)
  163. {
  164. g_dynamicDD_dropdownDiv.style.display='';
  165. if (setFocus) {
  166. document.getElementById('select_'+g_dynamicDD_controlName).focus();
  167. }
  168. }
  169. }
  170. // Build up the select control
  171. function populateDropDown() {
  172. var currentSelectedValue = document.getElementById(g_dynamicDD_controlName).value;
  173. var filterList = false;
  174. var container = document.getElementById('div_'+g_dynamicDD_controlName);
  175. var eleSelect = document.getElementById("select_"+g_dynamicDD_controlName);
  176. if (eleSelect) {
  177. eleSelect.parentNode.removeChild(eleSelect);
  178. }
  179. //build select control (dropDown)
  180. eleSelect = document.createElement('select');
  181. container.appendChild(eleSelect);
  182. eleSelect.name="select_"+g_dynamicDD_controlName;
  183. eleSelect.id="select_"+g_dynamicDD_controlName;
  184. eleSelect.size = 10;
  185. eleSelect.multiple = false;
  186. eleSelect.style.width="215";
  187. eleSelect.onclick=function(){onClickSelectControl(this)};
  188. eleSelect.onchange=function(){onChangeSelectControl(this)};
  189. eleSelect.onkeyup = handleSelectKey;
  190. var selectedIdx = -1;
  191. // build the options
  192. for (var i = 0; i < g_dynamicDD_array.length; i++) {
  193. DD_label = g_dynamicDD_array[i][IDX_LABEL];
  194. DD_value = g_dynamicDD_array[i][IDX_VALUE];
  195. if (g_dynamicDD_openedBy == 'downArrow' || g_dynamicDD_filterList != 'true' || DD_label.indexOf(g_dynamicDD_editField.value) == 0) {
  196. var eleOpt = document.createElement('option');
  197. eleSelect.appendChild(eleOpt);
  198. eleOpt.value = DD_value;
  199. eleOpt.innerHTML = htmlEncode(DD_label,true);
  200. // select the default
  201. if (DD_value == currentSelectedValue && g_dynamicDD_openedBy == 'downArrow') {
  202. selectedIdx=eleSelect.childNodes.length-1;
  203. g_dynamicDD_currentValue = DD_value;
  204. }
  205. }
  206. }
  207. eleSelect.selectedIndex=selectedIdx;
  208. }
  209. // Handle the onclick event of the select control
  210. function onClickSelectControl(obj) {
  211. g_dynamicDD_editField.focus();
  212. closeDropDown();
  213. }
  214. // Handle the onchange event of the select control
  215. function onChangeSelectControl(obj) {
  216. for (var i = 0; i < obj.options.length; i++) {
  217. if (obj[i].selected) {
  218. setEditAndHiddenFromDropDown(i);
  219. g_dynamicDD_currentValue = g_dynamicDD_array[i][IDX_VALUE];
  220. break;
  221. }
  222. }
  223. }
  224. // Updated the edit and hidden field using the selected item in the drop down
  225. function setEditAndHiddenFromDropDown(index) {
  226. var selectControl = document.getElementById("select_" + g_dynamicDD_controlName);
  227. document.getElementById(g_dynamicDD_controlName).value = selectControl[index].value; //hidden field
  228. g_dynamicDD_editField.value = selectControl[index].text;
  229. handleEditFieldChange();
  230. }
  231. // Set the hidden and edit field using the array
  232. function setEditAndHiddenFromArray(index) {
  233. if (index < g_dynamicDD_array.length && index >=0) {
  234. document.getElementById(g_dynamicDD_controlName).value = g_dynamicDD_array[index][IDX_VALUE];
  235. g_dynamicDD_editField.value = g_dynamicDD_array[index][IDX_LABEL];
  236. }
  237. }
  238. // Checks to see if the current value is in the array and returns true or false
  239. function isValidValue() {
  240. for (var i = 0; i < g_dynamicDD_array.length; i++) {
  241. if (isEditorEqualToListElement(i)) {
  242. return true;
  243. }
  244. }
  245. return false;
  246. }
  247. function isEditorEqualToListElement(idx) {
  248. return g_dynamicDD_editField.value.replace(regEx," ") == g_dynamicDD_array[idx][IDX_LABEL];
  249. }
  250. function htmlEncode(string, encodeSpaces) {
  251. tempString = string.replace(/&/g,"&amp;");
  252. tempString = tempString.replace(/"/g,"&quot;");
  253. tempString = tempString.replace(/</g,"&lt;");
  254. tempString = tempString.replace(/>/g,"&gt;");
  255. if (encodeSpaces) {
  256. tempString = tempString.replace(/\&nbsp;/g,"&amp;nbsp;").replace(/\s/g,"&nbsp;");
  257. }
  258. return tempString;
  259. }
  260. // Whenever the value changes, call the necessary javascript function if one was provided
  261. function handleEditFieldChange() {
  262. var callBackFunc = window[g_dynamicDD_callbackFunc];
  263. if (callBackFunc) {
  264. callBackFunc(g_dynamicDD_editField, isValidValue());
  265. }
  266. }
  267. // When the edit field takes focus
  268. function handleEditFieldFocus() {
  269. if (g_dynamicDD_dropdownDiv && g_dynamicDD_dropdownDiv.style.display != 'none' && g_dynamicDD_openedBy != 'typing') {
  270. closeDropDown();
  271. }
  272. }
  273. //Returns the value in the hidden input.
  274. //The hidden input name is handled by g_dynamicDD_controlName
  275. function getHiddenValue () {
  276. if (g_dynamicDD_controlName != null && document.getElementById(g_dynamicDD_controlName)) {
  277. return (document.getElementById(g_dynamicDD_controlName).value);
  278. } else {
  279. return ;
  280. }
  281. }
  282. //Update the hidden update (created by utml) that the hidden input has changed.
  283. //The hidden changed input is handled by 'changed_' + g_dynamicDD_controlName.
  284. function updateChanged () {
  285. if (g_dynamicDD_controlName != null && document.getElementById(g_dynamicDD_controlName)) {
  286. var changed = document.pform.elements['changed_' + g_dynamicDD_controlName];
  287. if (changed) {
  288. changed.value = 1;
  289. }
  290. }
  291. }
  292. //Set a value to the hidden field g_dynamicDD_controlName.
  293. //This is usually used for empty value.
  294. function setHiddenValue (val) {
  295. var valueField = null;
  296. if (g_dynamicDD_controlName != null) {
  297. valueField = document.getElementById(g_dynamicDD_controlName);
  298. }
  299. if (valueField) {
  300. valueField.value = val;
  301. updateChanged();
  302. }
  303. }
  304. //This function allows to update the value within the array corresponding to "Number"
  305. //This allows to know which entry has been selected "Unlimited" or "Number".
  306. function updateTheArray(index, value) {
  307. g_dynamicDD_array[index][IDX_VALUE] = value;
  308. }