tableselect.es6.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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.prev('table.sticky-header').addBack().find('th.select-all input[type="checkbox"]').each(function () {
  46. const $checkbox = $(this);
  47. const stateChanged = $checkbox.prop('checked') !== state;
  48. $checkbox.attr('title', state ? strings.selectNone : strings.selectAll);
  49. /**
  50. * @checkbox {HTMLElement}
  51. */
  52. if (stateChanged) {
  53. $checkbox.prop('checked', state).trigger('change');
  54. }
  55. });
  56. };
  57. // Find all <th> with class select-all, and insert the check all checkbox.
  58. $table.find('th.select-all').prepend($('<input type="checkbox" class="form-checkbox" />').attr('title', strings.selectAll)).on('click', (event) => {
  59. if ($(event.target).is('input[type="checkbox"]')) {
  60. // Loop through all checkboxes and set their state to the select all
  61. // checkbox' state.
  62. checkboxes.each(function () {
  63. const $checkbox = $(this);
  64. const stateChanged = $checkbox.prop('checked') !== event.target.checked;
  65. /**
  66. * @checkbox {HTMLElement}
  67. */
  68. if (stateChanged) {
  69. $checkbox.prop('checked', event.target.checked).trigger('change');
  70. }
  71. // Either add or remove the selected class based on the state of the
  72. // check all checkbox.
  73. /**
  74. * @checkbox {HTMLElement}
  75. */
  76. $checkbox.closest('tr').toggleClass('selected', this.checked);
  77. });
  78. // Update the title and the state of the check all box.
  79. updateSelectAll(event.target.checked);
  80. }
  81. });
  82. // For each of the checkboxes within the table that are not disabled.
  83. checkboxes = $table.find('td input[type="checkbox"]:enabled').on('click', function (e) {
  84. // Either add or remove the selected class based on the state of the
  85. // check all checkbox.
  86. /**
  87. * @this {HTMLElement}
  88. */
  89. $(this).closest('tr').toggleClass('selected', this.checked);
  90. // If this is a shift click, we need to highlight everything in the
  91. // range. Also make sure that we are actually checking checkboxes
  92. // over a range and that a checkbox has been checked or unchecked before.
  93. if (e.shiftKey && lastChecked && lastChecked !== e.target) {
  94. // We use the checkbox's parent <tr> to do our range searching.
  95. Drupal.tableSelectRange($(e.target).closest('tr')[0], $(lastChecked).closest('tr')[0], e.target.checked);
  96. }
  97. // If all checkboxes are checked, make sure the select-all one is checked
  98. // too, otherwise keep unchecked.
  99. updateSelectAll((checkboxes.length === checkboxes.filter(':checked').length));
  100. // Keep track of the last checked checkbox.
  101. lastChecked = e.target;
  102. });
  103. // If all checkboxes are checked on page load, make sure the select-all one
  104. // is checked too, otherwise keep unchecked.
  105. updateSelectAll((checkboxes.length === checkboxes.filter(':checked').length));
  106. };
  107. /**
  108. * @param {HTMLElement} from
  109. * The HTML element representing the "from" part of the range.
  110. * @param {HTMLElement} to
  111. * The HTML element representing the "to" part of the range.
  112. * @param {bool} state
  113. * The state to set on the range.
  114. */
  115. Drupal.tableSelectRange = function (from, to, state) {
  116. // We determine the looping mode based on the order of from and to.
  117. const mode = from.rowIndex > to.rowIndex ? 'previousSibling' : 'nextSibling';
  118. // Traverse through the sibling nodes.
  119. for (let i = from[mode]; i; i = i[mode]) {
  120. const $i = $(i);
  121. // Make sure that we're only dealing with elements.
  122. if (i.nodeType !== 1) {
  123. continue;
  124. }
  125. // Either add or remove the selected class based on the state of the
  126. // target checkbox.
  127. $i.toggleClass('selected', state);
  128. $i.find('input[type="checkbox"]').prop('checked', state);
  129. if (to.nodeType) {
  130. // If we are at the end of the range, stop.
  131. if (i === to) {
  132. break;
  133. }
  134. }
  135. // A faster alternative to doing $(i).filter(to).length.
  136. else if ($.filter(to, [i]).r.length) {
  137. break;
  138. }
  139. }
  140. };
  141. }(jQuery, Drupal));