-
Notifications
You must be signed in to change notification settings - Fork 0
/
bookBerry.js
126 lines (111 loc) · 3.49 KB
/
bookBerry.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
/*create a record of books*/
class Book {
constructor(
title,
authorFirst,
authorLast,
pubDate,
genres,
edition,
pages,
formatArr,
progress
) {
this.title = title;
this.authorFirst = authorFirst;
this.authorLast = authorLast;
this.genre = genres;
this.pubDate = pubDate;
this.edition = edition;
this.pages = pages;
this.format = formatArr;
this.progress = progress;
}
printBookInfo = function () {
let infoStr = `
${this.title}\n
${this.authorLast}, ${this.authorFirst}\n
\u00A9 ${this.pubDate} | Edition ${this.edition}\n
${this.pages} pages\n
${this.genre.join(", ")}\n
${this.format.join(", ")}\n
Reading Completed: ${this.progress}
`;
console.log(infoStr);
};
printAuthor = function () {
console.log(`${this.authorFirst} ${this.authorLast}`);
};
}
// factory function to make Book instances
function bookFactory() {
// set variables to target elements in DOM after they have been filled
const bookForm = document.getElementById("bookForm");
const title = document.getElementById("bookTitle").value;
const authorFirst = document.getElementById("authorFirst").value;
const authorLast = document.getElementById("authorLast").value;
const pubDate = document.getElementById("pubDate").value;
const edition = parseInt(document.getElementById("edition").value);
const pages = parseInt(document.getElementById("pages").value);
const progress = parseInt(document.getElementById("progress").value);
const genres = Array.from(
document.querySelectorAll("input[name=genre]:checked")
).map((genre) => genre.value);
const formats = Array.from(
document.querySelectorAll("input[name=format]:checked")
).map((format) => format.value);
// Reset all form data
bookForm.reset();
return new Book(
title,
authorFirst,
authorLast,
pubDate,
genres,
edition,
pages,
formats,
progress
);
}
// download function to download the library file created
function download(filename, text) {
var element = document.createElement("a");
element.setAttribute(
"href",
"data:application/json," + encodeURIComponent(text)
);
element.setAttribute("download", filename);
element.style.display = "none";
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
// target the span element progress_percentage
const progressPercentage = document.getElementById("progress_percentage");
// target the slider
const progress = document.getElementById("progress");
// target the button for downloading the library file
let download_lib = document.getElementById("download_lib");
// create the library array
let libraryArr = [];
// target button for form submission
const form_submit = document.getElementById("form_submit");
// on form button submit create new instance of class Book
form_submit.addEventListener("click", function () {
libraryArr.push(bookFactory()); //push book obj to libraryArr
download_lib.classList.remove("hidden");
});
// initiate download of library file when download_lib is clicked
download_lib.addEventListener("click", function () {
/*
creates JSON of library and begins download
*/
let json = JSON.stringify(libraryArr);
let filename = "bookBerry_download";
download(filename, json);
});
// update progress percentage number whenever slider is moved
progress.oninput = () => {
progressPercentage.innerText = progress.value;
};