/** * @file * Linkit Autocomplete based on jQuery UI. */ (function ($, Drupal, _) { 'use strict'; var autocomplete; /** * JQuery UI autocomplete source callback. * * @param {object} request * The request object. * @param {function} response * The function to call with the response. */ function sourceData(request, response) { var elementId = this.element.attr('id'); if (!(elementId in autocomplete.cache)) { autocomplete.cache[elementId] = {}; } /** * Transforms the data object into an array and update autocomplete results. * * @param {object} data * The data sent back from the server. */ function sourceCallbackHandler(data) { autocomplete.cache[elementId][term] = data.suggestions; response(data.suggestions); } // 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)) { response(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 * The event triggered. * @param {object} ui * The jQuery UI settings object. * * @return {boolean} * False to prevent further handlers. */ function selectHandler(event, ui) { var $form = $(event.target).closest('form'); if (!ui.item.path) { throw 'Missing path param.' + JSON.stringify(ui.item); } $('input[name="href_dirty_check"]', $form).val(ui.item.path); if (ui.item.entity_type_id || ui.item.entity_uuid || ui.item.substitution_id) { if (!ui.item.entity_type_id || !ui.item.entity_uuid || !ui.item.substitution_id) { throw 'Missing path param.' + JSON.stringify(ui.item); } $('input[name="attributes[data-entity-type]"]', $form).val(ui.item.entity_type_id); $('input[name="attributes[data-entity-uuid]"]', $form).val(ui.item.entity_uuid); $('input[name="attributes[data-entity-substitution]"]', $form).val(ui.item.substitution_id); } event.target.value = ui.item.path; return false; } /** * Override jQuery UI _renderItem function to output HTML by default. * * @param {object} ul * The