filter.admin.es6.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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.find('#filters-status-wrapper input.form-checkbox').once('filter-status').each(function () {
  18. const $checkbox = $(this);
  19. // Retrieve the tabledrag row belonging to this filter.
  20. const $row = $context.find(`#${$checkbox.attr('id').replace(/-status$/, '-weight')}`).closest('tr');
  21. // Retrieve the vertical tab belonging to this filter.
  22. const $filterSettings = $context.find(`#${$checkbox.attr('id').replace(/-status$/, '-settings')}`);
  23. const filterSettingsTab = $filterSettings.data('verticalTab');
  24. // Bind click handler to this checkbox to conditionally show and hide
  25. // the filter's tableDrag row and vertical tab pane.
  26. $checkbox.on('click.filterUpdate', () => {
  27. if ($checkbox.is(':checked')) {
  28. $row.show();
  29. if (filterSettingsTab) {
  30. filterSettingsTab.tabShow().updateSummary();
  31. }
  32. else {
  33. // On very narrow viewports, Vertical Tabs are disabled.
  34. $filterSettings.show();
  35. }
  36. }
  37. else {
  38. $row.hide();
  39. if (filterSettingsTab) {
  40. filterSettingsTab.tabHide().updateSummary();
  41. }
  42. else {
  43. // On very narrow viewports, Vertical Tabs are disabled.
  44. $filterSettings.hide();
  45. }
  46. }
  47. // Restripe table after toggling visibility of table row.
  48. Drupal.tableDrag['filter-order'].restripeTable();
  49. });
  50. // Attach summary for configurable filters (only for screen readers).
  51. if (filterSettingsTab) {
  52. filterSettingsTab.details.drupalSetSummary(() => ($checkbox.is(':checked') ? Drupal.t('Enabled') : Drupal.t('Disabled')));
  53. }
  54. // Trigger our bound click handler to update elements to initial state.
  55. $checkbox.triggerHandler('click.filterUpdate');
  56. });
  57. },
  58. };
  59. }(jQuery, Drupal));