Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
202 lines (165 loc) · 5.4 KB
/
Copy pathscript.js
File metadata and controls
202 lines (165 loc) · 5.4 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
var myLibrary = [];
var library = [];
let bookInfo = [
["The Fellowship of the Ring, JRR, 560 pages, The Book was read"],
["Two Towers, JRR, 700 pages, The Book was not read"],
["Return of the King, JRR, 690 pages, The Book was not read"],
["Shadow of what once was, Sanderson, 500 pages, The Book was read"],
["The way of Kings, Sanderson, 900 pages, The Book was read"],
["Hereticus, Abnett, 900 pages, The Book was read"],
["Malleus, Abnett, 800 pages, The Book was read"],
["Xenos, Abnett, 750 pages, The Book was read"]
]
let currentIndex = 0;
function Book(title, author, pages, read) {
this.title = title;
this.author = author;
this.pages = pages;
this.read = read;
this.id = self.crypto.randomUUID();
}
function bookCycle() {
if (currentIndex < bookInfo.length) {
const [title, author, pages, read] = bookInfo[currentIndex][0].split(', ');
const book = new Book(title, author, pages, read);
library.push(book);
newCard(book);
currentIndex++;
} else {
currentIndex = 0;
}
}
function addFirst() {
const [title, author, pages, read] = bookInfo[0][0].split(', ');
const book = new Book(title, author, pages, read);
library.push(book);
newCard(book);
}
function addSecond() {
const [title, author, pages, read] = bookInfo[1][0].split(', ');
const book = new Book(title, author, pages, read);
library.push(book);
newCard(book);
}
function addThird() {
const [title, author, pages, read] = bookInfo[2][0].split(', ');
const book = new Book(title, author, pages, read);
library.push(book);
newCard(book);
}
function addFourth() {
const [title, author, pages, read] = bookInfo[3][0].split(', ');
const book = new Book(title, author, pages, read);
library.push(book);
newCard(book);
}
function addFifth() {
const [title, author, pages, read] = bookInfo[4][0].split(', ');
const book = new Book(title, author, pages, read);
library.push(book);
newCard(book);
}
function addSixth() {
const [title, author, pages, read] = bookInfo[5][0].split(', ');
const book = new Book(title, author, pages, read);
library.push(book);
newCard(book);
}
function addSeventh() {
const [title, author, pages, read] = bookInfo[6][0].split(', ');
const book = new Book(title, author, pages, read);
library.push(book);
newCard(book);
}
function addEighth() {
const [title, author, pages, read] = bookInfo[7][0].split(', ');
const book = new Book(title, author, pages, read);
library.push(book);
newCard(book);
}
function removeItem(uuid) {
const index = library.findIndex(book => book.id === uuid);
if (index !== -1) {
library.splice(index, 1);
const card = document.querySelector(`[data-index-number="${uuid}"]`);
if (card) card.remove();
}
}
Book.prototype.toggleStatus = function () {
this.read = (this.read === "The Book was read") ? "The Book was not read" : "The Book was read";
};
function newCard(book) {
const newDiv = document.querySelector(".new-cards");
const content = document.createElement("div");
const remove = document.createElement("button");
const changeRead = document.createElement("button");
changeRead.classList.add("change-read-button");
changeRead.textContent = 'Change read status';
content.classList.add("new-card");
remove.classList.add("remove-button");
remove.textContent = 'Remove';
function updateCardText() {
content.textContent = `${book.title}, ${book.author}, ${book.pages}, ${book.read}`;
content.appendChild(changeRead);
content.appendChild(remove);
}
updateCardText();
if (book.title) {
content.textContent = `${book.title}, ${book.author}, ${book.pages}, ${book.read}`;
}
content.setAttribute("data-index-number", book.id);
newDiv.appendChild(content);
changeRead.addEventListener('click', () => {
book.toggleStatus();
updateCardText();
});
remove.addEventListener('click', () => {
removeItem(book.id);
});
content.appendChild(changeRead);
content.appendChild(remove);
}
const showButton = document.getElementById("showDialog");
const favDialog = document.getElementById("favDialog");
const outputBox = document.querySelector("output");
const selectEl = favDialog.querySelector("input");
const confirmBtn = favDialog.querySelector("#confirmBtn");
const dialogInput1 = document.getElementById('book-title')
const dialogInput2 = document.getElementById('author');
const dialogInput3 = document.getElementById('pages');
let value4 = "";
function Radio() {
if(document.getElementById('read-y').checked) {
value4 = "The Book was read"
} else if (document.getElementById('read-n').checked) {
value4 = "The Book was not read"
}
document.getElementById('read-y').checked = false;
document.getElementById('read-n').checked = false;
}
showButton.addEventListener("click", () => {
favDialog.showModal();
});
confirmBtn.addEventListener("click", (event) => {
event.preventDefault();
favDialog.close(selectEl.value);
});
//need to refactor so my values get added as an object to library
function getID() {
Radio()
const value1 = dialogInput1.value;
const value2 = dialogInput2.value;
const value3 = dialogInput3.value;
if (value1 === "" || value2 === "" || value3 === "") {
return
};
const book = new Book(value1, value2, value3, value4);
myLibrary.push(book);
library.push(book);
newCard(book);
dialogInput1.value = '';
dialogInput2.value = '';
dialogInput3.value = '';
console.log(library);
console.log(myLibrary);
}