tableselect.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 = $(i);
  62. if (i.nodeType !== 1) {
  63. continue;
  64. }
  65. $i.toggleClass('selected', state);
  66. $i.find('input[type="checkbox"]').prop('checked', state);
  67. if (to.nodeType) {
  68. if (i === to) {
  69. break;
  70. }
  71. } else if ($.filter(to, [i]).r.length) {
  72. break;
  73. }
  74. }
  75. };
  76. })(jQuery, Drupal);