-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.js
94 lines (59 loc) · 2.46 KB
/
renderer.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
// TODO: study ipc, ipcmain/ipcrenderer & ipcmainevent/ipcrendererevent
// TODO: init function
//TODO: read from db (DONE)
//TODO: come fare "quit" function che salva sul db?
////////////////Init////////////////////
////////////////EDIT MODE////////////////////
let editMode = false
const editModeBtn = document.querySelector('.edit-mode')
editModeBtn.addEventListener('click', () => editMode = !editMode)
//// ToDo CONTROLLER ////
const todoContainer = document.querySelector('.todo-container')
const todoBtn = document.querySelector('.add-todo--btn')
const todoText = document.querySelector('.add-todo--text')
todoBtn.addEventListener('click', (e) => createTodo(e))
todoContainer.addEventListener('click', (e) => handleTodo(e))
//// ToDo VIEW ////
function createTodo(e) {
console.log(e)
console.log(e.target)
// fill the div (model--create-todo)
const todoEl = document.createElement('div')
todoEl.classList.add('todo', 'todo-box')
const html = `
<input type="text" readonly value="${todoText.value}" class="todo--text">
<input type="checkbox" name="task-checkbox" id="checkbox">
<button class="rm-todo">Remove</button>
`
todoEl.innerHTML = html
// append todo to container (view--update-todos)
todoText.value ? todoContainer.appendChild(todoEl) : alert('insert a todo')
// other
}
///ToDo Model
function handleTodo(e) {
// console.log(e);
console.log(e.target.parentNode);
const target = e.target
// Rewrite TD function
if (target.classList.contains('todo--text') && editMode) {
target.removeAttribute('readonly')
target.addEventListener('blur', () => target.setAttribute('readonly', 'true'))
}
// Remove TD function
if (target.classList.contains('rm-todo')) target.parentNode.remove()
// Check TD function
if (target.checked) target.previousElementSibling.style.textDecoration = 'line-through'
if (!target.checked && target.previousElementSibling) target.previousElementSibling.style.textDecoration = 'none'
}
////////////////DATABASE--save///////////
// Link: https://stackoverflow.com/questions/48284207/emit-custom-events-in-electron-app-from-main-to-renderer
const saveBtn = document.querySelector('.db-save')
saveBtn.addEventListener('click', () => {
const html = todoContainer.innerHTML
window.todoDB.saveDBtoFile(html)
})
////////////////DATABASE--save-on-quit///////////
// app.on('did-something', () => {
// console.log('emit-hello')
// });