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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
(function () {
function handleItemDragStart(e)
{
this.style.opacity = '0.4';
dragSrcItem = this;
dragSrcColumn = this.parentNode;
e.dataTransfer.effectAllowed = 'copy';
e.dataTransfer.setData('text/plain', this.innerHTML);
}
function handleItemDragEnd(e)
{
// Restore styles
removeOver();
this.style.opacity = '1.0';
dragSrcColumn = null;
dragSrcItem = null;
}
function handleItemDragOver(e)
{
if (e.preventDefault) e.preventDefault();
e.dataTransfer.dropEffect = 'copy';
return false;
}
function handleItemDragEnter(e)
{
if (dragSrcItem != this) {
removeOver();
this.classList.add('over');
}
}
function handleItemDrop(e)
{
if (e.preventDefault) e.preventDefault();
if (e.stopPropagation) e.stopPropagation();
// Drop the element if the item is not the same
if (dragSrcItem != this) {
var position = getItemPosition(this);
var item = createItem(e.dataTransfer.getData('text/plain'));
if (countColumnItems(this.parentNode) == position) {
this.parentNode.appendChild(item);
}
else {
this.parentNode.insertBefore(item, this);
}
dragSrcItem.parentNode.removeChild(dragSrcItem);
saveBoard();
}
dragSrcColumn = null;
dragSrcItem = null;
return false;
}
function handleColumnDragOver(e)
{
if (e.preventDefault) e.preventDefault();
e.dataTransfer.dropEffect = 'copy';
return false;
}
function handleColumnDragEnter(e)
{
if (dragSrcColumn != this) {
removeOver();
this.classList.add('over');
}
}
function handleColumnDrop(e)
{
if (e.preventDefault) e.preventDefault();
if (e.stopPropagation) e.stopPropagation();
// Drop the element if the column is not the same
if (dragSrcColumn != this) {
var item = createItem(e.dataTransfer.getData('text/plain'));
this.appendChild(item);
dragSrcColumn.removeChild(dragSrcItem);
saveBoard();
}
return false;
}
function saveBoard()
{
var data = [];
var projectId = document.getElementById("board").getAttribute("data-project-id");
var cols = document.querySelectorAll('.column');
[].forEach.call(cols, function(col) {
var task_limit = col.getAttribute("data-task-limit");
if (task_limit != "" && task_limit != "0") {
task_limit = parseInt(task_limit);
if (col.children.length > task_limit) {
col.classList.add("task-limit-warning");
}
else {
col.classList.remove("task-limit-warning");
}
var counter = document.getElementById("task-number-column-" + col.getAttribute("data-column-id"));
if (counter) counter.innerHTML = col.children.length;
}
[].forEach.call(col.children, function(item) {
data.push({
"task_id": item.firstElementChild.getAttribute("data-task-id"),
"position": getItemPosition(item),
"column_id": col.getAttribute("data-column-id")
})
});
});
var xhr = new XMLHttpRequest();
xhr.open("POST", "?controller=board&action=save&project_id=" + projectId, true);
xhr.onreadystatechange = function(response) {
if (this.readyState == this.DONE) {
try {
var response = JSON.parse(this.responseText);
if (response.result == false) {
window.alert('Unable to update the board');
}
else if (response.refresh == true) {
window.location = "?controller=board&action=show&project_id=" + projectId;
}
}
catch (e) {}
}
};
xhr.send(JSON.stringify(data));
}
function getItemPosition(element)
{
var i = 0;
while ((element = element.previousSibling) != null) {
if (element.nodeName == "DIV" && element.className == "draggable-item") {
i++;
}
}
return i + 1;
}
function countColumnItems(element)
{
return element.children.length;
}
function createItem(html)
{
var item = document.createElement("div");
item.className = "draggable-item";
item.draggable = true;
item.innerHTML = html;
item.ondragstart = handleItemDragStart;
item.ondragend = handleItemDragEnd;
item.ondragenter = handleItemDragEnter;
item.ondragover = handleItemDragOver;
item.ondrop = handleItemDrop;
return item;
}
function removeOver()
{
// Remove column over
[].forEach.call(document.querySelectorAll('.column'), function (col) {
col.classList.remove('over');
});
// Remove item over
[].forEach.call(document.querySelectorAll('.draggable-item'), function (item) {
item.classList.remove('over');
});
}
// Drag and drop events
var dragSrcItem = null;
var dragSrcColumn = null;
var items = document.querySelectorAll('.draggable-item');
[].forEach.call(items, function(item) {
item.addEventListener('dragstart', handleItemDragStart, false);
item.addEventListener('dragend', handleItemDragEnd, false);
item.addEventListener('dragenter', handleItemDragEnter, false);
item.addEventListener('dragover', handleItemDragOver, false);
item.addEventListener('drop', handleItemDrop, false);
});
var cols = document.querySelectorAll('.column');
[].forEach.call(cols, function(col) {
col.addEventListener('dragenter', handleColumnDragEnter, false);
col.addEventListener('dragover', handleColumnDragOver, false);
col.addEventListener('drop', handleColumnDrop, false);
});
[].forEach.call(document.querySelectorAll('[data-task-id]'), function (item) {
item.addEventListener('click', function() {
window.location.href = '?controller=task&action=show&task_id=' + item.getAttribute('data-task-id');
});
});
// Filtering
function getSelectedUserFilter()
{
var select = document.getElementById("form-user_id");
return select.options[select.selectedIndex].value;
}
function hasDueDateFilter()
{
var dateFilter = document.getElementById("filter-due-date");
return dateFilter.classList.contains("filter-on");
}
function applyFilter(selectedUserId, filterDueDate)
{
[].forEach.call(document.querySelectorAll('[data-task-id]'), function (item) {
var ownerId = item.getAttribute("data-owner-id");
var dueDate = item.getAttribute("data-due-date");
if (ownerId != selectedUserId && selectedUserId != -1) {
item.style.opacity = "0.2";
}
else {
item.style.opacity = "1.0";
}
if (filterDueDate && (dueDate == "" || dueDate == "0")) {
item.style.opacity = "0.2";
}
});
}
var userFilter = document.getElementById("form-user_id");
var dateFilter = document.getElementById("filter-due-date");
if (userFilter) {
userFilter.onchange = function() {
applyFilter(getSelectedUserFilter(), hasDueDateFilter());
};
}
if (dateFilter) {
dateFilter.onclick = function(e) {
dateFilter.classList.toggle("filter-on");
applyFilter(getSelectedUserFilter(), hasDueDateFilter());
e.preventDefault();
};
}
}());
|