select.js 4.0 KB

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