tableselect.es6.js 5.6 KB

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