Skip to content

Commit 8432c4c

Browse files
Avoid reference error in kanban demo (#41853)
* Add case for when `draggedTarget` is `null` and merge `dropover` event code * Fix live sample * Fix `columns` variable * Minor update * Slight rewrite * Minor refactor --------- Co-authored-by: Joshua Chen <sidachen2003@gmail.com>
1 parent 1d75851 commit 8432c4c

File tree

1 file changed

+13
-2
lines changed
  • files/en-us/web/api/html_drag_and_drop_api/kanban_board

1 file changed

+13
-2
lines changed

files/en-us/web/api/html_drag_and_drop_api/kanban_board/index.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,15 @@ This defines the basic structure and styles for our application. The tasks are e
9595

9696
We want to make the task columns into valid [drop targets](/en-US/docs/Web/API/HTML_Drag_and_Drop_API#drop_target) for the dragged tasks. As a baseline, we need to listen for {{domxref("HTMLElement/dragover_event", "dragover")}} and cancel it. However, we take care and only cancel the event if the drag event is dragging a task—if we are trying to drop anything else, the column should not be a drop target.
9797

98+
First, save all columns in a global variable.
99+
98100
```js live-sample___kanban
99101
const columns = document.querySelectorAll(".task-column");
102+
```
100103

104+
Then, declare a `dragover` event handler for each column—this event handler will be expanded later.
105+
106+
```js
101107
columns.forEach((column) => {
102108
column.addEventListener("dragover", (event) => {
103109
// Test a custom type we will set later
@@ -177,12 +183,17 @@ function makePlaceholder(draggedTask) {
177183
}
178184
```
179185

180-
This indicator will be moved around on {{domxref("HTMLElement/dragover_event", "dragover")}}. This is the most complex of all, so we've extracted it into a separate function. We first get the elements we need:
186+
This indicator will be moved around on {{domxref("HTMLElement/dragover_event", "dragover")}}. This is the most complex of all, so we've extracted it into a separate function. The previous code for the `dragover` event has been moved to this function. We first get the elements we need, safely aborting if the drag is not a task:
181187

182188
```js live-sample___kanban
183189
function movePlaceholder(event) {
184-
const column = event.currentTarget;
190+
if (!event.dataTransfer.types.includes("task")) {
191+
return;
192+
}
193+
event.preventDefault();
194+
// Must exist because the ID is added for all drag events with a "task" data entry
185195
const draggedTask = document.getElementById("dragged-task");
196+
const column = event.currentTarget;
186197
const tasks = column.children[1];
187198
const existingPlaceholder = column.querySelector(".placeholder");
188199
```

0 commit comments

Comments
 (0)