multiselect.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*##############################################################################
  2. # Copyright (C) 2011 HPCC Systems.
  3. #
  4. # All rights reserved. This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as
  6. # published by the Free Software Foundation, either version 3 of the
  7. # License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. ############################################################################## */
  17. lastClicked = -1;
  18. lastChecked = false;
  19. checkedCount = 0;
  20. rowsChecked = false;
  21. totalItems = -1;
  22. selectAllCheckboxChecked = false;
  23. multiSelectTable = null;
  24. oneCheckboxPerRow = true;
  25. onCheckHandler = null;
  26. singleSelect = false;
  27. function initSelection(tableId, multipleCheckboxesPerRow, onCheck)
  28. {
  29. if (multipleCheckboxesPerRow)
  30. oneCheckboxPerRow = false;
  31. if (onCheck)
  32. onCheckHandler = onCheck;
  33. multiSelectTable = document.getElementById(tableId);
  34. countItems();//updates checkedCount and totalItems
  35. rowsChecked = checkedCount > 0;
  36. checkSelectAllCheckBoxes(checkedCount == totalItems);
  37. }
  38. /* this file handles check box states and a delete button on the form.
  39. Note that the html page must implement a callback method onRowCheck(checked, tableId)
  40. which gets invoked when check status across all rows changes (i.e. at least
  41. one row is checked or not). */
  42. function selectAllItems(select,from,to)
  43. {
  44. for (var r=from; r<=to; r++)
  45. {
  46. var row = multiSelectTable.rows[r];
  47. var numCells = row.cells.length;
  48. for (var c=0; c<numCells; c++)
  49. {
  50. var cell1 = row.cells[c];
  51. if (cell1.children.length > 0)
  52. {
  53. var o = cell1.children[0];
  54. if (o.tagName=='INPUT' && o.type=='checkbox' &&
  55. (!o.id || o.id.indexOf('selectAll') == -1) && o.checked != select)
  56. {
  57. o.checked=select;
  58. updateChecked(select);
  59. if (onCheckHandler)
  60. onCheckHandler(o);
  61. }
  62. }
  63. if (oneCheckboxPerRow)
  64. break;
  65. }
  66. }
  67. }
  68. function selectAll(select)
  69. {
  70. selectAllItems(select, 1, multiSelectTable.rows.length-1);
  71. checkedCount = select ? totalItems : 0;
  72. if (select != selectAllCheckboxChecked)
  73. checkSelectAllCheckBoxes(select);
  74. rowsChecked = select;
  75. onRowCheck(select, multiSelectTable.id);
  76. }
  77. function checkSelectAllCheckBoxes(check)
  78. {
  79. if(document.forms[0] != null)
  80. {
  81. //var all = document.forms[0].all;
  82. //if (all) {
  83. selectAllCell = document.getElementById("selectAll1");
  84. if (selectAllCell && selectAllCell.children[0])
  85. selectAllCell.children[0].checked = check;
  86. selectAllCell = document.getElementById("selectAll2");
  87. if (selectAllCell)
  88. selectAllCell.children[0].checked = check;
  89. selectAllCheckboxChecked = check;
  90. //}
  91. }
  92. }
  93. function clicked(o, event) {
  94. if (singleSelect && o.checked)
  95. {
  96. o.checked = false;
  97. selectAll(false);
  98. o.checked = true;
  99. }
  100. var cell = o.parentNode;
  101. var row = cell.parentNode;
  102. var rowNum = row.rowIndex;
  103. if (!event)
  104. event = window.event;
  105. if (!singleSelect && event && event.shiftKey && lastClicked != -1 && lastClicked != rowNum)
  106. {
  107. rc = lastChecked == o.checked;
  108. if (lastClicked < rowNum)
  109. selectAllItems(lastChecked, lastClicked, rowNum);
  110. else
  111. selectAllItems(lastChecked, rowNum, lastClicked);
  112. lastClicked = -1;
  113. }
  114. else
  115. {
  116. lastClicked = rowNum;
  117. lastChecked = o.checked;
  118. rc = true;
  119. }
  120. if (rc)
  121. {
  122. updateChecked(o.checked);
  123. select = checkedCount == totalItems;
  124. if (select != selectAllCheckboxChecked)
  125. checkSelectAllCheckBoxes(select);
  126. }
  127. else
  128. {
  129. //the window has already checked/unchecked the checkbox which
  130. //should not have been done since shift key was pressed
  131. updateChecked(!o.checked); //compensation
  132. }
  133. select = checkedCount > 0;
  134. if (rowsChecked != select)
  135. rowsChecked = select;
  136. onRowCheck(select, multiSelectTable.id);
  137. return rc;
  138. }
  139. function countItems()
  140. {
  141. if(multiSelectTable == null)
  142. return;
  143. checkedCount = 0;
  144. totalItems = 0;
  145. var numRows = multiSelectTable.rows.length;
  146. for (var r=1; r<numRows; r++) {
  147. var row = multiSelectTable.rows[r];
  148. var numCells = row.cells.length;
  149. for (var c = 0; c < numCells; c++) {
  150. var cell1 = row.cells[c];
  151. if (cell1.children.length > 0) {
  152. var o = cell1.children[0];
  153. if (o.tagName == 'INPUT' && o.type == 'checkbox' && (!o.id || o.id.indexOf('selectAll') == -1)) {
  154. totalItems++;
  155. if (o.checked) {
  156. checkedCount++;
  157. lastClicked = r;
  158. }
  159. }
  160. }
  161. if (oneCheckboxPerRow)
  162. break;
  163. }
  164. }
  165. }
  166. function updateChecked(select)
  167. {
  168. if (select)
  169. checkedCount++;
  170. else
  171. checkedCount--;
  172. }
  173. function isAnyRowChecked()
  174. {
  175. return rowsChecked;
  176. }
  177. function processSelectedItems(callbackFunction)
  178. {
  179. if(multiSelectTable == null)
  180. return;
  181. var checked = 0;
  182. var total = 0;
  183. var numRows = multiSelectTable.rows.length;
  184. for (var r=1; r<numRows && checked < checkedCount; r++)
  185. {
  186. var row = multiSelectTable.rows(r);
  187. var numCells = row.cells.length;
  188. for (var c=0; c<numCells; c++)
  189. {
  190. var cell1 = row.cells[c];
  191. if (cell1.children.length > 0)
  192. {
  193. var o = cell1.children[0];
  194. if (o.tagName=='INPUT' && o.type=='checkbox' && (!o.id || o.id.indexOf('selectAll')== -1) && o.checked)
  195. {
  196. checked++;
  197. if (!callbackFunction(o))
  198. return false;
  199. }
  200. }
  201. if (oneCheckboxPerRow)
  202. break;
  203. }
  204. }
  205. return true;
  206. }