PageSourceSearch

https://thephotographersgallery.org.uk/modules/contrib/better_exposed_filters/js/bef_select_all_none.js?v=4.x

js thephotographersgallery.org.uk collected 2026-09-24 09:02:26 UTC 5,246 bytes, 134 lines download raw bytes

1/**
2 * @file
3 * bef_select_all_none.js
4 *
5 * Adds select all/none toggle functionality to an exposed filter.
6 */
7
8(function ($, once) {
9  Drupal.behaviors.betterExposedFiltersSelectAllNone = {
10    attach: function (context) {
11      /*
12       * Add Select all/none links to specified checkboxes
13       */
14      var selected = $('.form-checkboxes.bef-select-all-none:not(.bef-processed)');
15      if (selected.length) {
16        var selAll = Drupal.t('Select All');
17        var selNone = Drupal.t('Select None');
18
19        // Set up a prototype link and event handlers.
20        var link = $('<a class="bef-toggle" href="#">' + selAll + '</a>')
21        link.click(function (event) {
22          // Don't actually follow the link...
23          event.preventDefault();
24          event.stopPropagation();
25
26          if (selAll == $(this).text()) {
27            // Select all the checkboxes.
28            $(this)
29              .html(selNone)
30              .siblings('.bef-select-all-none, .bef-tree')
31              .find('input:checkbox').each(function () {
32                $(this).prop('checked', true);
33                // @TODO:
34                // _bef_highlight(this, context);
35              })
36              .end()
37
38              // attr() doesn't trigger a change event, so we do it ourselves. But just on
39              // one checkbox otherwise we have many spinning cursors.
40              .find('input[type=checkbox]:first').change();
41          }
42          else {
43            // Unselect all the checkboxes.
44            $(this)
45              .html(selAll)
46              .siblings('.bef-select-all-none, .bef-tree')
47              .find('input:checkbox').each(function () {
48                $(this).prop('checked', false);
49                // @TODO:
50                // _bef_highlight(this, context);
51              })
52              .end()
53
54              // attr() doesn't trigger a change event, so we do it ourselves. But just on
55              // one checkbox otherwise we have many spinning cursors.
56              .find('input[type=checkbox]:first').change();
57          }
58        });
59
60        // Add link to the page for each set of checkboxes.
61        selected
62          .addClass('bef-processed')
63          .each(function (index) {
64            // Clone the link prototype and insert into the DOM.
65            var newLink = link.clone(true);
66
67            newLink.insertBefore($(this));
68
69            // If all checkboxes are already checked by default then switch to Select None.
70            if ($('input:checkbox:checked', this).length == $('input:checkbox', this).length) {
71              newLink.text(selNone);
72            }
73          });
74      }
75
76      // @TODO:
77      // Add highlight class to checked checkboxes for better theming
78      // $('.bef-tree input[type="checkbox"], .bef-checkboxes input[type="checkbox"]')
79      // Highlight newly selected checkboxes
80      //  .change(function () {
81      //    _bef_highlight(this, context);
82      //  })
83      //  .filter(':checked').closest('.form-item', context).addClass('highlight')
84      // ;
85      // @TODO: Put this somewhere else...
86      // Check for and initialize datepickers
87      // if (Drupal.settings.better_exposed_filters.datepicker) {
88      //  // Note: JavaScript does not treat "" as null
89      //  if (Drupal.settings.better_exposed_filters.datepicker_options.dateformat) {
90      //    $('.bef-datepicker').datepicker({
91      //      dateFormat: Drupal.settings.better_exposed_filters.datepicker_options.dateformat
92      //    });
93      //  }
94      //  else {
95      //    $('.bef-datepicker').datepicker();
96      //  }
97      // }
98    }                   // attach: function() {
99  };                    // Drupal.behaviors.better_exposed_filters = {.
100
101  Drupal.behaviors.betterExposedFiltersAllNoneNested = {
102    attach:function (context, settings) {
103      $(once('bef-all-none-nested', '.bef-select-all-none-nested ul li')).each(function () {
104        var $this = $(this);
105        // Check/uncheck child terms along with their parent.
106        $this.find('input:checkbox:first').change(function () {
107          $(this).closest('li').find('ul li input:checkbox').prop('checked', this.checked);
108        });
109
110        // When a child term is checked or unchecked, set the parent term's
111        // status as needed.
112        $this.find('ul input:checkbox').change(function () {
113          // Determine the number of unchecked sibling checkboxes.
114          var $this = $(this);
115          var uncheckedSiblings = $this.closest('li').siblings('li').find('> div > input:checkbox:not(:checked)').length;
116
117          // If this term or any siblings are unchecked, uncheck the parent and
118          // all ancestors.
119          if (uncheckedSiblings || !this.checked) {
120            $this.parents('ul').siblings('div').find('input:checkbox').prop('checked', false);
121          }
122
123          // If this and all sibling terms are checked, check the parent. Then
124          // trigger the parent's change event to see if that change affects the
125          // grandparent's checked state.
126          if (this.checked && !uncheckedSiblings) {
127            $(this).closest('ul').closest('li').find('input:checkbox:first').prop('checked', true).change();
128          }
129        });
130      });
131    }
132  }
133
134})(jQuery, once);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.