Skip to content

Commit 5bb4f7f

Browse files
committed
Added functionality to disable input and option fields that don't have matching values in the current location set after filtering when inclusive filtering is used (default)
1 parent 19ab36b commit 5bb4f7f

File tree

4 files changed

+173
-4
lines changed

4 files changed

+173
-4
lines changed

dist/assets/js/plugins/storeLocator/jquery.storelocator.js

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! jQuery Google Maps Store Locator - v3.1.9 - 2023-02-03
1+
/*! jQuery Google Maps Store Locator - v3.1.9 - 2023-02-05
22
* http://www.bjornblog.com/web/jquery-store-locator-plugin
33
* Copyright (c) 2023 Bjorn Holine; Licensed MIT */
44

@@ -870,6 +870,24 @@
870870
return objTest;
871871
},
872872

873+
/**
874+
* Checks to see if only a single taxonomy group has a selected value
875+
*
876+
* @param obj {Object} the object to check
877+
* @param key {string} Key value of current filter group
878+
*
879+
* @returns {boolean}
880+
*/
881+
hasSingleGroupFilterVal: function(obj, key) {
882+
this.writeDebug('hasSingleGroupFilterVal',arguments);
883+
884+
// Copy the object so the original doesn't change.
885+
var objCopy = Object.assign({}, obj);
886+
delete objCopy[key];
887+
888+
return this.hasEmptyObjectVals(objCopy);
889+
},
890+
873891
/**
874892
* Modal window close function
875893
*/
@@ -2762,6 +2780,67 @@
27622780
}
27632781
},
27642782

2783+
/**
2784+
* Disable input fields that aren't available within the current location set
2785+
*/
2786+
maybeDisableFilterOptions: function() {
2787+
this.writeDebug('maybeDisableFilterOptions');
2788+
var availableValues = [];
2789+
var _this = this;
2790+
2791+
// Initially reset any input/option fields that were previously disabled.
2792+
for (var taxKey in this.settings.taxonomyFilters) {
2793+
if (this.settings.taxonomyFilters.hasOwnProperty(taxKey)) {
2794+
for (var x = 0; x < this.settings.taxonomyFilters[taxKey].length; x++) {
2795+
$('#' + this.settings.taxonomyFilters[taxKey] + ' input,option').each(function () {
2796+
var disabled = $(this).attr('disabled')
2797+
2798+
if (typeof disabled !== 'undefined') {
2799+
$(this).removeAttr('disabled');
2800+
}
2801+
});
2802+
}
2803+
}
2804+
}
2805+
2806+
// Loop through current location set to determine what filter values are still available.
2807+
for (var location in locationset) {
2808+
if (locationset.hasOwnProperty(location)) {
2809+
// Loop through the location values.
2810+
for (var locationKey in locationset[location]) {
2811+
if (filters.hasOwnProperty(locationKey) && locationset[location][locationKey] !== '') {
2812+
if (availableValues.hasOwnProperty(locationKey)) {
2813+
var availableVal = availableValues[locationKey].concat(',', locationset[location][locationKey].replace(', ', ',').trim());
2814+
availableValues[locationKey] = Array.from(new Set(availableVal.split(','))).toString();
2815+
} else {
2816+
availableValues[locationKey] = locationset[location][locationKey].replace(', ', ',').trim();
2817+
}
2818+
}
2819+
}
2820+
}
2821+
}
2822+
2823+
// Update input and option fields to disabled if they're not available.
2824+
for (var key in this.settings.taxonomyFilters) {
2825+
if (this.settings.taxonomyFilters.hasOwnProperty(key)) {
2826+
for (var i = 0; i < this.settings.taxonomyFilters[key].length; i++) {
2827+
if (availableValues.hasOwnProperty(key)) {
2828+
$('#' + this.settings.taxonomyFilters[key] + ' input, #' + this.settings.taxonomyFilters[key] + ' option').each(function () {
2829+
if ($(this).val() !== '' && Array.from(new Set(availableValues[key].split(','))).indexOf($(this).val()) === -1) {
2830+
// Select options should still be available if only a select option has been selected.
2831+
if ($(this).prop('tagName') === 'OPTION' && _this.hasSingleGroupFilterVal(filters, key)) {
2832+
return;
2833+
}
2834+
2835+
$(this).attr('disabled', true);
2836+
}
2837+
});
2838+
}
2839+
}
2840+
}
2841+
}
2842+
},
2843+
27652844
/**
27662845
* Processes the location data
27672846
*
@@ -3027,6 +3106,11 @@
30273106
locationset = featuredset.concat(normalset);
30283107
}
30293108

3109+
// Disable filter inputs of there are no locations with the values left.
3110+
if (firstRun !== true && _this.settings.exclusiveFiltering === false) {
3111+
_this.maybeDisableFilterOptions();
3112+
}
3113+
30303114
// Slide in the map container
30313115
if (_this.settings.slideMap === true) {
30323116
$this.slideDown();

dist/assets/js/plugins/storeLocator/jquery.storelocator.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

readme.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ filtering.
3737

3838
### Version 3.1.9
3939

40-
* Extended nameAttribute settings so multiple attributes can be searched. Separate attributes with commas.
40+
* Added functionality to disable input and option fields that don't have matching values in the current location set after filtering when inclusive filtering is used (default).
41+
* Extended nameAttribute settings so multiple attributes can be searched. Separate attribute names with commas.
4142
* Fixed location set length scenario when fullMapStart is enabled and taxFilters is reset and name search and origin are empty.
4243
* Show empty result message if no locations with default sorting.
4344
* Temporary fix for missing Google Maps callback parameter console error - the parameter requirement was not previously enforced on the API side.

src/js/jquery.storelocator.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,24 @@
866866
return objTest;
867867
},
868868

869+
/**
870+
* Checks to see if only a single taxonomy group has a selected value
871+
*
872+
* @param obj {Object} the object to check
873+
* @param key {string} Key value of current filter group
874+
*
875+
* @returns {boolean}
876+
*/
877+
hasSingleGroupFilterVal: function(obj, key) {
878+
this.writeDebug('hasSingleGroupFilterVal',arguments);
879+
880+
// Copy the object so the original doesn't change.
881+
var objCopy = Object.assign({}, obj);
882+
delete objCopy[key];
883+
884+
return this.hasEmptyObjectVals(objCopy);
885+
},
886+
869887
/**
870888
* Modal window close function
871889
*/
@@ -2758,6 +2776,67 @@
27582776
}
27592777
},
27602778

