Merge branch 'MDL-59527-master' of git://github.com/damyon/moodle
[moodle.git] / lib / amd / src / form-autocomplete.js
index a640fea..43f37e1 100644 (file)
@@ -544,7 +544,7 @@ define(['jquery', 'core/log', 'core/str', 'core/templates', 'core/notification']
                                 updateAjax(e, options, state, originalSelect, ajaxHandler);
                             });
                         } else {
-                            // Else - open the suggestions list.
+                            // Open the suggestions list.
                             updateSuggestions(options, state, inputElement.val(), originalSelect);
                         }
                     }
@@ -621,11 +621,18 @@ define(['jquery', 'core/log', 'core/str', 'core/templates', 'core/notification']
         });
         if (options.showSuggestions) {
             var arrowElement = $(document.getElementById(state.downArrowId));
-            arrowElement.on('click', function() {
+            arrowElement.on('click', function(e) {
                 // Prevent the close timer, or we will open, then close the suggestions.
                 inputElement.focus();
-                // Show the suggestions list.
-                updateSuggestions(options, state, inputElement.val(), originalSelect);
+                // Handle ajax population of suggestions.
+                if (!inputElement.val() && options.ajax) {
+                    require([options.ajax], function(ajaxHandler) {
+                        updateAjax(e, options, state, originalSelect, ajaxHandler);
+                    });
+                } else {
+                    // Else - open the suggestions list.
+                    updateSuggestions(options, state, inputElement.val(), originalSelect);
+                }
             });
         }