drag-ref.d.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /**
  2. * @license
  3. * Copyright Google LLC All Rights Reserved.
  4. *
  5. * Use of this source code is governed by an MIT-style license that can be
  6. * found in the LICENSE file at https://angular.io/license
  7. */
  8. import { ElementRef, NgZone, ViewContainerRef, TemplateRef } from '@angular/core';
  9. import { ViewportRuler } from '@angular/cdk/scrolling';
  10. import { Direction } from '@angular/cdk/bidi';
  11. import { Subject, Observable } from 'rxjs';
  12. import { DropListRefInternal as DropListRef } from './drop-list-ref';
  13. import { DragDropRegistry } from './drag-drop-registry';
  14. /** Object that can be used to configure the behavior of DragRef. */
  15. export interface DragRefConfig {
  16. /**
  17. * Minimum amount of pixels that the user should
  18. * drag, before the CDK initiates a drag sequence.
  19. */
  20. dragStartThreshold: number;
  21. /**
  22. * Amount the pixels the user should drag before the CDK
  23. * considers them to have changed the drag direction.
  24. */
  25. pointerDirectionChangeThreshold: number;
  26. }
  27. /**
  28. * Internal compile-time-only representation of a `DragRef`.
  29. * Used to avoid circular import issues between the `DragRef` and the `DropListRef`.
  30. * @docs-private
  31. */
  32. export interface DragRefInternal extends DragRef {
  33. }
  34. /** Template that can be used to create a drag helper element (e.g. a preview or a placeholder). */
  35. interface DragHelperTemplate<T = any> {
  36. template: TemplateRef<T> | null;
  37. viewContainer: ViewContainerRef;
  38. context: T;
  39. }
  40. /**
  41. * Reference to a draggable item. Used to manipulate or dispose of the item.
  42. * @docs-private
  43. */
  44. export declare class DragRef<T = any> {
  45. private _config;
  46. private _document;
  47. private _ngZone;
  48. private _viewportRuler;
  49. private _dragDropRegistry;
  50. /** Element displayed next to the user's pointer while the element is dragged. */
  51. private _preview;
  52. /** Reference to the view of the preview element. */
  53. private _previewRef;
  54. /** Reference to the view of the placeholder element. */
  55. private _placeholderRef;
  56. /** Element that is rendered instead of the draggable item while it is being sorted. */
  57. private _placeholder;
  58. /** Coordinates within the element at which the user picked up the element. */
  59. private _pickupPositionInElement;
  60. /** Coordinates on the page at which the user picked up the element. */
  61. private _pickupPositionOnPage;
  62. /**
  63. * Reference to the element that comes after the draggable in the DOM, at the time
  64. * it was picked up. Used for restoring its initial position when it's dropped.
  65. */
  66. private _nextSibling;
  67. /**
  68. * CSS `transform` applied to the element when it isn't being dragged. We need a
  69. * passive transform in order for the dragged element to retain its new position
  70. * after the user has stopped dragging and because we need to know the relative
  71. * position in case they start dragging again. This corresponds to `element.style.transform`.
  72. */
  73. private _passiveTransform;
  74. /** CSS `transform` that is applied to the element while it's being dragged. */
  75. private _activeTransform;
  76. /** Inline `transform` value that the element had before the first dragging sequence. */
  77. private _initialTransform?;
  78. /**
  79. * Whether the dragging sequence has been started. Doesn't
  80. * necessarily mean that the element has been moved.
  81. */
  82. private _hasStartedDragging;
  83. /** Whether the element has moved since the user started dragging it. */
  84. private _hasMoved;
  85. /** Drop container in which the DragRef resided when dragging began. */
  86. private _initialContainer;
  87. /** Cached scroll position on the page when the element was picked up. */
  88. private _scrollPosition;
  89. /** Emits when the item is being moved. */
  90. private _moveEvents;
  91. /** Keeps track of the direction in which the user is dragging along each axis. */
  92. private _pointerDirectionDelta;
  93. /** Pointer position at which the last change in the delta occurred. */
  94. private _pointerPositionAtLastDirectionChange;
  95. /**
  96. * Root DOM node of the drag instance. This is the element that will
  97. * be moved around as the user is dragging.
  98. */
  99. private _rootElement;
  100. /**
  101. * Inline style value of `-webkit-tap-highlight-color` at the time the
  102. * dragging was started. Used to restore the value once we're done dragging.
  103. */
  104. private _rootElementTapHighlight;
  105. /** Subscription to pointer movement events. */
  106. private _pointerMoveSubscription;
  107. /** Subscription to the event that is dispatched when the user lifts their pointer. */
  108. private _pointerUpSubscription;
  109. /** Subscription to the viewport being scrolled. */
  110. private _scrollSubscription;
  111. /** Subscription to the viewport being resized. */
  112. private _resizeSubscription;
  113. /**
  114. * Time at which the last touch event occurred. Used to avoid firing the same
  115. * events multiple times on touch devices where the browser will fire a fake
  116. * mouse event for each touch event, after a certain time.
  117. */
  118. private _lastTouchEventTime;
  119. /** Time at which the last dragging sequence was started. */
  120. private _dragStartTime;
  121. /** Cached reference to the boundary element. */
  122. private _boundaryElement;
  123. /** Whether the native dragging interactions have been enabled on the root element. */
  124. private _nativeInteractionsEnabled;
  125. /** Cached dimensions of the preview element. */
  126. private _previewRect?;
  127. /** Cached dimensions of the boundary element. */
  128. private _boundaryRect?;
  129. /** Element that will be used as a template to create the draggable item's preview. */
  130. private _previewTemplate?;
  131. /** Template for placeholder element rendered to show where a draggable would be dropped. */
  132. private _placeholderTemplate?;
  133. /** Elements that can be used to drag the draggable item. */
  134. private _handles;
  135. /** Registered handles that are currently disabled. */
  136. private _disabledHandles;
  137. /** Droppable container that the draggable is a part of. */
  138. private _dropContainer?;
  139. /** Layout direction of the item. */
  140. private _direction;
  141. /** Axis along which dragging is locked. */
  142. lockAxis: 'x' | 'y';
  143. /**
  144. * Amount of milliseconds to wait after the user has put their
  145. * pointer down before starting to drag the element.
  146. */
  147. dragStartDelay: number;
  148. /** Whether starting to drag this element is disabled. */
  149. disabled: boolean;
  150. private _disabled;
  151. /** Emits as the drag sequence is being prepared. */
  152. beforeStarted: Subject<void>;
  153. /** Emits when the user starts dragging the item. */
  154. started: Subject<{
  155. source: DragRef<any>;
  156. }>;
  157. /** Emits when the user has released a drag item, before any animations have started. */
  158. released: Subject<{
  159. source: DragRef<any>;
  160. }>;
  161. /** Emits when the user stops dragging an item in the container. */
  162. ended: Subject<{
  163. source: DragRef<any>;
  164. distance: Point;
  165. }>;
  166. /** Emits when the user has moved the item into a new container. */
  167. entered: Subject<{
  168. container: DropListRef;
  169. item: DragRef<any>;
  170. currentIndex: number;
  171. }>;
  172. /** Emits when the user removes the item its container by dragging it into another container. */
  173. exited: Subject<{
  174. container: DropListRef;
  175. item: DragRef<any>;
  176. }>;
  177. /** Emits when the user drops the item inside a container. */
  178. dropped: Subject<{
  179. previousIndex: number;
  180. currentIndex: number;
  181. item: DragRef<any>;
  182. container: DropListRef;
  183. previousContainer: DropListRef;
  184. distance: Point;
  185. isPointerOverContainer: boolean;
  186. }>;
  187. /**
  188. * Emits as the user is dragging the item. Use with caution,
  189. * because this event will fire for every pixel that the user has dragged.
  190. */
  191. moved: Observable<{
  192. source: DragRef;
  193. pointerPosition: {
  194. x: number;
  195. y: number;
  196. };
  197. event: MouseEvent | TouchEvent;
  198. distance: Point;
  199. delta: {
  200. x: -1 | 0 | 1;
  201. y: -1 | 0 | 1;
  202. };
  203. }>;
  204. /** Arbitrary data that can be attached to the drag item. */
  205. data: T;
  206. /**
  207. * Function that can be used to customize the logic of how the position of the drag item
  208. * is limited while it's being dragged. Gets called with a point containing the current position
  209. * of the user's pointer on the page and should return a point describing where the item should
  210. * be rendered.
  211. */
  212. constrainPosition?: (point: Point, dragRef: DragRef) => Point;
  213. constructor(element: ElementRef<HTMLElement> | HTMLElement, _config: DragRefConfig, _document: Document, _ngZone: NgZone, _viewportRuler: ViewportRuler, _dragDropRegistry: DragDropRegistry<DragRef, DropListRef>);
  214. /**
  215. * Returns the element that is being used as a placeholder
  216. * while the current element is being dragged.
  217. */
  218. getPlaceholderElement(): HTMLElement;
  219. /** Returns the root draggable element. */
  220. getRootElement(): HTMLElement;
  221. /** Registers the handles that can be used to drag the element. */
  222. withHandles(handles: (HTMLElement | ElementRef<HTMLElement>)[]): this;
  223. /**
  224. * Registers the template that should be used for the drag preview.
  225. * @param template Template that from which to stamp out the preview.
  226. */
  227. withPreviewTemplate(template: DragHelperTemplate | null): this;
  228. /**
  229. * Registers the template that should be used for the drag placeholder.
  230. * @param template Template that from which to stamp out the placeholder.
  231. */
  232. withPlaceholderTemplate(template: DragHelperTemplate | null): this;
  233. /**
  234. * Sets an alternate drag root element. The root element is the element that will be moved as
  235. * the user is dragging. Passing an alternate root element is useful when trying to enable
  236. * dragging on an element that you might not have access to.
  237. */
  238. withRootElement(rootElement: ElementRef<HTMLElement> | HTMLElement): this;
  239. /**
  240. * Element to which the draggable's position will be constrained.
  241. */
  242. withBoundaryElement(boundaryElement: ElementRef<HTMLElement> | HTMLElement | null): this;
  243. /** Removes the dragging functionality from the DOM element. */
  244. dispose(): void;
  245. /** Checks whether the element is currently being dragged. */
  246. isDragging(): boolean;
  247. /** Resets a standalone drag item to its initial position. */
  248. reset(): void;
  249. /**
  250. * Sets a handle as disabled. While a handle is disabled, it'll capture and interrupt dragging.
  251. * @param handle Handle element that should be disabled.
  252. */
  253. disableHandle(handle: HTMLElement): void;
  254. /**
  255. * Enables a handle, if it has been disabled.
  256. * @param handle Handle element to be enabled.
  257. */
  258. enableHandle(handle: HTMLElement): void;
  259. /** Sets the layout direction of the draggable item. */
  260. withDirection(direction: Direction): this;
  261. /** Sets the container that the item is part of. */
  262. _withDropContainer(container: DropListRef): void;
  263. /**
  264. * Gets the current position in pixels the draggable outside of a drop container.
  265. */
  266. getFreeDragPosition(): Readonly<Point>;
  267. /**
  268. * Sets the current position in pixels the draggable outside of a drop container.
  269. * @param value New position to be set.
  270. */
  271. setFreeDragPosition(value: Point): this;
  272. /** Updates the item's sort order based on the last-known pointer position. */
  273. _sortFromLastPointerPosition(): void;
  274. /** Unsubscribes from the global subscriptions. */
  275. private _removeSubscriptions;
  276. /** Destroys the preview element and its ViewRef. */
  277. private _destroyPreview;
  278. /** Destroys the placeholder element and its ViewRef. */
  279. private _destroyPlaceholder;
  280. /** Handler for the `mousedown`/`touchstart` events. */
  281. private _pointerDown;
  282. /** Handler that is invoked when the user moves their pointer after they've initiated a drag. */
  283. private _pointerMove;
  284. /** Handler that is invoked when the user lifts their pointer up, after initiating a drag. */
  285. private _pointerUp;
  286. /**
  287. * Clears subscriptions and stops the dragging sequence.
  288. * @param event Browser event object that ended the sequence.
  289. */
  290. private _endDragSequence;
  291. /** Starts the dragging sequence. */
  292. private _startDragSequence;
  293. /**
  294. * Sets up the different variables and subscriptions
  295. * that will be necessary for the dragging sequence.
  296. * @param referenceElement Element that started the drag sequence.
  297. * @param event Browser event object that started the sequence.
  298. */
  299. private _initializeDragSequence;
  300. /** Cleans up the DOM artifacts that were added to facilitate the element being dragged. */
  301. private _cleanupDragArtifacts;
  302. /**
  303. * Updates the item's position in its drop container, or moves it
  304. * into a new one, depending on its current drag position.
  305. */
  306. private _updateActiveDropContainer;
  307. /**
  308. * Creates the element that will be rendered next to the user's pointer
  309. * and will be used as a preview of the element that is being dragged.
  310. */
  311. private _createPreviewElement;
  312. /**
  313. * Animates the preview element from its current position to the location of the drop placeholder.
  314. * @returns Promise that resolves when the animation completes.
  315. */
  316. private _animatePreviewToPlaceholder;
  317. /** Creates an element that will be shown instead of the current element while dragging. */
  318. private _createPlaceholderElement;
  319. /**
  320. * Figures out the coordinates at which an element was picked up.
  321. * @param referenceElement Element that initiated the dragging.
  322. * @param event Event that initiated the dragging.
  323. */
  324. private _getPointerPositionInElement;
  325. /** Determines the point of the page that was touched by the user. */
  326. private _getPointerPositionOnPage;
  327. /** Gets the pointer position on the page, accounting for any position constraints. */
  328. private _getConstrainedPointerPosition;
  329. /** Updates the current drag delta, based on the user's current pointer position on the page. */
  330. private _updatePointerDirectionDelta;
  331. /** Toggles the native drag interactions, based on how many handles are registered. */
  332. private _toggleNativeDragInteractions;
  333. /** Removes the manually-added event listeners from the root element. */
  334. private _removeRootElementListeners;
  335. /**
  336. * Applies a `transform` to the root element, taking into account any existing transforms on it.
  337. * @param x New transform value along the X axis.
  338. * @param y New transform value along the Y axis.
  339. */
  340. private _applyRootElementTransform;
  341. /**
  342. * Gets the distance that the user has dragged during the current drag sequence.
  343. * @param currentPosition Current position of the user's pointer.
  344. */
  345. private _getDragDistance;
  346. /** Cleans up any cached element dimensions that we don't need after dragging has stopped. */
  347. private _cleanupCachedDimensions;
  348. /**
  349. * Checks whether the element is still inside its boundary after the viewport has been resized.
  350. * If not, the position is adjusted so that the element fits again.
  351. */
  352. private _containInsideBoundaryOnResize;
  353. }
  354. /** Point on the page or within an element. */
  355. export interface Point {
  356. x: number;
  357. y: number;
  358. }
  359. export {};