UNPKG

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