-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctioning.js
178 lines (133 loc) · 5.5 KB
/
functioning.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
174
175
176
177
178
let addInput = document.querySelector('#Add-task');
let addButton = document.querySelector('#Add');
let output = document.querySelector('#output-container');
//Commented html i.e. to be created in javascript
let todos = [];
// let del = document.querySelector('#del-task');
// let up = document.querySelector('#upshift');
// let down = document.querySelector('#downshift');
// let addedTask = document.querySelector('#added-task');
// addButton.addEventListener('click', (event) => {
// let todo = addInput.value;
// todo = todo.trim(); //Remove Extra Spaces
// if (todo == '') {
// alert("Enter Some Task");
// } else {
// // console.log(todo);
// addTodo(todo);
// addInput.value = "";
// update(todo);
// }
// })
// function addTodo(todo) {
// todos.push(todo);
// let addedHtml = `
// <div class="task" >
// <input type="button" value="X" id="del-task">
// <input type="button" value="↑" id="upshift">
// <input type="button" value="↓" id="downshift">
// <input type="text" id="added-task" disabled value='${todo}'>
// </div>`
// output.innerHTML += addedHtml;
// }
// function update(todo) {
// let task = document.querySelectorAll('.task');
// // task.forEach((t) => {
// // // console.log(t);
// // var $curr = t;
// // console.log($curr)
// // t.addEventListener('click', (e) => {
// // // console.log(e.target.id);
// // if (e.target.id == 'del-task') {
// // console.log("Delete this task")
// // t.remove();
// // } else if (e.target.id == 'upshift') {
// // console.log(t.prev())
// // console.log("Shift Up")
// // } else if (e.target.id == 'downshift') {
// // console.log(t.next())
// // console.log("Shift Down")
// // }
// // })
// // })
// for (let i = 0; i < task.length; i++) {
// task[i].addEventListener('click', (e) => {
// // console.log(e.target.id)
// if (e.target.id == 'del-task') {
// console.log("Delete this task")
// task[i].remove();
// } else if (e.target.id === 'upshift') {
// console.log("Shift Up")
// console.log(task[i])
// console.log(task[i - 1]);
// var curr = task[i];
// var next = task[i - 1];
// task[i - 1] = curr;
// task[i] = next;
// } else if (e.target.id === 'downshift') {
// console.log("Shift Down")
// console.log(task[i])
// console.log(task[i + 1]);
// var curr = task[i];
// var next = task[i + 1];
// task[i], task[i + 1] = task[i + 1], task[i];
// }
// })
// }
// }
//render the section
const todoLoader = () => {
let htmlContent = "";
todos.map((item, index) => {
if (index == 0 && index == todos.length - 1) {
//single length
htmlContent += `<div class="task" >
<input type="button" onclick="deleteHandler(${index})" value="X" id="del-task">
<input type="button" disabled onclick="shiftHandler('up', ${index})" value="↑" id="upshift">
<input type="button" disabled onclick="shiftHandler('downf', ${index})" value="↓" id="downshift">
<input type="text" id="added-task" disabled value='${item}'>
</div>`
} else if (index == 0) {
htmlContent += `<div class="task" >
<input type="button" onclick="deleteHandler(${index})" value="X" id="del-task">
<input type="button" disabled onclick="shiftHandler('up', ${index})" value="↑" id="upshift">
<input type="button" onclick="shiftHandler('downf', ${index})" value="↓" id="downshift">
<input type="text" id="added-task" disabled value='${item}'>
</div>`
} else if (index == todos.length - 1) {
htmlContent += `<div class="task" >
<input type="button" onclick="deleteHandler(${index})" value="X" id="del-task">
<input type="button" onclick="shiftHandler('up', ${index})" value="↑" id="upshift">
<input type="button" disabled onclick="shiftHandler('downf', ${index})" value="↓" id="downshift">
<input type="text" id="added-task" disabled value='${item}'>
</div>`
} else {
htmlContent += `<div class="task" >
<input type="button" onclick="deleteHandler(${index})" value="X" id="del-task">
<input type="button" onclick="shiftHandler('up', ${index})" value="↑" id="upshift">
<input type="button" onclick="shiftHandler('downf', ${index})" value="↓" id="downshift">
<input type="text" id="added-task" disabled value='${item}'>
</div>`
}
});
document.getElementById('output-container').innerHTML = htmlContent;
return;
}
//add the todo
function addTodo() {
let value = document.getElementById('Add-task').value;
todos.push(value);
todoLoader();
return;
}
function deleteHandler(index) {
todos.splice(index, 1);
todoLoader();
return;
}
function shiftHandler(dir, index) {
let ele = todos[index];
dir == 'up' ? (todos[index] = todos[index - 1], todos[index - 1] = ele) : (todos[index] = todos[index + 1], todos[index + 1] = ele)
todoLoader();
}
//handle button clicks