filter.admin.es6.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /**
  2. * @file
  3. * Attaches administration-specific behavior for the Filter module.
  4. */
  5. (function($, Drupal) {
  6. /**
  7. * Displays and updates the status of filters on the admin page.
  8. *
  9. * @type {Drupal~behavior}
  10. *
  11. * @prop {Drupal~behaviorAttach} attach
  12. * Attaches behaviors to the filter admin view.
  13. */
  14. Drupal.behaviors.filterStatus = {
  15. attach(context, settings) {
  16. const $context = $(context);
  17. $context
  18. .find('#filters-status-wrapper input.form-checkbox')
  19. .once('filter-status')
  20. .each(function() {
  21. const $checkbox = $(this);
  22. // Retrieve the tabledrag row belonging to this filter.
  23. const $row = $context
  24. .find(`#${$checkbox.attr('id').replace(/-status$/, '-weight')}`)
  25. .closest('tr');
  26. // Retrieve the vertical tab belonging to this filter.
  27. const $filterSettings = $context.find(
  28. `#${$checkbox.attr('id').replace(/-status$/, '-settings')}`,
  29. );
  30. const filterSettingsTab = $filterSettings.data('verticalTab');
  31. // Bind click handler to this checkbox to conditionally show and hide
  32. // the filter's tableDrag row and vertical tab pane.
  33. $checkbox.on('click.filterUpdate', () => {
  34. if ($checkbox.is(':checked')) {
  35. $row.show();
  36. if (filterSettingsTab) {
  37. filterSettingsTab.tabShow().updateSummary();
  38. } else {
  39. // On very narrow viewports, Vertical Tabs are disabled.
  40. $filterSettings.show();
  41. }
  42. } else {
  43. $row.hide();
  44. if (filterSettingsTab) {
  45. filterSettingsTab.tabHide().updateSummary();
  46. } else {
  47. // On very narrow viewports, Vertical Tabs are disabled.
  48. $filterSettings.hide();
  49. }
  50. }
  51. // Restripe table after toggling visibility of table row.
  52. Drupal.tableDrag['filter-order'].restripeTable();
  53. });
  54. // Attach summary for configurable filters (only for screen readers).
  55. if (filterSettingsTab) {
  56. filterSettingsTab.details.drupalSetSummary(
  57. () =>
  58. $checkbox.is(':checked')
  59. ? Drupal.t('Enabled')
  60. : Drupal.t('Disabled'),
  61. );
  62. }
  63. // Trigger our bound click handler to update elements to initial state.
  64. $checkbox.triggerHandler('click.filterUpdate');
  65. });
  66. },
  67. };
  68. })(jQuery, Drupal);