forked from adopted-ember-addons/ember-drag-sort
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrag-sort-list.gts
More file actions
266 lines (226 loc) · 7.35 KB
/
Copy pathdrag-sort-list.gts
File metadata and controls
266 lines (226 loc) · 7.35 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import Component from '@glimmer/component';
import { action } from '@ember/object';
import { service } from '@ember/service';
import { on } from '@ember/modifier';
import { element } from 'ember-element-helper';
import type DragSort from '../services/drag-sort.js';
import DragSortItem from './drag-sort-item.gts';
import '../styles/ember-drag-sort.css';
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?: (args: {
event: DragEvent;
element: HTMLElement;
draggedItem: Item;
}) => void;
handle?: string;
items: Array<Item>;
isHorizontal?: boolean;
isRtl?: boolean;
group?: string;
sourceOnly?: boolean;
tagName?: string;
};
Blocks: {
default: [item: Item, index: number];
};
}
export default class DragSortList<Item extends object> extends Component<
DragSortListSignature<Item>
> {
<template>
{{#let (element (if @tagName @tagName "div")) as |Tag|}}
<Tag
class="dragSortList
{{if this.draggingEnabled '-draggingEnabled'}}
{{if this.isDragging '-isDragging'}}
{{if this.isDraggingOver '-isDraggingOver'}}
{{if this.isEmpty '-isEmpty'}}
{{if this.isExpanded '-isExpanded'}}
{{if @isHorizontal '-horizontal'}}
{{if @isRtl '-rtl'}}
{{if this.isVertical '-vertical'}}
{{if this.sourceOnly '-sourceOnlyList'}}"
{{on "dragenter" this.dragEnter}}
{{on "dragover" this.dragOver}}
...attributes
>
{{#each @items as |item index|}}
<DragSortItem
@additionalArgs={{@additionalArgs}}
@determineForeignPositionAction={{@determineForeignPositionAction}}
@draggingEnabled={{this.draggingEnabled}}
@dragEndAction={{@dragEndAction}}
@dragStartAction={{@dragStartAction}}
@group={{@group}}
@handle={{@handle}}
@index={{index}}
@isHorizontal={{@isHorizontal}}
@isRtl={{@isRtl}}
@item={{item}}
@items={{@items}}
@sourceOnly={{this.sourceOnly}}
@tagName={{@childTagName}}
class={{@childClass}}
>
{{yield item index}}
</DragSortItem>
{{/each}}
</Tag>
{{/let}}
</template>
@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;
}
@action
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();
}
}
@action
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);
}
@action
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 ?? null;
let targetIndex = 0;
if (isHorizontal) {
targetIndex = this.getClosestHorizontalIndex(event);
dragSort.isDraggingUp = false;
}
dragSort.dragEntering({
group,
items,
isHorizontal,
targetArgs,
targetIndex,
});
}
@action
getClosestHorizontalIndex(event: DragEvent) {
// Calculate which item is closest and make that the target
const itemsNodeList = (event.currentTarget as HTMLElement).querySelectorAll(
'.dragSortItem',
);
const draggableItems = Array.from(itemsNodeList) as HTMLElement[];
const positions = draggableItems.map((draggableItem: HTMLElement) =>
draggableItem.getBoundingClientRect(),
);
const tops = positions.map((pos: DOMRect) => pos.top);
const uniqueTops = [...new Set(tops)];
const rows = uniqueTops.sort();
const currentRowPosition = rows
.filter((row: number) => row < event.clientY)
.pop();
const closestItem = positions
.filter((pos: DOMRect) => pos.top === currentRowPosition)
.pop();
return closestItem ? positions.indexOf(closestItem) : 0;
}
@action
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 });
}
}
@action
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 });
}
}