3 * Table select functionality.
6 (function ($, Drupal) {
8 * Initialize tableSelects.
10 * @type {Drupal~behavior}
12 * @prop {Drupal~behaviorAttach} attach
13 * Attaches tableSelect functionality.
15 Drupal.behaviors.tableSelect = {
16 attach(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);
23 * Callback used in {@link Drupal.behaviors.tableSelect}.
25 Drupal.tableSelect = function () {
26 // Do not add a "Select all" checkbox if there are no rows with checkboxes
28 if ($(this).find('td input[type="checkbox"]').length === 0) {
32 // Keep track of the table, which checkbox is checked and alias the
37 const $table = $(table);
39 selectAll: Drupal.t('Select all rows in this table'),
40 selectNone: Drupal.t('Deselect all rows in this table'),
42 const updateSelectAll = function (state) {
43 // Update table's select-all checkbox (and sticky header's if available).
44 $table.prev('table.sticky-header').addBack().find('th.select-all input[type="checkbox"]').each(function () {
45 const $checkbox = $(this);
46 const stateChanged = $checkbox.prop('checked') !== state;
48 $checkbox.attr('title', state ? strings.selectNone : strings.selectAll);
51 * @checkbox {HTMLElement}
54 $checkbox.prop('checked', state).trigger('change');
59 // Find all <th> with class select-all, and insert the check all checkbox.
60 $table.find('th.select-all').prepend($('<input type="checkbox" class="form-checkbox" />').attr('title', strings.selectAll)).on('click', (event) => {
61 if ($(event.target).is('input[type="checkbox"]')) {
62 // Loop through all checkboxes and set their state to the select all
64 checkboxes.each(function () {
65 const $checkbox = $(this);
66 const stateChanged = $checkbox.prop('checked') !== event.target.checked;
69 * @checkbox {HTMLElement}
72 $checkbox.prop('checked', event.target.checked).trigger('change');
74 // Either add or remove the selected class based on the state of the
75 // check all checkbox.
78 * @checkbox {HTMLElement}
80 $checkbox.closest('tr').toggleClass('selected', this.checked);
82 // Update the title and the state of the check all box.
83 updateSelectAll(event.target.checked);
87 // For each of the checkboxes within the table that are not disabled.
88 checkboxes = $table.find('td input[type="checkbox"]:enabled').on('click', function (e) {
89 // Either add or remove the selected class based on the state of the
90 // check all checkbox.
95 $(this).closest('tr').toggleClass('selected', this.checked);
97 // If this is a shift click, we need to highlight everything in the
98 // range. Also make sure that we are actually checking checkboxes
99 // over a range and that a checkbox has been checked or unchecked before.
100 if (e.shiftKey && lastChecked && lastChecked !== e.target) {
101 // We use the checkbox's parent <tr> to do our range searching.
102 Drupal.tableSelectRange($(e.target).closest('tr')[0], $(lastChecked).closest('tr')[0], e.target.checked);
105 // If all checkboxes are checked, make sure the select-all one is checked
106 // too, otherwise keep unchecked.
107 updateSelectAll((checkboxes.length === checkboxes.filter(':checked').length));
109 // Keep track of the last checked checkbox.
110 lastChecked = e.target;
113 // If all checkboxes are checked on page load, make sure the select-all one
114 // is checked too, otherwise keep unchecked.
115 updateSelectAll((checkboxes.length === checkboxes.filter(':checked').length));
119 * @param {HTMLElement} from
120 * The HTML element representing the "from" part of the range.
121 * @param {HTMLElement} to
122 * The HTML element representing the "to" part of the range.
123 * @param {bool} state
124 * The state to set on the range.
126 Drupal.tableSelectRange = function (from, to, state) {
127 // We determine the looping mode based on the order of from and to.
128 const mode = from.rowIndex > to.rowIndex ? 'previousSibling' : 'nextSibling';
130 // Traverse through the sibling nodes.
131 for (let i = from[mode]; i; i = i[mode]) {
133 // Make sure that we're only dealing with elements.
134 if (i.nodeType !== 1) {
138 // Either add or remove the selected class based on the state of the
140 $i.toggleClass('selected', state);
141 $i.find('input[type="checkbox"]').prop('checked', state);
144 // If we are at the end of the range, stop.
149 // A faster alternative to doing $(i).filter(to).length.
150 else if ($.filter(to, [i]).r.length) {