Skip to content

Commit 4f73c90

Browse files
committed
Filtering zoom
1 parent 219e9ce commit 4f73c90

File tree

3 files changed

+109
-65
lines changed

3 files changed

+109
-65
lines changed

dist/js/jquery.storelocator.js

Lines changed: 54 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! jQuery Google Maps Store Locator - v1.4.9 - 2014-05-25
1+
/*! jQuery Google Maps Store Locator - v1.4.9 - 2014-06-09
22
* http://www.bjornblog.com/web/jquery-store-locator-plugin
33
* Copyright (c) 2014 Bjorn Holine; Licensed MIT */
44

@@ -74,7 +74,7 @@ $.fn.storeLocator = function(options){
7474
return this.each(function(){
7575

7676
var $this = $(this);
77-
var listTemplate, infowindowTemplate, dataTypeRead, originalData;
77+
var listTemplate, infowindowTemplate, dataTypeRead, originalData, originalZoom;
7878

7979
//KML is read as XML
8080
if (settings.dataType === 'kml') {
@@ -108,6 +108,9 @@ $.fn.storeLocator = function(options){
108108
originalData = data;
109109
});
110110

111+
//Save the original zoom setting so it can be retrieved if taxonomy filtering resets it
112+
originalZoom = settings.mapSettings.zoom;
113+
111114
/**
112115
* Load the external template files first
113116
*/
@@ -226,7 +229,7 @@ $.fn.storeLocator = function(options){
226229
});
227230

228231
//Handle filter updates - TODO: This is only working with the map already open at the moment - need to add something to the form submit
229-
$('.bh-storelocator-filters-container').on('change', 'input', function(e){
232+
$('.bh-storelocator-filters-container').on('change.'+prefix, 'input', function(e){
230233
e.stopPropagation();
231234

232235
//First check if it's a checkbox or select drop-down
@@ -254,25 +257,31 @@ $.fn.storeLocator = function(options){
254257
if($(this).prop('checked')){
255258
//Add ids to the filter arrays as they are checked
256259
filters[locFilterKey].push(catId);
257-
reset();
258-
if((olat) && (olng)){
259-
begin_mapping();
260-
}
261-
else{
262-
mapping(originalData);
260+
if($('#'+settings.mapDiv).hasClass('bh-storelocator-map-open') === true){
261+
reset();
262+
if((olat) && (olng)){
263+
settings.mapSettings.zoom = 0;
264+
begin_mapping();
265+
}
266+
else{
267+
mapping(originalData);
268+
}
263269
}
264270
}
265271
else {
266272
//Remove ids from the filter arrays as they are unchecked
267273
var filterIndex = filters[locFilterKey].indexOf(catId);
268274
if(filterIndex > -1){
269275
filters[locFilterKey].splice(filterIndex, 1);
270-
reset();
271-
if((olat) && (olng)){
272-
begin_mapping();
273-
}
274-
else {
275-
mapping(originalData);
276+
if($('#'+settings.mapDiv).hasClass('bh-storelocator-map-open') === true){
277+
reset();
278+
if((olat) && (olng)){
279+
settings.mapSettings.zoom = originalZoom; //TODO: Maybe move into reset function
280+
begin_mapping();
281+
}
282+
else {
283+
mapping(originalData);
284+
}
276285
}
277286
}
278287
}
@@ -579,14 +588,14 @@ $.fn.storeLocator = function(options){
579588
var destinationsArray = [];
580589

581590
//Set a variable for fullMapStart so we can detect the first run
582-
if(settings.fullMapStart === true && $(settings.mapDiv).hasClass('mapOpen') === false){
591+
if(settings.fullMapStart === true && $('#'+settings.mapDiv).hasClass('bh-storelocator-map-open') === false){
583592
firstRun = true;
584593
}
585594
else{
586595
reset();
587596
}
588597

589-
$(settings.mapDiv).addClass('mapOpen');
598+
$('#' + settings.mapDiv).addClass('bh-storelocator-map-open');
590599

591600
//Depending on your data structure and what you want to include in the maps, you may need to change the following variables or comment them out
592601
if(settings.dataType === 'json' || settings.dataType === 'jsonp'){
@@ -709,28 +718,41 @@ $.fn.storeLocator = function(options){
709718
var taxFilters = {};
710719

711720
$.each(filters, function(k,v){
712-
//Let's use regex
713-
for(var z = 0; z < v.length; z++){
714-
//Creating a new object so we don't mess up the original filters
715-
if(!taxFilters[k]){
716-
taxFilters[k] = [];
721+
if(v.length > 0){
722+
//Let's use regex
723+
for(var z = 0; z < v.length; z++){
724+
//Creating a new object so we don't mess up the original filters
725+
if(!taxFilters[k]){
726+
taxFilters[k] = [];
727+
}
728+
taxFilters[k][z] = '(?=.*\\b'
729+
+ v[z].replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1")
730+
+ '\\b)';
717731
}
718-
taxFilters[k][z] = '(?=.*\\b'
719-
+ v[z].replace(/([\^\$\/\.\*\+\?\|\(\)\[\]\{\}\\])/g, "\\$1")
720-
+ '\\b)';
721732
}
722733
});
723734

724735
//Filter the data
725-
var filteredset = $.grep(locationset, function(val, i){
726-
return checkFilters(val, taxFilters);
727-
});
736+
if(!$.isEmptyObject(taxFilters)){
737+
var filteredset = $.grep(locationset, function(val, i){
738+
return checkFilters(val, taxFilters);
739+
});
728740

729-
if(filteredset.length){
730741
locationset = filteredset;
731742
}
732743
}
733744

745+
//TODO: isEmptyObject not supported in old IE - use another method
746+
if($.isEmptyObject(locationset)){
747+
locationset[0] = {
748+
'address': 'No locations were found with the given criteria. Please modify your selections or input.',
749+
'address2': '',
750+
'lat': '',
751+
'lng': '',
752+
'name': 'No results'
753+
}
754+
}
755+
734756
//Distance calculation update testing - there is a 100 element request limit
735757
/*if(locationset.length < 26){
736758
var matrix = calculateDistances(originsArray, destinationsArray);
@@ -882,8 +904,8 @@ $.fn.storeLocator = function(options){
882904
}
883905

884906
//Google maps settings
885-
if((settings.fullMapStart === true && firstRun === true) || settings.mapSettings.zoom === 0){
886-
var myOptions = settings.mapSettings;
907+
if((settings.fullMapStart === true && firstRun === true) || (settings.mapSettings.zoom === 0)){
908+
var myOptions = settings.mapSettings;
887909
var bounds = new google.maps.LatLngBounds();
888910
}
889911
else{
@@ -930,7 +952,7 @@ $.fn.storeLocator = function(options){
930952
create_infowindow(marker);
931953
}
932954

933-
//Center and zoom if no origin or zoom was provided
955+
//Center and zoom if no origin or zoom was provided or upon taxonomy filter
934956
if((settings.fullMapStart === true && firstRun === true) || (settings.mapSettings.zoom === 0)){
935957
map.fitBounds(bounds);
936958
}

0 commit comments

Comments
 (0)