2779+
/**
2780+
* Disable input fields that aren't available within the current location set
2781+
*/
2782+
maybeDisableFilterOptions: function() {
2783+
this.writeDebug('maybeDisableFilterOptions');
2784+
var availableValues = [];
2785+
var _this = this;
2786+
2787+
// Initially reset any input/option fields that were previously disabled.
2788+
for (var taxKey in this.settings.taxonomyFilters) {
2789+
if (this.settings.taxonomyFilters.hasOwnProperty(taxKey)) {
2790+
for (var x = 0; x < this.settings.taxonomyFilters[taxKey].length; x++) {
2791+
$('#' + this.settings.taxonomyFilters[taxKey] + ' input,option').each(function () {
2792+
var disabled = $(this).attr('disabled')
2793+
2794+
if (typeof disabled !== 'undefined') {
2795+
$(this).removeAttr('disabled');
2796+
}
2797+
});
2798+
}
2799+
}
2800+
}
2801+
2802+
// Loop through current location set to determine what filter values are still available.
2803+
for (var location in locationset) {
2804+
if (locationset.hasOwnProperty(location)) {
2805+
// Loop through the location values.
2806+
for (var locationKey in locationset[location]) {
2807+
if (filters.hasOwnProperty(locationKey) && locationset[location][locationKey] !== '') {
2808+
if (availableValues.hasOwnProperty(locationKey)) {
2809+
var availableVal = availableValues[locationKey].concat(',', locationset[location][locationKey].replace(', ', ',').trim());
2810+
availableValues[locationKey] = Array.from(new Set(availableVal.split(','))).toString();
2811+
} else {
2812+
availableValues[locationKey] = locationset[location][locationKey].replace(', ', ',').trim();
2813+
}
2814+
}
2815+
}
2816+
}
2817+
}
2818+
2819+
// Update input and option fields to disabled if they're not available.
2820+
for (var key in this.settings.taxonomyFilters) {
2821+
if (this.settings.taxonomyFilters.hasOwnProperty(key)) {
2822+
for (var i = 0; i < this.settings.taxonomyFilters[key].length; i++) {
2823+
if (availableValues.hasOwnProperty(key)) {
2824+
$('#' + this.settings.taxonomyFilters[key] + ' input, #' + this.settings.taxonomyFilters[key] + ' option').each(function () {
2825+
if ($(this).val() !== '' && Array.from(new Set(availableValues[key].split(','))).indexOf($(this).val()) === -1) {
2826+
// Select options should still be available if only a select option has been selected.
2827+
if ($(this).prop('tagName') === 'OPTION' && _this.hasSingleGroupFilterVal(filters, key)) {
2828+
return;
2829+
}
2830+
2831+
$(this).attr('disabled', true);
2832+
}
2833+
});
2834+
}
2835+
}
2836+
}
2837+
}
2838+
},
2839+
27612840
/**
27622841
* Processes the location data
27632842
*
@@ -3023,6 +3102,11 @@
30233102
locationset = featuredset.concat(normalset);
30243103
}
30253104

3105+
// Disable filter inputs of there are no locations with the values left.
3106+
if (firstRun !== true && _this.settings.exclusiveFiltering === false) {
3107+
_this.maybeDisableFilterOptions();
3108+
}
3109+
30263110
// Slide in the map container
30273111
if (_this.settings.slideMap === true) {
30283112
$this.slideDown();

0 commit comments

Comments
 (0)