-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
164 lines (138 loc) · 4.53 KB
/
script.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
var apiKey = "kIcbQMoCW5nw4kogd4UhHlAJh1zG5-GC0uyb6nYCFjI";
var unsplashURL = "https://api.unsplash.com/search/?query=";
var id = "&client_id="
var isSearching = false;
var timeInterval;
var alarmDiv = $("<div class='alarm'></div>").css("height", "25px");
//Events=====================================
$("#searchInput").on("keyup", function(e) {
if(e.which == 13 || e.keyCode == 13) {
initiateSearch();
}
});
$(".button").on("click", function(){
initiateSearch();
});
//FUNCTIONS===================================
// initiate search
function initiateSearch(){
if(isSearching === false){
resetBar();
isSearching = true;
clearInterval(timeInterval);
search();
countDown();
setTimeout(function(){isSearching = false;}, 2000);
}
}
// Timer-----------------------------------
function countDown(){
var timeLeft = 60; //change time limit to 60 seconds - kenny 4/19
var barWidth = 100;
timeInterval = setInterval(function(){
timeLeft--;
barWidth -= (10/6); //visual correction for bar reduction for 60 secs in a 100% bar. - kenny 4/19
$("#progress1").css("width", barWidth + "%");
if (timeLeft < 0){
clearInterval(timeInterval);
alarmDiv.text("Reminder: You've been here for 1 minute.");
$(".outline").css("height", "25px");
$(".outline").prepend(alarmDiv);
// alert("times up!");
glowUp();
}
},1000);
}
// remove reminder text
function resetBar(){
alarmDiv.remove();
$(".outline").css("height", "5px");
clearInterval(glowInterval);
$("#contentShow").css("border-style", "none");
}
//card glow functions
var glow = false;
var glowInterval;
function glowUp(){
glowInterval = setInterval(function(){
if (glow === false){
glow = true;
cardGlow();
}
else{
glow = false;
cardDim();
}
},500);
}
function cardGlow(){
$("#contentShow").css("border-style", "solid");
$("#contentShow").css("border-color", "#87D99E");
}
function cardDim(){
$("#contentShow").css("border-style", "grove");
$("#contentShow").css("border-color", "#5bb975");
}
//Performs main search---------------------------------------------------------------------------------------------
function search() {
var search = $("#searchInput").val();
$("#hideCard").attr('style', 'display: block');
$('#searchInput').val('');
wikiSearch(search);
}
//Function for unsplash search-------------------------------------------------------------------------------------
function imageSearch(search) {
$.ajax({
url: unsplashURL + search + id + apiKey,
method: 'GET'
})
.then(function(response){
console.log(response);
console.log("total photos: " + response.photos.total);
if(response.photos.total === 0)
{
$("#cardImage").attr("src", "noImg.jpg");
}
else{
var url = response.photos.results[0].urls.regular; //change pull regular quality pictures instead of small
$("#cardImage").attr("src", url);
}
});
}
//Function to search wikipedia for possible pages to display--------------------------------------------------------
function wikiSearch(search) {
var resultNum = 0;
$.ajax({
url: `https://en.wikipedia.org/w/api.php?format=json&action=query&list=search&srsearch=${search}`,
method: 'GET',
crossDomain: true,
dataType: 'jsonp',
}).then(function(data) {
console.log(data);
wikiRequest(data.query.search[resultNum].title);
});
}
//Function to search and display chosen article----------------------------------------------------------------------
function wikiRequest(search) {
$.ajax({
url: `https://en.wikipedia.org/api/rest_v1/page/summary/${search}`,
method: 'GET',
success: function(response) {
console.log(response);
if(response.type === 'disambiguation') {
console.log('ambiguous');
$('#cardText').text('');
$('#cardTitleSlide').text('');
$("#cardImage").attr("src", 'refine-search.png');
}else {
$('#cardText').text(response.extract);
$('#cardTitleSlide').text(response.title);
imageSearch(search);
}
},
error: function() {
resultNum++;
wikiRequest(data.query.search[resultNum].title);
}
});
}