22import { buildOpenNowCriteria , buildSearchCriteria } from "./shared" ;
33import state from "../../js/state" ;
44import WidgetController from "../../widget.controller" ;
5-
6- const SearchLocationsModes = {
7- All : "All" ,
8- UserPosition : "UserPosition" ,
9- AreaRadius : "AreaRadius" ,
10- } ;
5+ import constants from "../../js/constants" ;
116
127const IntroSearchService = {
13- _setupNearPipelines ( query ) {
14- const pipelines = [ ] ;
15-
16- const $geoNear = {
17- near : { type : "Point" , coordinates : [ state . currentLocation . lng , state . currentLocation . lat ] } ,
18- key : "_buildfire.geo" ,
19- maxDistance : 100000 ,
20- distanceField : "distance" ,
21- query : { ...query }
22- } ;
23- pipelines . push ( { $geoNear } ) ;
24-
25- if ( state . searchCriteria . sort ) {
26- const $sort = { } ;
27- $sort [ state . introSort . sortBy ] = state . introSort . order ;
28- pipelines . push ( { $sort } ) ;
8+ _getUserCoordinates ( ) {
9+ const coordinates = [ ] ;
10+ if ( state . userPosition && state . userPosition . latitude && state . userPosition . longitude ) {
11+ coordinates . push ( state . userPosition . longitude ) ;
12+ coordinates . push ( state . userPosition . latitude ) ;
13+ } else {
14+ const defaultPosition = constants . getDefaultLocation ( ) ;
15+ coordinates . push ( defaultPosition . lng ) ;
16+ coordinates . push ( defaultPosition . lat ) ;
2917 }
30-
31- return pipelines ;
18+
19+ return coordinates ;
3220 } ,
3321
34- _setupAreaRadiusPipelines ( query ) {
22+ _setUpIntroGeoQuery ( query ) {
3523 const pipelines = [ ] ;
24+ let centerSphere , radius ;
25+
26+ if ( state . settings . introductoryListView . searchOptions ?. mode === constants . SearchLocationsModes . AreaRadius ) {
27+ centerSphere = [
28+ state . settings . introductoryListView . searchOptions ?. areaRadiusOptions ?. lng || 1 ,
29+ state . settings . introductoryListView . searchOptions ?. areaRadiusOptions ?. lat || 1 ,
30+ ] ;
31+ const _radiusMiles = state . settings . introductoryListView . searchOptions ?. areaRadiusOptions ?. radius || 1 ;
32+ radius = _radiusMiles / 3963.2 ; // convert miles to radians
33+ } else {
34+ centerSphere = IntroSearchService . _getUserCoordinates ( ) ;
35+ radius = 100 / 6378.1 ; // 100 km in radians
36+ }
3637
37- const lng = state . settings . introductoryListView . searchOptions ?. areaRadiusOptions ?. lng || 1 ;
38- const lat = state . settings . introductoryListView . searchOptions ?. areaRadiusOptions ?. lat || 1 ;
39- const radius = state . settings . introductoryListView . searchOptions ?. areaRadiusOptions ?. radius || 1 ;
38+ if ( state . currentLocation ) { // this is for user search by location name
39+ centerSphere = [ state . currentLocation . lng , state . currentLocation . lat ] ;
40+ }
4041
41- const $geoNear = {
42- near : { type : "Point" , coordinates : [ state . currentLocation . lng , state . currentLocation . lat ] } ,
42+ const $geoNear = { // near user position to calculate distance for each location
43+ near : { type : "Point" , coordinates : IntroSearchService . _getUserCoordinates ( ) } ,
4344 key : "_buildfire.geo" ,
4445 distanceField : "distance" ,
4546 query : { ...query }
4647 } ;
4748 const $match = {
4849 "_buildfire.geo" : {
49- $geoWithin : {
50- $centerSphere : [ [ lng , lat ] , radius / 3963.2 ] // convert miles to radians
50+ $geoWithin : { // find locations within a specific area
51+ $centerSphere : [ centerSphere , radius ]
5152 }
5253 }
5354 } ;
@@ -91,19 +92,16 @@ const IntroSearchService = {
9192 const query = buildSearchCriteria ( ) ;
9293 let pipelines ;
9394
94- if ( state . settings . introductoryListView . searchOptions ?. mode === SearchLocationsModes . All ) {
95+ if ( state . settings . introductoryListView . searchOptions ?. mode === constants . SearchLocationsModes . All && ! state . currentLocation ) {
9596 if ( ! state . fetchingAllNearReached ) {
96- pipelines = this . _setupNearPipelines ( query ) ;
97+ pipelines = this . _setUpIntroGeoQuery ( query ) ;
9798 } else {
9899 pipelines = this . _setupOtherLocationsPipelines ( query ) ;
99100 }
100- } else if ( state . settings . introductoryListView . searchOptions ?. mode === SearchLocationsModes . AreaRadius ) {
101- state . fetchingAllNearReached = true ;
102- pipelines = this . _setupAreaRadiusPipelines ( query ) ;
103101 } else {
104102 // the default search mode is UserPosition
105103 state . fetchingAllNearReached = true ;
106- pipelines = this . _setupNearPipelines ( query ) ;
104+ pipelines = this . _setUpIntroGeoQuery ( query ) ;
107105 }
108106
109107 return pipelines ;
@@ -125,7 +123,7 @@ const IntroSearchService = {
125123 state . fetchingNextPage = false ;
126124 state . fetchingEndReached = aggregateLocations . length < state . searchCriteria . pageSize && state . fetchingAllNearReached ;
127125
128- if ( aggregateLocations . length < state . searchCriteria . pageSize && ! state . fetchingAllNearReached && state . settings . introductoryListView . searchOptions ?. mode === SearchLocationsModes . All ) {
126+ if ( aggregateLocations . length < state . searchCriteria . pageSize && ! state . fetchingAllNearReached && state . settings . introductoryListView . searchOptions ?. mode === constants . SearchLocationsModes . All ) {
129127 state . fetchingAllNearReached = true ;
130128 state . searchCriteria . page = 0 ;
131129 state . printOtherLocationMessage = true ;
0 commit comments