|
1 | | -/*! jQuery Google Maps Store Locator - v1.4.9 - 2014-10-11 |
| 1 | +/*! jQuery Google Maps Store Locator - v1.4.9 - 2014-10-12 |
2 | 2 | * http://www.bjornblog.com/web/jquery-store-locator-plugin |
3 | 3 | * Copyright (c) 2014 Bjorn Holine; Licensed MIT */ |
4 | 4 |
|
|
7 | 7 | ;(function ($, window, document, undefined) { |
8 | 8 | 'use strict'; |
9 | 9 |
|
10 | | - var pluginName = "storeLocator"; |
| 10 | + var pluginName = 'storeLocator'; |
11 | 11 |
|
12 | 12 | // Only allow for one instantiation of this script |
13 | 13 | if (typeof $.fn[pluginName] !== 'undefined') { |
|
76 | 76 | 'listTemplateID' : null, |
77 | 77 | 'infowindowTemplateID' : null, |
78 | 78 | 'taxonomyFilters' : null, |
| 79 | + 'taxonomyFiltersContainer' : 'bh-sl-filters-container', |
79 | 80 | 'querystringParams' : false, |
80 | 81 | 'callbackBeforeSend' : null, |
81 | 82 | 'callbackSuccess' : null, |
|
97 | 98 |
|
98 | 99 | // Plugin constructor |
99 | 100 | function Plugin(element, options) { |
| 101 | + $this = $(element); |
100 | 102 | this.element = element; |
101 | 103 | this.settings = $.extend({}, defaults, options); |
102 | 104 | this._defaults = defaults; |
|
106 | 108 |
|
107 | 109 | // Avoid Plugin.prototype conflicts |
108 | 110 | $.extend(Plugin.prototype, { |
109 | | - /** |
110 | | - * Reset function |
111 | | - */ |
112 | | - reset: function () { |
113 | | - locationset = []; |
114 | | - featuredset = []; |
115 | | - normalset = []; |
116 | | - markers = []; |
117 | | - $(document).off('click.'+pluginName, '.' + this.settings.locationList + ' li'); |
118 | | - }, |
119 | | - |
120 | | - /** |
121 | | - * Distance calculations |
122 | | - */ |
123 | | - geoCodeCalcToRadian: function (v) { |
124 | | - return v * (Math.PI / 180); |
125 | | - }, |
126 | | - geoCodeCalcDiffRadian: function (v1, v2) { |
127 | | - return this.geoCodeCalcToRadian(v2) - this.geoCodeCalcToRadian(v1); |
128 | | - }, |
129 | | - geoCodeCalcCalcDistance: function (lat1, lng1, lat2, lng2, radius) { |
130 | | - return radius * 2 * Math.asin(Math.min(1, Math.sqrt(( Math.pow(Math.sin((this.geoCodeCalcDiffRadian(lat1, lat2)) / 2.0), 2.0) + Math.cos(this.geoCodeCalcToRadian(lat1)) * Math.cos(this.geoCodeCalcToRadian(lat2)) * Math.pow(Math.sin((this.geoCodeCalcDiffRadian(lng1, lng2)) / 2.0), 2.0) )))); |
131 | | - }, |
132 | | - |
133 | 111 | /** |
134 | 112 | * Init function |
135 | 113 | */ |
|
151 | 129 | else { |
152 | 130 | dataTypeRead = this.settings.dataType; |
153 | 131 | } |
154 | | - |
| 132 | + |
155 | 133 | // Set up the directionsService if it's true |
156 | 134 | if(this.settings.inlineDirections === true) { |
157 | 135 | directionsDisplay = new google.maps.DirectionsRenderer(); |
|
161 | 139 |
|
162 | 140 | // Save the original zoom setting so it can be retrieved if taxonomy filtering resets it |
163 | 141 | originalZoom = this.settings.mapSettings.zoom; |
164 | | - |
| 142 | + |
165 | 143 | // Add Handlebars helper for handling URL output |
166 | 144 | Handlebars.registerHelper('niceURL', function(url) { |
167 | 145 | if(url){ |
|
179 | 157 | // Clone the filters if there are any so they can be used in the modal |
180 | 158 | if (this.settings.taxonomyFilters !== null) { |
181 | 159 | // Clone the filters |
182 | | - $('.bh-sl-filters-container').clone(true, true).prependTo($this); |
| 160 | + $('.' + this.settings.taxonomyFiltersContainer).clone(true, true).prependTo($this); |
183 | 161 | } |
184 | | - |
| 162 | + |
185 | 163 | $this.wrap('<div class="' + this.settings.overlay + '"><div class="' + this.settings.modalWindow + '"><div class="' + this.settings.modalContent + '">'); |
186 | 164 | $('.' + this.settings.modalWindow).prepend('<div class="' + this.settings.closeIcon + '"></div>'); |
187 | 165 | $('.' + this.settings.overlay).hide(); |
188 | 166 | } |
189 | | - |
| 167 | + |
190 | 168 | // Load the templates and continue from there |
191 | 169 | this.loadTemplates(); |
192 | 170 | }, |
193 | 171 |
|
194 | 172 | /** |
195 | 173 | * Destroy |
196 | | - * |
| 174 | + * |
197 | 175 | * Note: The Google map is not destroyed here because Google recommends using a single instance and reusing it (it's not really supported) |
198 | 176 | */ |
199 | 177 | destroy: function () { |
200 | 178 | // Reset |
201 | 179 | this.reset(); |
202 | | - var mapContainer = $('#' + this.settings.mapID); |
| 180 | + var $mapDiv = $('#' + this.settings.mapID); |
203 | 181 |
|
204 | 182 | // Remove marker event listeners |
205 | 183 | if(markers.length) { |
206 | 184 | for(var i = 0; i <= markers.length; i++) { |
207 | 185 | google.maps.event.removeListener(markers[i]); |
208 | 186 | } |
209 | 187 | } |
210 | | - |
| 188 | + |
211 | 189 | // Remove markup |
212 | 190 | $('.' + this.settings.locationList + ' ul').empty(); |
213 | | - if(mapContainer.hasClass('bh-sl-map-open')) { |
214 | | - mapContainer.empty().removeClass('bh-sl-map-open'); |
| 191 | + if($mapDiv.hasClass('bh-sl-map-open')) { |
| 192 | + $mapDiv.empty().removeClass('bh-sl-map-open'); |
215 | 193 | } |
216 | | - |
| 194 | + |
217 | 195 | // Remove modal markup |
218 | 196 | if (this.settings.modal === true) { |
219 | 197 | $('. ' + this.settings.overlay).remove(); |
220 | 198 | } |
221 | | - |
| 199 | + |
222 | 200 | // Remove map style from cotnainer |
223 | | - mapContainer.attr('style', ''); |
224 | | - |
| 201 | + $mapDiv.attr('style', ''); |
| 202 | + |
225 | 203 | // Hide map container |
226 | 204 | $this.hide(); |
227 | 205 | // Remove data |
|
231 | 209 | // Unbind plugin |
232 | 210 | $this.unbind(); |
233 | 211 | }, |
| 212 | + |
| 213 | + /** |
| 214 | + * Reset function |
| 215 | + */ |
| 216 | + reset: function () { |
| 217 | + locationset = []; |
| 218 | + featuredset = []; |
| 219 | + normalset = []; |
| 220 | + markers = []; |
| 221 | + $(document).off('click.'+pluginName, '.' + this.settings.locationList + ' li'); |
| 222 | + }, |
| 223 | + |
| 224 | + /** |
| 225 | + * Distance calculations |
| 226 | + */ |
| 227 | + geoCodeCalcToRadian: function (v) { |
| 228 | + return v * (Math.PI / 180); |
| 229 | + }, |
| 230 | + geoCodeCalcDiffRadian: function (v1, v2) { |
| 231 | + return this.geoCodeCalcToRadian(v2) - this.geoCodeCalcToRadian(v1); |
| 232 | + }, |
| 233 | + geoCodeCalcCalcDistance: function (lat1, lng1, lat2, lng2, radius) { |
| 234 | + return radius * 2 * Math.asin(Math.min(1, Math.sqrt(( Math.pow(Math.sin((this.geoCodeCalcDiffRadian(lat1, lat2)) / 2.0), 2.0) + Math.cos(this.geoCodeCalcToRadian(lat1)) * Math.cos(this.geoCodeCalcToRadian(lat2)) * Math.pow(Math.sin((this.geoCodeCalcDiffRadian(lng1, lng2)) / 2.0), 2.0) )))); |
| 235 | + }, |
234 | 236 |
|
235 | 237 | /** |
236 | 238 | * Check for query string |
|
700 | 702 | paginationSetup: function (currentPage) { |
701 | 703 | var pagesOutput = ''; |
702 | 704 | var totalPages = locationset.length / this.settings.locationsPerPage; |
| 705 | + var $paginationList = $('.bh-sl-pagination-container .bh-sl-pagination'); |
703 | 706 |
|
704 | 707 | // Current page check |
705 | 708 | if (typeof currentPage === 'undefined') { |
706 | 709 | currentPage = 0; |
707 | 710 | } |
708 | 711 |
|
709 | 712 | // Initial pagination setup |
710 | | - if ($('.bh-sl-pagination-container .bh-sl-pagination').length === 0) { |
| 713 | + if ($paginationList.length === 0) { |
711 | 714 |
|
712 | 715 | pagesOutput = this.paginationOutput(currentPage, totalPages); |
713 | 716 | } |
714 | 717 | // Update pagination on page change |
715 | 718 | else { |
716 | 719 | // Remove the old pagination |
717 | | - $('.bh-sl-pagination-container .bh-sl-pagination').empty(); |
| 720 | + $paginationList.empty(); |
718 | 721 |
|
719 | 722 | // Add the numbers |
720 | 723 | pagesOutput = this.paginationOutput(currentPage, totalPages); |
721 | 724 | } |
722 | 725 |
|
723 | | - //TODO: Target this better |
724 | | - $('.bh-sl-pagination-container .bh-sl-pagination').append(pagesOutput); |
| 726 | + $paginationList.append(pagesOutput); |
725 | 727 | }, |
726 | 728 |
|
727 | 729 | /** |
|
928 | 930 | // Focus on the list |
929 | 931 | $('.' + _this.settings.locationList + ' li').removeClass('list-focus'); |
930 | 932 | var markerId = marker.get('id'); |
931 | | - var selectedLocation = $('.' + _this.settings.locationList + ' li[data-markerid=' + markerId + ']'); |
932 | | - selectedLocation.addClass('list-focus'); |
| 933 | + var $selectedLocation = $('.' + _this.settings.locationList + ' li[data-markerid=' + markerId + ']'); |
| 934 | + $selectedLocation.addClass('list-focus'); |
933 | 935 |
|
934 | 936 | // Scroll list to selected marker |
935 | | - var container = $('.' + _this.settings.locationList); |
936 | | - container.animate({ |
937 | | - scrollTop: selectedLocation.offset().top - container.offset().top + container.scrollTop() |
| 937 | + var $container = $('.' + _this.settings.locationList); |
| 938 | + $container.animate({ |
| 939 | + scrollTop: $selectedLocation.offset().top - $container.offset().top + $container.scrollTop() |
938 | 940 | }); |
939 | 941 | }); |
940 | 942 | } |
|
1077 | 1079 | /** |
1078 | 1080 | * Process the form values and/or query string |
1079 | 1081 | * |
1080 | | - * @param e {event} |
| 1082 | + * @param e {Object} event |
1081 | 1083 | */ |
1082 | 1084 | processForm: function (e) { |
1083 | 1085 | var _this = this; |
|
1196 | 1198 | * Process the location data |
1197 | 1199 | */ |
1198 | 1200 | originalDataRequest.then(function (data) { |
1199 | | - var mapContainer = $('#' + _this.settings.mapID); |
| 1201 | + var $mapDiv = $('#' + _this.settings.mapID); |
| 1202 | + // Get the length unit |
| 1203 | + var distUnit = (_this.settings.lengthUnit === 'km') ? _this.settings.kilometersLang : _this.settings.milesLang; |
1200 | 1204 |
|
1201 | 1205 | // Callback |
1202 | 1206 | if (_this.settings.callbackSuccess) { |
1203 | 1207 | _this.settings.callbackSuccess.call(this); |
1204 | 1208 | } |
1205 | 1209 |
|
1206 | 1210 | // Set a variable for fullMapStart so we can detect the first run |
1207 | | - if (_this.settings.fullMapStart === true && mapContainer.hasClass('bh-sl-map-open') === false) { |
| 1211 | + if (_this.settings.fullMapStart === true && $mapDiv.hasClass('bh-sl-map-open') === false) { |
1208 | 1212 | firstRun = true; |
1209 | 1213 | } |
1210 | 1214 | else { |
1211 | 1215 | _this.reset(); |
1212 | 1216 | } |
1213 | 1217 |
|
1214 | | - mapContainer.addClass('bh-sl-map-open'); |
| 1218 | + $mapDiv.addClass('bh-sl-map-open'); |
1215 | 1219 |
|
1216 | 1220 | // Process the location data depending on the data format type |
1217 | 1221 | if (_this.settings.dataType === 'json' || _this.settings.dataType === 'jsonp') { |
|
1348 | 1352 |
|
1349 | 1353 | if (_this.isEmptyObject(locationset)) { |
1350 | 1354 | // Hide the map and locations if they're showing |
1351 | | - if (mapContainer.hasClass('bh-sl-map-open')) { |
| 1355 | + if ($mapDiv.hasClass('bh-sl-map-open')) { |
1352 | 1356 | $this.hide(); |
1353 | 1357 | } |
1354 | 1358 |
|
|
1371 | 1375 | // Featured locations filtering |
1372 | 1376 | if (_this.settings.featuredLocations === true) { |
1373 | 1377 | // Create array for featured locations |
1374 | | - featuredset = $.grep(locationset, function (val, i) { |
| 1378 | + featuredset = $.grep(locationset, function (val) { |
1375 | 1379 | return val.featured === 'true'; |
1376 | 1380 | }); |
1377 | 1381 |
|
|
1385 | 1389 | locationset = featuredset.concat(normalset); |
1386 | 1390 | } |
1387 | 1391 |
|
1388 | | - // Get the length unit |
1389 | | - var distUnit = (_this.settings.lengthUnit === 'km') ? _this.settings.kilometersLang : _this.settings.milesLang; |
1390 | | - |
1391 | 1392 | // Check the closest marker |
1392 | 1393 | if (_this.settings.maxDistance === true && firstRun !== true && maxDistance) { |
1393 | 1394 | if (typeof locationset[0] === 'undefined' || locationset[0].distance > maxDistance) { |
|
1479 | 1480 | myOptions = _this.settings.mapSettings; |
1480 | 1481 | } |
1481 | 1482 |
|
| 1483 | + // Create the map |
1482 | 1484 | var map = new google.maps.Map(document.getElementById(_this.settings.mapID), myOptions); |
1483 | 1485 |
|
1484 | 1486 | // Re-center the map when the browser is resized |
|
1633 | 1635 | }); |
1634 | 1636 |
|
1635 | 1637 | // Handle filter updates |
1636 | | - $('.bh-sl-filters-container').on('change.'+pluginName, 'input, select', function (e) { |
| 1638 | + $('.' + this.settings.taxonomyFiltersContainer).on('change.'+pluginName, 'input, select', function (e) { |
1637 | 1639 | e.stopPropagation(); |
1638 | 1640 |
|
1639 | 1641 | var filterId, filterContainer, filterKey; |
|
1736 | 1738 | // preventing against multiple instantiations |
1737 | 1739 | $.fn[ pluginName ] = function (options) { |
1738 | 1740 | this.each(function () { |
1739 | | - $this = $(this); |
1740 | 1741 | if (!$.data(this, "plugin_" + pluginName)) { |
1741 | 1742 | $.data(this, "plugin_" + pluginName, new Plugin(this, options)); |
1742 | 1743 | } |
|
0 commit comments