-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistingController.js
183 lines (140 loc) · 6.94 KB
/
listingController.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
angular.module('listings').controller('ListingsController', ['$scope', 'Listings',
function($scope, Listings) {
$scope.listings = []; //contains search results of current page
$scope.nav = []; //contains next and/or prev page links
$scope.hits = []; //contains the number of results
$scope.curPage = 0; //stores the current page
$scope.curEndRange = 0; //stores the current end of the range
$scope.history = []; //stores search history
// (below) updates history var if items exist
if (window.localStorage.getItem('nasaHistory') != null) $scope.history = window.localStorage.getItem('nasaHistory').split(0x00);
/* loads listings with a default view so visitors have something to see by default */
/* search using query, then bind to scope */
Listings.search("earth").then(function(response) {
$scope.listings = response.data.collection.items;
$scope.nav = response.data.collection.links;
$scope.hits = response.data.collection.metadata.total_hits;
if ($scope.hits - (($scope.curPage+1)*100) < 0) $scope.curEndRange = $scope.hits;
else $scope.curEndRange = (($scope.curPage+1)*100);
console.log($scope.listings);
}, function(error) {
console.log('Unable to retrieve listings:', error);
});
$scope.search = function() {
// add to search history
$scope.history.push($scope.query);
// update localStorage history (separated by the null character)
window.localStorage.setItem('nasaHistory', $scope.history.join(0x00));
/* search using query, then bind to scope */
Listings.search($scope.query, $scope.startDate, $scope.endDate, $scope.isImage, $scope.isVideo, $scope.isAudio, $scope.location).then(function(response) {
$scope.listings = response.data.collection.items;
$scope.nav = response.data.collection.links;
$scope.hits = response.data.collection.metadata.total_hits;
$scope.curPage = 0;
if ($scope.hits - (($scope.curPage+1)*100) < 0) $scope.curEndRange = $scope.hits;
else $scope.curEndRange = (($scope.curPage+1)*100);
console.log($scope.listings);
}, function(error) {
console.log('Unable to retrieve listings:', error);
});
};
$scope.clearHistory = function() {
// wipes search history
$scope.history = [];
window.localStorage.removeItem('nasaHistory');
}
$scope.share = function(type, link, title, tags) {
if (type === "fb") { // facebook
window.open("https://www.facebook.com/sharer.php?u=" + encodeURIComponent(link.trim())); // encode for URL
}
else if (type === "tw") { // twitter
var hashTags = '';
for (var i = 0; i < tags.length; i++) { // iterate though keyworks and make them into a hashtag format that the twitter url understands (item1,item2,item3,etc...)
if (i < tags.length-1) hashTags += tags[i] + ',';
else hashTags += tags[i];
}
window.open('https://twitter.com/intent/tweet?url=' + encodeURIComponent('https://nasa-images-search.herokuapp.com/'.trim()) + '&text=Check+out+' + encodeURIComponent(title.trim()) + '+from+NASA!&hashtags=' + hashTags); // encode for URL
}
else { // email
window.open('mailto:' + '?subject=' + encodeURIComponent(title.trim()) + '&body=Check out this awesomeness from NASA! ' + encodeURIComponent(link.trim())); //encode for URL
}
}
$scope.getPrev = function() {
// if prev exists
if ($scope.nav.length > 0 && $scope.nav[0].rel === "prev") {
/* search using prev href, then bind to scope */
Listings.getLink($scope.nav[0].href).then(function(response) {
$scope.listings = response.data.collection.items;
$scope.nav = response.data.collection.links;
$scope.curPage = $scope.curPage - 1;
if ($scope.hits - (($scope.curPage+1)*100) < 0) $scope.curEndRange = $scope.hits;
else $scope.curEndRange = (($scope.curPage+1)*100);
console.log($scope.listings);
}, function(error) {
console.log('Unable to retrieve listings:', error);
});
}
}
$scope.getNext = function() {
// if next exists in first index
if ($scope.nav.length > 0 && $scope.nav[0].rel === "next") {
/* search using next href, then bind to scope */
Listings.getLink($scope.nav[0].href).then(function(response) {
$scope.listings = response.data.collection.items;
$scope.nav = response.data.collection.links;
$scope.curPage = $scope.curPage + 1;
if ($scope.hits - (($scope.curPage+1)*100) < 0) $scope.curEndRange = $scope.hits;
else $scope.curEndRange = (($scope.curPage+1)*100);
console.log($scope.listings);
}, function(error) {
console.log('Unable to retrieve listings:', error);
});
}
// check second index
if ($scope.nav.length > 1 && $scope.nav[1].rel === "next") {
/* search using next href, then bind to scope */
Listings.getLink($scope.nav[1].href).then(function(response) {
$scope.listings = response.data.collection.items;
$scope.nav = response.data.collection.links;
$scope.curPage = $scope.curPage + 1;
if ($scope.hits - (($scope.curPage+1)*100) < 0) $scope.curEndRange = $scope.hits;
else $scope.curEndRange = (($scope.curPage+1)*100);
console.log($scope.listings);
}, function(error) {
console.log('Unable to retrieve listings:', error);
});
}
}
$scope.getFile = function(type, url) {
/* search using url, then bind to scope */
Listings.getLink(url).then(function(response) {
if (type === "image") { // if image type
for (var i = 0; i < response.data.length; i++) { // iterate through respoonse data
if (response.data[i].search("jpg") != -1) { // looking for jpg
window.open(response.data[i]); // open in new tab
i = response.data.length;
}
}
}
else if (type === "video") { // if video type
for (var i = 0; i < response.data.length; i++) { // iterate through respoonse data
if (response.data[i].search("mp4") != -1) { // looking for mp4
window.open(response.data[i]); // open in new tab
i = response.data.length;
}
}
}
else if (type === "audio") { // if audio type
for (var i = 0; i < response.data.length; i++) { // iterate through respoonse data
if (response.data[i].contains("wav")) { // looking for mp4
window.open(response.data[i]); // open in new tab
i = response.data.length;
}
}
}
}, function(error) {
console.log('Unable to retrieve listings:', error);
});
}
}
]);