-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
157 lines (148 loc) · 4.54 KB
/
app.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
window.addEventListener('load', function () {
//variables
const todoList = document.querySelector('#todoList')
const objects = []
const myTodoForm = document.querySelector('#todoForm')
let _idCounter = 0
// MOCKUP OBJECT Array
const mockupObjectArray = [
{
id: 'todoElement_0',
text: 'buy new plants',
isImportant: true,
date: getDate(),
},
{
id: 'todoElement_1',
text: 'wash dishes',
isImportant: false,
date: getDate(),
},
{
id: 'todoElement_2',
text: 'throw out trash',
isImportant: true,
date: getDate(),
},
{
id: 'todoElement_3',
text: 'clean kitchen',
isImportant: false,
date: getDate(),
},
{
id: 'todoElement_4',
text: 'water plants',
isImportant: true,
date: getDate(),
},
{
id: 'todoElement_5',
text: 'practice coding',
isImportant: true,
date: getDate(),
},
]
mockupObjectArray.forEach((object) => {
renderHtmlContent(object)
objects.push(object)
})
// SUBMIT Button and Main Logic and Object
myTodoForm.addEventListener('submit', (e) => {
e.preventDefault()
const todoTextVal = document.querySelector('#todoText').value
const todoCheckboxChecked = document.querySelector('#todoCheckbox').checked
//Putting Output in an Object and the Object in an array
const object = {
id: `todoElement_${_idCounter}`,
text: todoTextVal,
isImportant: todoCheckboxChecked,
date: getDate(),
}
renderHtmlContent(object)
objects.push(object)
//reset form entries on submit
myTodoForm.reset()
})
//FUNCTIONS
//DATE Function
function getDate() {
const dateObject = new Date()
const dateObjectString = dateObject.toString()
const dateObjectStringSplit = dateObjectString.split('2')
const dateString = dateObjectStringSplit[0]
return dateString
}
console.log(getDate())
//Visual Render of Tile Elements (Entries)
function renderHtmlContent(object) {
//Create container element div
const todoElement = document.createElement('div')
todoElement.id = object.id
todoElement.classList.add('todoElement')
if (object.isImportant) {
todoElement.classList.add('importantTodo')
} else {
todoElement.classList.add('notImportantTodo')
}
_idCounter++
todoList.prepend(todoElement) //prepend adds tileElement before the last added tileElement (above)
const textKeyElement = document.createElement('p')
textKeyElement.classList.add('todoText')
textKeyElement.innerText = `${object.text}`
todoElement.append(textKeyElement)
//span for IMPORTANCE SIGN (svg)
const importantSign = document.createElement('span')
todoElement.append(importantSign)
if (object.isImportant) {
importantSign.classList.add('todoWrapperImportant')
const importantSVG = document.createElement('img')
importantSVG.src = './img/important-svg5.svg'
importantSign.append(importantSVG)
} else {
importantSign.classList.add('todoWrapperNotImportant')
const importantSVG = document.createElement('img')
importantSVG.src = './img/important-svg5.svg'
importantSign.append(importantSVG)
}
//Date Element
const dateElement = document.createElement('p')
dateElement.innerText = `${object.date}`
todoElement.append(dateElement)
dateElement.classList.add('dateElement')
//Delete Button
const btnElement = document.createElement('button')
btnElement.innerText = `done`
todoElement.append(btnElement)
btnElement.classList.add('btnElement')
document.querySelector('.btnElement').addEventListener('click', (ev) => {
todoElement.remove()
})
const clearAllTodosBtn = document
.querySelector('.deleteAllButton')
.addEventListener('click', () => {
console.log('click')
todoElement.remove()
})
}
let isFirstClick = true
//Filter Todos (important not important toggle)
const filterImportanceBtn = document.querySelector('.filterImportanceButton')
filterImportanceBtn.addEventListener('click', (e) => {
const todoNotImportant = document.querySelectorAll('.notImportantTodo')
if (isFirstClick) {
todoNotImportant.forEach((todo) => {
todo.style.display = 'none'
isFirstClick = false
})
} else {
todoNotImportant.forEach((todo) => {
todo.style.display = 'flex'
isFirstClick = true
})
}
})
// const todoWrapperImportant = document.querySelectorAll(
// '.todoWrapperImportant'
// )
})