tableselect.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. (function ($) {
  2. Drupal.behaviors.tableSelect = {
  3. attach: function (context, settings) {
  4. // Select the inner-most table in case of nested tables.
  5. $('th.select-all', context).closest('table').once('table-select', Drupal.tableSelect);
  6. }
  7. };
  8. Drupal.tableSelect = function () {
  9. // Do not add a "Select all" checkbox if there are no rows with checkboxes in the table
  10. if ($('td input:checkbox', this).length == 0) {
  11. return;
  12. }
  13. // Keep track of the table, which checkbox is checked and alias the settings.
  14. var table = this, checkboxes, lastChecked;
  15. var strings = { 'selectAll': Drupal.t('Select all rows in this table'), 'selectNone': Drupal.t('Deselect all rows in this table') };
  16. var updateSelectAll = function (state) {
  17. // Update table's select-all checkbox (and sticky header's if available).
  18. $(table).prev('table.sticky-header').andSelf().find('th.select-all input:checkbox').each(function() {
  19. $(this).attr('title', state ? strings.selectNone : strings.selectAll);
  20. this.checked = state;
  21. });
  22. };
  23. // Find all <th> with class select-all, and insert the check all checkbox.
  24. $('th.select-all', table).prepend($('<input type="checkbox" class="form-checkbox" />').attr('title', strings.selectAll)).click(function (event) {
  25. if ($(event.target).is('input:checkbox')) {
  26. // Loop through all checkboxes and set their state to the select all checkbox' state.
  27. checkboxes.each(function () {
  28. this.checked = event.target.checked;
  29. // Either add or remove the selected class based on the state of the check all checkbox.
  30. $(this).closest('tr').toggleClass('selected', this.checked);
  31. });
  32. // Update the title and the state of the check all box.
  33. updateSelectAll(event.target.checked);
  34. }
  35. });
  36. // For each of the checkboxes within the table that are not disabled.
  37. checkboxes = $('td input:checkbox:enabled', table).click(function (e) {
  38. // Either add or remove the selected class based on the state of the check all checkbox.
  39. $(this).closest('tr').toggleClass('selected', this.checked);
  40. // If this is a shift click, we need to highlight everything in the range.
  41. // Also make sure that we are actually checking checkboxes over a range and
  42. // that a checkbox has been checked or unchecked before.
  43. if (e.shiftKey && lastChecked && lastChecked != e.target) {
  44. // We use the checkbox's parent TR to do our range searching.
  45. Drupal.tableSelectRange($(e.target).closest('tr')[0], $(lastChecked).closest('tr')[0], e.target.checked);
  46. }
  47. // If all checkboxes are checked, make sure the select-all one is checked too, otherwise keep unchecked.
  48. updateSelectAll((checkboxes.length == $(checkboxes).filter(':checked').length));
  49. // Keep track of the last checked checkbox.
  50. lastChecked = e.target;
  51. });
  52. };
  53. Drupal.tableSelectRange = function (from, to, state) {
  54. // We determine the looping mode based on the the order of from and to.
  55. var mode = from.rowIndex > to.rowIndex ? 'previousSibling' : 'nextSibling';
  56. // Traverse through the sibling nodes.
  57. for (var i = from[mode]; i; i = i[mode]) {
  58. // Make sure that we're only dealing with elements.
  59. if (i.nodeType != 1) {
  60. continue;
  61. }
  62. // Either add or remove the selected class based on the state of the target checkbox.
  63. $(i).toggleClass('selected', state);
  64. $('input:checkbox', i).each(function () {
  65. this.checked = state;
  66. });
  67. if (to.nodeType) {
  68. // If we are at the end of the range, stop.
  69. if (i == to) {
  70. break;
  71. }
  72. }
  73. // A faster alternative to doing $(i).filter(to).length.
  74. else if ($.filter(to, [i]).r.length) {
  75. break;
  76. }
  77. }
  78. };
  79. })(jQuery);