-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtodo.js
More file actions
174 lines (130 loc) · 4.26 KB
/
Copy pathtodo.js
File metadata and controls
174 lines (130 loc) · 4.26 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
//ADD new To do
const addForm = document.querySelector('.add');
const list = document.querySelector('.todos');
const popup = document.querySelector('.popup');
const gPop = document.querySelector('.popup-wrapper');
const btn = document.querySelector('.btn');
const search = document.querySelector('.search input');
gPop.style.display = "none";
/***************reusable function********************/
/* Function pour l'alert et le popup qui va etre afficher (time control)*/
function start(duree)
{
var o=document.getElementById("sp");
if(duree > 0)
{
o.innerHTML = duree;
gPop.style.display = "block";
setTimeout("start("+duree+" -1)", 1000);
}
else
{
alert("enter a valid to do");
o.innerHTML ="Au revoir";
gPop.style.display="none";
popup.style.visibility ="hidden";
}};
/* Function Creation dynamique du POPUP */
function create(){
const div = document.createElement('div');
div.classList.add('popup-close');
div.setAttribute('id','closing');
const text = document.createTextNode('X');
div.appendChild(text);
popup.append(div);
const div2 = document.createElement('div');
div2.classList.add('popup-content');
const html = `
<span id="sp">1</span>
<h2>Fill the Input</h2>
<p>Don't forget</p>
<a href="#">Return</a>`;
div2.innerHTML=html;
popup.append(div2);
}
/* Function generation dynamique des TODOS */
const generateTemp = todo =>{
const html = `
<li class="list-group-item d-flex justify-content-between align-items-center">
<span>${todo}</span>
<i class="fas fa-trash delete"></i>
</li>
`;
list.innerHTML += html;
};
/* function pour controller l'evenement et pour ne pas etre repeté à chaque clique */
function onetime(node, type, callback) {
node.addEventListener(type, function(e) {
e.target.removeEventListener(e.type, arguments.callee);
return callback(e);
});
}
onetime(gPop,'click',handler);
function handler(e){
if(e.target.id='closing'){
gPop.style.display ="none";
}
}
/***************Fin reusable function********************/
/************* Adding TO DO**************/
function addElement () {
let Input = document.querySelector(".add input");
if (Input.value !== "") {
generateTemp(Input.value);
TheTodoS.push(Input.value);
Input.value = "";
} else {
create();
start(5000);
}
}
//Eventlistner Add TODOS
let TheTodoS = [];
btn.addEventListener('click', addElement);
/************* Fin Adding TO DO**************/
/*************Deleting TO DO**************/
list.addEventListener('click',e =>{
list.addEventListener('click', e => {
let targets = e.target;
if (targets.matches("i")) {
targets.parentElement.remove()
let x = targets.parentElement.firstElementChild.textContent.toUpperCase();
let theIndex = TheTodoS.indexOf(x);
TheTodoS.splice(theIndex, 1);
}
});
});
/************* Fin Deleting TO DO**************/
/************************************* SEARCH ITEM********************************************/
//filtering Todos :
//we will apply a class to the Todos that dont match and the that class will
// have keyup event
const retrieve = (e) => {
let elem = e.target.value.toUpperCase();
var TheTodos = Array.from(list.children);
let inclo = TheTodos.filter(e => {
let cont = e.firstElementChild.textContent;
return !cont.toUpperCase().indexOf(elem) !== -1;
})
.forEach(e => e.classList.add('filtre'))
let Ninclo = TheTodos.filter(e => {
let cont = e.firstElementChild.textContent;
return cont.toUpperCase().indexOf(elem) !== -1;
})
.forEach(e => e.classList.remove('filtre'))
//function pour faire un filtre i
};
//evenement de recherche des mots clés
search.addEventListener('keyup', (e) =>{
retrieve(e)
})
/*************************************Fin SEARCH ITEM********************************************/
window.addEventListener("beforeunload", e => localStorage.setItem('TheTodoS' , JSON.stringify(TheTodoS)))
window.addEventListener("load" ,function (){
let local = localStorage.getItem('TheTodoS');
local = JSON.parse(local);
for (let i = 0 ; i < local.length ;i++){
TheTodoS.push(local[i]);
generateTemp(local[i]);
}
} )