Skip to content

Commit 3deaabb

Browse files
committed
Moved originaldatarequest into mapping function so data can be passed, removed unused functions, worked through some todos
1 parent 7eed6bb commit 3deaabb

File tree

3 files changed

+157
-235
lines changed

3 files changed

+157
-235
lines changed

dist/js/jquery.storelocator.js

Lines changed: 78 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
zoom : 12,
3434
mapTypeId: google.maps.MapTypeId.ROADMAP
3535
},
36-
'pinColor' : 'fe7569',
36+
'pinColor' : 'fe7569', //TODO: New marker settings
3737
'pinTextColor' : '000000',
3838
'lengthUnit' : 'm',
3939
'storeLimit' : 26,
@@ -60,7 +60,7 @@
6060
'maxDistanceID' : 'maxdistance',
6161
'fullMapStart' : false,
6262
'noForm' : false,
63-
'loading' : false,
63+
'loading' : false, //TODO: Add loading back
6464
'loadingDiv' : 'bh-storelocator-loading',
6565
'featuredLocations' : false,
6666
'pagination' : false,
@@ -74,7 +74,6 @@
7474
'infowindowTemplateID' : null,
7575
'taxonomyFilters' : null,
7676
'callbackBeforeSend' : null,
77-
'callbackComplete' : null,
7877
'callbackSuccess' : null,
7978
'callbackModalOpen' : null,
8079
'callbackModalClose' : null,
@@ -136,24 +135,16 @@
136135
else {
137136
dataTypeRead = this.settings.dataType;
138137
}
139-
140-
// Do the initial data request
141-
originalDataRequest = this.getData();
142-
143-
// Save separately so we can avoid multiple AJAX requests
144-
originalDataRequest.done(function (data) {
145-
originalData = data;
146-
});
147-
148-
// Save the original zoom setting so it can be retrieved if taxonomy filtering resets it
149-
originalZoom = this.settings.mapSettings.zoom;
150138

151139
// Set up the directionsService if it's true
152140
if(this.settings.inlineDirections === true){
153141
directionsDisplay = new google.maps.DirectionsRenderer();
154142
directionsService = new google.maps.DirectionsService();
155143
$(this.settings.listDiv).prepend('<div class="bh-storelocator-directions-panel"></div>');
156144
}
145+
146+
// Save the original zoom setting so it can be retrieved if taxonomy filtering resets it
147+
originalZoom = this.settings.mapSettings.zoom;
157148

158149
// Add Handlebars helper for handling URL output
159150
Handlebars.registerHelper('niceURL', function(url) {
@@ -197,7 +188,6 @@
197188
}
198189
// Handle script tag template method
199190
else if (this.settings.listTemplateID !== null && this.settings.infowindowTemplateID !== null) {
200-
//TODO: Test this
201191
// Infowindows
202192
infowindowTemplate = Handlebars.compile($(this.settings.infowindowTemplateID).html());
203193

@@ -232,6 +222,31 @@
232222
});
233223
}
234224
},
225+
226+
/**
227+
* Primary locator function runs after the templates are loaded
228+
*/
229+
locator: function () {
230+
// Do taxonomy filtering if set
231+
if (this.settings.taxonomyFilters !== null) {
232+
this.taxonomyFiltering();
233+
}
234+
235+
// Add modal window divs if set
236+
if (this.settings.modalWindow === true) {
237+
$this.wrap('<div class="' + this.settings.overlayDiv + '"><div class="' + this.settings.modalWindowDiv + '"><div class="' + this.settings.modalContentDiv + '">');
238+
$('.' + this.settings.modalWindowDiv).prepend('<div class="' + this.settings.closeIconDiv + '"></div>');
239+
$('.' + this.settings.overlayDiv).hide();
240+
}
241+
242+
if (this.settings.slideMap === true) {
243+
// Let's hide the map container to begin
244+
$this.hide();
245+
}
246+
247+
this.start();
248+
this.processFormInput();
249+
},
235250

