-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
356 lines (310 loc) · 9.66 KB
/
index.html
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<!doctype html>
<html lang="en">
<head>
<title> Giphy </title>
<meta name="robots" content="index, follow" />
<meta charset="UTF-8">
<link rel="canonical" href="" />
<meta name="keywords" content="Giphy Single Page Application SPA CSS CSS3 HTML HTML5 Javascript AJAX Cayla Savitzky">
<meta name = "description" content = "A single page application that imitates Giphy. This is a demo page by Cayla Savitzky, which utilises AJAX but no framework">
<meta name="author" content="Cayla Savitzky">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!--
Possible reference material:
https://developers.giphy.com/docs
Optional Action Items:
hover animation over gifs
onclick doing something with gifs
adding some type of heading?
-->
<head>
<script>
var api_key = "ZiWvaBkvD0BNtOACTwJelb1CDZXJGWyE";
</script>
<style>
*:focus {
outline:none;
}
h1{
color:#66FF66;
text-align: center;
}
body {
background:black;
display: flex;
align-items: flex-start;
justify-content: center;
flex-wrap:wrap;
}
img, iframe {
border: 1px solid 777;
margin: 15px 0 15px 0;
}
.giphs_container {
margin:0 15px 0 15px;
width:200px;
}
.wide {
width:100%;
}
button {
background:black;
border: 1px solid #66FF66;
padding: 10 30 10 30;
color:#66FF66;
border-radius: 10px;
margin-left: 60%;
}
.search_holder{
position: relative;
display: flex;
justify-content: center;
}
#search_bar{
background:black;
border: 1px solid #66FF66;
padding: 10px 30px 10px 30px;
color:#66FF66;
margin-left:45px;
border-top-left-radius: 10px;
border-bottom-left-radius: 10px;
width: 60%;
}
.search_button {
background:black;
border: 1px solid #66FF66;
padding: 10px 30px 10px 30px;
color:#66FF66;
border-radius: 10px;
border-top-left-radius: 0px;
border-bottom-left-radius: 0px;
margin-left: 0;
position: relative;
right:45px;
}
</style>
<script>
var search_results = [];
var giphs_container = document.getElementsByClassName("giphs_container");
var giph_src_original_url_regex = /original":{("url":".*?")/g;
var giph_src_original_regex = /original":{.*?}/g;
var search_items = "cute+animals";
var view_number = 10;
var view_loads = 0;
var giph_original_array = [];
var giph_details = [];
var fruityRainbow = ["Radical Red","#FF355E",
"Wild Watermelon","#FD5B78",
"Outrageous Orange","#FF6037",
"Atomic Tangerine","#FF9966",
"Neon Carrot","#FF9933",
"Sunglow","#FFCC33",
"Laser Lemon","#FFFF66",
"Unmellow Yellow","#FFFF66",
"Electric Lime","#CCFF00",
"Screamin' Green","#66FF66",
"Magic Mint","#AAF0D1",
"Blizzard Blue","#50BFE6",
"Shocking Pink","#FF6EFF",
"Razzle Dazzle Rose","#EE34D2",
"Hot Magenta","#FF00CC",
"Purple Pizzazz","#FF00CC"]
function cleanurl (url)
{
var backslash_index = url.indexOf('\\');
while (backslash_index > -1) {
url = url.slice(0,url.indexOf('\\')) + url.slice(url.indexOf('\\')+1);
backslash_index = url.indexOf('\\');
}
return url;
}
function between_strings(element, start_item,end_item)
{
var start_loc = element.indexOf(start_item) + start_item.length;
var end_loc = element.indexOf(end_item,start_loc);
return element.slice(start_loc,end_loc);
}
function test_resize_need (){
var two_col = 245;
var col_width = 230;
var col_number = 1;
var screenwidth = window.innerWidth;
var next_total = two_col + col_number*col_width;
while (screenwidth>next_total){
col_number++;
next_total = two_col + col_number*col_width;
if (screenwidth < next_total){
return col_number;
}
}
}
function set_giphs_container (){
var body_header = document.body.innerHTML;
body_header = body_header.slice(0,body_header.indexOf("</header>") + "</header>".length)
var body = ""
var body_footer = document.body.innerHTML;
body_footer = body_footer.slice(body_footer.indexOf("<footer"),body_footer.indexOf("</footer>") + "</footer>".length)
var two_col = 245;
var col_width = 230;
var col_number = 1;
var screenwidth = window.innerWidth;
var next_total = two_col + col_number*col_width;
while (screenwidth>next_total){
col_number++;
next_total = two_col + col_number*col_width;
if (screenwidth < next_total){
for (i = 0; i < col_number; i++){
body = body + '<div class = "giphs_container"></div>';
}
}
}
if (col_number == 1){
for (i = 0; i < col_number; i++){
body = body + '<div class = "giphs_container"></div>';
}
}
document.body.innerHTML = body_header + body + body_footer;
var giphs_containers = document.getElementsByClassName("giphs_container");
}
function distribute_giphs (){
var giphs_containers = document.getElementsByClassName("giphs_container");
var giph_columns = giphs_container.length;
var giph_containers_heights = [];
for(var n = 0; n < giph_columns; n++){
giph_containers_heights.push(0);
}
/* POST GIPHS IN SUITABLE FASHION */
var i = 0;
giph_details.forEach(function(element){
var height = element[0]*200/element[1];
var smallest = 99999999999;
giph_containers_heights.forEach(function(element){if(element<smallest){smallest = element;}});
var smallest_item = giph_containers_heights.indexOf(smallest);
color = fruityRainbow[Math.floor(Math.random()*fruityRainbow.length/2)*2+1];
giphs_container[smallest_item].innerHTML = giphs_container[smallest_item].innerHTML + '<img src=' + element[2] + ' style = "width:200px; height:' + height+ 'px; background:'+ color + ';">';
i++;
giph_containers_heights[smallest_item] = giph_containers_heights[smallest_item] + height +30;
});
}
function resize_function (){
if (test_resize_need() != giphs_container.length){
set_giphs_container();
distribute_giphs();
console.log('resize');
}
}
function giph_request(){
search_request = "http://api.giphy.com/v1/gifs/search?q="+search_items+"&api_key=" + api_key+ "&limit="+view_number;
if (view_loads > 0){
search_request = search_request + "&offset="+view_loads*view_number;
}
var xhr = new XMLHttpRequest();
xhr.open("GET", search_request, true);
xhr.onload = function (e) {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
search_results.push(xhr.responseText);
var filtered_results = search_results[view_loads].match(giph_src_original_regex);
giph_original_array.push.apply(giph_original_array, filtered_results);
filtered_results.forEach(function(element) {
element_details = [];
element_details.push(cleanurl(between_strings(element,'height":"','"')));
element_details.push(cleanurl(between_strings(element,'width":"','"')));
element_details.push(cleanurl(between_strings(element,'url":"','"')));
element_details.push(cleanurl(between_strings(element,'mp4":"','"')));
giph_details.push(element_details);
});
set_giphs_container();
distribute_giphs();
view_loads++;
} else {
console.error(xhr.statusText);
}
}
};
xhr.onerror = function (e) {
console.error(xhr.statusText);
};
xhr.send(null);
}
giph_request();
/*
Regex for finding original giph
(/original":{("url":".*?"),"width":"([0-9]*)","height":"([0-9]*)","size":"([0-9]*)/g)
*/
function new_search(){
var search_bar_text = document.getElementById("search_bar").value;
var search_bar_text_original = search_bar_text.substr(0);
var position = 0;
position = search_bar_text.indexOf(" ");
search_items = "";
var n = 0;
while (position != -1){
if (position == 0){
search_bar_text = search_bar_text.substr(position+1);
}
else{
search_items = search_items + "+" + search_bar_text.slice(0,position)
search_bar_text = search_bar_text.substr(position);
}
position = search_bar_text.indexOf(" ");
n = n +1;
if (n == 10){
position = -1;
}
}
search_items = search_items + "+" + search_bar_text;
search_items = search_items.slice(1);
document.getElementById("search_bar").defaultValue = search_bar_text_original;
console.log(document.getElementById("search_bar").value);
view_loads = 0;
while (giph_original_array.length > 0)
{
giph_original_array.pop();
}
while (giph_details.length > 0)
{
giph_details.pop();
}
while (search_results.length > 0)
{
search_results.pop();
}
giph_request();
}
var j = [0,1,2,3,4,5]
function removej (){
while (j.length>0){
j.pop()
}
}
function keyEventHandler(event){
if (event.keyCode === 13) {
new_search();
}
}
</script>
</head>
<body onresize = "resize_function()">
<header class = "wide">
<div>
</div>
<div class = "wide">
<h1>
Fake-Giphy Knock-Off
</h1>
<div class = "search_holder">
<input id = "search_bar" type="subject" value = "Cute Animals" onkeypress = "keyEventHandler(event)">
<button class = "search_button" onclick="new_search();">
Search
</button>
</div>
</div>
</header>
<div class = "giphs_containers_holder">
</div>
<footer class = "wide">
<button onclick="giph_request();">Load More</button>
</footer>
</body>
</html>