PageSourceSearch

https://www.fhf.fr/modules/contrib/better_exposed_filters/js/auto_submit.js?v=6.x

js fhf.fr collected 2026-09-24 09:25:48 UTC 4,413 bytes, 123 lines download raw bytes

1/**
2 * @file
3 * auto_submit.js
4 *
5 * Provides a "form auto-submit" feature for the Better Exposed Filters module.
6 */
7
8(function ($, Drupal, once) {
9
10  /**
11   * To make a form auto submit, all you have to do is 3 things:.
12   *
13   * Use the "better_exposed_filters/auto_submit" js library.
14   *
15   * On gadgets you want to auto-submit when changed, add the
16   * data-bef-auto-submit attribute. With FAPI, add:
17   * @code
18   *  '#attributes' => array('data-bef-auto-submit' => ''),
19   * @endcode
20   *
21   * If you want to have auto-submit for every form element, add the
22   * data-bef-auto-submit-full-form to the form. With FAPI, add:
23   * @code
24   *   '#attributes' => array('data-bef-auto-submit-full-form' => ''),
25   * @endcode
26   *
27   * If you want to exclude a field from the bef-auto-submit-full-form auto
28   * submission, add an attribute of data-bef-auto-submit-exclude to the form
29   * element. With FAPI, add:
30   * @code
31   *   '#attributes' => array('data-bef-auto-submit-exclude' => ''),
32   * @endcode
33   *
34   * Finally, you have to identify which button you want clicked for autosubmit.
35   * The behavior of this button will be honored if it's ajax or not:
36   * @code
37   *  '#attributes' => array('data-bef-auto-submit-click' => ''),
38   * @endcode
39   *
40   * Currently only 'select', 'radio', 'checkbox' and 'textfield' types are
41   * supported. We probably could use additional support for HTML5 input types.
42   */
43  Drupal.behaviors.betterExposedFiltersAutoSubmit = {
44    attach: function (context) {
45      // When exposed as a block, the form #attributes are moved from the form
46      // to the block element, thus the second selector.
47      // @see \Drupal\block\BlockViewBuilder::preRender
48      var selectors = 'form[data-bef-auto-submit-full-form], [data-bef-auto-submit-full-form] form, [data-bef-auto-submit]';
49
50      // The change event bubbles so we only need to bind it to the outer form
51      // in case of a full form, or a single element when specified explicitly.
52      $(selectors, context).addBack(selectors).each(function (i, e) {
53        // Store the current form.
54        var $form = $(e);
55
56        // Retrieve the autosubmit delay for this particular form.
57        var autoSubmitDelay = $form.data('bef-auto-submit-delay') || 500;
58
59        // Attach event listeners.
60          $(once('bef-auto-submit', $form))
61          // On change, trigger the submit immediately.
62          .on('change', triggerSubmit)
63          // On keyup, wait for a specified number of milliseconds before
64          // triggering autosubmit. Each new keyup event resets the timer.
65          .on('keyup', Drupal.debounce(triggerSubmit, autoSubmitDelay));
66      });
67
68      /**
69       * Triggers form autosubmit when conditions are right.
70       *
71       * - Checks first that the element that was the target of the triggering
72       *   event is `:text` or `textarea`, but is not `.hasDatePicker`.
73       * - Checks that the keycode of the keyup was not in the list of ignored
74       *   keys (navigation keys etc).
75       *
76       * @param {object} e - The triggering event.
77       */
78      function triggerSubmit(e) {
79        // e.keyCode: key.
80        var ignoredKeyCodes = [
81          16, // Shift.
82          17, // Ctrl.
83          18, // Alt.
84          20, // Caps lock.
85          33, // Page up.
86          34, // Page down.
87          35, // End.
88          36, // Home.
89          37, // Left arrow.
90          38, // Up arrow.
91          39, // Right arrow.
92          40, // Down arrow.
93          9, // Tab.
94          13, // Enter.
95          27  // Esc.
96        ];
97
98        // Triggering element.
99        var $target = $(e.target);
100        var $submit = $target.closest('form').find('[data-bef-auto-submit-click]');
101
102        // Don't submit on changes to excluded elements or a submit element.
103        if ($target.is('[data-bef-auto-submit-exclude], :submit')) {
104          return true;
105        }
106
107        // Submit only if this is a non-datepicker textfield and if the
108        // incoming keycode is not one of the excluded values.
109        if (
110          $target.is(':text:not(.hasDatepicker), textarea')
111          && $.inArray(e.keyCode, ignoredKeyCodes) === -1
112        ) {
113          $submit.click();
114        }
115        // Only trigger submit if a change was the trigger (no keyup).
116        else if (e.type === 'change') {
117          $submit.click();
118        }
119      }
120    }
121  };
122
123}(jQuery, Drupal, 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.