3 * Table select functionality.
6 (function ($, Drupal) {
11 * Initialize tableSelects.
13 * @type {Drupal~behavior}
15 * @prop {Drupal~behaviorAttach} attach
16 * Attaches tableSelect functionality.
18 Drupal.behaviors.tableSelect = {
19 attach: function (context, settings) {
20 // Select the inner-most table in case of nested tables.
21 $(context).find('th.select-all').closest('table').once('table-select').each(Drupal.tableSelect);
26 * Callback used in {@link Drupal.behaviors.tableSelect}.
28 Drupal.tableSelect = function () {
29 // Do not add a "Select all" checkbox if there are no rows with checkboxes
31 if ($(this).find('td input[type="checkbox"]').length === 0) {
35 // Keep track of the table, which checkbox is checked and alias the
40 var $table = $(table);
42 selectAll: Drupal.t('Select all rows in this table'),
43 selectNone: Drupal.t('Deselect all rows in this table')
45 var updateSelectAll = function (state) {
46 // Update table's select-all checkbox (and sticky header's if available).
47 $table.prev('table.sticky-header').addBack().find('th.select-all input[type="checkbox"]').each(function () {
48 var $checkbox = $(this);
49 var stateChanged = $checkbox.prop('checked') !== state;
51 $checkbox.attr('title', state ? strings.selectNone : strings.selectAll);
54 * @checkbox {HTMLElement}
57 $checkbox.prop('checked', state).trigger('change');
62 // Find all <th> with class select-all, and insert the check all checkbox.
63 $table.find('th.select-all').prepend($('<input type="checkbox" class="form-checkbox" />').attr('title', strings.selectAll)).on('click', function (event) {
64 if ($(event.target).is('input[type="checkbox"]')) {
65 // Loop through all checkboxes and set their state to the select all
67 checkboxes.each(function () {
68 var $checkbox = $(this);
69 var stateChanged = $checkbox.prop('checked') !== event.target.checked;
72 * @checkbox {HTMLElement}
75 $checkbox.prop('checked', event.target.checked).trigger('change');
77 // Either add or remove the selected class based on the state of the
78 // check all checkbox.
81 * @checkbox {HTMLElement}
83 $checkbox.closest('tr').toggleClass('selected', this.checked);
85 // Update the title and the state of the check all box.
86 updateSelectAll(event.target.checked);
90 // For each of the checkboxes within the table that are not disabled.
91 checkboxes = $table.find('td input[type="checkbox"]:enabled').on('click', function (e) {
92 // Either add or remove the selected class based on the state of the
93 // check all checkbox.
98 $(this).closest('tr').toggleClass('selected', this.checked);
100 // If this is a shift click, we need to highlight everything in the
101 // range. Also make sure that we are actually checking checkboxes
102 // over a range and that a checkbox has been checked or unchecked before.
103 if (e.shiftKey && lastChecked && lastChecked !== e.target) {
104 // We use the checkbox's parent <tr> to do our range searching.
105 Drupal.tableSelectRange($(e.target).closest('tr')[0], $(lastChecked).closest('tr')[0], e.target.checked);
108 // If all checkboxes are checked, make sure the select-all one is checked
109 // too, otherwise keep unchecked.
110 updateSelectAll((checkboxes.length === checkboxes.filter(':checked').length));
112 // Keep track of the last checked checkbox.
113 lastChecked = e.target;
116 // If all checkboxes are checked on page load, make sure the select-all one
117 // is checked too, otherwise keep unchecked.
118 updateSelectAll((checkboxes.length === checkboxes.filter(':checked').length));
122 * @param {HTMLElement} from
123 * The HTML element representing the "from" part of the range.
124 * @param {HTMLElement} to
125 * The HTML element representing the "to" part of the range.
126 * @param {bool} state
127 * The state to set on the range.
129 Drupal.tableSelectRange = function (from, to, state) {
130 // We determine the looping mode based on the order of from and to.
131 var mode = from.rowIndex > to.rowIndex ? 'previousSibling' : 'nextSibling';
133 // Traverse through the sibling nodes.
134 for (var i = from[mode]; i; i = i[mode]) {
136 // Make sure that we're only dealing with elements.
137 if (i.nodeType !== 1) {
141 // Either add or remove the selected class based on the state of the
143 $i.toggleClass('selected', state);
144 $i.find('input[type="checkbox"]').prop('checked', state);
147 // If we are at the end of the range, stop.
152 // A faster alternative to doing $(i).filter(to).length.
153 else if ($.filter(to, [i]).r.length) {