-
Notifications
You must be signed in to change notification settings - Fork 93
/
resource-list-item.ts
344 lines (301 loc) · 9.88 KB
/
resource-list-item.ts
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import { action } from "@ember/object";
import Component from "@glimmer/component";
import { draggable } from "@atlaskit/pragmatic-drag-and-drop/element/adapter";
import {
dropTargetForElements,
ElementDropTargetEventBasePayload,
ElementDropTargetGetFeedbackArgs,
} from "@atlaskit/pragmatic-drag-and-drop/element/adapter";
import {
attachClosestEdge,
Edge,
extractClosestEdge,
} from "@atlaskit/pragmatic-drag-and-drop-hitbox/closest-edge";
import { assert } from "@ember/debug";
import { combine } from "@atlaskit/pragmatic-drag-and-drop/combine";
import { tracked } from "@glimmer/tracking";
import { RelatedResource } from "../related-resources";
import { setCustomNativeDragPreview } from "@atlaskit/pragmatic-drag-and-drop/element/set-custom-native-drag-preview";
import { announce } from "@atlaskit/pragmatic-drag-and-drop-live-region";
import { guidFor } from "@ember/object/internals";
import { schedule } from "@ember/runloop";
enum Edges {
Top = "top",
Bottom = "bottom",
}
interface ProjectResourceListItemComponentSignature {
Element: HTMLLIElement;
Args: {
isReadOnly: boolean;
item: RelatedResource;
itemCount: number;
index: number;
onSave: (currentIndex: number, newIndex: number) => void;
};
Blocks: {
default: [
{
moveToTop: () => void;
moveUp: () => void;
moveDown: () => void;
moveToBottom: () => void;
},
];
};
}
export default class ProjectResourceListItemComponent extends Component<ProjectResourceListItemComponentSignature> {
/**
* A unique identifier used for the `customNativeDragPreview` container.
* Used by `in-element` to render the drag preview in the correct location.
*/
protected id = guidFor(this);
/**
* Whether the item is currently being dragged.
* Set true `onGenerateDragPreview` and false `onDrop`.
* Used to conditionally render the drag preview.
*/
@tracked protected isDragging = false;
/**
* Whether the item is currently being dragged over.
* Set true `onDragEnter` and false `onDragLeave`.
* Used to conditionally render the drop indicator.
*/
@tracked protected dragHasEntered = false;
/**
* The closest edge of the drop target element.
* Used to determine where the drop indicator should be rendered.
*/
@tracked protected closestEdge: Edge | null = null;
/**
* The list item element. Registered on render and used in drag-and-drop
* functions and scoped `querySelector` calls.
*/
@tracked protected el: HTMLElement | null = null;
/**
* The drag handle element. Targeted for focus after a drop.
*/
@tracked protected _dragHandle: HTMLButtonElement | null = null;
/**
* The title of the item, whether it's a document or a resource.
* Used in the `customNativeDragPreview`.
*/
protected get itemTitle() {
if ("title" in this.args.item) {
return this.args.item.title;
} else {
return this.args.item.name;
}
}
/**
* The document number of the item, if it's a document.
* Used in the `customNativeDragPreview`.
*/
protected get docNumber() {
if ("documentNumber" in this.args.item) {
return this.args.item.documentNumber;
} else {
return null;
}
}
/**
* An asserted-true getter for the drag handle element.
*/
protected get dragHandle() {
assert("dragHandle must exist", this._dragHandle);
return this._dragHandle;
}
/**
* The action to announce the movement of an item to screen readers.
* Called when an item is moved up, down, to the top, or to the bottom.
*/
private announceMovement(direction: string) {
announce(`${this.itemTitle} moved ${direction}`);
}
private scheduleFocusDragHandle() {
schedule("afterRender", () => {
this.dragHandle.focus();
});
}
/**
* The action to register the list item.
* Called on render and used as a target for drag-and-drop functions.
*/
@action protected registerElement(element: HTMLElement) {
this.el = element;
}
/**
* The action to move an item to the top of the list.
* Called on click of the "move to top" button.
*/
@action protected moveToTop() {
this.args.onSave(this.args.index, 0);
this.announceMovement("to top");
this.scheduleFocusDragHandle();
}
/**
* The action to move an item up in the list.
* Called on click of the "move up" button.
*/
@action protected moveUp() {
this.args.onSave(this.args.index, this.args.index - 1);
this.announceMovement("up");
this.scheduleFocusDragHandle();
}
/**
* The action to move an item down in the list.
* Called on click of the "move down" button.
*/
@action protected moveDown() {
this.args.onSave(this.args.index, this.args.index + 1);
this.announceMovement("down");
this.scheduleFocusDragHandle();
}
/**
* The action to move an item to the bottom of the list.
* Called on click of the "move to bottom" button.
*/
@action protected moveToBottom() {
this.args.onSave(this.args.index, this.args.itemCount - 1);
this.announceMovement("to bottom");
this.scheduleFocusDragHandle();
}
private isHoveringSameParent(e: ElementDropTargetEventBasePayload) {
const sourceElement = e.source.element;
const selfElement = e.self.element;
const sourceElementParent = sourceElement.parentElement;
const selfElementParent = selfElement.parentElement;
return sourceElementParent === selfElementParent;
}
/**
* The action to configure the drag-and-drop functionality.
* Called on render if the list is interactive. Configures the drag-and-drop
* functionality for the list item.
*/
@action protected configureDragAndDrop() {
assert("element must exist", this.el);
const element = this.el;
this._dragHandle = this.el.querySelector(`.drag-handle`);
combine(
draggable({
element,
dragHandle: this.dragHandle,
onGenerateDragPreview: ({ nativeSetDragImage }) => {
this.isDragging = true;
setCustomNativeDragPreview({
nativeSetDragImage,
render: ({ container }) => {
// Create a target for `in-element`
container.id = this.id;
},
});
},
onDrop: () => {
this.isDragging = false;
schedule("afterRender", () => {
this.dragHandle.focus();
});
},
getInitialData: () => {
return {
index: this.args.index,
};
},
}),
dropTargetForElements({
element,
getData(e: ElementDropTargetGetFeedbackArgs) {
return attachClosestEdge(
{},
{
element,
input: e.input,
allowedEdges: [Edges.Top, Edges.Bottom],
},
);
},
canDrop: (e: ElementDropTargetGetFeedbackArgs) => {
return e.source.element !== element;
},
onDrag: (e: ElementDropTargetEventBasePayload) => {
const isSource = e.source.element === element;
if (isSource) {
this.closestEdge = null;
return;
}
const closestEdge = extractClosestEdge(e.self.data);
const sourceIndex = e.source.data["index"];
assert(
"sourceIndex must be a number",
typeof sourceIndex === "number",
);
const dropTarget = e.location.current.dropTargets[0];
if (dropTarget) {
const dataIndex = dropTarget.element.getAttribute("data-index");
assert("data-index must exist", dataIndex);
const index = parseInt(dataIndex, 10);
const isItemBeforeSource = index === sourceIndex - 1;
const isItemAfterSource = index === sourceIndex + 1;
const isDropIndicatorHidden =
(isItemBeforeSource && closestEdge === Edges.Bottom) ||
(isItemAfterSource && closestEdge === Edges.Top);
if (isDropIndicatorHidden) {
this.closestEdge = null;
return;
} else {
this.closestEdge = closestEdge;
}
} else {
this.closestEdge = null;
return;
}
},
onDragEnter: (e: ElementDropTargetEventBasePayload) => {
const isHoveringSelf = e.source.element === element;
if (!isHoveringSelf && this.isHoveringSameParent(e)) {
this.dragHasEntered = true;
}
},
onDragLeave: () => {
this.dragHasEntered = false;
this.closestEdge = null;
},
onDrop: (e: ElementDropTargetEventBasePayload) => {
if (!this.isHoveringSameParent(e)) {
return;
}
const { data } = e.source;
const index = data["index"];
assert("index must be a number", typeof index === "number");
const dropTargetElement = e.location.current.dropTargets[0]?.element;
let newIndex = parseInt(
dropTargetElement?.getAttribute("data-index") ?? "",
10,
);
const closestEdge =
dropTargetElement?.getAttribute("data-closest-edge");
const isDraggingDown = index < newIndex;
const isDraggingUp = index > newIndex;
if (closestEdge === "bottom" && isDraggingUp) {
newIndex += 1;
} else if (closestEdge === "top" && isDraggingDown) {
newIndex -= 1;
}
this.args.onSave(index, newIndex);
// Announce to screen readers
announce(
`${this.itemTitle} moved from position ${index + 1} to ${
newIndex + 1
}`,
);
this.dragHasEntered = false;
this.closestEdge = null;
},
}),
);
}
}
declare module "@glint/environment-ember-loose/registry" {
export default interface Registry {
"Project::ResourceListItem": typeof ProjectResourceListItemComponent;
}
}