UNPKG

23 kBTypeScriptView Raw
1// Type definitions for D3JS d3-drag module 3.0
2// Project: https://github.com/d3/d3-drag/, https://d3js.org/d3-drag
3// Definitions by: Tom Wanzek <https://github.com/tomwanzek>
4// Alex Ford <https://github.com/gustavderdrache>
5// Boris Yankov <https://github.com/borisyankov>
6// Nathan Bierema <https://github.com/Methuselah96>
7// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
8
9// Last module patch version validated against: 3.0.0
10
11import { Selection, ValueFn } from 'd3-selection';
12
13// --------------------------------------------------------------------------
14// Shared Type Definitions and Interfaces
15// --------------------------------------------------------------------------
16
17/**
18 * DraggedElementBaseType serves as an alias for the 'minimal' data type which can be selected
19 * without 'd3-drag' (and related code in 'd3-selection') trying to use properties internally which would otherwise not
20 * be supported.
21 */
22export type DraggedElementBaseType = Element;
23
24/**
25 * Container element type usable for mouse/touch functions
26 */
27export type DragContainerElement = HTMLElement | SVGSVGElement | SVGGElement; // HTMLElement includes HTMLCanvasElement
28
29/**
30 * The subject datum should at a minimum expose x and y properties, so that the relative position
31 * of the subject and the pointer can be preserved during the drag gesture.
32 */
33export interface SubjectPosition {
34 /**
35 * x-coordinate
36 */
37 x: number;
38 /**
39 * y-coordinate
40 */
41 y: number;
42}
43
44/**
45 * A D3 Drag Behavior
46 *
47 * The first generic refers to the type of element to be dragged.
48 * The second generic refers to the type of the datum of the dragged element.
49 * The third generic refers to the type of the drag behavior subject.
50 *
51 * The subject of a drag gesture represents the thing being dragged.
52 * It is computed when an initiating input event is received,
53 * such as a mousedown or touchstart, immediately before the drag gesture starts.
54 * The subject is then exposed as event.subject on subsequent drag events for this gesture.
55 *
56 * The default subject is the datum of the element in the originating selection (see drag)
57 * that received the initiating input event; if this datum is undefined,
58 * an object representing the coordinates of the pointer is created.
59 * When dragging circle elements in SVG, the default subject is thus the datum of the circle being dragged.
60 * With Canvas, the default subject is the canvas element’s datum (regardless of where on the canvas you click).
61 * In this case, a custom subject accessor would be more appropriate,
62 * such as one that picks the closest circle to the mouse within a given search radius.
63 */
64export interface DragBehavior<GElement extends DraggedElementBaseType, Datum, Subject> extends Function {
65 /**
66 * Applies the drag behavior to the selected elements.
67 * This function is typically not invoked directly, and is instead invoked via selection.call.
68 *
69 * For details see: {@link https://github.com/d3/d3-drag#_drag}
70 *
71 * @param selection A D3 selection of elements.
72 * @param args Optional arguments to be passed in.
73 */
74 (selection: Selection<GElement, Datum, any, any>, ...args: any[]): void;
75
76 /**
77 * Returns the current container accessor function.
78 */
79 container(): ValueFn<GElement, Datum, DragContainerElement>;
80 /**
81 * Sets the container accessor to the specified function and returns the drag behavior.
82 *
83 * The container of a drag gesture determines the coordinate system of subsequent drag events, affecting event.x and event.y.
84 * The element returned by the container accessor is subsequently passed to d3.pointer to determine the local coordinates of the pointer.
85 *
86 * The default container accessor returns the parent node of the element in the originating selection (see drag)
87 * that received the initiating input event. This is often appropriate when dragging SVG or HTML elements,
88 * since those elements are typically positioned relative to a parent. For dragging graphical elements with a Canvas,
89 * however, you may want to redefine the container as the initiating element itself, using "this" in the accessor
90 * function.
91 *
92 * @param accessor A container accessor function which is evaluated for each selected element,
93 * in order, being passed the current datum (d), the current index (i), and the current group (nodes),
94 * with this as the current DOM element. The function returns the container element.
95 */
96 container(accessor: ValueFn<GElement, Datum, DragContainerElement>): this;
97 /**
98 * Sets the container accessor to the specified object and returns the drag behavior.
99 *
100 * The container of a drag gesture determines the coordinate system of subsequent drag events, affecting event.x and event.y.
101 * The element returned by the container accessor is subsequently passed to d3.pointer to determine the local coordinates of the pointer.
102 *
103 * The default container accessor returns the parent node of the element in the originating selection (see drag)
104 * that received the initiating input event. This is often appropriate when dragging SVG or HTML elements,
105 * since those elements are typically positioned relative to a parent. For dragging graphical elements with a Canvas,
106 * however, you may want to redefine the container as the initiating element itself, such as drag.container(canvas).
107 *
108 * @param container Container element for the drag gesture.
109 */
110 container(container: DragContainerElement): this;
111
112 /**
113 * Returns the current filter function.
114 */
115 filter(): (this: GElement, event: any, d: Datum) => boolean;
116 /**
117 * Sets the event filter to the specified filter function and returns the drag behavior.
118 *
119 * If the filter returns falsey, the initiating event is ignored and no drag gesture is started.
120 * Thus, the filter determines which input events are ignored. The default filter ignores mousedown events on secondary buttons,
121 * since those buttons are typically intended for other purposes, such as the context menu.
122 *
123 * @param filterFn A filter function which is evaluated for each selected element,
124 * in order, being passed the current event (event) and datum d, with the this context as the current DOM element.
125 * The function returns a boolean value.
126 */
127 filter(filterFn: (this: GElement, event: any, d: Datum) => boolean): this;
128
129 /**
130 * Returns the current touch support detector, which defaults to a function returning true,
131 * if the "ontouchstart" event is supported on the current element.
132 */
133 touchable(): ValueFn<GElement, Datum, boolean>;
134 /**
135 * Sets the touch support detector to the specified boolean value and returns the drag behavior.
136 *
137 * Touch event listeners are only registered if the detector returns truthy for the corresponding element when the drag behavior is applied.
138 * The default detector works well for most browsers that are capable of touch input, but not all; Chrome’s mobile device emulator, for example,
139 * fails detection.
140 *
141 * @param touchable A boolean value. true when touch event listeners should be applied to the corresponding element, otherwise false.
142 */
143 touchable(touchable: boolean): this;
144 /**
145 * Sets the touch support detector to the specified function and returns the drag behavior.
146 *
147 * Touch event listeners are only registered if the detector returns truthy for the corresponding element when the drag behavior is applied.
148 * The default detector works well for most browsers that are capable of touch input, but not all; Chrome’s mobile device emulator, for example,
149 * fails detection.
150 *
151 * @param touchable A touch support detector function, which returns true when touch event listeners should be applied to the corresponding element.
152 * The function is evaluated for each selected element to which the drag behavior was applied, in order, being passed the current datum (d),
153 * the current index (i), and the current group (nodes), with this as the current DOM element. The function returns a boolean value.
154 */
155 touchable(touchable: ValueFn<GElement, Datum, boolean>): this;
156
157 /**
158 * Returns the current subject accessor functions.
159 */
160 subject(): (this: GElement, event: any, d: Datum) => Subject;
161 /**
162 * Sets the subject accessor to the specified function and returns the drag behavior.
163 *
164 * The subject of a drag gesture represents the thing being dragged.
165 * It is computed when an initiating input event is received,
166 * such as a mousedown or touchstart, immediately before the drag gesture starts.
167 * The subject is then exposed as event.subject on subsequent drag events for this gesture.
168 *
169 * The default subject is the datum of the element in the originating selection (see drag)
170 * that received the initiating input event; if this datum is undefined,
171 * an object representing the coordinates of the pointer is created.
172 * When dragging circle elements in SVG, the default subject is thus the datum of the circle being dragged.
173 * With Canvas, the default subject is the canvas element’s datum (regardless of where on the canvas you click).
174 * In this case, a custom subject accessor would be more appropriate,
175 * such as one that picks the closest circle to the mouse within a given search radius.
176 *
177 * The subject of a drag gesture may not be changed after the gesture starts.
178 *
179 * During the evaluation of the subject accessor, event is a beforestart drag event.
180 * Use event.sourceEvent to access the initiating input event and event.identifier to access the touch identifier.
181 * The event.x and event.y are relative to the container, and are computed using d3.pointer.
182 *
183 * @param accessor An extent accessor function which is evaluated for each selected element,
184 * in order, being passed the current event (`event`) and datum `d`, with the `this` context as the current DOM element.
185 * The returned subject should be an object that exposes x and y properties,
186 * so that the relative position of the subject and the pointer can be preserved during the drag gesture.
187 * If the subject is null or undefined, no drag gesture is started for this pointer;
188 * however, other starting touches may yet start drag gestures.
189 */
190 subject(accessor: (this: GElement, event: any, d: Datum) => Subject): this;
191
192 /**
193 * Return the current click distance threshold, which defaults to zero.
194 */
195 clickDistance(): number;
196 /**
197 * Set the maximum distance that the mouse can move between mousedown and mouseup that will trigger
198 * a subsequent click event. If at any point between mousedown and mouseup the mouse is greater than or equal to
199 * distance from its position on mousedown, the click event following mouseup will be suppressed.
200 *
201 * @param distance The distance threshold between mousedown and mouseup measured in client coordinates (event.clientX and event.clientY).
202 * The default is zero.
203 */
204 clickDistance(distance: number): this;
205
206 /**
207 * Return the first currently-assigned listener matching the specified typenames, if any.
208 *
209 * @param typenames The typenames is a string containing one or more typename separated by whitespace.
210 * Each typename is a type, optionally followed by a period (.) and a name, such as "drag.foo"" and "drag.bar";
211 * the name allows multiple listeners to be registered for the same type. The type must be one of the following:
212 * start (after a new pointer becomes active [on mousedown or touchstart]), drag (after an active pointer moves [on mousemove or touchmove], or
213 * end (after an active pointer becomes inactive [on mouseup, touchend or touchcancel].)
214 */
215 on(typenames: string): ((this: GElement, event: any, d: Datum) => void) | undefined;
216 /**
217 * Remove the current event listeners for the specified typenames, if any, return the drag behavior.
218 *
219 * @param typenames The typenames is a string containing one or more typename separated by whitespace.
220 * Each typename is a type, optionally followed by a period (.) and a name, such as "drag.foo"" and "drag.bar";
221 * the name allows multiple listeners to be registered for the same type. The type must be one of the following:
222 * start (after a new pointer becomes active [on mousedown or touchstart]), drag (after an active pointer moves [on mousemove or touchmove], or
223 * end (after an active pointer becomes inactive [on mouseup, touchend or touchcancel].)
224 * @param listener Use null to remove the listener.
225 */
226 on(typenames: string, listener: null): this;
227 /**
228 * Set the event listener for the specified typenames and return the drag behavior.
229 * If an event listener was already registered for the same type and name,
230 * the existing listener is removed before the new listener is added.
231 * When a specified event is dispatched, each listener will be invoked with the same context and arguments as selection.on listeners.
232 *
233 * Changes to registered listeners via drag.on during a drag gesture do not affect the current drag gesture.
234 * Instead, you must use event.on, which also allows you to register temporary event listeners for the current drag gesture.
235 * Separate events are dispatched for each active pointer during a drag gesture.
236 * For example, if simultaneously dragging multiple subjects with multiple fingers, a start event is dispatched for each finger,
237 * even if both fingers start touching simultaneously.
238 *
239 * @param typenames The typenames is a string containing one or more typename separated by whitespace.
240 * Each typename is a type, optionally followed by a period (.) and a name, such as "drag.foo"" and "drag.bar";
241 * the name allows multiple listeners to be registered for the same type. The type must be one of the following:
242 * start (after a new pointer becomes active [on mousedown or touchstart]), drag (after an active pointer moves [on mousemove or touchmove], or
243 * end (after an active pointer becomes inactive [on mouseup, touchend or touchcancel].)
244 * @param listener An event listener function which is evaluated for each selected element,
245 * in order, being passed the current event (event) and datum d, with the this context as the current DOM element.
246 */
247 on(typenames: string, listener: (this: GElement, event: any, d: Datum) => void): this;
248}
249
250/**
251 * Creates a new drag behavior. The returned behavior, drag, is both an object and a function, and is
252 * typically applied to selected elements via selection.call.
253 *
254 * Use this signature when using the default subject accessor.
255 *
256 * The first generic refers to the type of element to be dragged.
257 * The second generic refers to the type of the datum of the dragged element.
258 */
259// eslint-disable-next-line no-unnecessary-generics
260export function drag<GElement extends DraggedElementBaseType, Datum>(): DragBehavior<GElement, Datum, Datum | SubjectPosition>;
261/**
262 * Creates a new drag behavior. The returned behavior, drag, is both an object and a function, and is
263 * typically applied to selected elements via selection.call.
264 *
265 * Use this signature when using a custom subject accessor.
266 *
267 * The first generic refers to the type of element to be dragged.
268 * The second generic refers to the type of the datum of the dragged element.
269 * The third generic refers to the type of the drag behavior subject.
270 */
271// eslint-disable-next-line no-unnecessary-generics
272export function drag<GElement extends DraggedElementBaseType, Datum, Subject>(): DragBehavior<GElement, Datum, Subject>;
273
274/**
275 * D3 Drag event
276 *
277 * The first generic refers to the type of element to be dragged.
278 * The second generic refers to the type of the datum of the dragged element.
279 * The third generic refers to the type of the drag behavior subject.
280 */
281export interface D3DragEvent<GElement extends DraggedElementBaseType, Datum, Subject> {
282 /**
283 * The DragBehavior associated with the event
284 */
285 target: DragBehavior<GElement, Datum, Subject>;
286 /**
287 * The event type for the DragEvent
288 */
289 type: 'start' | 'drag' | 'end' | string; // Leave failsafe string type for cases like 'drag.foo'
290 /**
291 * The drag subject, defined by drag.subject.
292 */
293 subject: Subject;
294 /**
295 * The new x-coordinate of the subject, relative to the container
296 */
297 x: number;
298 /**
299 * The new y-coordinate of the subject, relative to the container
300 */
301 y: number;
302 /**
303 * The change in x-coordinate since the previous drag event.
304 */
305 dx: number;
306 /**
307 * The change in y-coordinate since the previous drag event.
308 */
309 dy: number;
310 /**
311 * The string “mouse”, or a numeric touch identifier.
312 */
313 identifier: 'mouse' | number;
314 /**
315 * The number of currently active drag gestures (on start and end, not including this one).
316 *
317 * The event.active field is useful for detecting the first start event and the last end event
318 * in a sequence of concurrent drag gestures: it is zero when the first drag gesture starts,
319 * and zero when the last drag gesture ends.
320 */
321 active: number;
322 /**
323 * The underlying input event, such as mousemove or touchmove.
324 */
325 sourceEvent: any;
326 /**
327 * Return the first currently-assigned listener matching the specified typenames, if any.
328 *
329 * Equivalent to drag.on, but only applies to the current drag gesture. Before the drag gesture starts,
330 * a copy of the current drag event listeners is made. This copy is bound to the current drag gesture
331 * and modified by event.on. This is useful for temporary listeners that only receive events for the current drag gesture.
332 *
333 * @param typenames The typenames is a string containing one or more typename separated by whitespace.
334 * Each typename is a type, optionally followed by a period (.) and a name, such as "drag.foo"" and "drag.bar";
335 * the name allows multiple listeners to be registered for the same type. The type must be one of the following:
336 * start (after a new pointer becomes active [on mousedown or touchstart]), drag (after an active pointer moves [on mousemove or touchmove], or
337 * end (after an active pointer becomes inactive [on mouseup, touchend or touchcancel].)
338 */
339 on(typenames: string): ((this: GElement, event: any, d: Datum) => void) | undefined;
340 /**
341 * Remove the current event listeners for the specified typenames, if any, return the drag behavior.
342 *
343 * Equivalent to drag.on, but only applies to the current drag gesture. Before the drag gesture starts,
344 * a copy of the current drag event listeners is made. This copy is bound to the current drag gesture
345 * and modified by event.on. This is useful for temporary listeners that only receive events for the current drag gesture.
346 *
347 * @param typenames The typenames is a string containing one or more typename separated by whitespace.
348 * Each typename is a type, optionally followed by a period (.) and a name, such as "drag.foo"" and "drag.bar";
349 * the name allows multiple listeners to be registered for the same type. The type must be one of the following:
350 * start (after a new pointer becomes active [on mousedown or touchstart]), drag (after an active pointer moves [on mousemove or touchmove], or
351 * end (after an active pointer becomes inactive [on mouseup, touchend or touchcancel].)
352 * @param listener Use null to remove the listener.
353 */
354 on(typenames: string, listener: null): this;
355 /**
356 * Set the event listener for the specified typenames and return the drag behavior.
357 * If an event listener was already registered for the same type and name,
358 * the existing listener is removed before the new listener is added.
359 * When a specified event is dispatched, each listener will be invoked with the same context and arguments as selection.on listeners.
360 *
361 * Equivalent to drag.on, but only applies to the current drag gesture. Before the drag gesture starts,
362 * a copy of the current drag event listeners is made. This copy is bound to the current drag gesture
363 * and modified by event.on. This is useful for temporary listeners that only receive events for the current drag gesture.
364 *
365 * @param typenames The typenames is a string containing one or more typename separated by whitespace.
366 * Each typename is a type, optionally followed by a period (.) and a name, such as "drag.foo"" and "drag.bar";
367 * the name allows multiple listeners to be registered for the same type. The type must be one of the following:
368 * start (after a new pointer becomes active [on mousedown or touchstart]), drag (after an active pointer moves [on mousemove or touchmove], or
369 * end (after an active pointer becomes inactive [on mouseup, touchend or touchcancel].)
370 * @param listener An event listener function which is evaluated for each selected element,
371 * in order, being passed the current event (event) and datum d, with the this context as the current DOM element.
372 */
373 on(typenames: string, listener: (this: GElement, event: any, d: Datum) => void): this;
374}
375
376/**
377 * Prevents native drag-and-drop and text selection on the specified window.
378 * As an alternative to preventing the default action of mousedown events,
379 * this method prevents undesirable default actions following mousedown. In supported browsers,
380 * this means capturing dragstart and selectstart events, preventing the associated default actions,
381 * and immediately stopping their propagation. In browsers that do not support selection events,
382 * the user-select CSS property is set to none on the document element.
383 * This method is intended to be called on mousedown, followed by d3.dragEnable on mouseup.
384 *
385 * @param window The window for which drag should be disabled.
386 */
387export function dragDisable(window: Window): void;
388
389/**
390 * Allows native drag-and-drop and text selection on the specified window; undoes the effect of d3.dragDisable.
391 * This method is intended to be called on mouseup, preceded by d3.dragDisable on mousedown.
392 * If noclick is true, this method also temporarily suppresses click events.
393 * The suppression of click events expires after a zero-millisecond timeout,
394 * such that it only suppress the click event that would immediately follow the current mouseup event, if any.
395 *
396 * @param window The window for which drag should be (re-)enabled.
397 * @param noClick An optional flag. If noclick is true, this method also temporarily suppresses click events.
398 */
399export function dragEnable(window: Window, noClick?: boolean): void;
400
\No newline at end of file