-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
112 lines (95 loc) · 2.79 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
const commandEl = document.getElementById("command")
const contentEl = document.getElementById("content")
const boardEl = document.getElementById("board")
const todoEl = document.getElementById("todo")
const doingEl = document.getElementById("doing")
const doneEl = document.getElementById("done")
document.getElementById("submit").addEventListener("click", inputCommand)
let tasks = []
function inputCommand () {
let commandValue = commandEl.value
let commands = commandValue.split(" ")
const action = commands[0]
const rest = commandValue.substring(action.length + 2, commandValue.length - 1)
if (action == "create") {
const currentLength = tasks.length
tasks.push({id: currentLength + 1, name: rest, type: 'todo'})
} else if (action == "move") {
const id = commands[1]
const target = commands[2]
const targets = ["todo", "doing", "done"]
if (targets.indexOf(target) > -1) {
moveTask(id, target)
} else { alert(`Sorry, only ${targets.join(", ")} are allowed`) }
} else if (action == "remove") {
const id = commands[1]
removeTask(id)
}
render()
}
function moveTask (id, target) {
const index = tasks.findIndex(val => val.id == id)
if (index > -1) {
tasks[index] = {...tasks[index], type: target}
}
render()
}
function removeTask (id) {
const index = tasks.findIndex(val => val.id == id)
if (index > -1) {
const removeConfirm = confirm("Remove task #" + id + "?")
if (removeConfirm == true) {
moveTask(id, 'deleted')
}
} else {
alert("Tidak ada task untuk ID tersebut")
}
}
function render () {
renderTasks()
}
function renderTasks () {
doingEl.innerHTML = ""
todoEl.innerHTML = ""
doneEl.innerHTML = ""
tasks.map(val => {
const taskDiv = document.createElement("DIV")
const taskText = document.createTextNode(`#${val.id} ${val.name}`)
taskDiv.classList.add("task")
taskDiv.setAttribute("id", `task-${val.id}`)
taskDiv.setAttribute("draggable", true)
taskDiv.addEventListener("dragstart", drag)
taskDiv.appendChild(taskText)
if (val.type == 'todo') {
todoEl.appendChild(taskDiv)
}
if (val.type == 'doing') {
doingEl.appendChild(taskDiv)
}
if (val.type == 'done') {
doneEl.appendChild(taskDiv)
}
})
}
function allowDrop(ev) {
ev.preventDefault();
}
function drag(ev) {
ev.dataTransfer.setData("id", ev.target.id);
}
function drop(ev) {
ev.preventDefault();
const id = ev.dataTransfer.getData("id");
const tasks = id.split("-")
const targetId = ev.target.id
if (targetId == 'add-todo') {
moveTask(tasks[1], 'todo')
} else if (targetId == 'add-doing') {
moveTask(tasks[1], 'doing')
} else if (targetId == 'add-done') {
moveTask(tasks[1], 'done')
} else if (targetId == 'remove') {
removeTask(tasks[1])
}
}
render()