Merge branch 'MDL-59982_master' of git://github.com/markn86/moodle
authorDamyon Wiese <damyon@moodle.com>
Thu, 2 Nov 2017 04:21:39 +0000 (12:21 +0800)
committerDamyon Wiese <damyon@moodle.com>
Thu, 2 Nov 2017 04:21:39 +0000 (12:21 +0800)
user/amd/build/unified_filter.min.js
user/amd/build/unified_filter_datasource.min.js
user/amd/src/unified_filter.js
user/amd/src/unified_filter_datasource.js

index 958d951..bb8a603 100644 (file)
Binary files a/user/amd/build/unified_filter.min.js and b/user/amd/build/unified_filter.min.js differ
index 237dc0c..567ed4a 100644 (file)
Binary files a/user/amd/build/unified_filter_datasource.min.js and b/user/amd/build/unified_filter_datasource.min.js differ
index d7da350..bfb2df1 100644 (file)
@@ -62,11 +62,45 @@ define(['jquery', 'core/form-autocomplete', 'core/str', 'core/notification'],
         var last = $(SELECTORS.UNIFIED_FILTERS).val();
         $(SELECTORS.UNIFIED_FILTERS).on('change', function() {
             var current = $(this).val();
+            var listoffilters = [];
+            var updatedselectedfilters = false;
 
+            $.each(current, function(index, catoption) {
+                var catandoption = catoption.split(':', 2);
+                if (catandoption.length !== 2) {
+                    return true; // Skip.
+                }
+
+                var category = catandoption[0];
+                var option = catandoption[1];
+
+                // The last option (eg. 'Teacher') out of a category (eg. 'Role') in this loop is the one that was last
+                // selected, so we want to use that if there are multiple options from the same category. Eg. The user
+                // may have chosen to filter by the 'Student' role, then wanted to filter by the 'Teacher' role - the
+                // last option in the category to be selected (in this case 'Teacher') will come last, so will overwrite
+                // 'Student' (after this if). We want to let the JS know that the filters have been updated.
+                if (typeof listoffilters[category] !== 'undefined') {
+                    updatedselectedfilters = true;
+                }
+
+                listoffilters[category] = option;
+                return true;
+            });
+
+            // Check if we have something to remove from the list of filters.
+            if (updatedselectedfilters) {
+                // Go through and put the list into something we can use to update the list of filters.
+                var updatefilters = [];
+                for (var category in listoffilters) {
+                    updatefilters.push(category + ":" + listoffilters[category]);
+                }
+                $(this).val(updatefilters);
+            }
+
+            // Prevent form from submitting unnecessarily, eg. on blur when no filter is selected.
             if (last.join(',') != current.join(',')) {
                 this.form.submit();
             }
-            last = current;
         });
     };
 
index 234a8c4..60700ac 100644 (file)
@@ -39,14 +39,6 @@ define(['jquery', 'core/ajax', 'core/notification'], function($, Ajax, Notificat
             var el = $(selector);
             var originalOptions = $(selector).data('originaloptionsjson');
             var selectedFilters = el.val();
-            var categoriesToSkip = [];
-            $.each(selectedFilters, function(index, filter) {
-                var filterCatAndValue = filter.split(':', 2);
-                if (filterCatAndValue.length === 2) {
-                    var category = filterCatAndValue[0];
-                    categoriesToSkip.push(category);
-                }
-            });
             $.each(originalOptions, function(index, option) {
                 // Skip option if it does not contain the query string.
                 if ($.trim(query) !== '' && option.label.toLocaleLowerCase().indexOf(query.toLocaleLowerCase()) === -1) {
@@ -56,14 +48,6 @@ define(['jquery', 'core/ajax', 'core/notification'], function($, Ajax, Notificat
                 if ($.inArray(option.value, selectedFilters) > -1) {
                     return true;
                 }
-                // Skip filters for categories that belong to the already selected filters.
-                var optionCatAndValue = option.value.split(':', 2);
-                if (optionCatAndValue.length === 2) {
-                    var category = optionCatAndValue[0];
-                    if ($.inArray(category, categoriesToSkip) > -1) {
-                        return true;
-                    }
-                }
 
                 filteredOptions.push(option);
                 return true;