/** * @file * Linkit Autocomplete based on jQuery UI. */ (function ($, Drupal, _, document) { 'use strict'; var autocomplete; /** * JQuery UI autocomplete source callback. * * @param {object} request * @param {function} response */ function sourceData(request, response) { var elementId = this.element.attr('id'); if (!(elementId in autocomplete.cache)) { autocomplete.cache[elementId] = {}; } /** * @param {object} suggestions */ function showSuggestions(suggestions) { response(suggestions.matches); } /** * Transforms the data object into an array and update autocomplete results. * * @param {object} data */ function sourceCallbackHandler(data) { autocomplete.cache[elementId][term] = data; showSuggestions(data); } // Get the desired term and construct the autocomplete URL for it. var term = request.term; // Check if the term is already cached. if (autocomplete.cache[elementId].hasOwnProperty(term)) { showSuggestions(autocomplete.cache[elementId][term]); } else { var options = $.extend({success: sourceCallbackHandler, data: {q: term}}, autocomplete.ajax); $.ajax(this.element.attr('data-autocomplete-path'), options); } } /** * Handles an autocomplete select event. * * @param {jQuery.Event} event * @param {object} ui * * @return {boolean} */ function selectHandler(event, ui) { if (ui.item.hasOwnProperty('path')) { event.target.value = ui.item.path; } $(document).trigger('linkit.autocomplete.select', [event, ui]); return false; } /** * Override jQuery UI _renderItem function to output HTML by default. * * @param {object} ul * The