forked from unhcr-jordan/services-advisor
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsearch.js
295 lines (252 loc) · 10.2 KB
/
search.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
var services = angular.module('services');
var gju = require('../../../node_modules/geojson-utils');
var Fuse = require('fuse.js');
/**
* Holds the state of the current search and the current results of that search
*/
services.factory('Search', [
'$q', '$rootScope', '$location', '_', 'LongTask', 'SiteSpecificConfig', 'RegionList', 'SectorList', 'ServicesList', 'Map',
($q, $rootScope, $location, _, LongTask, SiteSpecificConfig, RegionList, SectorList, ServicesList, Map) => {
var crossfilter = require('crossfilter')();
var fuse;
var categoryDimension = crossfilter.dimension((f) => f.servicesProvided);
var categoryGroup = categoryDimension.groupAll().reduce(reduceAdd('servicesProvided'), reduceRemove('servicesProvided'), reduceInitial);
var regionDimension = crossfilter.dimension((f) => f.region);
var regionGroup = regionDimension.groupAll().reduce(reduceAdd('region'), reduceRemove('region'), reduceInitial);
var textDimension = crossfilter.dimension((f) => f.id || undefined);
var partnerDimension = crossfilter.dimension((f) => f.organization.name || undefined);
var nationalityDimension = crossfilter.dimension((f) => f.nationality.split(', ') || undefined);
var locationDimension = crossfilter.dimension((f) => {
var [lon, lat] = f.location.geometry.coordinates;
return `${lon}, ${lat}` || "";
});
var idDimension = crossfilter.dimension((f) => f.id);
var referralsDimension = crossfilter.dimension((f) => f.referral.required);
/** Used to get list of currently filtered services rather than re-using an existing dimension **/
var metaDimension = crossfilter.dimension(function (f) { return f.id; });
var allDimensions = [
categoryDimension, regionDimension, textDimension, partnerDimension,
nationalityDimension, locationDimension, idDimension,
referralsDimension
];
// asynchronously initialize crossfilter
fetchData()
.then(indexServices)
.then(() => $rootScope.$broadcast('FILTER_CHANGED', _getCurrResults()));
return {
selectId: withClearAndEmit(selectId),
currResults: _getCurrResults,
filterByUrlParameters: () => LongTask.run(withClearAndEmit(filterByUrlParameters)),
getCategoryGroup: categoryGroup,
getRegionGroup: regionGroup,
// selectCategory: withClearAndEmit(selectCategory),
// selectRegion: withClearAndEmit(selectRegion),
// selectPartner: withClearAndEmit(selectPartner),
// selectOrganizations: _selectOrganizations,
// clearOrganizations: _clearOrganizations,
// selectLocation: withClearAndEmit(selectLocation),
// filterByProximity: withClearAndEmit(filterByProximity),
// selectReferrals : _selectReferrals,
};
function fetchData() {
return Promise.all([
RegionList.get(),
SectorList.get(),
ServicesList.get()
]);
}
function reduceInitial() {
return {};
}
function reduceAdd(property) {
return (p, v) => {
_.each(v[property], (value) => {
if (p[value] === undefined) {
p[value] = 0;
}
p[value]++;
});
return p;
}
}
function reduceRemove(property) {
return (p, v) => {
_.each(v[property], (value) => {
p[value]--;
});
return p;
}
}
function indexServices([regions, sectors, services]) {
$rootScope.allServices = services;
crossfilter.add(services);
fuse = new Fuse(services, SiteSpecificConfig.search);
}
function _getCurrResults() {
return metaDimension.top(Infinity);
}
function clearAll() {
angular.forEach(allDimensions, (filter) => filter.filterAll());
}
// this function allows us to wrap another function with clearAll() and $emit to reduce boilerplate
function withClearAndEmit(fn) {
return function clearAndEmit() {
clearAll();
var results = fn.apply(this, arguments);
$rootScope.$broadcast('FILTER_CHANGED', results);
return results;
};
}
function selectId(id) {
idDimension.filter((serviceId) => serviceId == id);
return _getCurrResults();
}
// function selectCategory(category) {
// _selectCategory([category]);
// return _getCurrResults();
// }
// function selectRegion(region) {
// _selectRegion([region]);
// return _getCurrResults();
// }
// function selectPartner(partner) {
// partnerDimension.filter(function(servicePartner) {
// return servicePartner == partner;
// });
// return _getCurrResults();
// }
// function selectLocation(region) {
// var activeRegionLayer = null;
// // TODO: Where is polygonLayer coming from??
// Map.polygonLayer().getLayers().forEach(function(f) {
// if (f.feature.properties.adm1_name == region) {
// activeRegionLayer = f;
// }
// });
// if (activeRegionLayer) {
// locationDimension.filter((servicePoint) => {
// var pp = servicePoint.split(',');
// var point = {
// type: "Point",
// coordinates: [parseFloat(pp[1]), parseFloat(pp[0])]
// };
// return gju.pointInPolygon(point, activeRegionLayer.toGeoJSON().geometry);
// });
// }
// }
// function filterByProximity(geoLocation){
// var requiredArgumentGiven = _.has(geoLocation, 'latitude') &&
// _.has(geoLocation, 'longitude') && _.has(geoLocation, 'radius');
// if (requiredArgumentGiven) {
// var center = gju.rectangleCentroid({
// "type": "Polygon",
// "coordinates": [[ [geoLocation.latitude, geoLocation.longitude],
// [geoLocation.latitude, geoLocation.longitude],
// [geoLocation.latitude, geoLocation.longitude]
// ]]
// });
// var radius = geoLocation.radius;
// locationDimension.filter((servicePoint) => {
// var pp = servicePoint.split(',');
// var point = {
// type: "Point",
// coordinates: [parseFloat(pp[1]), parseFloat(pp[0])]
// };
// return gju.geometryWithinRadius(point, center, radius);
// });
// } else {
// console.log(' Please provide the pass in object into filterByProximity method with the following keys: latitude, longitude, and radius');
// }
// }
// Crossfilter dimensions
function _searchText(search) {
var result = search.length > 0 ? fuse.search(search): [];
textDimension.filter((serviceId) => {
return _.contains(result, serviceId);
});
}
// function _clearOrganizations() {
// partnerDimension.filterAll();
// }
function _selectOrganizations(organizations) {
partnerDimension.filter((serviceOrganization) => {
return organizations.indexOf(serviceOrganization) > -1;
});
}
function _selectNationalities(nationalities) {
nationalityDimension.filter((serviceNationalities) => {
return _.intersection(nationalities, serviceNationalities).length > 0;
});
}
function _selectReferrals(selection) {
referralsDimension.filter(function(service_requires_referral) {
// if they've selected all, then we return everything, otherwise we try to match
if (selection == 'all'){
return true;
} else if (service_requires_referral && selection == 'referral-required'){
return true;
} else if (!service_requires_referral && selection == 'referral-not-required'){
return true;
} else {
return false;
}
});
}
function _selectCategory(categories) {
categoryDimension.filter((f) => {
var intersection = _.intersection(f, categories);
return _.isEqual(intersection, categories);
});
return _getCurrResults();
}
function _selectRegion(regions) {
regionDimension.filter(function (f) {
var intersection = _.intersection(f, regions);
return _.isEqual(intersection, regions);
});
}
function _selectLocation(location) {
var activeLocationLayer = null;
Map.polygonLayer().getLayers().forEach((f) => {
if (f.feature.properties.adm1_name == location) {
activeLocationLayer = f;
}
});
if (activeLocationLayer) {
locationDimension.filter((servicePoint) => {
var pp = servicePoint.split(',');
var point = {
type: "Point",
coordinates: [parseFloat(pp[1]), parseFloat(pp[0])]
};
return gju.pointInPolygon(point, activeLocationLayer.toGeoJSON().geometry);
});
}
return _getCurrResults();
}
function filterByUrlParameters() {
var parameters = $location.search();
if (_.has(parameters, 'search')) {
_searchText(parameters.search);
}
if (_.has(parameters, 'organization') && parameters.organization.length > 0) {
_selectOrganizations(parameters.organization);
}
if (_.has(parameters, 'nationality') && parameters.nationality.length > 0) {
_selectNationalities(parameters.nationality);
}
if (_.has(parameters, 'referrals')) {
_selectReferrals(parameters.referrals);
}
if (_.has(parameters, 'category')) {
_selectCategory(parameters.category);
}
if (_.has(parameters, 'region')) {
_selectRegion(parameters.region);
}
if (_.has(parameters, 'location')) {
_selectLocation(parameters.location);
}
return _getCurrResults();
}
}]);