MDL-59366 core_amd: Modifications for form-autocomplete
authorJun Pataleta <jun@moodle.com>
Thu, 20 Jul 2017 05:32:50 +0000 (13:32 +0800)
committerJun Pataleta <jun@moodle.com>
Fri, 28 Jul 2017 08:11:38 +0000 (16:11 +0800)
* New optional parameter 'closeSuggestionsOnSelect' for the enhance()
function for form-autocomplete. Setting this to true will close the
suggestions popup immediately after an option has been selected. If not
specified, it defaults to true for single-select elements and false
for multiple-select elements.

Part of MDL-59290.

lib/amd/build/form-autocomplete.min.js
lib/amd/src/form-autocomplete.js
lib/upgrade.txt

index 5e301a0..233e46e 100644 (file)
Binary files a/lib/amd/build/form-autocomplete.min.js and b/lib/amd/build/form-autocomplete.min.js differ
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 = [];
index ff60008..bf81ed5 100644 (file)
@@ -38,6 +38,9 @@ information provided here is intended especially for developers.
   - get_enrolled_with_capabilities_join()
   Setting this parameter to a non-zero value will add a condition to the query such that only users that were enrolled
   with this enrolment method will be returned.
+* New optional parameter 'closeSuggestionsOnSelect' for the enhance() function for form-autocomplete. Setting this to true will
+  close the suggestions popup immediately after an option has been selected. If not specified, it defaults to true for single-select
+  elements and false for multiple-select elements.
 
 === 3.3.1 ===