PageSourceSearch

https://www.hologic.co.uk/modules/contrib/search_api_autocomplete/js/search_api_autocomplete.js?tm4wcr

js hologic.co.uk collected 2026-09-30 20:50:56 UTC 3,562 bytes, 107 lines download raw bytes

1/**
2 * @file
3 * Expands the behavior of the default autocompletion.
4 */
5
6(($, Drupal, drupalSettings, once) => {
7  // As a safety precaution, bail if the Drupal Core autocomplete framework is
8  // not present.
9  if (!Drupal.autocomplete) {
10    return;
11  }
12
13  const autocomplete = {};
14
15  /**
16   * Retrieves the custom settings for an autocomplete-enabled input field.
17   *
18   * @param {HTMLElement} input
19   *   The input field.
20   * @param {object} globalSettings
21   *   The object containing global settings. If none is passed, drupalSettings
22   *   is used instead.
23   *
24   * @return {object}
25   *   The effective settings for the given input fields, with defaults set if
26   *   applicable.
27   */
28  autocomplete.getSettings = (input, globalSettings) => {
29    globalSettings = globalSettings || drupalSettings || {};
30    // Set defaults for all known settings.
31    const settings = {
32      auto_submit: false,
33      delay: 0,
34      min_length: 1,
35      selector: ':submit',
36    };
37    const search = $(input).data('search-api-autocomplete-search');
38    if (
39      search &&
40      globalSettings.search_api_autocomplete &&
41      globalSettings.search_api_autocomplete[search]
42    ) {
43      $.extend(settings, globalSettings.search_api_autocomplete[search]);
44    }
45    return settings;
46  };
47
48  /**
49   * Attaches our custom autocomplete settings to all affected fields.
50   *
51   * @type {Drupal~behavior}
52   *
53   * @prop {Drupal~behaviorAttach} attach
54   *   Attaches the autocomplete behaviors.
55   */
56  Drupal.behaviors.searchApiAutocomplete = {
57    attach(context, settings) {
58      // Find all our fields with autocomplete settings.
59      const s = '.ui-autocomplete-input[data-search-api-autocomplete-search]';
60      $(once('search-api-autocomplete', s, context)).each(function foreach() {
61        const uiAutocomplete = $(this).data('ui-autocomplete');
62        if (!uiAutocomplete) {
63          return;
64        }
65
66        // Append the autocomplete to the filter that triggers it.
67        uiAutocomplete.element.autocomplete('option', 'appendTo', uiAutocomplete.element.parent());
68
69        const $element = uiAutocomplete.menu.element;
70        $element.data('search-api-autocomplete-input-id', this.id);
71        $element.addClass('search-api-autocomplete-search');
72        $element.attr('tabindex', '-1');
73        const elementSettings = autocomplete.getSettings(this, settings);
74        if (elementSettings.delay) {
75          uiAutocomplete.options.delay = elementSettings.delay;
76        }
77        if (elementSettings.min_length) {
78          uiAutocomplete.options.minLength = elementSettings.min_length;
79        }
80        // Override the "select" callback of the jQuery UI autocomplete.
81        const oldSelect = uiAutocomplete.options.select;
82        uiAutocomplete.options.select = function select(event, ui, ...args) {
83          // If this is a URL suggestion, instead of autocompleting we redirect
84          // the user to that URL.
85          if (ui.item.url) {
86            if (event.keyCode === 9) {
87              return false;
88            }
89            window.location.href = ui.item.url;
90            return false;
91          }
92
93          const ret = oldSelect.apply(this, [event, ui, ...args]);
94
95          // If auto-submit is enabled, submit the form.
96          if (elementSettings.auto_submit && elementSettings.selector) {
97            $(elementSettings.selector, this.form).trigger('click');
98          }
99
100          return ret;
101        };
102      });
103    },
104  };
105
106  Drupal.SearchApiAutocomplete = autocomplete;
107})(jQuery, Drupal, drupalSettings, 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.