UNPKG

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