-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
208 lines (178 loc) · 5.51 KB
/
index.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
'use strict';
const SEARCH_URL = "https://api.foursquare.com/v2/venues/";//explore";
// "https://api.foursquare.com/v2/venues/VENUE_ID";
const JOKE_SEARCH_URL = 'https://official-joke-api.appspot.com/random_joke';//'https://08ad1pao69.execute-api.us-east-1.amazonaws.com/dev/random_joke';
var myJoke ="";
var myPunchline="";
var venHours = "";
var venRating = 0;//==
/////////////////// FOURSQUARE API
function getDataFromApi(venueSearch, locationSearch, urlEXtension, callback) {
const query = {
client_id: 'WZZAVWT5OF4P4UPBSXZF53UJX3NLO3QPEHKIIBYYTAYQDINZ',
client_secret: 'F205HL4CMHFLA2LLXWXZDAGM2AONX354VWEFCW3MBBY2KKHN',
intent: 'global',
query: venueSearch, //`${locationSearch}`,
near: locationSearch, //`${venueSearch}`,
v: 20180604,
limit: 50
}
let result = $.ajax({
url: SEARCH_URL+urlEXtension,
data: query,
dataType: "json",
type: "GET"
})
.done(function(result) {
if (urlEXtension == "explore"){
callback(result);
}
})
.fail(function(result) {
console.log( "$.get failed" );
});
}
function callback(data) {
let display = data.response.groups['0'].items.map((item, index) => {
return renderResults(item)});
$("#searchResults").html(display);
makeCollapsible();
}
//how to render foursquare results
function renderResults(item, joke, punchline) {
let venueName = item.venue.name;
let venueAddress = item.venue.location.address;
let venueCrossStreet = item.venue.location.crossStreet;
let venueID = item.venue.id;
if (venueAddress == undefined) {
venueAddress = "Sorry, none provided.";
}
if (venueCrossStreet == undefined) {
venueCrossStreet = "Sorry, none provided.";
}
return `
<button class="collapsible focus">${venueName}</button>
<div class="content">
<ul>
<li>Address: ${venueAddress}</li>
<li>Cross Streets: ${venueCrossStreet}</li>
<li attr="${venueID}" class="goToVenue">
<a class="venue-url" href="#" data-venueID="${venueID}" target="_blank" >Go to Venue website ></a>
</li>
</ul>
</div>`
}// <p onclick="window.open('https://xxx.com')">xxx</p>
///// have to do a separate call to the foursquare api because the url is different to get details of venue... explore vs venue_id
function goToVenue(venueID,self){
const query = {
client_id: 'WZZAVWT5OF4P4UPBSXZF53UJX3NLO3QPEHKIIBYYTAYQDINZ',
client_secret: 'F205HL4CMHFLA2LLXWXZDAGM2AONX354VWEFCW3MBBY2KKHN',
intent: 'global',
v: 20180604,
limit: 5
}
let result = $.ajax({
url: `https://api.foursquare.com/v2/venues/${venueID}`,
data: query,
dataType: "json",
type: "GET"
})
.done(function(result) {
if (result.response.venue.url !== (undefined)){
console.log(self);
$(self).attr('href', result.response.venue.url);
$(self)[0].click();
// window.open(result.response.venue.url);
}
else {
alert("Sorry, No URL provided.");
}
})
.fail(function(result) {
console.log( "$.get gotovenue failed" );
});
}
//make search results list collapsible
function makeCollapsible(){
var coll = document.getElementsByClassName("collapsible");
var i;
for (i = 0; i < coll.length; i++) {
coll[i].addEventListener("click", function() {
this.classList.toggle("active");
var content = this.nextElementSibling;
if (content.style.maxHeight){
content.style.maxHeight = null;
} else {
content.style.maxHeight = content.scrollHeight + "px";
}
});
}};
//handle search button
function watchSubmit() {
$('#js-search-form').on('submit', function (event) {
event.preventDefault();
let venueSearch = $('#venue').val();
let locationSearch = $("#location").val();
let exploreURL = "explore";
$('#searchResults').prop('hidden', false);
//$("#initialrow").removeClass('col-12').addClass('col-6');
getDataFromApi(venueSearch, locationSearch, exploreURL, callback);
//$('.search').click(() => {
$('html, body').animate({
scrollTop: $("#searchResults").offset().top
},
1000);
//});
});
$('#searchResults').on('click','.venue-url',function(e){
if($(this).attr('href')=='#'){
e.preventDefault();
var venueID = $(this).attr('data-venueID');
goToVenue(venueID,this);
} else {
e.stopPropagation();
return true;
}
});
}
$(watchSubmit);
/////////////////// JOKE API
document.addEventListener('DOMContentLoaded', function() {
getDataFromJOKESApi();
}, false);
function getDataFromJOKESApi(){
$.getJSON((JOKE_SEARCH_URL), function(myResult) {
myJoke = myResult.setup;
myPunchline = myResult.punchline;
displayJokeData(myJoke, myPunchline);
});
;}
function renderResult(joke, punchline) {
return `
<div class="jokeCollapsible">
${joke}
</div>
<div class="jokeContent">
<p>${punchline}</p>
</div>
`;
}
function displayJokeData(joke, punchline) {
const results = renderResult(joke, punchline);
$('#jokeResults').html(results);
makeCollapsible2();
}
function makeCollapsible2(){
var coll = document.getElementsByClassName("jokeCollapsible");
var i;
for (i = 0; i < coll.length; i++) {
coll[i].addEventListener("click", function() {
this.classList.toggle("jokeActive");
var content = this.nextElementSibling;
if (content.style.maxHeight){
content.style.maxHeight = null;
} else {
content.style.maxHeight = content.scrollHeight + "px";
}
});
}};