1( function( $ ) { 2 3 "use strict"; 4 5 const initLocationDistanceFilter = function() { 6 7 window.JetSmartFilters.filtersList.JetEngineLocationDistance = 'jet-smart-filters-location-distance'; 8 window.JetSmartFilters.filters.JetEngineLocationDistance = class JetEngineLocationDistance extends window.JetSmartFilters.filters.Search { 9 10 name = 'location-distance'; 11 currentLocationVerbose = 'Your current location'; 12 defaultLocation = null; 13 $locationParent = null; 14 $locationInput = null; 15 $locationDropdown = null; 16 $distanceInput = null; 17 18 constructor( $container ) { 19 20 const $filter = $container.find( '.jsf-location-distance' ); 21 22 super( $container, $filter ); 23 24 this.locationData = {}; 25 26 this.$locationParent = $filter.find( '.jsf-location-distance__location' ); 27 this.$locationInput = $filter.find( '.jsf-location-distance__location-input' ); 28 this.$locationDropdown = $filter.find( '.jsf-location-distance__location-dropdown' ); 29 this.$distanceInput = $filter.find( '.jsf-location-distance__distance' ); 30 31 if ( $filter.data( 'geolocation-placeholder' ) ) { 32 this.currentLocationVerbose = $filter.data( 'geolocation-placeholder' ); 33 } 34 35 this.$distanceInput.on( 'change', ( event ) => { 36 this.updateLocationData( { distance: event.target.value } ); 37 } ); 38 39 this.defaultLocation = $filter.data( 'current-location' ); 40 41 if ( this.defaultLocation ) { 42 this.updateLocationData( this.defaultLocation, true ); 43 } 44 45 if ( this.defaultLocation.address ) { 46 this.$locationInput.val( this.defaultLocation.address ); 47 } 48 49 if ( this.defaultLocation.distance ) { 50 this.selectDistance( this.defaultLocation.distance ); 51 } 52 53 this.$locationInput.on( 'keyup', ( event ) => { 54 55 var keyCode = ( event.keyCode ? event.keyCode : event.which ); 56 57 if ( event.target.value.length ) { 58 this.switchClear( true ); 59 this.switchLocate(); 60 } else { 61 this.switchClear(); 62 this.switchLocate( true ); 63 } 64 65 if ( 3 > event.target.value.length ) { 66 this.clearDropdown(); 67 return; 68 } 69 70 this.updateLocationData( { 71 address: event.target.value, 72 }, true ); 73 74 if ( 13 === keyCode ) { 75 this.maybeApplyFilter(); 76 this.clearDropdown(); 77 return; 78 } 79 80 $.ajax({ 81 url: window.JetMapListingLocationDistanceFilterData.apiAutocomplete, 82 type: 'GET', 83 dataType: 'json', 84 data: { query: event.target.value }, 85 }).done( ( response ) => { 86 87 this.clearDropdown(); 88 89 if ( ! response || ! response.success ) { 90 return; 91 } 92 93 this.$locationDropdown.addClass( 'is-active' ); 94 95 for ( var i = 0; i < response.data.length; i++ ) { 96 this.$locationDropdown.append( 97 '<div class="jsf-location-distance__location-dropdown-item" data-address="' + response.data[ i ].address + '">' 98 + response.data[ i ].address + 99 '</div>' 100 ); 101 } 102 103 }); 104 105 } ); 106 107 this.$locationDropdown.on( 'click', '.jsf-location-distance__location-dropdown-item', ( event ) => { 108 109 const $dropdownItem = $( event.target ); 110 111 this.$locationInput.val( $dropdownItem.data( 'address' ) ); 112 this.clearDropdown(); 113 114 this.updateLocationData( { 115 address: $dropdownItem.data( 'address' ), 116 latitude: 0, 117 longitude: 0, 118 } ); 119 120 } ); 121 122 this.$locationParent.on( 'click', '.jsf-location-distance__location-clear', ( event ) => { 123 //this.clearLocation(); 124 this.reset(); 125 } ); 126 127 this.$locationParent.on( 'click', '.jsf-location-distance__location-locate', ( event ) => { 128 129 this.switchLocate(); 130 this.switchLoading( true ); 131 132 navigator.geolocation.getCurrentPosition( ( position ) => { 133 134 this.switchLoading(); 135 this.$locationInput.val( this.currentLocationVerbose ); 136 this.switchClear( true ); 137 138 this.updateLocationData( { 139 address: 0, 140 latitude: position.coords.latitude, 141 longitude: position.coords.longitude, 142 } ); 143 144 } ); 145 146 } ); 147 148 $( document ).on( 'click', ( event ) => { 149 150 const $parent = $( event.target ).closest( '.jsf-location-distance__location' ); 151 152 if ( ! $parent.length ) { 153 this.clearDropdown(); 154 } 155 156 } ); 157 158 } 159 160 maybeApplyFilter() { 161 162 const applyOn = [ 'ajax', 'mixed' ]; 163 164 if ( ! applyOn.includes( this.applyType ) ) { 165 return; 166 } 167 168 this.emitFiterApply(); 169 170 } 171 172 reset() { 173 this.resetLocationData( false ); 174 } 175 176 resetLocationData( silent ) { 177 this.updateLocationData( {}, silent, [ 'address', 'longitude', 'latitude', 'distance' ] ); 178 this.clearDistance(); 179 this.clearLocation(); 180 } 181 182 setData( newData ) { 183 184 this.resetLocationData( true ); 185 186 if ( newData ) { 187 this.updateLocationData( { ...newData }, true ); 188 } 189 190 if ( newData && undefined !== newData.address ) { 191 192 if ( 0 == newData.address && newData.latitude && newData.longitude ) { 193 this.$locationInput.val( this.currentLocationVerbose ); 194 } else { 195 this.$locationInput.val( newData.address ); 196 } 197 198 this.switchClear( true ); 199 this.switchLocate(); 200 201 } 202 203 if ( newData && newData.distance ) { 204 this.selectDistance( newData.distance ); 205 } 206 207 } 208 209 selectDistance( distance ) { 210 this.clearDistance(); 211 this.$distanceInput.find( 'option[value="' + distance + '"]' ) 212 .prop( 'selected', true ) 213 .attr( 'selected', true ); 214 } 215 216 clearDistance() { 217 this.$distanceInput.find( 'option[selected]' ) 218 .removeProp( 'selected' ) 219 .removeAttr( 'selected' ); 220 } 221 222 get activeValue() { 223 224 if ( ! this.hasLocation() && ! this.locationData.distance ) { 225 return false;
226 } 227 228 const activeValue = []; 229 230 if ( this.locationData.address && 0 != this.locationData.address ) { 231 activeValue.push( this.locationData.address ); 232 } else if ( this.locationData.latitude && this.locationData.longitude ) { 233 activeValue.push( this.currentLocationVerbose ); 234 } 235 236 activeValue.push( this.locationData.distance + this.locationData.units ); 237 238 return activeValue.join( ', ' ); 239 240 } 241 242 locationDataIsEmpty() { 243 244 if ( ! this.locationData || 0 === Object.keys( this.locationData ).length ) { 245 return true; 246 } 247 248 if ( ! this.hasLocation() && ! this.locationData.distance ) { 249 return true; 250 } 251 252 return false; 253 } 254 255 // Getters 256 get data() { 257 258 if ( this.dataValue && ! this.disabled ) { 259 return { ...this.dataValue }; 260 } else if ( ! this.locationDataIsEmpty() && ! this.disabled ) { 261 return { ...this.locationData }; 262 } else { 263 //console.log( 'Empty' ); 264 return false; 265 } 266 267 } 268 269 processData() { 270 if ( ! this.locationDataIsEmpty ) { 271 this.dataValue = { ...this.locationData }; 272 } else { 273 this.dataValue = false; 274 } 275 } 276 277 clearLocation() { 278 279 this.$locationInput.val( '' ); 280 this.clearDropdown(); 281 282 this.switchClear(); 283 this.switchLocate( true ); 284 285 } 286 287 hasLocation() { 288 if ( 289 ! this.locationData 290 || ( ! this.locationData.address 291 && ! this.locationData.latitude 292 && ! this.locationData.longitude ) 293 ) { 294 return false; 295 } else { 296 return true; 297 } 298 } 299 300 clearDropdown() { 301 this.$locationDropdown.html( '' ).removeClass( 'is-active' ); 302 } 303 304 switchClear( show ) { 305 if ( show && ! this.$locationParent.hasClass( 'jsf-show-clear' ) ) { 306 this.$locationParent.addClass( 'jsf-show-clear' ); 307 } else if ( ! show ) { 308 this.$locationParent.removeClass( 'jsf-show-clear' ); 309 } 310 } 311 312 switchLoading( show ) { 313 if ( show && ! this.$locationParent.hasClass( 'jsf-show-loading' ) ) { 314 this.$locationParent.addClass( 'jsf-show-loading' ); 315 } else if ( ! show ) { 316 this.$locationParent.removeClass( 'jsf-show-loading' ); 317 } 318 } 319 320 switchLocate( show ) { 321 322 if ( ! navigator.geolocation ) { 323 this.$locationParent.removeClass( 'jsf-show-locate' ); 324 return; 325 } 326 327 if ( show && ! this.$locationParent.hasClass( 'jsf-show-locate' ) ) { 328 this.$locationParent.addClass( 'jsf-show-locate' ); 329 } else if ( ! show ) { 330 this.$locationParent.removeClass( 'jsf-show-locate' ); 331 } 332 } 333 334 updateLocationData( newData, silent, deleteData ) { 335 336 if ( 0 !== Object.keys( newData ).length ) { 337 338 // The following condition is commented to prevent conflicts with Reload or Mixed apply types. 339 // if ( 0 !== Object.keys( this.defaultLocation ).length ) { 340 // newData = { ...this.defaultLocation, ...newData }; 341 // } 342 343 for ( const prop in newData ) { 344 this.locationData[ prop ] = newData[ prop ]; 345 } 346 347 if ( ! this.locationData.distance ) { 348 this.locationData.distance = this.$distanceInput.data( 'default' ); 349 } 350 351 if ( ! this.locationData.units ) { 352 this.locationData.units = this.$distanceInput.data( 'units' ); 353 } 354 355 } 356 357 if ( deleteData ) { 358 for ( var i = 0; i < deleteData.length; i++ ) { 359 delete this.locationData[ deleteData[ i ] ]; 360 } 361 } 362 363 if ( ! silent ) { 364 this.emitFiterChange(); 365 this.maybeApplyFilter(); 366 } 367 368 } 369 370 }; 371 } 372 373 // Added the filter name to the `filterNames` property on `DOMContentLoaded` event before `elementor/frontend/init` event 374 // to better compatibility with Elementor Popup. 375 window.addEventListener( 'DOMContentLoaded', () => { 376 if ( window.JetSmartFilters && -1 === window.JetSmartFilters.filterNames.indexOf( 'location-distance' ) ) { 377 window.JetSmartFilters.filterNames.push( 'location-distance' ); 378 } 379 } ); 380 381 if ( window.JetMapListingGeolocationFilterData && 'jet-smart-filters/before-init' === window.JetMapListingGeolocationFilterData.initEvent ) { 382 document.addEventListener( 'jet-smart-filters/before-init', ( e ) => { 383 initLocationDistanceFilter(); 384 }); 385 } else { 386 window.addEventListener( 'DOMContentLoaded', ( e ) => { 387 initLocationDistanceFilter(); 388 }); 389 } 390 391}( jQuery ) );
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.