tableselect.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /**
  2. * @file
  3. * Table select functionality.
  4. */
  5. (function ($, Drupal) {
  6. 'use strict';
  7. /**
  8. * Initialize tableSelects.
  9. *
  10. * @type {Drupal~behavior}
  11. *
  12. * @prop {Drupal~behaviorAttach} attach
  13. * Attaches tableSelect functionality.
  14. */
  15. Drupal.behaviors.tableSelect = {
  16. attach: function (context, settings) {
  17. // Select the inner-most table in case of nested tables.
  18. $(context).find('th.select-all').closest('table').once('table-select').each(Drupal.tableSelect);
  19. }
  20. };
  21. /**
  22. * Callback used in {@link Drupal.behaviors.tableSelect}.
  23. */
  24. Drupal.tableSelect = function () {
  25. // Do not add a "Select all" checkbox if there are no rows with checkboxes
  26. // in the table.
  27. if ($(this).find('td input[type="checkbox"]').length === 0) {
  28. return;
  29. }
  30. // Keep track of the table, which checkbox is checked and alias the
  31. // settings.
  32. var table = this;
  33. var checkboxes;
  34. var lastChecked;
  35. var $table = $(table);
  36. var strings = {
  37. selectAll: Drupal.t('Select all rows in this table'),
  38. selectNone: Drupal.t('Deselect all rows in this table')
  39. };
  40. var updateSelectAll = function (state) {
  41. // Update table's select-all checkbox (and sticky header's if available).
  42. $table.prev('table.sticky-header').addBack().find('th.select-all input[type="checkbox"]').each(function () {
  43. var $checkbox = $(this);
  44. var stateChanged = $checkbox.prop('checked') !== state;
  45. $checkbox.attr('title', state ? strings.selectNone : strings.selectAll);
  46. /**
  47. * @checkbox {HTMLElement}
  48. */
  49. if (stateChanged) {
  50. $checkbox.prop('checked', state).trigger('change');
  51. }
  52. });
  53. };
  54. // Find all <th> with class select-all, and insert the check all checkbox.
  55. $table.find('th.select-all').prepend($('<input type="checkbox" class="form-checkbox" />').attr('title', strings.selectAll)).on('click', function (event) {
  56. if ($(event.target).is('input[type="checkbox"]')) {
  57. // Loop through all checkboxes and set their state to the select all
  58. // checkbox' state.
  59. checkboxes.each(function () {
  60. var $checkbox = $(this);
  61. var stateChanged = $checkbox.prop('checked') !== event.target.checked;
  62. /**
  63. * @checkbox {HTMLElement}
  64. */
  65. if (stateChanged) {
  66. $checkbox.prop('checked', event.target.checked).trigger('change');
  67. }
  68. // Either add or remove the selected class based on the state of the
  69. // check all checkbox.
  70. /**
  71. * @checkbox {HTMLElement}
  72. */
  73. $checkbox.closest('tr').toggleClass('selected', this.checked);
  74. });
  75. // Update the title and the state of the check all box.
  76. updateSelectAll(event.target.checked);
  77. }
  78. });
  79. // For each of the checkboxes within the table that are not disabled.
  80. checkboxes = $table.find('td input[type="checkbox"]:enabled').on('click', function (e) {
  81. // Either add or remove the selected class based on the state of the
  82. // check all checkbox.
  83. /**
  84. * @this {HTMLElement}
  85. */
  86. $(this).closest('tr').toggleClass('selected', this.checked);
  87. // If this is a shift click, we need to highlight everything in the
  88. // range. Also make sure that we are actually checking checkboxes
  89. // over a range and that a checkbox has been checked or unchecked before.
  90. if (e.shiftKey && lastChecked && lastChecked !== e.target) {
  91. // We use the checkbox's parent <tr> to do our range searching.
  92. Drupal.tableSelectRange($(e.target).closest('tr')[0], $(lastChecked).closest('tr')[0], e.target.checked);
  93. }
  94. // If all checkboxes are checked, make sure the select-all one is checked
  95. // too, otherwise keep unchecked.
  96. updateSelectAll((checkboxes.length === checkboxes.filter(':checked').length));
  97. // Keep track of the last checked checkbox.
  98. lastChecked = e.target;
  99. });
  100. // If all checkboxes are checked on page load, make sure the select-all one
  101. // is checked too, otherwise keep unchecked.
  102. updateSelectAll((checkboxes.length === checkboxes.filter(':checked').length));
  103. };
  104. /**
  105. * @param {HTMLElement} from
  106. * The HTML element representing the "from" part of the range.
  107. * @param {HTMLElement} to
  108. * The HTML element representing the "to" part of the range.
  109. * @param {bool} state
  110. * The state to set on the range.
  111. */
  112. Drupal.tableSelectRange = function (from, to, state) {
  113. // We determine the looping mode based on the order of from and to.
  114. var mode = from.rowIndex > to.rowIndex ? 'previousSibling' : 'nextSibling';
  115. // Traverse through the sibling nodes.
  116. for (var i = from[mode]; i; i = i[mode]) {
  117. var $i;
  118. // Make sure that we're only dealing with elements.
  119. if (i.nodeType !== 1) {
  120. continue;
  121. }
  122. $i = $(i);
  123. // Either add or remove the selected class based on the state of the
  124. // target checkbox.
  125. $i.toggleClass('selected', state);
  126. $i.find('input[type="checkbox"]').prop('checked', state);
  127. if (to.nodeType) {
  128. // If we are at the end of the range, stop.
  129. if (i === to) {
  130. break;
  131. }
  132. }
  133. // A faster alternative to doing $(i).filter(to).length.
  134. else if ($.filter(to, [i]).r.length) {
  135. break;
  136. }
  137. }
  138. };
  139. })(jQuery, Drupal);