Skip to content

Commit

Permalink
MDL-59982 core_user: allow user to use filters from same category
Browse files Browse the repository at this point in the history
  • Loading branch information
mdjnelson committed Nov 1, 2017
1 parent 40f1801 commit 9a428ae
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 19 deletions.
2 changes: 1 addition & 1 deletion user/amd/build/unified_filter.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion user/amd/build/unified_filter_datasource.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 35 additions & 1 deletion user/amd/src/unified_filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
};

Expand Down
16 changes: 0 additions & 16 deletions user/amd/src/unified_filter_datasource.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand Down

0 comments on commit 9a428ae

Please sign in to comment.