-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
173 lines (160 loc) · 5.83 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
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
173
var todo_array = [];
var done_array = [];
var array_id = 1;
var del_num = 0;
function add_to_array() {
if (document.getElementById('todo_text').value.length !== 0) {
let id_num = array_id++;
let todo_parse = document.getElementById('todo_text').value;
todo_array.push({id_num, todo_parse});
document.getElementById('todo_text').value = '';
storage();
render();
} else {
alert('Введите задачу')
}
}
function render() {
delete_old_render()
for (let i = 0; i < todo_array.length; i++) {
let new_todo = document.createElement('div');
new_todo.className = 'new_todo';
let array_data_id = todo_array[i].id_num;
new_todo.id = array_data_id;
del_num = array_data_id;
let array_data_text = todo_array[i].todo_parse;
new_todo.innerHTML = '<b>' + array_data_id++ + ': ' + array_data_text + '</b>';
document.getElementById('results').append(new_todo);
add_del_btn(del_num);
add_edit_btn(del_num);
add_check(del_num);
}
}
function delete_old_render() {
document.querySelectorAll('.new_todo').forEach(function (a) {
a.remove();
}
)
}
function storage() {
let store_todo = JSON.stringify(todo_array);
localStorage.setItem('todo_list', store_todo);
}
function render_from_storage() {
document.getElementById('done').style.visibility = 'hidden';
todo_array = (JSON.parse(window.localStorage.getItem('todo_list')));
array_id = 1;
if (todo_array !== null) {
for (let i = 0; i < todo_array.length; i++) {
let new_todo = document.createElement('div');
new_todo.className = 'new_todo';
let array_data_id = todo_array[i].id_num;
new_todo.id = array_data_id;
del_num = array_data_id;
let array_data_text = todo_array[i].todo_parse;
new_todo.innerHTML = '<b>' + array_data_id++ + ': ' + array_data_text + '</b>';
document.getElementById('results').append(new_todo);
array_id = array_data_id;
add_del_btn(del_num);
add_edit_btn(del_num);
add_check(del_num);
}
} else {
todo_array = []
}
}
function lstorage_clr() {
for (let i = 0; i < todo_array.length; i++) {
document.querySelectorAll('.new_todo').forEach(function (a) {
a.remove();
}
)
}
;
for (let i = 0; i < done_array.length; i++) {
document.querySelectorAll('.done').forEach(function (a) {
a.remove();
}
)
}
;
window.localStorage.clear();
todo_array = [];
array_id = 1;
render();
document.getElementById('done').style.visibility = 'hidden';
}
function add_del_btn(num) {
let new_del_btn = document.createElement('button');
new_del_btn.value = "delete";
new_del_btn.id = 'del_btn';
new_del_btn.className = 'del_btn';
new_del_btn.setAttribute('onclick', 'delete_todo' + '(' + num + ')');
new_del_btn.innerHTML = 'Удалить'
document.getElementById(num).append(new_del_btn);
}
function delete_todo(del_num) {
let index = todo_array.findIndex(i => i.id_num === del_num);
todo_array.splice(index, 1);
array_id = 0;
render();
storage();
}
function add_edit_btn(num) {
let new_edit_btn = document.createElement('button');
new_edit_btn.value = "edit";
new_edit_btn.id = 'edit_btn' + num;
new_edit_btn.className = 'edit_btn';
new_edit_btn.setAttribute('onclick', 'edit_todo' + '(' + num + ')');
new_edit_btn.innerHTML = 'Редактировать'
document.getElementById(num).append(new_edit_btn);
}
function edit_todo(num) {
let edit_input = document.createElement("input");
edit_input.placeholder = 'Отредактируйте задачу';
edit_input.className = 'edit_input';
edit_input.id = ('edit_input' + num);
edit_input.setAttribute(onsubmit, 'edit_input_close' + '(' + num + ')');
document.getElementById(num).append(edit_input);
let edit_btn = document.getElementById('edit_btn' + num);
edit_btn.setAttribute('onclick', 'edit_input_close(' + num + ')');
edit_btn.innerHTML = 'Сохранить';
}
function edit_input_close(num) {
let index = todo_array.findIndex(i => i.id_num === num);
let old_todo_text = todo_array[index].todo_parse;
let old_todo_id = todo_array[index].id_num;
let edited_todo = ({id_num: old_todo_id, todo_parse: document.getElementById('edit_input' + num).value});
todo_array.splice(index, 1, edited_todo);
let edit_close = document.getElementById('edit_input' + num);
edit_close.remove();
let edit_btn = document.getElementById('edit_btn' + num);
edit_btn.setAttribute('onclick', 'edit_todo' + '(' + num + ')')
render();
storage();
}
function add_check(num) {
let checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.id = 'check' + num;
checkbox.className = 'checkbox';
checkbox.setAttribute('onclick', 'add_to_done(' + num + ')');
document.getElementById(num).append(checkbox);
}
function add_to_done(num) {
document.getElementById('done').style.visibility = 'visible';
let index = todo_array.findIndex(i => i.id_num === num);
let id_done = num;
let text_done = todo_array[index].todo_parse;
done_array.push({id_done, text_done});
let store_todo = JSON.stringify(done_array);
localStorage.setItem('done_list', store_todo);
let done = document.createElement('div');
done.className = 'done';
let array_data_id = done_array[done_array.length - 1].id_done;
done.id = array_data_id;
let array_data_text = done_array[done_array.length - 1].text_done;
done.innerHTML = '<i>' + num + ': ' + array_data_text + '</i>';
document.getElementById('done').append(done);
delete_todo(num);
}