src/drag-source-specification.ts
The spec passed to DndService.
Note the two type parameters. Both must represent plain JS objects.
Item
is the type you return from beginDrag()
, and available in monitor.getItem()
.
Limitations in TypeScript may prevent inferring this in many circumstances,
and it will default to {}
. It is best if you are strong-typing to pass a
type parameter like so:
interface MyDraggedItemType { id: number; }
// ...
source = this.dnd.dragSource<MyDraggedItemType>(..., {
beginDrag: monitor => ({ id: this.id })
})
DropResult
is the type you expect a drop target to return from drop()
.
It is the type of the object returned by monitor.getDropResult()
during endDrag()
.
Note that your backend may assign some extra properties. You should define a
DropResult
type that recognises these, such as:
interface HTML5DropResult { dropEffect: string; }
interface MyDropResult extends HTML5DropResult { id: number; }
target = this.dnd.dropTarget<..., MyDropResult>(..., {
drop: monitor => ({ id: this.id })
});
source = this.dnd.dragSource<... MyDropResult>(..., {
endDrag: monitor => {
const result = monitor.getDropResult();
if (result.dropEffect === 'copy') {
// user had alt key pressed + the GreenPlus icon when they dropped,
// so copy instead of move
}
}
});
Methods |
|
beginDrag | ||||||
beginDrag(monitor: DragSourceMonitor<void | void>)
|
||||||
Required. When the dragging starts,
Parameters :
Returns :
Item
|
Optional canDrag | ||||||
canDrag(monitor: DragSourceMonitor<void | void>)
|
||||||
Optional. Queries your component to determine whether this source can be dragged. Default returns true; this is often sufficient.
Parameters :
Returns :
boolean
|
Optional endDrag | ||||||
endDrag(monitor: DragSourceMonitor<Item | DropResult>)
|
||||||
Optional. Notifies your component when dragging ends. This is a good place to fire actions or modify a component. You will often want to check DragSourceMonitor and DragSourceMonitor for more details.
Parameters :
Returns :
void
|
Optional isDragging | ||||||
isDragging(monitor: DragSourceMonitor<Item | void>)
|
||||||
By default, only the drag source that initiated the drag operation is considered to be dragging. You might override this by matching on the DragSourceMonitor.id and your component's id, or similar. Do this if the original component may be unmounted during the dragging and later "resurrected" with a different parent. For example, when moving a card across the lists in a Kanban board, you want it to retain the dragged appearance—even though technically, the component gets unmounted and a different one gets mounted every time you hover over another list. Note: You may not call DragSourceMonitor inside this method. NOTE: runs outside Angular change detection. This is for performance
reasons. You shouldn't be making changes to your component here anyway. If
you do change your component inside this callback, it may not appear
immediately, and if you use
Parameters :
Returns :
boolean
|