// = require_self $(() => { const $search = $("#data_picker-autocomplete"); const $results = $("#add-translations-results"); const $template = $("template", $results); const $form = $search.parents("form"); let xhr = null; let currentSearch = ""; $search.on("keyup", function() { currentSearch = $search.val(); }); $search.autoComplete({ minChars: 2, cache: 0, source: function(term, response) { try { xhr.abort(); } catch (exception) { xhr = null; } const url = $form.attr("action"); xhr = $.getJSON( url, $form.serializeArray(), function(data) { response(data); } ); }, renderItem: function (item, search) { const sanitizedSearch = search.replace(/[-/\\^$*+?.()|[\]{}]/g, "\\$&"); const re = new RegExp(`(${sanitizedSearch.split(" ").join("|")})`, "gi"); const modelId = item[0]; const title = item[1]; // The terms are already escaped but when they are rendered to a data // attribute, they get unescaped when those values are used. The only // character we need to replace is the ampersand const value = title.replace(/&/g, "&"); const val = `${title} - ${modelId}`; return `