-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathindex.js
113 lines (92 loc) · 2.35 KB
/
index.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
let todos = [
{
id: 1,
name: "Teach Class at Nagarro",
done: true,
d:"2019-11-23"
},
{
id: 2,
name: "Get Coffee",
done: false,
d:"2020-11-28"
}
];
function render(state) {
return state
.map(todo => {
// const li = document.createElement('li')
// li.classList.add("striked")
// document.body.append(li)
const classString = todo.done ? `class = "list-group-item striked"` : `class = "list-group-item"`
return `<li data-todo="${todo.id}" ${classString}> ${todo.name} ${todo.d}<button class="btn" data-todo="${todo.id}">Up</button> ${' '}<button class="btn" data-todo="${todo.id}">Down</button> </li>`;
})
.join("");
}
function paint() {
$("ul").html(render(todos));
const upBtn=`<button id="ubtn">⬆</button>`
}
function moveUp(id){
const tempArray = [].concat(todos);
tempArray.push(tempArray.shift())
todos = tempArray.map((el, index) => ({
...el,
id: index + 1,
}))
paint()
}
function moveDown(id){
const tempArray = [].concat(todos);
tempArray.unshift(tempArray.pop())
todos = tempArray.map((el, index) => ({
...el,
id: index + 1,
}))
paint()
}
function addTodo() {
// document.getElementById('newTodo') != $('#newTodo')
const inputdate=$('#dateInput')
const inputBox = $('#newTodo')
todos.push({
id: todos.length + 1,
name: inputBox.val(),
done: false,
d:inputdate.val()
})
inputBox.val('')
paint()
}
function removeTodos() {
todos = todos.filter(todo => !todo.done)
paint()
}
function resetItems(){
const inputBox = $('#newTodo')
inputBox.val('')
}
$('#sortitems').click(()=>{
todos.sort((b,a)=> new Date(a.d) - new Date(b.d) );
paint()
})
$('ul').on("click", function (e) {
if(e.target.localName === 'button'){
if(e.target.innerText === "Up"){
moveUp(+e.target.dataset.todo)
}else if (e.target.innerText === "Down"){
moveDown(+e.target.dataset.todo)
}
}else{
const idToFind = e.target.dataset.todo
const todo = todos.find(todo => todo.id == idToFind)
todo.done = !todo.done
paint()
}
})
$('#newTodo').on("keypress", function (e) {
if (e.which == 13) {
addTodo()
}
})
paint();