-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
420 lines (377 loc) · 11.5 KB
/
scripts.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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
$(document).ready(function () {
// Quotes ===============================================================
/**
* Creates a carousel item and appends it to the DOM
* @data {Array} List of data to be displayed as Quotes
* @return {undefined} No return
*/
function displayQuotes(data) {
let classItem = "";
for (let i in data) {
classItem = i == 0 ? "carousel-item active" : "carousel-item";
let $carouselItem = $(`
<blockquote class="${classItem}">
<div class="row mx-auto align-items-center">
<div class="col-12 col-sm-2 col-lg-2 offset-lg-1 text-center">
<img
src="${data[i].pic_url}"
class="d-block align-self-center"
alt="Carousel Pic ${i}"
/>
</div>
<div class="col-12 col-sm-7 offset-sm-2 col-lg-9 offset-lg-0">
<div class="quote-text">
<p class="text-white pr-md-4 pr-lg-5">
${data[i].text}
</p>
<h4 class="text-white font-weight-bold">${data[i].name}</h4>
<span class="text-white">${data[i].title}</span>
</div>
</div>
</div>
</blockquote>;
`);
$("#carousel-items").append($carouselItem);
}
// END OF displayQuotes
}
/**
* Modifies to logic of bootstrap carousel in order for
* it to move 1 slide at a time
* @id {string} Id of a carousel item slide
* @return {undefined} No return
*/
function slideOne(id) {
$(`#${id} .carousel-item`).each(function () {
let minPerSlide = 4;
let next = $(this).next();
if (!next.length) {
next = $(this).siblings(":first");
}
next.children(":first-child").clone().appendTo($(this));
for (let i = 0; i < minPerSlide; i++) {
next = next.next();
if (!next.length) {
next = $(this).siblings(":first");
}
next.children(":first-child").clone().appendTo($(this));
}
});
}
/**
* Creates the string equivalent of a card element in bootstrap
* @cardData {object} An object containing data for creating the card
* @return {string} string equivalent of a card element in bootstrap
*/
function createCard(cardData) {
let starState = "";
let starString = "";
let star;
for (let i = 1; i <= 5; i++) {
if (i <= cardData.star) {
starState = "images/star_on.png";
} else {
starState = "images/star_off.png";
}
star = `<img src="${starState}" alt="star on" width="15px" />`;
starString += i == 1 ? star : "\n" + star;
}
let card = `
<div class="card">
<img
src="${cardData.thumb_url}"
class="card-img-top"
alt="Video thumbnail"
/>
<div class="card-img-overlay text-center">
<img
src="images/play.png"
alt="Play"
width="64px"
class="align-self-center play-overlay"
/>
</div>
<div class="card-body">
<h5 class="card-title font-weight-bold">${cardData.title}</h5>
<p class="card-text text-muted">
${cardData["sub-title"]}
</p>
<div class="creator d-flex align-items-center">
<img
src="${cardData.author_pic_url}"
alt="Creator of Video"
width="30px"
class="rounded-circle"
/>
<h6 class="pl-3 m-0 main-color">${cardData.author}</h6>
</div>
<div class="info pt-3 d-flex justify-content-between">
<div class="rating">
${starString}
</div>
<span class="main-color">${cardData.duration}</span>
</div>
</div>
</div>
`;
return card;
// END OF createCard
}
/**
* Creates cards and attaches them to the DOM in order to display
* the Popular Videos section
* @data {Arrray} list of data for creating cards
* @return {undefined} no return
*/
function displayPopular(data) {
let classItem = "";
for (let i in data) {
classItem = i == 0 ? "carousel-item active" : "carousel-item";
let card = createCard(data[i]);
let $carouselItem = $(`
<div class="${classItem}">
<div class="col-12 col-sm-6 col-lg-3 d-flex justify-content-center">
${card}
</div>
</div>
`);
$("#popular-items").append($carouselItem);
}
slideOne("popular");
// END OF displayPopular
}
/**
* Creates cards and attaches them to the DOM in order to display
* the Latest Videos section
* @data {Array} list of data for creating cards
* @return {undefined} no return
*/
function displayLatest(data) {
let classItem = "";
for (let i in data) {
classItem = i == 0 ? "carousel-item active" : "carousel-item";
let card = createCard(data[i]);
let $carouselItem = $(`
<div class="${classItem}">
<div class="col-12 col-sm-6 col-lg-3 d-flex justify-content-center">
${card}
</div>
</div>
`);
$("#latest-videos-items").append($carouselItem);
}
slideOne("latest-videos");
// END OF displayLatest
}
/**
* Retrieves the needed data of the search bar in the DOM
* the popular section
* @return {object} An object containing the different arguments of search
* in lowercase and without spaces
*/
function searchObject() {
let searchObj = {
q: $("#keywords-input").val(),
topic: $("#topic").text().toLowerCase(),
sort: $("#sort-by").text().toLowerCase().replace(" ", "_"),
};
return searchObj;
}
/**
* Creates cards and attaches them to the DOM in order to display
* the popular section
* @data {Array} list of data for creating cards
* @return {undefined} no return
*/
function searchRequest() {
let searchObj = searchObject();
let $results = $("#results-items");
$results.empty();
$("#results-count").text("");
for (let r of requestsCourses) {
requestData(r.url, displayResults, r.id, searchObj);
}
}
/**
* Returns title to original condition with corresponding
* caps and spaces
* @title {string} title to be parsed
* @return {string} parsed title
*/
function parseTitle(title) {
if (title) {
title = title.charAt(0).toUpperCase() + title.slice(1).replace("_", " ");
}
return title;
}
/**
* Creates the dropdown menu with the corresponding options from the api
* @list {Array} list of options to be displayed
* @$DOMElement {list} DOM Element to attach the menu to
* @$titleElement {list} corresponding title element in search bar of DOM
* @return {undefined} no return
*/
function displayDropdown(list, $DOMElement, $titleElement) {
if (list.length) {
for (let l of list) {
let s = parseTitle(l);
let $item = $(`
<a class="dropdown-item" href="#">${s}</a>
`);
$item.click(function () {
$titleElement.text(s);
searchRequest();
});
$DOMElement.append($item);
}
}
}
/**
* Displays the whole search section in dom
* @data {object} object containing information
* about the topics and sort options
* @return {undefined} no return
*/
function displaySearch(data) {
let title;
let topics = data.topics;
let sorts = data.sorts;
let $TopicDropdown = $("#topic-dropdown");
let $TopicTitle = $("#topic");
title = parseTitle(data.topic);
$TopicTitle.text(title);
displayDropdown(topics, $TopicDropdown, $TopicTitle);
let $SortDropdown = $("#sort-dropdown");
let $SortTitle = $("#sort-by");
title = parseTitle(data.sort);
$SortTitle.text(title);
displayDropdown(sorts, $SortDropdown, $SortTitle);
let $KeywordsInput = $("#keywords-input");
$KeywordsInput.val(data.q);
$KeywordsInput.change(function () {
searchRequest();
});
}
/**
* Shows all of the videos, obtained after request to API, in DOM
* @data {object} object containing result from API response
* @return {undefined} no return
*/
function displayResults(data) {
let courses = data.courses;
if (!courses) return;
let $results = $("#results-items");
let count = Object.keys(courses).length;
$("#results-count").text(`${count} videos`);
if (Object.keys(courses).length) {
for (let c of courses) {
let card = createCard(c);
let $resultItem = $(`
<div class="col-12 col-sm-4 col-lg-3 d-flex justify-content-center">
${card}
</div>
`);
$results.append($resultItem);
}
}
}
/**
* Calls the needed functions in order to correctly display
* the search options in dom and results from API response
* @data {data} object containing data from API response
* @return {string} parsed title
*/
function displaySearchAndResults(data) {
displayResults(data);
displaySearch(data);
// END OF displayResults
}
/**
* Function to show loader when waiting for API response
* @active {boolean} indicates whether loader should be displayed or
* removed
* @id {string} Id of DOM object to attach loader to
* @return {string} parsed title
*/
function displayLoader(active, id) {
if (active) {
let $loader = $(`<div class="loader" id="loader-${id}"></div>`);
$(`#${id}`).append($loader);
} else {
let $loader = $(`#loader-${id}`);
$loader.remove();
}
// END OF displayLoader
}
/**
* Function to make GET request to API
* @url {string} url of API endpoint
* @callback {function} callback function to be executed
* and that depends on the section of the web page which is being
* generated by JS
* @id {string} Value needed to display loader
* @data {object} object to send in API request
* @return {string} parsed title
*/
function requestData(url, callback, id, data = {}) {
displayLoader(true, id);
$.ajax({
url: url,
type: "GET",
data: data,
headers: { "Content-Type": "application/json" },
success: function (response) {
displayLoader(false, id);
callback(response);
},
error: function (error) {
alert(`Error Getting Data from ${url}`);
},
});
// END OF requestData
}
// PERFORM DYNAMIC CONTENT REQUESTS ===================================
let requestsHomepage = [
{
url: "https://smileschool-api.hbtn.info/quotes",
func: displayQuotes,
id: "carousel-items",
},
{
url: "https://smileschool-api.hbtn.info/popular-tutorials",
func: displayPopular,
id: "popular-items",
},
{
url: "https://smileschool-api.hbtn.info/latest-videos",
func: displayLatest,
id: "latest-videos-items",
},
];
let requestsPricing = [
{
url: "https://smileschool-api.hbtn.info/quotes",
func: displayQuotes,
id: "carousel-items",
},
];
let requestsCourses = [
{
url: "https://smileschool-api.hbtn.info/courses",
func: displaySearchAndResults,
id: "results-items",
},
];
let $homepage = $("#homepage");
let $pricing = $("#pricing");
let $courses = $("#courses");
let requestObject;
if (Object.keys($homepage).length) requestObject = requestsHomepage;
else if (Object.keys($pricing).length) requestObject = requestsPricing;
else if (Object.keys($courses).length) requestObject = requestsCourses;
for (let r of requestObject) {
requestData(r.url, r.func, r.id);
}
// ======================================================================
// END OF DOCUMENT READY
});