-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
261 lines (221 loc) · 9.36 KB
/
Copy pathscript.js
File metadata and controls
261 lines (221 loc) · 9.36 KB
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
// GLOBAL VARIABLES
let request = new XMLHttpRequest();
let url = 'https://www.googleapis.com/books/v1/volumes?q=';
let searchTerms = '';
// This api is restricted to only make calls from this domain
let apiKey = '&maxResults=20&key=AIzaSyBqfUHvXPiHKcQc9KeWA7tVqT05-rHMWHM';
let data = {};
let searchBox = document.querySelector('#search_box');
let submitSearch = document.querySelector('#submit');
let resultShelf = document.querySelector('#result_shelf');
let myShelf = '';
let shelfElement = document.querySelector('#shelf');
// makeRequest()
// takes the declared Ajax object and calls on
// the google books api. If the request is successful,
// the function calls the createBook() function,
// passing the parsed JSON response stored in the
// declared variable `data`.
function makeRequest() {
request.open('GET', url + searchTerms + apiKey, true);
request.onload = function () {
data = JSON.parse(this.response);
if (request.status >= 200 && request.status < 400){
fetchShelf();
data.totalItems < 1 ?
document.querySelector('#notification').innerText = '... no results found, please try again ...' :
null;
data.items.forEach(function(book){
let imgLink = '';
let title = '';
let author = '';
let publisher = '';
let onShelf = false;
if(book.volumeInfo.imageLinks == undefined){
imgLink = './public/empty_book_cover.png';
}else{
imgLink = book.volumeInfo.imageLinks.thumbnail;
};
if(book.volumeInfo.title == undefined){
title = 'Title Not Available';
}else{
(book.volumeInfo.title).length > 80 ?
title = (book.volumeInfo.title).slice(0, 79) + '...' : title = book.volumeInfo.title;
};
if(book.volumeInfo.authors == undefined){
author = 'Author Not Available';
}else{
let authorList = (book.volumeInfo.authors).join(', ');
authorList.length > 50 ?
author = authorList.slice(0, 49) + '...' : author = authorList;
}
if(book.volumeInfo.publisher == undefined){
publisher = 'Publisher Not Available'
}else{
publisher = book.volumeInfo.publisher;
}
let infoLink = book.volumeInfo.infoLink;
myShelf.indexOf(`${title}; ${author}; ${publisher}; ${imgLink}; ${infoLink} / `) != -1 ? onShelf = true : null;
createBook(title, author, publisher, imgLink, infoLink, false, onShelf);
});
} else {
document.querySelector('#notification').innerText = '... no results found, please try again ...';
}
};
request.send();
}
// Event Listeners
// 1) searchBox is the input field in which the user
// types their query. An event listener listens for
// the user to press the `enter` or `return` key.
// The listener triggers `grabSearchText()` and then
// the `makeRequest()` function.
// 2) submitSearch is the submit button attached to the
// input field. It is given an event listener for a
// click on the botton.
// The listener triggers `grabSearchText()` and then
// the `makeRequest()` function.
// 3) Listener for the `my shelf` display.
// 4) See the createBook() function for additional listeners
searchBox.addEventListener('keyup', function(event) {
if(event.key == 'Enter'){
shelfElement.innerText == 'my search' ? shelfElement.innerText = 'my shelf' : null;
grabSearchText();
makeRequest();
};
})
submitSearch.addEventListener('click', function () {
shelfElement.innerText == 'my search' ? shelfElement.innerText = 'my shelf' : null;
grabSearchText();
makeRequest();
})
shelfElement.addEventListener('click', function(){
if(shelfElement.innerText == 'my shelf'){
shelfElement.innerText = 'my search';
populateShelf();
}else{
shelfElement.innerText = 'my shelf';
grabSearchText();
makeRequest();
};
})
// grabSearchText()
// takes the value currently in the input box,
// splits it into an array, then joins it back
// into a string with `%20` in place of the spaces.
// This is to format the string to be added as a search
// query on the api call.
// It then grabs all elements within the `resultShelf` parent
// which have a class assignment of `.book`, assigns them to
// a local variable, `bookArr`, then uses a forEach method
// to remove them from the parent.
function grabSearchText(){
searchTerms = searchBox.value.split(' ').join('%20');
let bookArr = document.querySelectorAll('.book');
bookArr.forEach(book => resultShelf.removeChild(book));
}
// createBook()
// takes in the data from the request, or from the user bookshelf,
// and populates the data in the form of an element of class
// `.book`. Each created book is then added to the DOM for viewing.
function createBook(title, author, publisher, imgLink, infoLink, shelf, onShelf) {
document.querySelector('#notification').innerText = '';
let bookDiv = document.createElement('div');
bookDiv.classList.add('book');
let info = document.createElement('a');
info.setAttribute('href', infoLink);
info.setAttribute('target', '_blank');
let img = document.createElement('img');
img.setAttribute('src', imgLink);
img.classList.add('image');
// Set up a favorite button for the book that will
// save the book info in localStorage
let fav = document.createElement('p');
fav.classList.add('fav');
if(onShelf){
fav.innerText = 'ON SHELF';
}else if(shelf == false){
fav.innerText = 'ADD TO SHELF';
fav.addEventListener('click', function(){
addToShelf(title, author, publisher, imgLink, infoLink);
fav.innerText = 'ADDED';
});
}else{
fav.innerText = 'REMOVE FROM SHELF';
fav.addEventListener('click', function(){
removeFromShelf(title, author, publisher, imgLink, infoLink);
});
}
let titleElement = document.createElement('p');
titleElement.classList.add('title');
titleElement.innerText = title;
let authorElement = document.createElement('p');
authorElement.classList.add('author');
authorElement.innerText = author;
let publisherElement = document.createElement('p');
publisherElement.classList.add('publisher');
publisherElement.innerText = publisher;
// Assemble Element
info.appendChild(img);
bookDiv.appendChild(info);
bookDiv.appendChild(fav);
bookDiv.appendChild(titleElement);
bookDiv.appendChild(authorElement);
bookDiv.appendChild(publisherElement);
// Append full element to the DOM
resultShelf.appendChild(bookDiv);
}
// fetchShelf()
// checks to see if `myShelf` has been created in the user's
// localStorage. If not, it creates the myShelf key, and set's the
// value to an empty string and assigns an empty string to the global
// variable `myShelf`.
// If `myShelf` is established in localStorage, the function retrieves
// the value and assigns it to the global variable `myShelf`.
function fetchShelf(){
if(localStorage.getItem('myShelf') != null){
myShelf = localStorage.getItem('myShelf');
}else{
localStorage.setItem('myShelf', '');
myShelf = '';
};
}
// addToShelf()
// calls `fetchShelf()` to retrieve/create the localStorage value.
// Then concatenates the value with a new string that represents book
// information of the clicked book element.
function addToShelf(title, author, publisher, img, url){
fetchShelf();
myShelf += `${title}; ${author}; ${publisher}; ${img}; ${url} / `;
localStorage.setItem('myShelf', myShelf);
}
// removeFromShelf()
// calls `fetchShelf()` to retrieve the localStorage value
// Then searches for the book to be removed, and deletes entry.
// The new `myShelf` value is assigned back into localStorage.
// The shelf is repopulated on the DOM for viewing.
function removeFromShelf(title, author, publisher, img, url){
fetchShelf();
myShelf = myShelf.replace(`${title}; ${author}; ${publisher}; ${img}; ${url} / `, '');
localStorage.setItem('myShelf', myShelf);
populateShelf();
}
// populateShelf()
// fetches the myShelf value from localStorage using the `fetchShelf` function,
// then splits the returned string by `/` which dilineates each book and places
// each book as an array within the local `bookArr` variable.
// It then
function populateShelf(){
let bookElementsArr = document.querySelectorAll('.book');
bookElementsArr.forEach(book => resultShelf.removeChild(book));
fetchShelf();
myShelf.length == 0 ?
document.querySelector('#notification').innerText = `... your shelf is currently empty ...` : null;
let myShelfArr = [];
let bookArr = myShelf.split(' / ');
bookArr.forEach(book => myShelfArr.push(book.split('; ')));
myShelfArr.pop();
myShelfArr.forEach(function(shelfItem){
createBook(shelfItem[0], shelfItem[1], shelfItem[2], shelfItem[3], shelfItem[4]);
});
}