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;
});
};
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) {
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;