236251
/**
237252
* Reset function
@@ -288,14 +303,26 @@
288303
},
289304

290305
/**
291-
* Changing AJAX call to this function. TODO: Add old callbacks, maybe switch to when/then
306+
* AJAX data request
292307
*/
293-
getData: function () {
308+
getData: function (lat, lng, address) {
294309
var d = $.Deferred();
310+
311+
// Before send callback
312+
if (this.settings.callbackBeforeSend) {
313+
this.settings.callbackBeforeSend.call(this);
314+
}
295315

316+
// AJAX request
296317
$.ajax({
297318
type : 'GET',
298319
url : this.settings.dataLocation + (this.settings.dataType === 'jsonp' ? (this.settings.dataLocation.match(/\?/) ? '&' : '?') + 'callback=?' : ''),
320+
// Passing the lat, lng, and address with the AJAX request so they can optionally be used by back-end languages
321+
data: {
322+
'origLat' : lat,
323+
'origLng' : lng,
324+
'origAddress': address
325+
},
299326
dataType : dataTypeRead,
300327
jsonpCallback: (this.settings.dataType === 'jsonp' ? this.settings.jsonpCallback : null)
301328
}).done(function (p) {
@@ -382,18 +409,18 @@
382409
r.geocode({'latLng': latlng}, function (data) {
383410
if (data !== null) {
384411
var originAddress = data.address;
385-
_this.mapping(originalData, _this.settings.defaultLat, _this.settings.defaultLng, originAddress);
412+
_this.mapping(_this.settings.defaultLat, _this.settings.defaultLng, originAddress);
386413
} else {
387414
// Unable to geocode
388-
alert(this.settings.addressErrorAlert);
415+
alert(_this.settings.addressErrorAlert);
389416
}
390417
});
391418
}
392419

393420
// If show full map option is true
394421
if (this.settings.fullMapStart === true) {
395422
// Just do the mapping without an origin
396-
this.mapping(originalData);
423+
this.mapping();
397424
}
398425

399426
// HTML5 geolocation API option
@@ -569,57 +596,6 @@
569596
}
570597
},
571598

572-
/**
573-
* Google distance calculation WIP
574-
*/
575-
calcRoute: function (start, end) {
576-
var unitSystem;
577-
var directionsService = new google.maps.DirectionsService();
578-
579-
if (this.settings.lengthUnit === 'm') {
580-
unitSystem = google.maps.UnitSystem.IMPERIAL;
581-
}
582-
else {
583-
unitSystem = google.maps.UnitSystem.METRIC;
584-
}
585-
586-
var request = {
587-
origin : start,
588-
destination: end,
589-
travelMode : google.maps.TravelMode.DRIVING, //Make new setting
590-
unitSystem : unitSystem
591-
};
592-
directionsService.route(request, function (response, status) {
593-
if (status === google.maps.DirectionsStatus.OK) {
594-
return(response);
595-
}
596-
});
597-
},
598-
599-
/**
600-
* Google distance matrix request probably not going to use but save for later
601-
*
602-
* @param origins {array} origins - in this case, the origin will always be what the user enters
603-
* @param destinations {array} locations
604-
*/
605-
calculateDistances: function (origins, destinations) {
606-
var service = new google.maps.DistanceMatrixService();
607-
var request = {
608-
origins : origins,
609-
destinations : destinations,
610-
travelMode : google.maps.TravelMode.DRIVING,
611-
unitSystem : google.maps.UnitSystem.IMPERIAL, //TODO: make setting
612-
avoidHighways: false,
613-
avoidTolls : false
614-
};
615-
616-
service.getDistanceMatrix(request, function (response, status) {
617-
if (status === google.maps.DistanceMatrixStatus.OK) {
618-
return(response);
619-
}
620-
});
621-
},
622-
623599
/**
624600
* Map marker setup
625601
*
@@ -632,11 +608,7 @@
632608
createMarker: function (point, name, address, letter, map) {
633609
var marker;
634610

635-
// Set up pin icon with the Google Charts API for all of our markers
636-
/*var pinImage = new google.maps.MarkerImage('https://chart.googleapis.com/chart?chst=d_map_pin_letter&chld=' + letter + '|' + this.settings.pinColor + '|' + this.settings.pinTextColor,
637-
new google.maps.Size(21, 34),
638-
new google.maps.Point(0, 0),
639-
new google.maps.Point(10, 34));*/
611+
// Letter markers image
640612
var pinImage = new google.maps.MarkerImage('http://mt.googleapis.com/vt/icon/name=icons/spotlight/spotlight-waypoint-b.png&text=' + letter + '&psize=16&font=fonts/Roboto-Regular.ttf&color=ff333333&ax=44&ay=48');
641613

642614
// Create the markers
@@ -648,6 +620,7 @@
648620
});
649621
}
650622
else {
623+
// Default dot marker
651624
marker = new google.maps.Marker({
652625
position : point,
653626
map : map,
@@ -705,26 +678,24 @@
705678
}
706679

707680
// Define location data
708-
var locations = {
681+
return {
709682
location: [$.extend(locationData, {
710683
'markerid': markerId,
711684
'marker' : indicator,
712685
'length' : distLength,
713686
'origin' : userInput
714687
})]
715688
};
716-
717-
return locations;
718689
},
719690

720691
/**
721-
* Set up the list templates TODO: Maybe rename this function because it's doing more than just handling the clicks
692+
* Set up the list templates
722693
*
723694
* @param marker {object} Google Maps marker
724695
* @param storeStart {number} optional first location on the current page
725696
* @param page {number} optional current page
726697
*/
727-
listClick: function (marker, storeStart, page) {
698+
listSetup: function (marker, storeStart, page) {
728699
// Define the location data
729700
var locations = this.defineLocationData(marker, storeStart, page);
730701

@@ -777,7 +748,7 @@
777748
r.geocode({'latLng': latlng}, function (data) {
778749
if (data !== null) {
779750
var originAddress = data.address;
780-
_this.mapping(originalData, position.coords.latitude, position.coords.longitude, originAddress);
751+
_this.mapping(position.coords.latitude, position.coords.longitude, originAddress);
781752
} else {
782753
// Unable to geocode
783754
alert(this.settings.addressErrorAlert);
@@ -808,7 +779,7 @@
808779
}
809780

810781
this.reset();
811-
this.mapping(originalData, olat, olng, userInput, maxDistance, newPage);
782+
this.mapping(olat, olng, userInput, maxDistance, newPage);
812783
},
813784

814785
/**
@@ -923,7 +894,9 @@
923894
if (data !== null) {
924895
olat = data.latitude;
925896
olng = data.longitude;
926-
_this.mapping(originalData, olat, olng, userInput, distance);
897+
898+
// Run the mapping function
899+
_this.mapping(olat, olng, userInput, distance);
927900
} else {
928901
// Unable to geocode
929902
alert(this.settings.addressErrorAlert);
@@ -941,7 +914,7 @@
941914
* @param origin {string} origin address
942915
* @param maxDistance {number} optional maximum distance
943916
*/
944-
mapping: function (data, orig_lat, orig_lng, origin, maxDistance, page) {
917+
mapping: function (orig_lat, orig_lng, origin, maxDistance, page) {
945918
var _this = this;
946919
var firstRun, marker, bounds, storeStart, storeNumToShow, myOptions;
947920
var i = 0;
@@ -956,10 +929,23 @@
956929
page = 0;
957930
}
958931

932+
// Do the initial data request - doing this here so the lat/lng and address can be passed over and used if needed
933+
originalDataRequest = _this.getData(olat, olng, origin);
934+
935+
// Save data separately so we can avoid multiple AJAX requests
936+
originalDataRequest.done(function (data) {
937+
// Success callback
938+
if (_this.settings.callbackSuccess) {
939+
_this.settings.callbackSuccess.call(this);
940+
}
941+
942+
originalData = data;
943+
});
944+
959945
/**
960946
* Process the location data
961947
*/
962-
originalDataRequest.then(function () {
948+
originalDataRequest.then(function (data) {
963949
// Callback
964950
if (_this.settings.callbackSuccess) {
965951
_this.settings.callbackSuccess.call(this);
@@ -1162,7 +1148,7 @@
11621148

11631149
// Pop up the modal window
11641150
$('.' + _this.settings.overlayDiv).fadeIn();
1165-
// Close modal when close icon is clicked and when background overlay is clicked TODO: Make sure this works with multiple
1151+
// Close modal when close icon is clicked and when background overlay is clicked
11661152
$(document).on('click', '.' + _this.settings.closeIconDiv + ', .' + _this.settings.overlayDiv, function () {
11671153
_this.modalClose();
11681154
});
@@ -1298,7 +1284,7 @@
12981284
$(markers).each(function (x, marker) {
12991285
var letter = String.fromCharCode('A'.charCodeAt(0) + x);
13001286
var currentMarker = markers[x];
1301-
_this.listClick(currentMarker, storeStart, page);
1287+
_this.listSetup(currentMarker, storeStart, page);
13021288
});
13031289

13041290
// Handle clicks from the list
@@ -1376,7 +1362,7 @@
13761362
_this.beginMapping();
13771363
}
13781364
else {
1379-
_this.mapping(originalData);
1365+
_this.mapping();
13801366
}
13811367
}
13821368
}
@@ -1397,7 +1383,7 @@
13971383
_this.beginMapping();
13981384
}
13991385
else {
1400-
_this.mapping(originalData);
1386+
_this.mapping();
14011387
}
14021388
}
14031389
}
@@ -1424,7 +1410,7 @@
14241410
_this.beginMapping();
14251411
}
14261412
else {
1427-
_this.mapping(originalData);
1413+
_this.mapping();
14281414
}
14291415
}
14301416
}
@@ -1440,36 +1426,11 @@
14401426
_this.beginMapping();
14411427
}
14421428
else {
1443-
_this.mapping(originalData);
1429+
_this.mapping();
14441430
}
14451431
}
14461432
}
14471433
});
1448-
},
1449-
1450-
/**
1451-
* Primary locator function runs after the templates are loaded
1452-
*/
1453-
locator: function () {
1454-
// Do taxonomy filtering if set
1455-
if (this.settings.taxonomyFilters !== null) {
1456-
this.taxonomyFiltering();
1457-
}
1458-
1459-
// Add modal window divs if set
1460-
if (this.settings.modalWindow === true) {
1461-
$this.wrap('<div class="' + this.settings.overlayDiv + '"><div class="' + this.settings.modalWindowDiv + '"><div class="' + this.settings.modalContentDiv + '">');
1462-
$('.' + this.settings.modalWindowDiv).prepend('<div class="' + this.settings.closeIconDiv + '"></div>');
1463-
$('.' + this.settings.overlayDiv).hide();
1464-
}
1465-
1466-
if (this.settings.slideMap === true) {
1467-
// Let's hide the map container to begin
1468-
$this.hide();
1469-
}
1470-
1471-
this.start();
1472-
this.processFormInput();
14731434
}
14741435

14751436
});

0 commit comments

Comments
 (0)