tableselect.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * DO NOT EDIT THIS FILE.
  3. * See the following change record for more information,
  4. * https://www.drupal.org/node/2815083
  5. * @preserve
  6. **/
  7. (function ($, Drupal) {
  8. Drupal.behaviors.tableSelect = {
  9. attach: function attach(context, settings) {
  10. $(context).find('th.select-all').closest('table').once('table-select').each(Drupal.tableSelect);
  11. }
  12. };
  13. Drupal.tableSelect = function () {
  14. if ($(this).find('td input[type="checkbox"]').length === 0) {
  15. return;
  16. }
  17. var table = this;
  18. var checkboxes = void 0;
  19. var lastChecked = void 0;
  20. var $table = $(table);
  21. var strings = {
  22. selectAll: Drupal.t('Select all rows in this table'),
  23. selectNone: Drupal.t('Deselect all rows in this table')
  24. };
  25. var updateSelectAll = function updateSelectAll(state) {
  26. $table.prev('table.sticky-header').addBack().find('th.select-all input[type="checkbox"]').each(function () {
  27. var $checkbox = $(this);
  28. var stateChanged = $checkbox.prop('checked') !== state;
  29. $checkbox.attr('title', state ? strings.selectNone : strings.selectAll);
  30. if (stateChanged) {
  31. $checkbox.prop('checked', state).trigger('change');
  32. }
  33. });
  34. };
  35. $table.find('th.select-all').prepend($('<input type="checkbox" class="form-checkbox" />').attr('title', strings.selectAll)).on('click', function (event) {
  36. if ($(event.target).is('input[type="checkbox"]')) {
  37. checkboxes.each(function () {
  38. var $checkbox = $(this);
  39. var stateChanged = $checkbox.prop('checked') !== event.target.checked;
  40. if (stateChanged) {
  41. $checkbox.prop('checked', event.target.checked).trigger('change');
  42. }
  43. $checkbox.closest('tr').toggleClass('selected', this.checked);
  44. });
  45. updateSelectAll(event.target.checked);
  46. }
  47. });
  48. checkboxes = $table.find('td input[type="checkbox"]:enabled').on('click', function (e) {
  49. $(this).closest('tr').toggleClass('selected', this.checked);
  50. if (e.shiftKey && lastChecked && lastChecked !== e.target) {
  51. Drupal.tableSelectRange($(e.target).closest('tr')[0], $(lastChecked).closest('tr')[0], e.target.checked);
  52. }
  53. updateSelectAll(checkboxes.length === checkboxes.filter(':checked').length);
  54. lastChecked = e.target;
  55. });
  56. updateSelectAll(checkboxes.length === checkboxes.filter(':checked').length);
  57. };
  58. Drupal.tableSelectRange = function (from, to, state) {
  59. var mode = from.rowIndex > to.rowIndex ? 'previousSibling' : 'nextSibling';
  60. for (var i = from[mode]; i; i = i[mode]) {
  61. var $i;
  62. if (i.nodeType !== 1) {
  63. continue;
  64. }
  65. $i = $(i);
  66. $i.toggleClass('selected', state);
  67. $i.find('input[type="checkbox"]').prop('checked', state);
  68. if (to.nodeType) {
  69. if (i === to) {
  70. break;
  71. }
  72. } else if ($.filter(to, [i]).r.length) {
  73. break;
  74. }
  75. }
  76. };
  77. })(jQuery, Drupal);