Files
DefinitelyTyped/d3-brush/index.d.ts
Tom Wanzek c633e6b9b7 [types-2.0] D3 strictNullChecks Batch 1 (#12858)
* [Enhancements]: strictNullCheck and filtering:
* (d3-selection) [Enhancement]: Validated, updated and activated to enforece `strictNullChecks`. `BaseType` updated to include `null`. As a result of this approach, the developer must explicitly include `| null`, if a Selection or a (sub-)selecting method may contain `null` element(s). By the same token, the `node()` method, will return `null` only, if the Selection to which it was applied may contain `null` element(s).
* (d3-selection) [Enhancement]: Added additional signature to `filter(...)` method such that a generic can be used to control the type of the filtered elements, should they be a subset of the unfiltered types.
* (d3-selection) [Enhancement]: Added `Document` to `BaseType` union type.
* (d3-transition) [Enhancement]: Validated, updated and activated to enforece `strictNullChecks`. `BaseType` updated to include `null`. As a result of this approach, the developer must explicitly include `| null`, if a Transition or a (sub-)selecting method may contain `null` element(s). By the same token, the `node()` method, will return `null` only, if the Transition to which it was applied may contain `null` element(s).
* (d3-transition) [Enhancement]: Added additional signature to `filter(...)` method such that a generic can be used to control the type of the filtered elements, should they be a subset of the unfiltered types.
* (d3-selection-multi) [Enhancement]: updated `ValueMap` type use extension of selection/transition `BaseType` as defined above consistent  with `strictNullChecks`.
* (d3-selection-multi) [Test Fix]: Updated tests where a Value Function is used to return a ValueMap for each selected item, as the ValueMap must be defined as a mapping object idiomatically, the test was changed to never return `null` as a result of the value function. Instead it returns `{}`, when no updates are required for the currently iterated selected element.
* In general, for all three above modules: Tests have been updated to be more explicit about type-checking inside callback functions. Also, where shortcuts where taken in the shape tests to simply test interface/signature contracts, without necessary variable initialization, mock initializations have been included. This is necessitated by enabling `strictNullChecks`.

* d3-force
* (d3-force) [Enhancement]: Validated, updated  and activated to allow for `strictNullChecks`. When using `force(name)` getter on `Simulation`, it is now up to the developer to use the right generic narrowing to exclude `undefined` as a return type, if it is known with certainty that the retrieved force has prevsiously been defined. By default, it may be `undefined` as is the standard API specification.
* (d3-force) [Fix]: The initialize method has been added explicitly to the predefined forces `ForceCollide`, `ForceCenter`, `ForceLink`, `ForceManyBody`, `ForceX` and `ForceY`. This conforms to the implementation of these forces, where  it is implemented. Inhertitance from the generic `Force` inaccurately let it appear optional.
* (d3-force) [Chore] Tests added/updated and some linting.

* d3-force ForceLink JSDoc update:
* (d3-force) [Chore] Update JSDoc comment for `id(...)` accessor on `ForceLink`. This is related to the corresponding wording change in the API documentation in follow-up to the changes made to d3-force issue 72.

* d3-zoom strictNullChecks
* (d3-zoom) [Enhancement] Validated,  updated and activated for use with `strictNullChecks`. Tests updated.

* d3-brush strictNullChecks
* (d3-brush) [Enhancement] Validate, update and activate for use with `strictNullChecks`. Tests updated.

* d3-drag strictNullChecks
* (d3-drag) [Enhancement]: Validate, update and activate for use with strictNullChecks.

* (d3-zoom) to v1.1.0
* (d3-zoom) [Feature]: Adds suppport for `zoom.interpolate(...)`. Tests added
* (d3-zoom) [Chore]: Updated JSDoc comment for `zoomTransform(...)`

* (d3-transition) JSDoc comment
* (d3-transition) [Chore] Updated JSDoc comment on `selection.transition(...)`
* (d3-transition) [Chore] bumped version to 1.0.3 to indicate up to which module version the definitions were reviewed

* Bump patch versions of underlying modules
* (d3-brush) (d3-drag) (d3-force) [Chore]: To indicate up to which patch version the files definitions were validated, patch version number has been bumped

* TS Lint and Minor Changes:
* (d3-brush) [Chore]: Added tslint.json to disable "unified-signatures" rule
* (d3-drag) [Chore]: Added tslint.json to disable "unified-signatures" rule. Linted tests.
* (d3-force) [Chore]: Added tslint.json to disable "unified-signatures" rule
* (d3-force) [Chore]: Completed linting.
* (d3-selection) [Chore]: Added tslint.json to disable "unified-signatures" rule
* (d3-selection) [Fix]: Changed `CustomEventParameters` and `NamespaceLocalObject` and `NamespaceMap` to be interfaces as opposed to types.
* (d3-selection) [Chore]: Completed linting of defintion and tests,
* (d3-selection-multi) [Chore]: Added tslint.json to disable "unified-signatures" rule
* (d3-selection-multi) [Chore]: Completed linting of definition and tests. Used `// tslint:disable-next-line:interface-over-type-literal` for type alias ValueMap. Using type is better here for IDE hints, as the hint replaces alias with its content. This saves separate "peeking" as would be the case for interface. Also, there is no need to extend the type. For the entire test file `// tslint:disable:object-literal-shorthand` was applied, as the object literals actually need to use functions and not arrow function "short hand" ("this" context will be bound rather than lexically scoped)
* (d3-selection-multi) [Chore]: Minor JSDoc comment additions for completeness.
* (d3-transition) [Chore]: Added tslint.json to disable "unified-signatures" rule
* (d3-transition) [Chore]: Minor JSDoc comment additions for completeness.
* (d3-transition) [Chore]: Completed linting.
* (d3-zoom) [Chore]: Added tslint.json to disable "unified-signatures" rule
* (d3-zoom) [Chore]: Completed linting.

* strictNullChecks
* (d3-selection) [Fix]: Re-inserted `| null` into return type of `node()`. Fixed all tests, for mock-purposes using `!` post-fix where necessary. Note, this includes using post-fix on  document.querySelector(...) which now also includes possible `null` return type.
* (d3-transition) [Fix]: Re-inserted `| null` into return type of `node()`. Fixed all tests, for mock-purposes using `!` post-fix where necessary. Note, this includes using post-fix on  document.querySelector(...) which now also includes possible `null` return type.
* (d3-force) [Fix]: Moved return type of `force('forcename')` getter to include `| undefined` unconditionally. This is a change from allowing the casting using the generic to exclude `undefined`. This change was made for consistency with the treatment of e.g. `node()` (see above)
* (d3-drag) [Chore]: Adjust test to mock-assert non-null using `!` when using `node()`

* d3-zoom fix test
* (d3-zoom) [Chore] Needed to assert non-null node() return-type for testing purposes only.

* Update header comments to new convention

* Removed "v" from version header.
2016-12-02 19:28:01 -08:00

249 lines
13 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Type definitions for D3JS d3-brush module 1.0
// Project: https://github.com/d3/d3-brush/
// Definitions by: Tom Wanzek <https://github.com/tomwanzek>, Alex Ford <https://github.com/gustavderdrache>, Boris Yankov <https://github.com/borisyankov>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// Last module patch version validated against: 1.0.3
import { ArrayLike, Selection, TransitionLike, ValueFn } from 'd3-selection';
/**
* Type alias for a BrushSelection. For a two-dimensional brush, it must be defined as [[x0, y0], [x1, y1]],
* where x0 is the minimum x-value, y0 is the minimum y-value, x1 is the maximum x-value, and y1 is the maximum y-value.
* For an x-brush, it must be defined as [x0, x1]; for a y-brush, it must be defined as [y0, y1].
*/
export type BrushSelection = [[number, number], [number, number]] | [number, number];
/**
* A D3 brush behavior
*
* The generic refers to the type of the datum for the group element on which brush behavior is defined.
*/
export interface BrushBehavior<Datum> {
/**
* Applies the brush to the specified group, which must be a selection of SVG G elements.
* This function is typically not invoked directly, and is instead invoked via selection.call.
*
* For details see: {@link https://github.com/d3/d3-brush#_brush}
*
* @param group A D3 selection of SVG G elements.
* @param args Optional arguments to be passed in.
*/
(group: Selection<SVGGElement, Datum, any, any>, ...args: any[]): void;
/**
* Clear the active selection of the brush on the specified SVG G element(s) selection.
*
* @param group A D3 selection of SVG G elements.
* @param selection Use null to clear the active brush selection.
*/
move(group: Selection<SVGGElement, Datum, any, any>, selection: null): void;
/**
* Sets the active selection of the brush on the specified SVG G element(s) selection
* to the provided array.
*
* @param group A D3 selection of SVG G elements.
* @param selection An array specifying the new active brush selection. For a two-dimensional brush,
* it must be defined as [[x0, y0], [x1, y1]], where x0 is the minimum x-value, y0 is the minimum y-value,
* x1 is the maximum x-value, and y1 is the maximum y-value. For an x-brush, it must be defined as [x0, x1];
* for a y-brush, it must be defined as [y0, y1].
*/
move(group: Selection<SVGGElement, Datum, any, any>, selection: BrushSelection): void;
/**
* Sets the active selection of the brush on the specified SVG G element(s) selection
* based on the array returned by a value function invoked for each selection element.
*
* @param group A D3 selection of SVG G elements.
* @param selection A selection value function which is evaluated for each selected element,
* in order, being passed the current datum (d), the current index (i), and the current group (nodes),
* with this as the current DOM element. The function returns an array specifying the new active brush selection.
* For a two-dimensional brush, it must be defined as [[x0, y0], [x1, y1]], where x0 is the minimum x-value, y0 is the minimum y-value,
* x1 is the maximum x-value, and y1 is the maximum y-value. For an x-brush, it must be defined as [x0, x1];
* for a y-brush, it must be defined as [y0, y1].
*/
move(group: Selection<SVGGElement, Datum, any, any>, selection: ValueFn<SVGGElement, Datum, BrushSelection>): void;
/**
* Clear the active selection of the brush on the specified SVG G element(s) transition.
*
* @param group A D3 transition on SVG G elements.
* @param selection Use null to clear the active brush selection.
*/
move(group: Selection<SVGGElement, Datum, any, any>, selection: null): void;
/**
* Sets the active selection of the brush on the specified SVG G element(s) transition
* to the provided array.
*
* @param group A D3 transition on SVG G elements.
* @param selection An array specifying the new active brush selection. For a two-dimensional brush,
* it must be defined as [[x0, y0], [x1, y1]], where x0 is the minimum x-value, y0 is the minimum y-value,
* x1 is the maximum x-value, and y1 is the maximum y-value. For an x-brush, it must be defined as [x0, x1];
* for a y-brush, it must be defined as [y0, y1].
*/
move(group: TransitionLike<SVGGElement, Datum>, selection: BrushSelection): void;
/**
* Sets the active selection of the brush on the specified SVG G element(s) transition
* based on the array returned by a value function invoked for each transitioning element.
*
* @param group A D3 transition on SVG G elements.
* @param selection A selection value function which is evaluated for each selected element,
* in order, being passed the current datum (d), the current index (i), and the current group (nodes),
* with this as the current DOM element. The function returns an array specifying the new active brush selection.
* For a two-dimensional brush, it must be defined as [[x0, y0], [x1, y1]], where x0 is the minimum x-value, y0 is the minimum y-value,
* x1 is the maximum x-value, and y1 is the maximum y-value. For an x-brush, it must be defined as [x0, x1];
* for a y-brush, it must be defined as [y0, y1].
*/
move(group: TransitionLike<SVGGElement, Datum>, selection: ValueFn<SVGGElement, Datum, BrushSelection>): void;
/**
* Returns the current extent accessor.
*/
extent(): ValueFn<SVGGElement, Datum, [[number, number], [number, number]]>;
/**
* Set the brushable extent to the specified array of points and returns this brush.
*
* The brush extent determines the size of the invisible overlay and also constrains the brush selection;
* the brush selection cannot go outside the brush extent.
*
* @param extent array of points [[x0, y0], [x1, y1]], where [x0, y0] is the top-left corner
* and [x1, y1] is the bottom-right corner.
*/
extent(extent: [[number, number], [number, number]]): this;
/**
* Set the brushable extent to the specified array of points returned by the accessor function
* evaluated for each element in the selection/transition and returns this brush.
*
* The brush extent determines the size of the invisible overlay and also constrains the brush selection;
* the brush selection cannot go outside the brush extent.
*
* @param extent An extent accessor function which is evaluated for each selected element,
* in order, being passed the current datum (d), the current index (i), and the current group (nodes),
* with this as the current DOM element. The function returns an array of points [[x0, y0], [x1, y1]],
* where [x0, y0] is the top-left corner and [x1, y1] is the bottom-right corner.
*/
extent(extent: ValueFn<SVGGElement, Datum, [[number, number], [number, number]]>): this;
/**
* Returns the current filter function.
*/
filter(): ValueFn<SVGGElement, Datum, boolean>;
/**
* Sets the filter to the specified filter function and returns the brush.
*
* If the filter returns falsey, the initiating event is ignored and no brush gesture is started.
* Thus, the filter determines which input events are ignored. The default filter ignores mousedown events on secondary buttons,
* since those buttons are typically intended for other purposes, such as the context menu.
*
* @param filterFn A filter function which is evaluated for each selected element,
* in order, being passed the current datum (d), the current index (i), and the current group (nodes),
* with this as the current DOM element. The function returns a boolean value.
*/
filter(filterFn: ValueFn<SVGGElement, Datum, boolean>): this;
/**
* Returns the current handle size, which defaults to six.
*/
handleSize(): number;
/**
* Sets the size of the brush handles to the specified number and returns the brush.
*
* This method must be called before applying the brush to a selection;
* changing the handle size does not affect brushes that were previously rendered.
* The default size is 6.
*
* @param size Size of the handle.
*/
handleSize(size: number): this;
/**
* Returns the first currently-assigned listener matching the specified typenames, if any.
*
* @param typenames The typenames is a string containing one or more typename separated by whitespace.
* Each typename is a type, optionally followed by a period (.) and a name, such as "brush.foo"" and "brush.bar";
* the name allows multiple listeners to be registered for the same type. The type must be one of the following:
* start (at the start of a brush gesture, such as on mousedown), brush (when the brush moves, such as on mousemove), or
* end (at the end of a brush gesture, such as on mouseup.)
*/
on(typenames: string): ValueFn<SVGGElement, Datum, void> | undefined;
/**
* Removes the current event listeners for the specified typenames, if any.
*
* @param typenames The typenames is a string containing one or more typename separated by whitespace.
* Each typename is a type, optionally followed by a period (.) and a name, such as "brush.foo"" and "brush.bar";
* the name allows multiple listeners to be registered for the same type. The type must be one of the following:
* start (at the start of a brush gesture, such as on mousedown), brush (when the brush moves, such as on mousemove), or
* end (at the end of a brush gesture, such as on mouseup.)
* @param listener Use null to remove the listener.
*/
on(typenames: string, listener: null): this;
/**
* Sets the event listener for the specified typenames and returns the brush.
* If an event listener was already registered for the same type and name,
* the existing listener is removed before the new listener is added.
* When a specified event is dispatched, each listener will be invoked with the same context and arguments as selection.on listeners.
*
* @param typenames The typenames is a string containing one or more typename separated by whitespace.
* Each typename is a type, optionally followed by a period (.) and a name, such as "brush.foo"" and "brush.bar";
* the name allows multiple listeners to be registered for the same type. The type must be one of the following:
* start (at the start of a brush gesture, such as on mousedown), brush (when the brush moves, such as on mousemove), or
* end (at the end of a brush gesture, such as on mouseup.)
* @param listener An event listener function which is evaluated for each selected element,
* in order, being passed the current datum (d), the current index (i), and the current group (nodes),
* with this as the current DOM element.
*/
on(typenames: string, listener: ValueFn<SVGGElement, Datum, void>): this;
}
/**
* Create a new two-dimensional brush.
*
* The generic "Datum" refers to the type of the data of the selected svg:g element to
* which the returned BrushBEhavoir will be applied.
*/
export function brush<Datum>(): BrushBehavior<Datum>;
/**
* Creates a new one-dimensional brush along the x-dimension.
*
* The generic "Datum" refers to the type of the data of the selected svg:g element to
* which the returned BrushBEhavoir will be applied.
*/
export function brushX<Datum>(): BrushBehavior<Datum>;
/**
* Creates a new one-dimensional brush along the y-dimension.
*
* The generic "Datum" refers to the type of the data of the selected svg:g element to
* which the returned BrushBEhavoir will be applied.
*/
export function brushY<Datum>(): BrushBehavior<Datum>;
/**
* Return the current brush selection for the specified node. Internally, an elements brush state is stored as element.__brush;
* however, you should use this method rather than accessing it directly. If the given node has no selection, returns null.
* Otherwise, the selection is defined as an array of numbers.
*
* @param node The node for which the brush selection should be returned.
*/
export function brushSelection(node: SVGGElement): BrushSelection | null;
/**
* D3 brush event
*
* The generic refers to the type of the datum for the group element on which brush was defined.
*/
export interface D3BrushEvent<Datum> {
/**
* The BrushBehavior associated with the event
*/
target: BrushBehavior<Datum>;
/**
* The event type for the BrushEvent
*/
type: 'start' | 'brush' | 'end' | string; // Leave failsafe string type for cases like 'brush.foo'
/**
* The current brush selection associated with the event.
*/
selection: BrushSelection;
/**
* The underlying input event, such as mousemove or touchmove.
*/
sourceEvent: any;
}