-
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathdrag-sort-list.ts
More file actions
210 lines (169 loc) · 5.56 KB
/
drag-sort-list.ts
File metadata and controls
210 lines (169 loc) · 5.56 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
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
import Component from '@glimmer/component';
import { service } from '@ember/service';
import { A } from '@ember/array';
import type DragSort from 'ember-drag-sort/services/drag-sort';
interface DragSortListSignature<Item extends object> {
Element: HTMLDivElement;
Args: {
additionalArgs: object;
childClass?: string;
childTagName?: string;
determineForeignPositionAction?: (args: {
draggedItem: unknown;
items: Array<Item>;
}) => number;
draggingEnabled: boolean;
dragEndAction?: unknown;
dragStartAction?: unknown;
handle?: string;
items: Array<Item>;
isHorizontal?: boolean;
isRtl?: boolean;
group?: string;
sourceOnly: boolean;
};
Blocks: {
default: [item: Item, index: number];
};
}
export default class DragSortList<Item extends object> extends Component<
DragSortListSignature<Item>
> {
@service declare dragSort: DragSort<Item>;
declare el: HTMLElement;
get draggingEnabled() {
return this.args.draggingEnabled ?? true;
}
get isDragging() {
const isDragging = this.dragSort.isDragging;
const group = this.args.group;
const groupFromService = this.dragSort.group;
return isDragging && group === groupFromService;
}
get isDraggingOver() {
const isDragging = this.isDragging;
const items = this.args.items;
const targetList = this.dragSort.targetList;
return isDragging && items === targetList;
}
get isVertical() {
return !this.args.isHorizontal;
}
get isExpanded() {
const isDragging = this.isDragging;
const isEmpty = this.isEmpty;
const isOnlyElementDragged = this.isOnlyElementDragged;
return isDragging && (isEmpty || isOnlyElementDragged);
}
get isEmpty() {
return !this.args.items?.length;
}
get isOnlyElementDragged() {
const count = this.args.items?.length;
const items = this.args.items;
const sourceList = this.dragSort.sourceList;
const sourceIndex = this.dragSort.sourceIndex;
return count === 1 && items === sourceList && !sourceIndex;
}
get sourceOnly() {
return this.args.sourceOnly ?? false;
}
elementInserted = (element: HTMLElement) => {
this.el = element;
};
dragEnter = (event: DragEvent) => {
// Ignore irrelevant drags
if (!this.dragSort.isDragging) return;
// Ignore irrelevant groups
const group = this.args.group;
const activeGroup = this.dragSort.group;
if (group !== activeGroup) return;
event.stopPropagation();
// Ignore duplicate events (explanation: https://github.com/lolmaus/jquery.dragbetter#what-this-is-all-about )
const items = this.args.items;
const lastDragEnteredList = this.dragSort.lastDragEnteredList;
if (items === lastDragEnteredList) return;
this.dragEntering(event);
if (this.args.determineForeignPositionAction) {
this.forceDraggingOver();
}
};
dragOver = (event: DragEvent) => {
// This event is only used for placing the dragged element into the end of a horizontal list
if (this.isVertical) {
return;
}
// Ignore irrelevant drags
if (!this.dragSort.isDragging || this.args.determineForeignPositionAction)
return;
const group = this.args.group;
const activeGroup = this.dragSort.group;
if (group !== activeGroup) return;
event.stopPropagation();
this.isDraggingOverHorizontal(event);
};
dragEntering = (event: DragEvent) => {
const group = this.args.group;
const items = this.args.items;
const dragSort = this.dragSort;
const isHorizontal = this.args.isHorizontal;
const targetArgs = this.args.additionalArgs;
let targetIndex = 0;
if (isHorizontal) {
targetIndex = this.getClosestHorizontalIndex(event);
dragSort.isDraggingUp = false;
}
dragSort.dragEntering({
group,
items,
isHorizontal,
targetArgs,
targetIndex,
});
};
getClosestHorizontalIndex = (event: DragEvent) => {
// Calculate which item is closest and make that the target
const itemsNodeList = this.el.querySelectorAll('.dragSortItem');
const draggableItems = A<HTMLElement>(
Array.prototype.slice.call(itemsNodeList),
);
const positions = A(
draggableItems.map((draggableItem) =>
draggableItem.getBoundingClientRect(),
),
);
const rows = positions.uniqBy('top').mapBy('top').sort();
const currentRowPosition = rows.filter((row) => row < event.clientY).pop();
const closestItem = positions.filterBy('top', currentRowPosition).pop();
return closestItem ? positions.indexOf(closestItem) : 0;
};
forceDraggingOver = () => {
const determineForeignPositionAction =
this.args.determineForeignPositionAction;
const group = this.args.group;
const items = this.args.items;
const itemsLength = items?.length;
const draggedItem = this.dragSort.draggedItem;
const sourceList = this.dragSort.sourceList;
let isDraggingUp = true;
if (draggedItem) {
let index =
items === sourceList
? items.indexOf(draggedItem) + 1
: determineForeignPositionAction!({ draggedItem, items });
if (index >= itemsLength) {
index = itemsLength - 1;
isDraggingUp = false;
}
this.dragSort.draggingOver({ group, index, items, isDraggingUp });
}
};
isDraggingOverHorizontal = (event: DragEvent) => {
const dragSort = this.dragSort;
const group = this.args.group;
const items = this.args.items;
const index = this.getClosestHorizontalIndex(event);
const isDraggingUp = false;
dragSort.draggingOver({ group, index, items, isDraggingUp });
};
}