From 5c2230dddc0c931ba8edd6a430378758d7ea2df6 Mon Sep 17 00:00:00 2001 From: Deimos Date: Wed, 24 Apr 2019 23:06:34 -0600 Subject: [PATCH] Include "leftover" text in tags input on submit Previously, the autocompleting tag input would only actually submit the tags that had been converted to "chips". This meant that if the user had a "leftover" tag that they had typed in but not actually converted to a chip (by typing a comma or using the autocomplete dropdown), it would be lost when they submitted the form. This appends it to the tags before submitting, so that it's not lost. --- .../static/js/behaviors/autocomplete-input.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tildes/static/js/behaviors/autocomplete-input.js b/tildes/static/js/behaviors/autocomplete-input.js index 5d6cca1..bb6cd3f 100644 --- a/tildes/static/js/behaviors/autocomplete-input.js +++ b/tildes/static/js/behaviors/autocomplete-input.js @@ -42,6 +42,25 @@ $.onmount('[data-js-autocomplete-input]', function() { // move the "tags" name to the hidden input (so the form works without JS) $(this).removeAttr("name"); $("[data-js-autocomplete-hidden-input]").attr("name", "tags"); + + // attach an event handler to the form that will add the input's value to + // the end of the tags list before submitting (to include any tag that's + // still in the input and wasn't converted to a chip) + $(this).closest("form").on("beforeSend.ic", function(evt, elt, data, settings, xhr, requestId) { + $autocompleteInput = $(elt).find("[data-js-autocomplete-input]").first(); + if (!$autocompleteInput.val()) { + return; + } + + var dataPieces = settings.data.split("&"); + for (i = 0; i < dataPieces.length; i++) { + if (dataPieces[i].indexOf("tags=") === 0) { + dataPieces[i] += $autocompleteInput.val(); + break; + } + } + settings.data = dataPieces.join("&"); + }); } if ($(this).val() !== '') {