filter.admin.es6.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. $checkbox.is(':checked')
  58. ? Drupal.t('Enabled')
  59. : Drupal.t('Disabled'),
  60. );
  61. }
  62. // Trigger our bound click handler to update elements to initial state.
  63. $checkbox.triggerHandler('click.filterUpdate');
  64. });
  65. },
  66. };
  67. })(jQuery, Drupal);