-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodos.js
152 lines (108 loc) · 3.31 KB
/
todos.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
//Bloquear Cookies de Terceiros impede o uso de localStorage
var listElement = document.querySelector("#app ul");
var inputElement = document.querySelector("#app input");
var buttonElement = document.querySelector("#app button");
buttonElement.onclick = addTodo;
//caso o valor retornado seja null, passa-se à segunda opção, que é []
var TODOS = JSON.parse(localStorage.getItem("lista__")) || [];
function renderTodos() {
listElement.innerHTML = "";
for (todo of TODOS){
var divElement0 = document.createElement("div");
divElement0.setAttribute("class","zero");
var divElement = document.createElement("div");
divElement.setAttribute("class","listitem");
var daysOffElement = document.createElement("pre");
var daysOffText = document.createTextNode(todo.span);
daysOffElement.appendChild(daysOffText);
var dateElement = document.createElement("input");
dateElement.value = todo.data;
dateElement.setAttribute("type","date");
dateElement.setAttribute("name","date");
dateElement.setAttribute("onchange","changeStyle()");
var todoElement = document.createElement("li");
var todoText = document.createTextNode(todo.item);
var linkElement = document.createElement("a");
linkElement.setAttribute("href", "#");
var pos = TODOS.indexOf(todo);
linkElement.setAttribute("onclick", "deleteTodo(" + pos + ")");
var linkText = document.createTextNode("⛔ Excluir");
linkElement.appendChild(linkText);
todoElement.appendChild(todoText);
todoElement.appendChild(linkElement);
divElement.appendChild(daysOffElement);
divElement.appendChild(dateElement);
divElement.appendChild(todoElement);
divElement0.appendChild(divElement);
listElement.appendChild(divElement0);
}
}
function changeStyle(){
var counter = -1;
for(date of document.getElementsByName("date")){
counter +=1;
TODOS[counter].data = date.value;
TODOS[counter].span = dateDifference(TODOS[counter].data,todayAsString());
var theDate = new Date(date.value);
var hoje = new Date(todayAsString());
var span = date.previousElementSibling;
span.innerText = TODOS[counter].span + " dias";
if( theDate > hoje){
date.setAttribute("class","tomorrow");
span.setAttribute("class","tomorrow");
}
else if(theDate < hoje ){
date.setAttribute("class","yesterday");
span.setAttribute("class","yesterday");
}
else{
date.setAttribute("class","today");
span.setAttribute("class","today");
}
}
saveToStorage();
}
function todayAsString(){
var dateObject = new Date();
var dateVector = [dateObject.getFullYear(),dateObject.getMonth()+1,dateObject.getDate()];
if(dateVector[1]<10){
dateVector[1] = "0" + dateVector[1] ;
}
var dateText = dateVector.join("-");
return dateText;
}
function addTodo() {
var todoText = inputElement.value;
if (todoText){
var todoItem = {
item: todoText,
data: todayAsString(),
span: dateDifference(todayAsString(),todayAsString()),
}
TODOS.push(todoItem);
inputElement.value = "";
}
update();
}
function deleteTodo(pos) {
TODOS.splice(pos,1); //.splice remove item em uma lista
update();
}
function saveToStorage() {
localStorage.setItem("lista__", JSON.stringify(TODOS));
}
function update(){
saveToStorage();
loadData();
renderTodos();
changeStyle();
}
function loadData(){
TODOS = JSON.parse(localStorage.getItem("lista__")) || [];
}
function dateDifference(xStr,yStr){
x = new Date(xStr);
y = new Date(yStr);
return parseInt((x.getTime()-y.getTime())/(1000*60*60*24));
}
update();