-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
172 lines (146 loc) · 5.54 KB
/
script.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
let books = JSON.parse(localStorage.getItem("books")) || [];
// Renderiza todos os livros da variavel books e monta o card
function renderBooks() {
let bookList = document.querySelector("main");
bookList.innerHTML = "";
books.forEach((book, index) => {
// TODO: Criar um container para botar o editIcon e o input do link
bookList.innerHTML += `
<section class="book-card">
<div class="book-title">
<h2 class="title">
<a href="${book.link}">${book.title}</a>
<input type="text" value="${book.title}" class="disable editable title-input"/>
</h2>
<input type="text" placeholder="Novo Link" class="disable link-input"/>
</div>
<img
src="./img/edit-icon.png"
alt="Editar"
class="editBtn"
onclick="toggleEditTitle(${index})"
/>
<img
src="./img/delete-icon.png"
alt="Excluir"
class="deleteBtn"
onclick="deleteBook(${index})"
/>
<div class="page-container">
<h3>Página:</h3>
<img src="./img/arrow-left-icon.png" alt="Subtrai 1" onclick="minus(${index})"/>
<p class="page" ondblclick="toggleEditPageNum(this)" >${book.page}</p>
<img src="./img/arrow-right-icon.png" alt="Adiciona 1" onclick="sum(${index})" />
</div>
</section>
`;
});
}
// Liga e desliga a capacidade de editar o titulo e o link do livro
function toggleEditTitle(index) {
let bookTitle = document.querySelectorAll(".title a")[index];
let bookTitleInput = document.querySelectorAll(".title input")[index];
let bookLinkInput = document.querySelectorAll(".book-title > input")[index];
if (bookTitle.classList.contains("disable")) {
bookTitle.classList.toggle("disable");
bookTitleInput.classList.toggle("disable");
bookLinkInput.classList.toggle("disable");
save(index);
renderBooks();
} else {
bookTitle.classList.toggle("disable");
bookTitleInput.classList.toggle("disable");
bookLinkInput.classList.toggle("disable");
}
bookTitleInput.addEventListener("keydown", (e) =>
closeThisTitleEditInKeydown(e, "Enter", index)
);
bookTitleInput.addEventListener("keydown", (e) =>
closeThisTitleEditInKeydown(e, "Close", index)
);
}
function closeThisTitleEditInKeydown(e, key, index) {
let element = e.target;
let isEditable = !element.classList.contains("disable");
if (e.key === key) {
if (isEditable) {
toggleEditTitle(index);
renderBooks();
}
}
}
// Liga e desliga a capacidade de editar o numero de paginas dando douple click e depois salva
function toggleEditPageNum(pageNum) {
let pageElements = document.querySelectorAll(".page");
let index = Array.prototype.indexOf.call(pageElements, pageNum);
let isEditable = pageNum.getAttribute("contenteditable");
if (isEditable === "true") {
pageNum.setAttribute("contenteditable", "false");
pageNum.classList.toggle("editable");
} else {
pageNum.setAttribute("contenteditable", "true");
pageNum.classList.toggle("editable");
}
pageNum.addEventListener("keydown", (e) => {
closeThisPageEditInKeydown(e, "Enter");
});
pageNum.addEventListener("keydown", (e) => {
closeThisPageEditInKeydown(e, "Close");
});
save(index);
}
function closeThisPageEditInKeydown(e, key) {
let isPageEditable = e.target.getAttribute("contenteditable");
let page = e.target
if (e.key === key) {
if (isPageEditable === "true") {
page.setAttribute("contenteditable", "false");
page.classList.toggle("editable");
let pageElements = document.querySelectorAll(".page");
let index = Array.prototype.indexOf.call(pageElements, e.target);
save(index);
}
}
}
// Soma 1 no número de páginas
function sum(index) {
let bookPage = document.querySelectorAll(".page")[index];
let pageNum = bookPage.textContent;
bookPage.textContent = String(Number(pageNum) + 1);
save(index);
}
// Subtrai 1 no número de páginas
function minus(index) {
let bookPage = document.querySelectorAll(".page")[index];
let pageNum = bookPage.textContent;
bookPage.textContent = String(Number(pageNum) - 1);
save(index);
}
// Cria um livro
function addBook() {
let title = `Titulo do livro`;
let link = ``;
let page = 1;
let newBook = { title, link, page };
books.push(newBook);
localStorage.setItem("books", JSON.stringify(books));
renderBooks();
}
// Salva tudo
function save(index) {
let title = document.querySelectorAll(".title input")[index].value;
let link =
document.querySelectorAll(".link-input")[index].value ||
document.querySelectorAll(".title a")[index].href;
let page = document.querySelectorAll(".page")[index].textContent;
books[index] = { title, link, page };
localStorage.setItem("books", JSON.stringify(books));
}
// Deleta um livro
function deleteBook(index) {
books.splice(index, 1);
localStorage.setItem("books", JSON.stringify(books));
renderBooks();
}
addBookBtn.onclick = addBook;
window.onload = renderBooks;