MDL-68196 core: not expand autocomplete elements too soon on IE
[moodle.git] / lib / amd / src / form-autocomplete.js
index 2cffd17..4738bb6 100644 (file)
@@ -834,6 +834,12 @@ function($, log, str, templates, notification, LoadingIcon) {
         });
         // Whenever the input field changes, update the suggestion list.
         if (options.showSuggestions) {
+            // Store the value of the field as its last value, when the field gains focus.
+            inputElement.on('focus', function(e) {
+                var query = $(e.currentTarget).val();
+                $(e.currentTarget).data('last-value', query);
+            });
+
             // If this field uses ajax, set it up.
             if (options.ajax) {
                 require([options.ajax], function(ajaxHandler) {
@@ -893,7 +899,15 @@ function($, log, str, templates, notification, LoadingIcon) {
                     };
 
                     // Trigger an ajax update after the text field value changes.
-                    inputElement.on("input", throttledHandler);
+                    inputElement.on('input', function(e) {
+                        var query = $(e.currentTarget).val();
+                        var last = $(e.currentTarget).data('last-value');
+                        // IE11 fires many more input events than required - even when the value has not changed.
+                        if (last !== query) {
+                            throttledHandler(e);
+                        }
+                        $(e.currentTarget).data('last-value', query);
+                    });
                 });
             } else {
                 inputElement.on('input', function(e) {