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 const $element = uiAutocomplete.menu.element; 66 $element.data('search-api-autocomplete-input-id', this.id); 67 $element.addClass('search-api-autocomplete-search'); 68 $element.attr('tabindex', '-1'); 69 const elementSettings = autocomplete.getSettings(this, settings); 70 if (elementSettings.delay) { 71 uiAutocomplete.options.delay = elementSettings.delay; 72 } 73 if (elementSettings.min_length) { 74 uiAutocomplete.options.minLength = elementSettings.min_length; 75 } 76 // Override the "select" callback of the jQuery UI autocomplete. 77 const oldSelect = uiAutocomplete.options.select; 78 uiAutocomplete.options.select = function select(event, ui, ...args) { 79 // If this is a URL suggestion, instead of autocompleting we redirect 80 // the user to that URL. 81 if (ui.item.url) { 82 if (event.keyCode === 9) { 83 return false; 84 } 85 window.location.href = ui.item.url; 86 return false; 87 } 88 89 const ret = oldSelect.apply(this, [event, ui, ...args]); 90 91 // If auto-submit is enabled, submit the form. 92 if (elementSettings.auto_submit && elementSettings.selector) { 93 $(elementSettings.selector, this.form).trigger('click'); 94 } 95 96 return ret; 97 }; 98 }); 99 }, 100 }; 101 102 Drupal.SearchApiAutocomplete = autocomplete; 103})(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.