select.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. function initSelection()
  18. {
  19. totalItems = countItems(document.forms[0], false);
  20. checkedCount = countItems(document.forms[0], true);
  21. rowsChecked = checkedCount > 0;
  22. selectAllCheckboxChecked = checkedCount == totalItems;
  23. checkSelectAllCheckBoxes(selectAllCheckboxChecked);
  24. }
  25. /* this file handles check box states and a delete button on the form.
  26. Note that the html page must implement a callback method onRowCheck(checked)
  27. which gets invoked when check status across all rows changes (i.e. at least
  28. one row is checked or not). */
  29. function selectAllItems(o,select,from,to)
  30. {
  31. if (o.tagName=='INPUT' && o.type=='checkbox')
  32. {
  33. if (o.checked != select && o.onclick && o.onclick.toString().indexOf('clicked(this)') != -1 &&
  34. (!from || !to || o.value>=from && to>=o.value || o.value>=to && from>=o.value))
  35. {
  36. o.checked=select;
  37. updateChecked(select);
  38. }
  39. return;
  40. }
  41. var ch=o.children;
  42. if (ch)
  43. for (var i in ch)
  44. selectAllItems(ch[i],select,from,to);
  45. }
  46. function selectAll(select)
  47. {
  48. selectAllItems(document.forms[0], select);
  49. if (select != selectAllCheckboxChecked)
  50. {
  51. selectAllCheckboxChecked = select;
  52. checkSelectAllCheckBoxes(select);
  53. }
  54. rowsChecked = select;
  55. onRowCheck(select);
  56. }
  57. function checkSelectAllCheckBoxes(check)
  58. {
  59. selectAllCell = document.forms[0].all.selectAll1;
  60. if (selectAllCell)
  61. selectAllCell.children[0].checked = check;
  62. selectAllCell = document.forms[0].all.selectAll2;
  63. if (selectAllCell)
  64. selectAllCell.children[0].checked = check;
  65. }
  66. function clicked(o)
  67. {
  68. if (lastClicked == 'Y')
  69. lastClicked = o.value;
  70. if (window.event.shiftKey)
  71. {
  72. rc = o.checked == true;
  73. selectAllItems(document.forms[0],true,lastClicked,o.value);
  74. }
  75. else if (window.event.altKey)
  76. {
  77. rc = o.checked == false;
  78. selectAllItems(document.forms[0],false,lastClicked,o.value);
  79. }
  80. else
  81. rc = true;
  82. if (rc)
  83. {
  84. updateChecked(o.checked);
  85. select = checkedCount == totalItems;
  86. if (select != selectAllCheckboxChecked)
  87. {
  88. selectAllCheckboxChecked = select;
  89. checkSelectAllCheckBoxes(select);
  90. }
  91. }
  92. else //the window has already checked/unchecked the checkbox which should not have been done since alt/shift key was pressed
  93. updateChecked(!o.checked); //compensation
  94. lastClicked=o.value;
  95. select = checkedCount > 0;
  96. if (rowsChecked != select)
  97. rowsChecked = select;
  98. onRowCheck(select);
  99. return rc;
  100. }
  101. function countItems(o, selectedOnly)
  102. {
  103. if (o.tagName=='INPUT' && o.type=='checkbox' && o.onclick && o.onclick.toString().indexOf('clicked(this)') != -1)
  104. {
  105. if (selectedOnly)
  106. return o.checked ? 1 : 0;
  107. else
  108. return 1;
  109. }
  110. var nItems = 0;
  111. var ch=o.children;
  112. if (ch)
  113. for (var i in ch)
  114. nItems += countItems(ch[i], selectedOnly);
  115. return nItems;
  116. }
  117. function updateChecked(select)
  118. {
  119. if (select)
  120. checkedCount++;
  121. else
  122. checkedCount--;
  123. }
  124. function isAnyRowChecked()
  125. {
  126. return rowsChecked;
  127. }
  128. lastClicked = 'Y';
  129. checkedCount = 0;
  130. rowsChecked = false;
  131. totalItems = -1;
  132. selectAllCheckboxChecked = false;