tableselect.es6.js 5.3 KB

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