-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.js
245 lines (187 loc) · 6.73 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
var defaultSections = [
{
id: 1,
title: 'New',
tasks: []
},
{
id: 2,
title: 'Active',
tasks: []
},
{
id: 3,
title: 'Done',
tasks: []
},
];
var sections = localStorage.getItem('kanban_sections');
sections = sections ? JSON.parse(sections) : defaultSections;
const sectionsElement = document.getElementById('sections');
/**
* @desc Render all sections with tasks in inside.
*/
function renderBoard() {
console.log("Rendering", sections);
sectionsElement.innerHTML = sections.map(section => {
return `
<div class="section" section-id="${section.id}">
<div class="title">
<div class="text">${section.title} ${section.tasks.length ? '(' + section.tasks.length + ')' : ''}</div>
<div class="toolbar">
<button title="Add Task" class="add" onclick="addTask('${section.id}')"><i class="fa fa-plus"></i></button>
<button title="Edit Section" class="edit" onclick="editSection('${section.id}')"><i class="fa fa-edit"></i></button>
<button title="Delete Section" class="delete" onclick="deleteSection('${section.id}')"><i class="fa fa-trash"></i></button>
</div>
</div>
<div class="tasks">
<!-- <div class="dropzone" section-id="${section.id}">
DROP HERE
</div> -->
${!section.tasks.length ? `
<div class="no-tasks">
<i class="fa fa-exclamation-circle"></i>
<div>No Tasks</div>
</div>
`: ''}
${section.tasks.map(task => {
return `
<div class="task" task-id="${task.id}">
<div class="title">
<div class="text">${task.title}</div>
<div class="toolbar">
<button title="Edit Task" class="edit" onclick="editTask('${section.id}', '${task.id}')"><i class="fa fa-edit"></i></button>
<button title="Delete Task" class="delete" onclick="deleteTask('${section.id}', '${task.id}')"><i class="fa fa-trash"></i></button>
</div>
</div>
<div class="description">${task.description}</div>
<i task-id="${task.id}" section-id="${section.id}" class="handle fa fa-grip-horizontal">...</i>
</div>
`
}).join("")}
</div>
</div>
`;
}).join("");
// Update localStorage
localStorage.setItem('kanban_sections', JSON.stringify(sections))
}
/**
* @desc Add a section
*/
function addSection() {
let section = askInput(['title']);
section.id = Date.now();
section.tasks = [];
sections.push(section);
renderBoard();
}
/**
* @desc Delete a section
*/
function deleteSection(id) {
sections = sections.filter(x => x.id != id);
renderBoard();
}
/**
* @desc Edit a section
*/
function editSection(id) {
let section = sections.find(x => x.id == id);
section.title = askInput(['title'], section).title;
renderBoard();
}
/**
* @desc Add a section
*/
function addTask(id) {
let section = sections.find(x => x.id == id);
let task = askInput(['title', 'description']);
task.id = Date.now();
section.tasks.push(task);
renderBoard();
}
/**
* @desc Delete a task
*/
function deleteTask(sectionId, taskId) {
let section = sections.find(x => x.id == sectionId);
section.tasks = section.tasks.filter(task => task.id != taskId);
renderBoard();
}
/**
* @desc Edit a task
*/
function editTask(sectionId, taskId) {
let section = sections.find(x => x.id == sectionId);
let task = section.tasks.find(task => task.id == taskId);
let output = askInput(['title', 'description'], task);
task.title = output.title;
task.description = output.description;
renderBoard();
}
/**
* @desc Generic function to ask user input using prompt
*/
function askInput(input, defaults) {
let result = {};
input.forEach(item => {
result[item] = prompt("Enter " + item, defaults && defaults[item] ? defaults[item] : "");
})
return result;
}
renderBoard();
// let op = askInput(['name', 'description', 'age']);
// console.log(op);
/**
* DRAG & DROP FEATURE
*/
let activeDrag = {};
let activeDragElement;
function mouseDown(event) {
console.log(event);
if (event.target.hasAttribute('task-id') && event.target.hasAttribute('section-id')) {
activeDrag.task = event.target.getAttribute('task-id');
activeDrag.section = event.target.getAttribute('section-id');
activeDragElement = event.target.parentElement;
activeDrag.width = activeDragElement.clientWidth;
activeDrag.height = activeDragElement.clientHeight;
activeDragElement.style.position = 'absolute';
activeDragElement.style.width = activeDrag.width - 20 + 'px';
mouseMove(event)
// Listen for mouse move and mouse up
document.addEventListener('mousemove', mouseMove)
document.addEventListener('mouseup', mouseUp)
}
}
document.addEventListener('mousedown', mouseDown);
function mouseMove(event) {
// console.log("Dragging", event.x, event.y);
activeDragElement.style.top = event.y + window.scrollY - activeDrag.height - 2 + "px";
activeDragElement.style.left = event.x + window.scrollX - (activeDrag.width / 2) + "px";
// activeDragElement.style.transform = 'translateY(20px)'
// console.log(activeDragElement);
// console.log(event.x, event.y);
}
function mouseUp(event) {
console.log("Dropped", event);
activeDragElement.style.position = 'initial';
activeDragElement.style.top = 'unset';
activeDragElement.style.left = 'unset';
document.removeEventListener('mousemove', mouseMove);
// document.removeEventListener('mousedown', mouseDown);
document.removeEventListener('mouseup', mouseUp);
if (event.target.hasAttribute('section-id')) {
console.log("Correct place");
let section = sections.find(x => x.id == activeDrag.section);
let task = section.tasks.find(x => x.id == activeDrag.task);
// Remove from where drag started
section.tasks = section.tasks.filter(x => x.id != activeDrag.task)
// Add to where drag ended
let dropSection = event.target.getAttribute('section-id');
sections.find(x => x.id == dropSection).tasks.unshift(task)
renderBoard();
}
}
// Remove select listener
document.addEventListener('selectstart', e => { e.preventDefault() })