-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
118 lines (107 loc) · 2.91 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
const textNewNode = document.getElementById('input_new_Node');
const btnCreate = document.getElementById('create');
const btnSuccess = document.getElementById('btn-success');
const btnDelete = document.getElementById('btn-delete');
const listElements = document.getElementById('list');
const itemText = document.getElementById('item-text');
const listNotes = [
{
title: "Записатся к врачу",
state: false
},
{
title: "Сходить в магазин",
state: false
},
{
title: "Дочетать книгу",
state: false
}
];
function deleteElement(arr, index) {
return arr.splice(index,1);
}
function render(arr) {
clearingList(listElements);
if(arr.length == 0){
addAlternativeText();
} else{
for(let i = 0; i < arr.length; i++){
addNote(createNote(arr[i].title, i, arr[i].state));
}
}
}
function chengeState(index){
let arrElm = listNotes[index];
if(arrElm.state == true){
arrElm.state = false;
} else{
arrElm.state = true;
}
}
function clearingList(list) {
list.innerHTML="";
}
function addNote(note){
listElements.insertAdjacentHTML('beforeend', note);
}
function createNote(text, index, state) {
let meaningClassText = "";
let meaningClassBtn = "";
if(state == false){
meaningClassText = "";
meaningClassBtn = "btn-success"
} else {
meaningClassText = "done";
meaningClassBtn = "btn-warning"
}
return`
<li class="item ">
<span class="${meaningClassText}">${text}</span>
<div>
<button
class="btn btn-small ${meaningClassBtn}"
id="btn-success"
data-index="${index}"
data-type="toggle">✓</button>
<button
class="btn btn-small btn-danger"
id="btn-delete"
data-index="${index}"
data-type="delete">×</button>
</div>
</li>`
}
listElements.onclick = function(event){
const indexElm = event.target.dataset.index;
let typeElm = event.target.dataset.type;
if(typeElm == "delete"){
deleteElement(listNotes, indexElm);
render(listNotes);
} else if(typeElm == "toggle"){
chengeState(indexElm);
render(listNotes);
}
}
function addAlternativeText(){
return addNote(`
<span class="item-text" id="item-text">
Нет заметок
</span>`
);
}
btnCreate.onclick = function () {
if(textNewNode.value.length == 0){
return
}
listNotes.push({title: textNewNode.value, state: false})
render(listNotes)
textNewNode.value ='';
}
textNewNode.addEventListener('keypress', function (event) {
let key = event.keyCode;
if (key === 13) {
btnCreate.onclick();
}
});
render(listNotes);