/
kanban.html
162 lines (148 loc) · 4.91 KB
/
kanban.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Kanban Board</title>
<style>
#board {
display: table;
margin: 0;
padding: 0;
border-spacing: 5px;
}
.section {
display: table-cell;
margin: 0;
border: 1px solid #666;
padding: 5px;
width: 300px;
}
.section.droppable {
border: 1px dashed #666;
}
.section > h1 {
margin: 0;
border-bottom: 1px solid #999;
padding: 0;
font-size: 12pt;
text-align: center;
}
.card {
display: inline-block;
vertical-align: top;
margin: 10px 5px;
padding: 10px;
width: 100px;
height: 100px;
color: black;
background: #ff8;
cursor: move;
text-align: center;
font-family: "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif;
box-shadow: 2px 2px 2px #eee;
}
</style>
</head>
<body>
<h1>Kanban Board</h1>
<div id="board">
<div id="todo" class="section">
<h1>To Do</h1>
<div id="c2" class="card">Make Killer App</div>
<div id="c3" class="card"><em>Retire!</em></div>
</div>
<div id="doing" class="section">
<h1>Doing</h1>
<div id="c1" class="card">Learn HTML5</div>
</div>
<div id="done" class="section">
<h1>Done</h1>
</div>
</div>
<script>
var cards = document.querySelectorAll('.card');
for (var i = 0, n = cards.length; i < n; i++) {
var card = cards[i];
card.draggable = true;
};
var board = document.getElementById('board');
var hideMe;
board.onselectstart = function(e) {
e.preventDefault();
}
board.ondragstart = function(e) {
console.log('dragstart');
hideMe = e.target;
e.dataTransfer.setData('card', e.target.id);
e.dataTransfer.effectAllowed = 'move';
};
board.ondragend = function(e) {
e.target.style.visibility = 'visible';
};
var lastEneterd;
board.ondragenter = function(e) {
console.log('dragenter');
if (hideMe) {
hideMe.style.visibility = 'hidden';
hideMe = null;
}
// Save this to check in dragleave.
lastEntered = e.target;
var section = closestWithClass(e.target, 'section');
// TODO: Check that it's not the original section.
if (section) {
section.classList.add('droppable');
e.preventDefault(); // Not sure if these needs to be here. Maybe for IE?
return false;
}
};
board.ondragover = function(e) {
// TODO: Check data type.
// TODO: Check that it's not the original section.
if (closestWithClass(e.target, 'section')) {
e.preventDefault();
}
};
board.ondragleave = function(e) {
// FF is raising this event on text nodes so only check elements.
if (e.target.nodeType === 1) {
// dragleave for outer elements can trigger after dragenter for inner elements
// so make sure we're really leaving by checking what we just entered.
// relatedTarget is missing in WebKit: https://bugs.webkit.org/show_bug.cgi?id=66547
var section = closestWithClass(e.target, 'section');
if (section && !section.contains(lastEntered)) {
section.classList.remove('droppable');
}
}
lastEntered = null; // No need to keep this around.
};
board.ondrop = function(e) {
var section = closestWithClass(e.target, 'section');
var id = e.dataTransfer.getData('card');
if (id) {
var card = document.getElementById(id);
// Might be a card from another window.
if (card) {
if (section !== card.parentNode) {
section.appendChild(card);
}
} else {
alert('couldn\'t find card #' + id);
}
}
section.classList.remove('droppable');
e.preventDefault();
};
function closestWithClass(target, className) {
while (target) {
if (target.nodeType === 1 &&
target.classList.contains(className)) {
return target;
}
target = target.parentNode;
}
return null;
}
</script>
</body>
</html>