PageSourceSearch

https://www.lannoopublishers.com/sites/all/modules/contrib/search_…/search_api_autocomplete.js?tkukhp

js lannoopublishers.com collected 2026-09-25 18:39:12 UTC 6,317 bytes, 204 lines download raw bytes

1(function ($) {
2
3// Auto-submit main search input after autocomplete
4if (typeof Drupal.jsAC != 'undefined') {
5
6  var getSetting = function (input, setting, defaultValue) {
7    // Earlier versions of jQuery, like the default for Drupal 7, don't properly
8    // convert data-* attributes to camel case, so we access it via the verbatim
9    // name from the attribute (which also works in newer versions).
10    var search = $(input).data('search-api-autocomplete-search');
11    if (typeof search == 'undefined'
12        || typeof Drupal.settings.search_api_autocomplete == 'undefined'
13        || typeof Drupal.settings.search_api_autocomplete[search] == 'undefined'
14        || typeof Drupal.settings.search_api_autocomplete[search][setting] == 'undefined') {
15      return defaultValue;
16    }
17    return Drupal.settings.search_api_autocomplete[search][setting];
18  };
19
20  var oldJsAC = Drupal.jsAC;
21  /**
22   * An AutoComplete object.
23   *
24   * Overridden to set the proper "role" attribute on the input element.
25   */
26  Drupal.jsAC = function ($input, db) {
27    if ($input.data('search-api-autocomplete-search')) {
28      $input.attr('role', 'combobox');
29      $input.parent().attr('role', 'search');
30    }
31    this.inSelect = false;
32    oldJsAC.call(this, $input, db);
33  };
34  Drupal.jsAC.prototype = oldJsAC.prototype;
35
36  /**
37   * Handler for the "keyup" event.
38   *
39   * Extend from Drupal's autocomplete.js to automatically submit the form
40   * when Enter is hit.
41   */
42  var default_onkeyup = Drupal.jsAC.prototype.onkeyup;
43  Drupal.jsAC.prototype.onkeyup = function (input, e) {
44    if (!e) {
45      e = window.event;
46    }
47    // Fire standard function.
48    default_onkeyup.call(this, input, e);
49
50    if (13 == e.keyCode && $(input).hasClass('auto_submit')) {
51      var selector = getSetting(input, 'selector', ':submit');
52      $(selector, input.form).trigger('click');
53    }
54  };
55
56  /**
57   * Handler for the "keydown" event.
58   *
59   * Extend from Drupal's autocomplete.js to avoid ajax interfering with the
60   * autocomplete.
61   */
62  var default_onkeydown = Drupal.jsAC.prototype.onkeydown;
63  Drupal.jsAC.prototype.onkeydown = function (input, e) {
64    if (!e) {
65      e = window.event;
66    }
67    // Fire standard function.
68    default_onkeydown.call(this, input, e);
69
70    // Prevent that the ajax handling of Views fires too early and thus
71    // misses the form update.
72    if (13 == e.keyCode && $(input).hasClass('auto_submit')) {
73      e.preventDefault();
74      return false;
75    }
76  };
77
78  Drupal.jsAC.prototype.select = function(node) {
79    // Protect against an (potentially infinite) recursion.
80    if (this.inSelect) {
81      return false;
82    }
83    this.inSelect = true;
84
85    var autocompleteValue = $(node).data('autocompleteValue');
86    // Check whether this is not a suggestion but a "link".
87    if (autocompleteValue.charAt(0) == ' ') {
88      window.location.href = autocompleteValue.substr(1);
89      this.inSelect = false;
90      return false;
91    }
92    this.input.value = autocompleteValue;
93    $(this.input).trigger('autocompleteSelect', [node]);
94    if ($(this.input).hasClass('auto_submit')) {
95      if (typeof Drupal.search_api_ajax != 'undefined') {
96        // Use Search API Ajax to submit
97        Drupal.search_api_ajax.navigateQuery($(this.input).val());
98      }
99      else {
100        var selector = getSetting(this.input, 'selector', ':submit');
101        $(selector, this.input.form).trigger('click');
102      }
103      this.inSelect = false;
104      return true;
105    }
106    this.inSelect = false;
107  };
108
109  /**
110   * Overwrite default behaviour.
111   *
112   * Just always return true to make it possible to submit even when there was
113   * an autocomplete suggestion list open.
114   */
115  Drupal.autocompleteSubmit = function () {
116    $('#autocomplete').each(function () {
117      this.owner.hidePopup();
118    });
119    return true;
120  };
121
122  /**
123   * Performs a cached and delayed search.
124   */
125  Drupal.ACDB.prototype.search = function (searchString) {
126    this.searchString = searchString;
127
128    // Check allowed length of string for autocomplete.
129    var data = $(this.owner.input).first().data('min-autocomplete-length');
130    if (data && searchString.length < data) {
131      return;
132    }
133
134    // See if this string needs to be searched for anyway.
135    if (searchString.match(/^\s*$/)) {
136      return;
137    }
138
139    // Prepare search string.
140    searchString = searchString.replace(/^\s+/, '');
141    searchString = searchString.replace(/\s+/g, ' ');
142
143    // See if this key has been searched for before.
144    if (this.cache[searchString]) {
145      return this.owner.found(this.cache[searchString]);
146    }
147
148    var db = this;
149    this.searchString = searchString;
150
151    // Initiate delayed search.
152    if (this.timer) {
153      clearTimeout(this.timer);
154    }
155    var sendAjaxRequest = function () {
156      db.owner.setStatus('begin');
157
158      var url;
159
160      // Allow custom Search API Autocomplete overrides for specific searches.
161      if (getSetting(db.owner.input, 'custom_path', false)) {
162        var queryChar = db.uri.indexOf('?') >= 0 ? '&' : '?';
163        url = db.uri + queryChar + 'search=' + encodeURIComponent(searchString);
164      }
165      else {
166        // We use Drupal.encodePath instead of encodeURIComponent to allow
167        // autocomplete search terms to contain slashes.
168        url = db.uri + '/' + Drupal.encodePath(searchString);
169      }
170
171      // Ajax GET request for autocompletion.
172      $.ajax({
173        type: 'GET',
174        url: url,
175        dataType: 'json',
176        success: function (matches) {
177          if (typeof matches.status == 'undefined' || matches.status != 0) {
178            db.cache[searchString] = matches;
179            // Verify if these are still the matches the user wants to see.
180            if (db.searchString == searchString) {
181              db.owner.found(matches);
182            }
183            db.owner.setStatus('found');
184          }
185        },
186        error: function (xmlhttp) {
187          if (xmlhttp.status) {
188            alert(Drupal.ajaxError(xmlhttp, db.uri));
189          }
190        }
191      });
192    };
193    // Make it possible to override the delay via a setting.
194    var delay = getSetting(this.owner.input, 'delay', this.delay);
195    if (delay > 0) {
196      this.timer = setTimeout(sendAjaxRequest, delay);
197    }
198    else {
199      sendAjaxRequest.apply();
200    }
201  };
202}
203
204})(jQuery);

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.