MDL-60874 core_form: allow autocomplete AJAX handlers return a string

Custom AJAX handlers for the form autocomplete fields can now optionally
return string in their processResults() callback. If a string is
returned, it is displayed instead of the list of suggested items.

The string is displayed same way as we inform about no available
suggestions.
This commit is contained in:
David Mudrák
2018-07-16 21:25:40 +02:00
parent b80a22f5ba
commit 73df15dd67
3 changed files with 26 additions and 11 deletions
+19 -10
View File
@@ -355,7 +355,10 @@ define(['jquery', 'core/log', 'core/str', 'core/templates', 'core/notification']
});
// If we found any matches, show the list.
inputElement.attr('aria-expanded', true);
if (matchingElements) {
if (originalSelect.attr('data-notice')) {
// Display a notice rather than actual suggestions.
suggestionsElement.html(originalSelect.attr('data-notice'));
} else if (matchingElements) {
// We only activate the first item in the list if tags is false,
// because otherwise "Enter" would select the first item, instead of
// creating a new tag.
@@ -512,15 +515,21 @@ define(['jquery', 'core/log', 'core/str', 'core/templates', 'core/notification']
var option = $('<option>');
originalSelect.append(option);
}
// And add all the new ones returned from ajax.
$.each(processedResults, function(resultIndex, result) {
if (existingValues.indexOf(String(result.value)) === -1) {
var option = $('<option>');
option.append(result.label);
option.attr('value', result.value);
originalSelect.append(option);
}
});
if ($.isArray(processedResults)) {
// Add all the new ones returned from ajax.
$.each(processedResults, function(resultIndex, result) {
if (existingValues.indexOf(String(result.value)) === -1) {
var option = $('<option>');
option.append(result.label);
option.attr('value', result.value);
originalSelect.append(option);
}
});
originalSelect.attr('data-notice', '');
} else {
// The AJAX handler returned a string instead of the array.
originalSelect.attr('data-notice', processedResults);
}
// Update the list of suggestions now from the new values in the select list.
updateSuggestions(options, state, '', originalSelect);
M.util.js_complete(pendingKey);