multiselect.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*##############################################################################
  2. # HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. ############################################################################## */
  16. lastClicked = -1;
  17. lastChecked = false;
  18. checkedCount = 0;
  19. rowsChecked = false;
  20. totalItems = -1;
  21. selectAllCheckboxChecked = false;
  22. multiSelectTable = null;
  23. oneCheckboxPerRow = true;
  24. onCheckHandler = null;
  25. singleSelect = false;
  26. function initSelection(tableId, multipleCheckboxesPerRow, onCheck)
  27. {
  28. if (multipleCheckboxesPerRow)
  29. oneCheckboxPerRow = false;
  30. if (onCheck)
  31. onCheckHandler = onCheck;
  32. multiSelectTable = document.getElementById(tableId);
  33. countItems();//updates checkedCount and totalItems
  34. rowsChecked = checkedCount > 0;
  35. checkSelectAllCheckBoxes(checkedCount == totalItems);
  36. }
  37. /* this file handles check box states and a delete button on the form.
  38. Note that the html page must implement a callback method onRowCheck(checked, tableId)
  39. which gets invoked when check status across all rows changes (i.e. at least
  40. one row is checked or not). */
  41. function selectAllItems(select,from,to)
  42. {
  43. for (var r=from; r<=to; r++)
  44. {
  45. var row = multiSelectTable.rows[r];
  46. var numCells = row.cells.length;
  47. for (var c=0; c<numCells; c++)
  48. {
  49. var cell1 = row.cells[c];
  50. if (cell1.children.length > 0)
  51. {
  52. var o = cell1.children[0];
  53. if (o.tagName=='INPUT' && o.type=='checkbox' &&
  54. (!o.id || o.id.indexOf('selectAll') == -1) && o.checked != select)
  55. {
  56. o.checked=select;
  57. updateChecked(select);
  58. if (onCheckHandler)
  59. onCheckHandler(o);
  60. }
  61. }
  62. if (oneCheckboxPerRow)
  63. break;
  64. }
  65. }
  66. }
  67. function selectAll(select)
  68. {
  69. selectAllItems(select, 1, multiSelectTable.rows.length-1);
  70. checkedCount = select ? totalItems : 0;
  71. if (select != selectAllCheckboxChecked)
  72. checkSelectAllCheckBoxes(select);
  73. rowsChecked = select;
  74. onRowCheck(select, multiSelectTable.id);
  75. }
  76. function checkSelectAllCheckBoxes(check)
  77. {
  78. if(document.forms[0] === null)
  79. return;
  80. selectAllCell = document.getElementById("selectAll1");
  81. if (selectAllCell && selectAllCell.children[0])
  82. selectAllCell.children[0].checked = check;
  83. selectAllCell = document.getElementById("selectAll2");
  84. if (selectAllCell && selectAllCell.children[0])
  85. selectAllCell.children[0].checked = check;
  86. selectAllCheckboxChecked = check;
  87. selectRemoveSuperfile = document.getElementById("removeSuperfile");
  88. if (selectRemoveSuperfile)
  89. {
  90. selectRemoveSuperfile.checked = false; /*default: not delete superfile*/
  91. if (check)
  92. selectRemoveSuperfile.disabled = false; /*enable it only when all files are selected to delete*/
  93. else
  94. selectRemoveSuperfile.disabled = true;
  95. }
  96. }
  97. function clicked(o, event) {
  98. if (singleSelect && o.checked)
  99. {
  100. o.checked = false;
  101. selectAll(false);
  102. o.checked = true;
  103. }
  104. var cell = o.parentNode;
  105. var row = cell.parentNode;
  106. var rowNum = row.rowIndex;
  107. if (!event)
  108. event = window.event;
  109. if (!singleSelect && event && event.shiftKey && lastClicked != -1 && lastClicked != rowNum)
  110. {
  111. rc = lastChecked == o.checked;
  112. if (lastClicked < rowNum)
  113. selectAllItems(lastChecked, lastClicked, rowNum);
  114. else
  115. selectAllItems(lastChecked, rowNum, lastClicked);
  116. lastClicked = -1;
  117. }
  118. else
  119. {
  120. lastClicked = rowNum;
  121. lastChecked = o.checked;
  122. rc = true;
  123. }
  124. if (rc)
  125. {
  126. updateChecked(o.checked);
  127. select = checkedCount == totalItems;
  128. if (select != selectAllCheckboxChecked)
  129. checkSelectAllCheckBoxes(select);
  130. }
  131. else
  132. {
  133. //the window has already checked/unchecked the checkbox which
  134. //should not have been done since shift key was pressed
  135. updateChecked(!o.checked); //compensation
  136. }
  137. select = checkedCount > 0;
  138. if (rowsChecked != select)
  139. rowsChecked = select;
  140. onRowCheck(select, multiSelectTable.id);
  141. return rc;
  142. }
  143. function countItems()
  144. {
  145. if(multiSelectTable == null)
  146. return;
  147. checkedCount = 0;
  148. totalItems = 0;
  149. var numRows = multiSelectTable.rows.length;
  150. for (var r=1; r<numRows; r++) {
  151. var row = multiSelectTable.rows[r];
  152. var numCells = row.cells.length;
  153. for (var c = 0; c < numCells; c++) {
  154. var cell1 = row.cells[c];
  155. if (cell1.children.length > 0) {
  156. var o = cell1.children[0];
  157. if (o.tagName == 'INPUT' && o.type == 'checkbox' && (!o.id || o.id.indexOf('selectAll') == -1)) {
  158. totalItems++;
  159. if (o.checked) {
  160. checkedCount++;
  161. lastClicked = r;
  162. }
  163. }
  164. }
  165. if (oneCheckboxPerRow)
  166. break;
  167. }
  168. }
  169. }
  170. function updateChecked(select)
  171. {
  172. if (select)
  173. checkedCount++;
  174. else
  175. checkedCount--;
  176. }
  177. function isAnyRowChecked()
  178. {
  179. return rowsChecked;
  180. }
  181. function processSelectedItems(callbackFunction)
  182. {
  183. if(multiSelectTable == null)
  184. return;
  185. var checked = 0;
  186. var total = 0;
  187. var numRows = multiSelectTable.rows.length;
  188. for (var r=1; r<numRows && checked < checkedCount; r++)
  189. {
  190. var row = multiSelectTable.rows(r);
  191. var numCells = row.cells.length;
  192. for (var c=0; c<numCells; c++)
  193. {
  194. var cell1 = row.cells[c];
  195. if (cell1.children.length > 0)
  196. {
  197. var o = cell1.children[0];
  198. if (o.tagName=='INPUT' && o.type=='checkbox' && (!o.id || o.id.indexOf('selectAll')== -1) && o.checked)
  199. {
  200. checked++;
  201. if (!callbackFunction(o))
  202. return false;
  203. }
  204. }
  205. if (oneCheckboxPerRow)
  206. break;
  207. }
  208. }
  209. return true;
  210. }