드래그 앤 드롭 소스코드

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
// v.0.1 app.js 1407줄
// 드래그 앤 드롭
$calendarDates.addEventListener('mousedown', e => {
const initialMousePos = {
x: e.clientX,
y: e.clientY
};

const $selectedMoveBtn = closest(e.target, 'item-move-btn', 'calendar-dates');

if (!$selectedMoveBtn) return;

const draggable = closest(e.target, 'item', 'calendar-dates');
draggable.classList.add('dragging');

e.target.style['pointer-events'] = 'none';
e.target.parentElement.style['pointer-events'] = 'none';
e.target.parentElement.parentElement.style['pointer-events'] = 'none';

[...document.querySelectorAll('.item-control-btn')].forEach(
$itemControlBtn => {
$itemControlBtn.classList.toggle('--invisible');
}
);

const eventFunctions = (() => {
let $container = null;
let $prevContainer = null;
let $nextElement = null;
let $prevNextElement = null;

return {
mouseMoveEvent(e) {
draggable.style.transform = `translate3d(${
e.clientX - initialMousePos.x
}px, ${e.clientY - initialMousePos.y}px, 0)`;
},
mouseOverEvent(e) {
if ($prevNextElement) $prevNextElement.style.border = 'none';
if ($prevContainer) $prevContainer.style.border = 'none';

$container = e.target.closest('.items');
if (!$container) return;

$nextElement = e.target.closest('li');

if ($nextElement) {
$nextElement.style['border-top'] = 'solid 5px gray';
$prevNextElement = $nextElement;
return;
}

const $lastElement = $container.lastElementChild;

// 이동할 위치에 아무 일정도 없는 경우
if (!$lastElement) {
$container.style['border-top'] = 'solid 5px gray';
$prevContainer = $container;
return;
}

// 이동할 위치에 현재 이동중인 자신의 일정이 있는데
// 그게 이동할 위치의 마지막이고 자신보다 위에 일정이 있는 경우
const $prevElement = draggable.previousElementSibling;
if ($lastElement === draggable && $prevElement) {
$prevElement.style['border-bottom'] = 'solid 5px gray';
$prevNextElement = $prevElement;
return;
}

// 이동할 위치에 현재 이동중인 자신의 일정만 있는 경우
if ($lastElement === draggable) {
$container.style['border-top'] = 'solid 5px gray';
$prevContainer = $container;
}

// 이동할 위치에 현재 이동중인 자신의 일정이 있는데
// 그게 이동할 위치의 마지막이 아니고 들어가려고 하는 위치는 마지막인 경우
if ($lastElement !== draggable) {
$lastElement.style['border-bottom'] = 'solid 5px gray';
$prevNextElement = $lastElement;
}
},
mouseUpEvent() {
draggable.classList.remove('dragging');
draggable.style.transform = 'none';

if ($prevNextElement) $prevNextElement.style.border = 'none';
if ($prevContainer) $prevContainer.style.border = 'none';

e.target.style['pointer-events'] = 'auto';
e.target.parentElement.style['pointer-events'] = 'auto';
e.target.parentElement.parentElement.style['pointer-events'] = 'auto';

[...document.querySelectorAll('.item-control-btn')].forEach(
$itemControlBtn => {
$itemControlBtn.classList.toggle('--invisible');
}
);

$calendarDates.removeEventListener(
'mousemove',
eventFunctions.mouseMoveEvent
);
$calendarDates.removeEventListener(
'mouseover',
eventFunctions.mouseOverEvent
);
document.removeEventListener('mouseup', eventFunctions.mouseUpEvent);

if (!$container) return;

if (!$nextElement) {
$container.appendChild(draggable);
const editItem = {
id: draggable.dataset.id,
date: $container.parentElement.dataset.date,
order:
$container.lastElementChild === draggable
? null
: data.filter(
item =>
item.category === currentCategory &&
item.date === $container.parentElement.dataset.date
).length + 1
};
modifyDataArray(editItem);

return;
}

$container.insertBefore(draggable, $nextElement);
[...$container.children].forEach(($li, idx) => {
data.find(item => item.id === +$li.dataset.id).order = idx + 1;
});
data.find(item => item.id === +draggable.dataset.id).date =
$container.parentElement.dataset.date;
}
};
})();

$calendarDates.addEventListener('mousemove', eventFunctions.mouseMoveEvent);
$calendarDates.addEventListener('mouseover', eventFunctions.mouseOverEvent);
document.addEventListener('mouseup', eventFunctions.mouseUpEvent);
});