-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
175 lines (155 loc) · 4.64 KB
/
Copy pathscript.js
File metadata and controls
175 lines (155 loc) · 4.64 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
//let library = [];
// define book
function Book(title, author, pages, isRead, date) {
return {
title,
author,
pages,
isRead,
date,
};
}
// define access to book info
//
class LocalStorage {
constructor() {
this.ls = window.localStorage;
let txt = this.ls.getItem('keys');
if (txt) {
this.keys = JSON.parse(txt);
if (!(this.keys)) {
this.keys = [];
this.ls.setItem('keys', []);
}
} else {
this.keys = [];
}
}
setBookObj(bookObj) {
let len = this.keys.length;
this.ls.setItem(bookObj.date, JSON.stringify(bookObj));
this.keys.push(bookObj.date);
this.ls.setItem('keys', JSON.stringify(this.keys));
}
getBookObj(key) {
let obj = JSON.parse(this.ls.getItem(key));
return obj;
}
getAllItems() {
let arr = [];
for (let i = 0; i < this.keys.length; i ++) {
let itm = this.getBookObj(this.keys[i]);
if (itm) {
arr.push(itm);
}
}
return arr;
}
delItem(obj) {
this.delKeys(obj.date);
this.ls.removeItem(obj.date);
}
// HELPER funcs
delKeys(val) {
this.keys = this.keys.filter(x => x !== val);
this.ls.setItem('keys', JSON.stringify(this.keys));
}
push(obj) {
this.setBookObj(obj);
}
}
function info(obj) {
return `${obj.title} by ${obj.author}, ${obj.pages} pages, ${obj.isRead}.`
}
//let theHobbit = new Book("The Hobbit", "J. R. R. Tolkien", 295, false);
//for(key in theHobbit){if(typeof(theHobbit[key]) != 'function')console.log(key);};
let library = new LocalStorage();
//library.setBookObj(theHobbit);
// define form and modal
let modal = document.querySelector("#modal");
let titleInput = document.querySelector("#inptitle");
let authorInput = document.querySelector("#inpauthor");
let pagesInput = document.querySelector("#inppages");
let readBox = document.querySelector("#readbox");
// define html library
let gallery = document.querySelector("#gallery");
function BookBlock(book) {
// define all elements
this.block = document.createElement('div');
this.cardButtons = document.createElement('div');
this.cardInfos = document.createElement('div');
this.delbtn = document.createElement('button');
this.editbtn = document.createElement('button');
// assign class to all elements
this.block.classList.add("cardblock");
this.cardButtons.classList.add('cardbutton');
this.cardInfos.classList.add('cardinfo');
this.delbtn.classList.add('delbtn');
this.editbtn.classList.add('editbtn');
// attach all info to card info
console.log("bukunya nih ", book);
for(let key in book){
if(typeof(book[key]) != 'function' && typeof(book[key]) != 'object'){
let section = document.createElement('div');
let head = document.createElement('h4');
let detail = document.createElement('p');
head.textContent = key;
detail.textContent = book[key];
section.classList.add('section');
section.appendChild(head);
section.appendChild(detail);
this.cardInfos.appendChild(section);
}
}
// add icon and attach all buttons
this.delbtn.classList.add("fas");
this.delbtn.classList.add("fa-trash");
this.editbtn.classList.add("fas");
this.editbtn.classList.add("fa-edit");
this.cardButtons.appendChild(this.delbtn);
this.cardButtons.appendChild(this.editbtn);
// attach info and button
this.block.appendChild(this.cardInfos);
this.block.appendChild(this.cardButtons);
// corelate both book and block
book.block = this;
this.book = book;
// attach onclick function to delete
this.delbtn.addEventListener("click", () => deleteBook(this.book));
}
function deleteBook(book){
library.delItem(book);
//library = library.filter(b => b !== book);
updateGallery();
}
function openForm(){
modal.style.display = "flex";
}
function closeForm(){
titleInput.value = authorInput.value = pagesInput.value = '';
readBox.checked = false;
modal.style.display = "none";
}
function submitForm() {
let book = new Book(titleInput.value, authorInput.value, pagesInput.value, readBox.checked, (new Date()).toISOString());
library.push(book);
printLibrary();
closeForm();
updateGallery();
}
function printLibrary() {
library.getAllItems().forEach(b => console.log(info(b)));
}
function updateGallery() {
console.log("let's update")
while(gallery.firstElementChild){
gallery.removeChild(gallery.firstElementChild);
};
let items = library.getAllItems();
for(let i in items){
let block = new BookBlock(items[i]);
console.log(block.block);
gallery.appendChild(block.block);
};
}
window.onLoad = updateGallery();