MDL-59366 core_amd: Modifications for form-autocomplete
[moodle.git] / lib / amd / src / form-autocomplete.js
index b43080a..17e7adf 100644 (file)
@@ -451,7 +451,7 @@ define(['jquery', 'core/log', 'core/str', 'core/templates', 'core/notification']
         // Notifiy that the selection changed.
         notifyChange(originalSelect);
 
-        if (!options.multiple) {
+        if (options.closeSuggestionsOnSelect) {
             // Clear the input element.
             inputElement.val('');
             // Close the list of suggestions.
@@ -718,9 +718,11 @@ define(['jquery', 'core/log', 'core/str', 'core/templates', 'core/notification']
          * @param {Boolean} caseSensitive - If search has to be made case sensitive.
          * @param {Boolean} showSuggestions - If suggestions should be shown
          * @param {String} noSelectionString - Text to display when there is no selection
+         * @param {Boolean} closeSuggestionsOnSelect - Whether to close the suggestions immediately after making a selection.
          * @return {Promise}
          */
-        enhance: function(selector, tags, ajax, placeholder, caseSensitive, showSuggestions, noSelectionString) {
+        enhance: function(selector, tags, ajax, placeholder, caseSensitive, showSuggestions, noSelectionString,
+                          closeSuggestionsOnSelect) {
             // Set some default values.
             var options = {
                 selector: selector,
@@ -769,6 +771,13 @@ define(['jquery', 'core/log', 'core/str', 'core/templates', 'core/notification']
             };
             options.multiple = originalSelect.attr('multiple');
 
+            if (typeof closeSuggestionsOnSelect !== "undefined") {
+                options.closeSuggestionsOnSelect = closeSuggestionsOnSelect;
+            } else {
+                // If not specified, this will close suggestions by default for single-select elements only.
+                options.closeSuggestionsOnSelect = !options.multiple;
+            }
+
             var originalLabel = $('[for=' + state.selectId + ']');
             // Create the new markup and insert it after the select.
             var suggestions = [];