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-item.gts
More file actions
321 lines (263 loc) · 8.31 KB
/
Copy pathdrag-sort-item.gts
File metadata and controls
321 lines (263 loc) · 8.31 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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/* eslint-disable ember/no-runloop */
import Component from '@glimmer/component';
import { assert } from '@ember/debug';
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 { next } from '@ember/runloop';
import { tracked } from '@glimmer/tracking';
function getComputedStyleInt(element: HTMLElement, cssProp: string) {
const computedStyle = window.getComputedStyle(element, null);
const valueStr = computedStyle.getPropertyValue(cssProp);
return parseInt(valueStr, 10);
}
interface DragSortItemSignature<Item extends object> {
Element: HTMLElement;
Args: {
additionalArgs?: object;
determineForeignPositionAction?: unknown;
draggingEnabled?: boolean;
dragEndAction?: unknown;
dragStartAction?: (args: {
event: DragEvent;
element: HTMLElement;
draggedItem: Item;
}) => void;
group?: string;
handle?: string;
index: number;
isHorizontal?: boolean;
isRtl?: boolean;
item: Item;
items: Array<Item>;
sourceOnly: boolean;
tagName?: string;
};
Blocks: {
default: [];
};
}
export default class DragSortItem<Item extends object> extends Component<
DragSortItemSignature<Item>
> {
<template>
{{#let (element (if @tagName @tagName "div")) as |Tag|}}
<Tag
class="dragSortItem
{{@index}}
{{this.dragSort.targetIndex}}
{{if this.isDraggingOver '-isDraggingOver'}}
{{if this._isDragged '-isDragged'}}
{{if this.shouldShowPlaceholderBefore '-placeholderBefore'}}
{{if this.shouldShowPlaceholderAfter '-placeholderAfter'}}"
draggable={{this.draggable}}
{{on "dragstart" this.dragStart}}
{{on "dragend" this.dragEnd}}
{{on "dragover" this.dragOver}}
{{on "drop" this.drop}}
...attributes
>
{{yield}}
</Tag>
{{/let}}
</template>
@service declare dragSort: DragSort<Item>;
declare el: HTMLElement;
@tracked _isDragged = false;
get draggable() {
const handle = this.args.handle;
const draggingEnabled = this.args.draggingEnabled;
return !handle && draggingEnabled ? true : null;
}
get isDragged() {
const isDragging = this.dragSort.isDragging;
const items = this.args.items;
const sourceList = this.dragSort.sourceList;
const index = this.args.index;
const sourceIndex = this.dragSort.sourceIndex;
return isDragging && items === sourceList && index === sourceIndex;
}
get isDraggingOver() {
const isDragging = this.dragSort.isDragging;
const items = this.args.items;
const targetList = this.dragSort.targetList;
const index = this.args.index;
const targetIndex = this.dragSort.targetIndex;
const isDragged = this.isDragged;
const sourceOnly = this.args.sourceOnly;
return (
!sourceOnly &&
isDragging &&
items === targetList &&
index === targetIndex &&
!isDragged
);
}
get isLast() {
const index = this.args.index;
const count = this.args.items?.length;
return index === count - 1;
}
get isVertical() {
return !this.dragSort.isHorizontal;
}
get shouldShowPlaceholderBefore() {
const isDraggingOver = this.isDraggingOver;
const isDraggingUp = this.dragSort.isDraggingUp;
const sourceOnly = this.args.sourceOnly;
return !sourceOnly && isDraggingOver && isDraggingUp;
}
get shouldShowPlaceholderAfter() {
const isDraggingOver = this.isDraggingOver;
const isDraggingUp = this.dragSort.isDraggingUp;
const sourceOnly = this.args.sourceOnly;
return !sourceOnly && isDraggingOver && !isDraggingUp;
}
@action
dragStart(event: DragEvent) {
const element = event.currentTarget as HTMLElement;
// Ignore irrelevant drags
if (!this.args.draggingEnabled) return;
if (!this.isHandleUsed(event)) {
event.preventDefault();
return;
}
event.stopPropagation();
// Required for Firefox. http://stackoverflow.com/a/32592759/901944
if (event.dataTransfer) {
if (event.dataTransfer.setData) event.dataTransfer.setData('text', '');
if (event.dataTransfer.setDragImage)
event.dataTransfer.setDragImage(element, 0, 0);
}
const dragStartAction = this.args.dragStartAction;
if (dragStartAction) {
const item = this.args.item;
dragStartAction({
event,
element,
draggedItem: item,
});
}
this.startDragging();
}
@action
dragEnd(event: DragEvent) {
// Ignore irrelevant drags
if (!this.dragSort.isDragging) return;
event.stopPropagation();
event.preventDefault();
this.endDragging();
}
// Required for Firefox. http://stackoverflow.com/a/32592759/901944
@action
drop(event: DragEvent) {
event.preventDefault();
}
@action
dragOver(event: DragEvent) {
// Ignore irrelevant drags
if (
!this.dragSort.isDragging ||
this.args.determineForeignPositionAction ||
this.args.sourceOnly
)
return;
const group = this.args.group;
const activeGroup = this.dragSort.group;
if (group !== activeGroup) return;
event.stopPropagation();
event.preventDefault();
this.draggingOver(event);
}
@action
startDragging() {
this.collapse();
const additionalArgs = this.args.additionalArgs;
const item = this.args.item;
const index = this.args.index;
const items = this.args.items;
const group = this.args.group;
const dragSort = this.dragSort;
const isHorizontal = this.args.isHorizontal;
dragSort.startDragging({
additionalArgs,
item,
index,
items,
group,
isHorizontal,
});
}
@action
endDragging() {
this.restore();
this.dragSort.endDragging({ action: this.args.dragEndAction });
}
@action
draggingOver(event: DragEvent) {
const element = event.currentTarget as HTMLElement;
const sourceOnly = this.args.sourceOnly;
if (sourceOnly) {
event.preventDefault();
return;
}
const { group, index, items } = this.args;
const isHorizontal = this.dragSort.isHorizontal;
const isRtl = this.args.isRtl && isHorizontal;
const isPlaceholderBefore = this.shouldShowPlaceholderBefore;
const isPlaceholderAfter = this.shouldShowPlaceholderAfter;
const dragSort = this.dragSort;
const placeholderModifier = isRtl ? -1 : 1;
let beforeAttribute = 'padding-top';
let afterAttribute = 'padding-bottom';
if (isHorizontal) {
beforeAttribute = isRtl ? 'padding-right' : 'padding-left';
afterAttribute = isRtl ? 'padding-left' : 'padding-right';
}
const placeholderCorrection = isPlaceholderBefore
? getComputedStyleInt(element, beforeAttribute) * placeholderModifier
: isPlaceholderAfter
? -getComputedStyleInt(element, afterAttribute) * placeholderModifier
: 0;
const offset = isHorizontal
? element.getBoundingClientRect().left
: element.getBoundingClientRect().top;
const itemSize = isHorizontal ? element.offsetWidth : element.offsetHeight;
const mousePosition = isHorizontal ? event.clientX : event.clientY;
const isDraggingUp = isRtl
? mousePosition - offset > (itemSize + placeholderCorrection) / 2
: mousePosition - offset < (itemSize + placeholderCorrection) / 2;
dragSort.draggingOver({ group, index, items, isDraggingUp });
}
@action
collapse() {
// The delay is necessary for HTML classes to update with a delay.
// Otherwise, dragging is finished immediately.
next(() => {
if (this.isDestroying || this.isDestroyed) return;
this._isDragged = true;
});
}
@action
restore() {
// The delay is necessary for HTML class to update with a delay.
// Otherwise, dragging is finished immediately.
next(() => {
if (this.isDestroying || this.isDestroyed) return;
this._isDragged = false;
});
}
@action
isHandleUsed(event: DragEvent) {
const handle = this.args.handle;
const target = event.target as HTMLElement;
if (!handle) return true;
const handleElement = (event.currentTarget as HTMLElement).querySelector(
handle,
);
assert('Handle not found', !!handleElement);
return handleElement === target || handleElement.contains(target);
}
}