Drupal investigation

system.modules.js 3.4KB

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