system.modules.es6.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /**
  2. * @file
  3. * Module page behaviors.
  4. */
  5. (function ($, Drupal, debounce) {
  6. /**
  7. * Filters the module list table by a text input search string.
  8. *
  9. * Additionally accounts for multiple tables being wrapped in "package" details
  10. * elements.
  11. *
  12. * Text search input: input.table-filter-text
  13. * Target table: input.table-filter-text[data-table]
  14. * Source text: .table-filter-text-source, .module-name, .module-description
  15. *
  16. * @type {Drupal~behavior}
  17. */
  18. Drupal.behaviors.tableFilterByText = {
  19. attach(context, settings) {
  20. const $input = $('input.table-filter-text').once('table-filter-text');
  21. const $table = $($input.attr('data-table'));
  22. let $rowsAndDetails;
  23. let $rows;
  24. let $details;
  25. let searching = false;
  26. function hidePackageDetails(index, element) {
  27. const $packDetails = $(element);
  28. const $visibleRows = $packDetails.find('tbody tr:visible');
  29. $packDetails.toggle($visibleRows.length > 0);
  30. }
  31. function filterModuleList(e) {
  32. const query = $(e.target).val();
  33. // Case insensitive expression to find query at the beginning of a word.
  34. const re = new RegExp(`\\b${query}`, 'i');
  35. function showModuleRow(index, row) {
  36. const $row = $(row);
  37. const $sources = $row.find('.table-filter-text-source, .module-name, .module-description');
  38. const textMatch = $sources.text().search(re) !== -1;
  39. $row.closest('tr').toggle(textMatch);
  40. }
  41. // Search over all rows and packages.
  42. $rowsAndDetails.show();
  43. // Filter if the length of the query is at least 2 characters.
  44. if (query.length >= 2) {
  45. searching = true;
  46. $rows.each(showModuleRow);
  47. // Note that we first open all <details> to be able to use ':visible'.
  48. // Mark the <details> elements that were closed before filtering, so
  49. // they can be reclosed when filtering is removed.
  50. $details.not('[open]').attr('data-drupal-system-state', 'forced-open');
  51. // Hide the package <details> if they don't have any visible rows.
  52. // Note that we first show() all <details> to be able to use ':visible'.
  53. $details.attr('open', true).each(hidePackageDetails);
  54. Drupal.announce(
  55. Drupal.t(
  56. '!modules modules are available in the modified list.',
  57. { '!modules': $rowsAndDetails.find('tbody tr:visible').length },
  58. ),
  59. );
  60. }
  61. else if (searching) {
  62. searching = false;
  63. $rowsAndDetails.show();
  64. // Return <details> elements that had been closed before filtering
  65. // to a closed state.
  66. $details.filter('[data-drupal-system-state="forced-open"]')
  67. .removeAttr('data-drupal-system-state')
  68. .attr('open', false);
  69. }
  70. }
  71. function preventEnterKey(event) {
  72. if (event.which === 13) {
  73. event.preventDefault();
  74. event.stopPropagation();
  75. }
  76. }
  77. if ($table.length) {
  78. $rowsAndDetails = $table.find('tr, details');
  79. $rows = $table.find('tbody tr');
  80. $details = $rowsAndDetails.filter('.package-listing');
  81. $input.on({
  82. keyup: debounce(filterModuleList, 200),
  83. keydown: preventEnterKey,
  84. });
  85. }
  86. },
  87. };
  88. }(jQuery, Drupal, Drupal.debounce));