-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
109 lines (79 loc) · 2.9 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
//todo list js
document.querySelector('.create-todo').addEventListener('click',function(){
document.querySelector('.new-item').style.display='block';
});
document.querySelector('.new-item button').addEventListener('click',function()
{
var itemName = document.querySelector('.new-item input').value;
if(itemName != ''){
var itemsStorage = localStorage.getItem('todo-items');
var itemsArr = newFunction(itemsStorage);
itemsArr = itemsArr || [];
itemsArr.push({"item":itemName, "status":0});
saveItems(itemsArr);
fetchItems();
document.querySelector('.new-item input').value='';
document.querySelector('.new-item').style.display='none';
}
});
function newFunction(itemsStorage) {
return JSON.parse(itemsStorage);
}
function fetchItems(){
const itemsList = document.querySelector('ul.todo-items');
itemsList.innerHTML = '';
var newItemHTML = '';
try{
var itemsStorage = localStorage.getItem('todo-items');
var itemsArr = JSON.parse(itemsStorage);
for (var i = 0; i < itemsArr.length; i++) {
var status = '';
if(itemsArr[i].status == 1){
status = 'class="done"';
}
newItemHTML += `<li data-itemindex="${i}" ${status}>
<span class="item">${itemsArr[i].item}</span>
<div><span class="itemComplete"><i class="far fa-check-circle"></i></span><span class="itemDelete"><i class="fa fa-trash"></i></span></div>
</li>`;
}
itemsList.innerHTML = newItemHTML;
var itemsListUL = document.querySelectorAll('ul li');
for (var i = 0; i < itemsListUL.length; i++) {
itemsListUL[i].querySelector('.itemComplete').addEventListener('click', function(){
//
var index = this.parentNode.parentNode.dataset.itemindex;
itemComplete(index);
});
itemsListUL[i].querySelector('.itemDelete').addEventListener('click', function(){
//
var index = this.parentNode.parentNode.dataset.itemindex;
itemDelete(index);
});
}
}catch(e){
//.
//create a deafut item list..
}
}
function itemComplete(index){
var itemsStorage = localStorage.getItem('todo-items');
var itemsArr = JSON.parse(itemsStorage);
itemsArr[index].status = 1;
saveItems(itemsArr);
document.querySelector('ul.todo-items li[data-itemindex="'+index+'"]').className='done';
}
function itemDelete(index){
var itemsStorage = localStorage.getItem('todo-items');
var itemsArr = JSON.parse(itemsStorage);
itemsArr.splice(index, 1);
saveItems(itemsArr);
document.querySelector('ul.todo-items li[data-itemindex="'+index+'"]').remove();
}
function saveItems(obj){
var string = JSON.stringify(obj);
localStorage.setItem('todo-items', string);
}
fetchItems();
import App from "App.js";
const root = document.getElementById("app");
const app = new App(root);