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