-
Notifications
You must be signed in to change notification settings - Fork 0
/
listingFactory.js
45 lines (32 loc) · 1.6 KB
/
listingFactory.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
angular.module('listings').factory('Listings', function($http) {
var methods = {
search: function(query, startDate, endDate, isImage, isVideo, isAudio, location) {
// This function handles converting the search query and any filter inputs into a url which the NASA api is happy with
query = encodeURIComponent(query.trim()); // encode for URL
// below filter conditions makes no sense, assume user got confused
if (startDate != undefined && endDate != undefined && startDate > endDate) [startDate, endDate] = [endDate, startDate];
var search = 'https://images-api.nasa.gov/search?q=' + query;
var media = '';
if (startDate != undefined) search += '&year_start=' + startDate;
if (endDate != undefined) search += '&year_end=' + endDate;
if (location != undefined) search += '&location=' + location;
// the great thing about how this (below) code works, is that if the user has all 3 boxes (image, video, audio) UNchecked,
// then it will just search using all three as if they were checked. There's no point in searching for nothing.
if (isImage === true) media += 'image';
if (isVideo === true) {
if (media.length > 0) media += ',';
media += 'video';
}
if (isAudio === true) {
if (media.length > 0) media += ',';
media += 'audio';
}
if (media.length > 0) search += '&media_type=' + media;
return $http.get(search);
},
getLink: function(link) { // We pass in a link, and return the json. Very simple, but works really well
return $http.get(link);
}
};
return methods;
});