1/* eslint-disable */ 2 3var WPViews = WPViews || {}; 4 5WPViews.DistanceFilterFrontend = function ( $ ) { 6 var self = this; 7 8 const API_GOOGLE = 'google'; 9 const API_AZURE = 'azure'; 10 11 self.distanceValueSelector = ".js-toolset-maps-distance-value"; 12 self.distanceUnitSelector = ".js-toolset-maps-distance-unit"; 13 self.distanceCenterSelector = ".js-toolset-maps-distance-center"; 14 self.userLocationSelector = ".js-toolset-maps-distance-current-location"; 15 self.distanceCenterLatSelector = '.js-toolset-maps-distance-center-lat'; 16 self.distanceCenterLngSelector = '.js-toolset-maps-distance-center-lng'; 17 self.hiddenCoordsFormSelector = '#js-toolset-maps-coords-form'; 18 19 self.current_lat = null; 20 self.current_long = null; 21 self.is_using_current_location = false; 22 self.view_id = $('div.js-wpv-view-layout').first().data('viewnumber'); 23 24 self.hiddenCoordsForm = '<form id="js-toolset-maps-coords-form" class="js-wpv-ajax-results-enabled"></form>'; 25 26 self.eventsOn = function() { 27 $( document ).on( 'geocode:result', self.distanceCenterSelector, function(evt, data) { 28 var addressLat = data.geometry.location.lat(); 29 var addressLng = data.geometry.location.lng(); 30 31 self.is_using_current_location = false; 32 33 self.updateFilterCenter({ 34 location: { 35 latitude: addressLat, 36 longitude: addressLng 37 }, 38 address_update: true 39 }); 40 41 $( self.userLocationSelector ).prop('disabled', false); 42 }); 43 44 $( document ).on('toolset_maps_views_distance_filter_center_updated', function() { 45 $( self.distanceCenterLatSelector ).val(self.current_lat); 46 $( self.distanceCenterLngSelector ).val(self.current_long); 47 48 if( self.is_using_current_location ) { 49 $( self.distanceCenterSelector ).val( self.getCurrentLatLng() ); 50 } 51 52 // Trigger change event on other selector to start Ajax when using immediate update, 53 // Because distanceCenterSelector is set to not react before proper values are filled in. 54 // (Sending enter key pressed event to distanceCenterSelector doesn't work because geocomplete 55 // library seems to eat it and Views never get it.) 56 // Note that this change event wipes the input when split view is used (separate shortcodes for form and 57 // loop output). That's why we are checking here and only triggering this when needed by immediate update. 58 // But that still leaves one unfixable edge case that doesn't work: immediate update + split view + Google 59 // maps (because Azure maps engine doesn't use geocomplete and is immune to this problem). 60 if ( $( self.distanceCenterSelector ).parents( 'form' ).hasClass( 'js-wpv-ajax-results-enabled' ) ) { 61 $( self.distanceValueSelector ).change(); 62 } 63 } ); 64 65 // If the form is being reset, we also want to reset current user location. 66 $( document ).on( 'click', '.js-wpv-reset-trigger', function() { 67 self.is_using_current_location = false; 68 } ); 69 }; 70 71 self.init = function ( ) { 72 self.initFormFunctionality(); 73 74 self.eventsOn(); 75 self.updateQueryFilterWithUserLocation(); 76 77 //Check if the browser supports geolocation services and if the site uses ssl 78 if ( navigator.geolocation && self.isSecurePage() ) { 79 $( document ).on('click', self.userLocationSelector, function (event) { 80 event.preventDefault(); 81 self.acquireCurrentLocation(self.updateFilterCenter); 82 }); 83 84 $( document ).on('toolset_maps_views_distance_filter_user_location_acquired', function() { 85 $( self.userLocationSelector ).prop('disabled', true); 86 }); 87 88 // If needed, add hidden form for ajax results loading 89 if ( !self.areDistanceCenterFormElementsPresentOnPage() ) { 90 $('.js-wpv-view-layout-'+self.view_id).append( self.hiddenCoordsForm ); 91 } 92 } 93
94 // Re-init form functionality after ajax pagination 95 $( document ).on('js_event_wpv_pagination_completed', function() { 96 self.initFormFunctionality(); 97 }); 98 99 // Re-init form functionality after form updated (reset button) 100 $( document ).on('js_event_wpv_parametric_search_form_updated', function( event, data ) { 101 // When using current visitors location, refresh that data 102 if ( self.is_using_current_location ) { 103 $( self.distanceCenterSelector ).val(self.getCurrentLatLng()); 104 $( self.userLocationSelector ).prop('disabled', true); 105 } 106 107 self.initFormFunctionality(); 108 }); 109 }; 110 111 /** 112 * Enables extra functionality on custom search form - geocomplete and geolocation 113 */ 114 self.initFormFunctionality = function() { 115 // Only init geocomplete if it's loaded (and that happens only if using Google API) 116 if ( $.fn.geocomplete ) { 117 // Delay init until google library is (maybe lazy) loaded. Note that this needs a Map somewhere on the page, 118 // otherwise google will never be loaded. (See WPViews.view_addon_maps.maybeLazyLoadGoogle). 119 if ( typeof google === 'undefined' ) { 120 _.delay( self.initFormFunctionality, 200 ); 121 return; 122 } 123 $(self.distanceCenterSelector).geocomplete({types: []}); 124 } else { 125 WPViews.mapsAddressAutocomplete.initField($(self.distanceCenterSelector)); 126 } 127 }; 128 129 self.areDistanceCenterFormElementsPresentOnPage = function() { 130 return ( 131 Boolean( $( self.distanceCenterLatSelector ).length ) 132 && Boolean( $( self.distanceCenterLngSelector ).length ) 133 ); 134 }; 135 136 self.isSecurePage = function () { 137 return (location.protocol === 'https:'); 138 } 139 140 self.acquireCurrentLocation = function(success, error) { 141 navigator.geolocation.getCurrentPosition(success, error); 142 }; 143 144 self.updateFilterCenter = function(position) { 145 if(position.coords) { 146 self.is_using_current_location = true; 147 148 self.current_lat = position.coords.latitude; 149 self.current_long = position.coords.longitude; 150 151 $( document ).trigger('toolset_maps_views_distance_filter_user_location_acquired'); 152 } else if (position.address_update) { 153 self.current_lat = position.location.latitude; 154 self.current_long = position.location.longitude; 155 } 156 157 $( document ).trigger('toolset_maps_views_distance_filter_center_updated', { 158 position: position 159 }); 160 }; 161 162 self.getCurrentLatLng = function() { 163 var current_latlng = ""; 164 if(self.current_lat != null && self.current_long != null) { 165 current_latlng = self.current_lat + ', ' + self.current_long; 166 } 167 return current_latlng; 168 169 } 170 171 self.updateQueryFilterWithUserLocation = function() { 172 if( 173 window.hasOwnProperty('toolset_maps_distance_filter_settings') 174 && toolset_maps_distance_filter_settings.use_user_location 175 ) { 176 self.acquireCurrentLocation(function(data) { 177 // Put coords into URL 178 var url = WPViews.view_frontend_utils.updateUrlQuery( 179 'toolset_maps_visitor_coords', 180 data.coords.latitude + ',' + data.coords.longitude, 181 window.location.href 182 ); 183 window.history.pushState(null, '', url); 184 185 // Put parametric data into (hidden) form 186 $( self.hiddenCoordsFormSelector ).data('parametric', { 187 id: self.view_id, 188 widget_id: self.view_id, 189 sort: {}, 190 environment: {} 191 } ); 192 193 var queryResultsPromise = WPViews.view_frontend_utils.get_updated_query_results( 194 self.view_id, 195 0, 196 $( self.hiddenCoordsFormSelector ), 197 'full' 198 ); 199 200 queryResultsPromise.done(function(data, textStatus, jqXHR){ 201 // We get the new form and layout together, one after each other. But, because there is no 202 // guaranteed container element, put new stuff in place of form, and remove layout, resulting in 203 // same order as before. Oh, and do that on the exact form and layout, using view id, because there 204 // may be multiple on page. 205 $( 'form[name^="wpv-filter-'+data.data.id+'"' ).replaceWith( data.data.full ); 206 $( 'div[id^=wpv-view-layout-'+data.data.id+']' ).remove(); 207 208 // After getting data by ajax, init maps to collect map data and render maps 209 // If G API key is missing, there may not be a map to render, so check for that too 210 if ( WPViews.view_addon_maps ) { 211 WPViews.view_addon_maps.init(); 212 WPViews.view_addon_maps.init_maps(); 213 } 214 }); 215 }, function(error) { 216 window.alert( toolset_maps_distance_filter_settings.geolocation_error + error.message ); 217 }); 218 } 219 }; 220 221 self.init(); 222}; 223 224jQuery( function( $ ) { 225 WPViews.distance_filter_frontend = new WPViews.DistanceFilterFrontend( $ ); 226});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.