Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Drag

An object which manages a drag-drop operation. The namespace for the Drag class statics.

A drag object dispatches four different events to drop targets:

  • 'p-dragenter' - Dispatched when the mouse enters the target element. This event must be canceled in order to receive any of the other events.

  • 'p-dragover' - Dispatched when the mouse moves over the drop target. It must cancel the event and set the dropAction to one of the supported actions in order to receive drop events.

  • 'p-dragleave' - Dispatched when the mouse leaves the target element. This includes moving the mouse into child elements.

  • 'p-drop'- Dispatched when the mouse is released over the target element when the target indicates an appropriate drop action. If the event is canceled, the indicated drop action is returned to the initiator through the resolved promise.

A drag operation can be terminated at any time by pressing Escape or by disposing the drag object.

A drag object has the ability to automatically scroll a scrollable element when the mouse is hovered near one of its edges. To enable this, add the data-p-dragscroll attribute to any element which the drag object should consider for scrolling.

Notes

This class is designed to be used when dragging and dropping custom data within a single application. It is not a replacement for the native drag-drop API. Instead, it provides an API which allows drag operations to be initiated programmatically and enables the transfer of arbitrary non-string objects; features which are not possible with the native drag-drop API.

Hierarchy

  • Drag

Implements

  • IDisposable

Index

Constructors

constructor

  • Construct a new drag object.

    Parameters

    • options: IOptions

      The options for initializing the drag.

    Returns Drag

Properties

Private _currentElement

_currentElement: Element | null = null

Private _currentTarget

_currentTarget: Element | null = null

Private _disposed

_disposed: boolean = false

Private _dropAction

_dropAction: DropAction = "none"

Private _override

_override: IDisposable | null = null

Private _promise

_promise: Promise<DropAction> | null = null

Private _resolve

_resolve: function | null = null

Private _scrollTarget

_scrollTarget: IScrollTarget | null = null

dragImage

dragImage: HTMLElement | null

The drag image element for the drag object.

mimeData

mimeData: MimeData

The mime data for the drag object.

proposedAction

proposedAction: DropAction

The proposed drop action for the drag object.

source

source: any

Get the drag source for the drag object.

supportedActions

supportedActions: SupportedActions

The supported drop actions for the drag object.

Static overrideCursorID

overrideCursorID: number = 0

The internal id for the active cursor override.

Accessors

isDisposed

  • get isDisposed(): boolean
  • Test whether the drag object is disposed.

    Returns boolean

Methods

Private _addListeners

  • _addListeners(): void
  • Add the document event listeners for the drag object.

    Returns void

Private _attachDragImage

  • _attachDragImage(clientX: number, clientY: number): void
  • Attach the drag image element at the specified location.

    This is a no-op if there is no drag image element.

    Parameters

    • clientX: number
    • clientY: number

    Returns void

Private _detachDragImage

  • _detachDragImage(): void
  • Detach the drag image element from the DOM.

    This is a no-op if there is no drag image element.

    Returns void

Private _evtKeyDown

  • _evtKeyDown(event: KeyboardEvent): void
  • Handle the 'keydown' event for the drag object.

    Parameters

    • event: KeyboardEvent

    Returns void

Private _evtMouseMove

  • _evtMouseMove(event: MouseEvent): void
  • Handle the 'mousemove' event for the drag object.

    Parameters

    • event: MouseEvent

    Returns void

Private _evtMouseUp

  • _evtMouseUp(event: MouseEvent): void
  • Handle the 'mouseup' event for the drag object.

    Parameters

    • event: MouseEvent

    Returns void

Private _finalize

  • Finalize the drag operation and resolve the drag promise.

    Parameters

    Returns void

Private _moveDragImage

  • _moveDragImage(clientX: number, clientY: number): void
  • Move the drag image element to the specified location.

    This is a no-op if there is no drag image element.

    Parameters

    • clientX: number
    • clientY: number

    Returns void

Private _onScrollFrame

  • _onScrollFrame(): void
  • The scroll loop handler function.

    Returns void

Private _removeListeners

  • _removeListeners(): void
  • Remove the document event listeners for the drag object.

    Returns void

Private _setDropAction

  • Set the internal drop action state and update the drag cursor.

    Parameters

    Returns void

Private _updateCurrentTarget

  • _updateCurrentTarget(event: MouseEvent): void
  • Update the current target node using the given mouse event.

    Parameters

    • event: MouseEvent

    Returns void

Private _updateDragScroll

  • _updateDragScroll(event: MouseEvent): void
  • Update the drag scroll element under the mouse.

    Parameters

    • event: MouseEvent

    Returns void

dispose

  • dispose(): void
  • Dispose of the resources held by the drag object.

    Notes

    This will cancel the drag operation if it is active.

    Returns void

handleEvent

  • handleEvent(event: Event): void
  • Handle the DOM events for the drag operation.

    Parameters

    • event: Event

      The DOM event sent to the drag object.

      Notes

      This method implements the DOM EventListener interface and is called in response to events on the document. It should not be called directly by user code.

    Returns void

start

  • start(clientX: number, clientY: number): Promise<DropAction>
  • Start the drag operation at the specified client position.

    Parameters

    • clientX: number

      The client X position for the drag start.

    • clientY: number

      The client Y position for the drag start.

    Returns Promise<DropAction>

    A promise which resolves to the result of the drag.

    Notes

    If the drag has already been started, the promise created by the first call to start is returned.

    If the drag operation has ended, or if the drag object has been disposed, the returned promise will resolve to 'none'.

    The drag object will be automatically disposed when drag operation completes. This means Drag objects are for single-use only.

    This method assumes the left mouse button is already held down.

Static overrideCursor

  • overrideCursor(cursor: string): IDisposable
  • Override the cursor icon for the entire document.

    Parameters

    • cursor: string

      The string representing the cursor style.

    Returns IDisposable

    A disposable which will clear the override when disposed.

    Notes

    The most recent call to overrideCursor takes precedence. Disposing an old override has no effect on the current override.

    This utility function is used by the Drag class to override the mouse cursor during a drag-drop operation, but it can also be used by other classes to fix the cursor icon during normal mouse drags.

    Example

    import { Drag } from '@phosphor/dragdrop';
    
    // Force the cursor to be 'wait' for the entire document.
    let override = Drag.overrideCursor('wait');
    
    // Clear the override by disposing the return value.
    override.dispose();

Generated using TypeDoc