-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrag-manager.js
More file actions
163 lines (134 loc) · 5.37 KB
/
Copy pathdrag-manager.js
File metadata and controls
163 lines (134 loc) · 5.37 KB
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
// Drag Manager Module
// Handles drag-and-drop reordering of language sections
class DragManager {
constructor(container, onOrderChange) {
this.container = container;
this.onOrderChange = onOrderChange; // callback(newOrder: string[])
this.draggedElement = null;
this.placeholder = null;
this._onDragStart = this._onDragStart.bind(this);
this._onDragOver = this._onDragOver.bind(this);
this._onDragLeave = this._onDragLeave.bind(this);
this._onDrop = this._onDrop.bind(this);
this._onDragEnd = this._onDragEnd.bind(this);
this._attachContainerListeners();
}
// Attach drag events to the container (event delegation)
_attachContainerListeners() {
this.container.addEventListener('dragover', this._onDragOver);
this.container.addEventListener('dragleave', this._onDragLeave);
this.container.addEventListener('drop', this._onDrop);
this.container.addEventListener('dragend', this._onDragEnd);
}
// Initialize drag handle for a section element.
// Called by app-main after a section is added to the DOM.
initSection(sectionElement) {
const handle = sectionElement.querySelector('.drag-handle');
if (!handle) return;
// When mouse presses on the handle, make the section draggable
handle.addEventListener('mousedown', () => {
sectionElement.setAttribute('draggable', 'true');
});
// After drag ends or mouse leaves, remove draggable
handle.addEventListener('mouseup', () => {
sectionElement.removeAttribute('draggable');
});
sectionElement.addEventListener('dragstart', this._onDragStart);
}
_onDragStart(e) {
this.draggedElement = e.currentTarget;
// Set drag data (required for Firefox)
e.dataTransfer.effectAllowed = 'move';
e.dataTransfer.setData('text/plain', this.draggedElement.dataset.language || '');
// Create placeholder
this.placeholder = document.createElement('div');
this.placeholder.className = 'drag-placeholder';
// Apply dragging style after a frame so the drag image captures the original look
requestAnimationFrame(() => {
if (this.draggedElement) {
this.draggedElement.classList.add('dragging');
}
});
}
_onDragOver(e) {
e.preventDefault();
e.dataTransfer.dropEffect = 'move';
if (!this.draggedElement) return;
const target = this._getDraggableTarget(e.target);
if (!target || target === this.draggedElement || target === this.placeholder) return;
// Skip "Overall Trending" section (data-language="all")
if (target.dataset.language === 'all') return;
const rect = target.getBoundingClientRect();
const midY = rect.top + rect.height / 2;
// Remove existing placeholder
if (this.placeholder.parentNode) {
this.placeholder.parentNode.removeChild(this.placeholder);
}
// Insert placeholder before or after the target based on cursor position
if (e.clientY < midY) {
target.parentNode.insertBefore(this.placeholder, target);
} else {
target.parentNode.insertBefore(this.placeholder, target.nextSibling);
}
}
_onDragLeave(e) {
// Only handle when leaving the container entirely
if (!this.container.contains(e.relatedTarget) && this.placeholder && this.placeholder.parentNode) {
this.placeholder.parentNode.removeChild(this.placeholder);
}
}
_onDrop(e) {
e.preventDefault();
if (!this.draggedElement || !this.placeholder || !this.placeholder.parentNode) {
this._cleanup();
return;
}
// Move the dragged element to the placeholder position
this.placeholder.parentNode.insertBefore(this.draggedElement, this.placeholder);
this._cleanup();
this._emitOrderChange();
}
_onDragEnd() {
this._cleanup();
}
_cleanup() {
if (this.draggedElement) {
this.draggedElement.classList.remove('dragging');
this.draggedElement.removeAttribute('draggable');
}
if (this.placeholder && this.placeholder.parentNode) {
this.placeholder.parentNode.removeChild(this.placeholder);
}
this.draggedElement = null;
this.placeholder = null;
}
// Walk up the DOM to find the language-section element
_getDraggableTarget(el) {
while (el && el !== this.container) {
if (el.classList && el.classList.contains('language-section') && el.dataset.language) {
return el;
}
el = el.parentElement;
}
return null;
}
// Read the current DOM order and fire the callback
_emitOrderChange() {
const sections = this.container.querySelectorAll('.language-section[data-language]');
const order = [];
sections.forEach(section => {
const lang = section.dataset.language;
// Skip "Overall Trending"
if (lang !== 'all') {
order.push(lang);
}
});
if (this.onOrderChange) {
this.onOrderChange(order);
}
}
}
// Export for global use
if (typeof window !== 'undefined') {
window.DragManager = DragManager;
}