datatable-tree 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +251 -2
  3. package/dist/DataTable.d.ts +583 -0
  4. package/dist/DataTable.js +620 -0
  5. package/dist/Draggable.d.ts +35 -0
  6. package/dist/Draggable.js +91 -0
  7. package/dist/ListBox.d.ts +132 -0
  8. package/dist/ListBox.js +243 -0
  9. package/dist/ListVirtualizer.d.ts +59 -0
  10. package/dist/ListVirtualizer.js +180 -0
  11. package/dist/TreeList.d.ts +196 -0
  12. package/dist/TreeList.js +300 -0
  13. package/dist/config.d.ts +55 -0
  14. package/dist/config.js +33 -0
  15. package/dist/datatable.css +161 -0
  16. package/dist/hooks.d.ts +27 -0
  17. package/dist/hooks.js +115 -0
  18. package/dist/index.d.ts +12 -0
  19. package/dist/index.js +15 -0
  20. package/dist/interfaces.d.ts +269 -0
  21. package/dist/interfaces.js +3 -0
  22. package/dist/mobx-adapter.d.ts +12 -0
  23. package/dist/mobx-adapter.js +21 -0
  24. package/dist/reactivity.d.ts +112 -0
  25. package/dist/reactivity.js +155 -0
  26. package/dist/styles.d.ts +64 -0
  27. package/dist/styles.js +86 -0
  28. package/dist/theme.d.ts +160 -0
  29. package/dist/theme.js +239 -0
  30. package/dist/util.d.ts +28 -0
  31. package/dist/util.js +45 -0
  32. package/package.json +143 -4
  33. package/src/DataTable.tsx +1332 -0
  34. package/src/Draggable.tsx +133 -0
  35. package/src/ListBox.tsx +423 -0
  36. package/src/ListVirtualizer.ts +234 -0
  37. package/src/TreeList.tsx +590 -0
  38. package/src/config.tsx +96 -0
  39. package/src/css.d.ts +1 -0
  40. package/src/datatable.css +161 -0
  41. package/src/hooks.ts +138 -0
  42. package/src/index.ts +15 -0
  43. package/src/interfaces.ts +309 -0
  44. package/src/mobx-adapter.ts +24 -0
  45. package/src/reactivity.ts +222 -0
  46. package/src/styles.tsx +102 -0
  47. package/src/theme.ts +346 -0
  48. package/src/util.ts +60 -0
@@ -0,0 +1,161 @@
1
+ /* datatable-tree structural stylesheet.
2
+ *
3
+ * All class names are stable and prefixed with `dtbl-`. Colors and spacing read CSS custom
4
+ * properties (--dtbl-*) whose fallback values equal the built-in default theme
5
+ * (`lightTheme`, except that the fallback text color is `inherit`); see theme.ts for
6
+ * the TableTheme type that sets these variables on a container element. */
7
+
8
+ /* Scrollable wrapper rendered by <DataTable> */
9
+ .dtbl-outer {
10
+ overflow: auto;
11
+ }
12
+ /* Scrollable wrapper rendered by <ListBox> and <TreeList> */
13
+ .dtbl-list-outer {
14
+ overflow-y: auto;
15
+ }
16
+
17
+ .dtbl-table {
18
+ width: 100%;
19
+ table-layout: fixed;
20
+ border-spacing: 0;
21
+ /* Line up digits vertically in numeric columns (fonts with proportional or old-style
22
+ * figures otherwise make number columns look ragged) */
23
+ font-variant-numeric: tabular-nums lining-nums;
24
+ }
25
+
26
+ /* Table rows, including the header row. dtbl-row-padded is the default; dtbl-row is used
27
+ * when the `noPadding` item prop is set. */
28
+ .dtbl-row, .dtbl-row-padded {
29
+ text-align: left;
30
+ color: var(--dtbl-text, inherit);
31
+ background-color: var(--dtbl-row-bg, #ffffff);
32
+ }
33
+ .dtbl-row td, .dtbl-row th,
34
+ .dtbl-row-padded td, .dtbl-row-padded th {
35
+ display: table-cell;
36
+ vertical-align: middle;
37
+ border-bottom: var(--dtbl-row-border, 1px solid #d9e0dd);
38
+ }
39
+ .dtbl-row-padded td, .dtbl-row-padded th {
40
+ padding: var(--dtbl-cell-padding, 6px 6px);
41
+ }
42
+ .dtbl-row th, .dtbl-row-padded th {
43
+ position: sticky;
44
+ top: 0;
45
+ /* A background is required so that sticky headings appear on top of data rows.
46
+ * Without a background, data rows may be "mixed" with headings (drawn at the
47
+ * same location, which is ugly and perhaps unreadable.) */
48
+ background-color: var(--dtbl-header-bg, inherit);
49
+ /* While sticky headings are normally drawn on top of data cells, data with opacity
50
+ * less than 1 is drawn on top of headings by default. A z-index greater than 0
51
+ * fixes that. */
52
+ z-index: 1;
53
+ /* The default is bold for header cells, which doesn't quite fit our aesthetic */
54
+ font-weight: normal;
55
+ }
56
+
57
+ /* Base background and text color of every selectable row/item. List items need this too:
58
+ * without it (and with the state tints below replacing background-color, as they once did),
59
+ * translucent theme colors composited against the PAGE behind the table, so a dark table on
60
+ * a light page got light hovered rows under light text. */
61
+ .dtbl-selected, .dtbl-unselected, .dtbl-parent-selected {
62
+ color: var(--dtbl-text, inherit);
63
+ background-color: var(--dtbl-row-bg, #ffffff);
64
+ }
65
+ /* Optional striping of alternate table rows, starting with the first (--dtbl-stripe-bg;
66
+ * default: transparent, i.e. off). Drawn as a background-image layer like the state tints
67
+ * below; :where() keeps specificity at (0,1,0) so the (0,2,0) selection/hover rules always
68
+ * replace the stripe on their row. */
69
+ :where(tbody) > :where(.dtbl-row, .dtbl-row-padded):nth-child(odd) {
70
+ background-image: linear-gradient(var(--dtbl-stripe-bg, transparent),
71
+ var(--dtbl-stripe-bg, transparent));
72
+ }
73
+
74
+ /* Selection/hover states, drawn as a background-image layer so the (often translucent)
75
+ * tint composites over the base row background above, never over the page. The class name
76
+ * is doubled to outrank single-class rules in app stylesheets regardless of rule order. */
77
+ .dtbl-selected.dtbl-selected {
78
+ background-image: linear-gradient(var(--dtbl-selected-bg, rgba(11, 58, 46, 0.22)),
79
+ var(--dtbl-selected-bg, rgba(11, 58, 46, 0.22)));
80
+ }
81
+ .dtbl-selected:hover {
82
+ background-image: linear-gradient(var(--dtbl-hover-selected-bg, rgba(11, 58, 46, 0.32)),
83
+ var(--dtbl-hover-selected-bg, rgba(11, 58, 46, 0.32)));
84
+ }
85
+ .dtbl-unselected:hover {
86
+ background-image: linear-gradient(var(--dtbl-hover-unselected-bg, rgba(11, 58, 46, 0.08)),
87
+ var(--dtbl-hover-unselected-bg, rgba(11, 58, 46, 0.08)));
88
+ }
89
+ /* Rows whose (grand)parent row is selected (see `highlightChildrenOfSelectedParents`) */
90
+ .dtbl-parent-selected.dtbl-parent-selected {
91
+ background-image: linear-gradient(var(--dtbl-parent-selected-bg, rgba(11, 58, 46, 0.08)),
92
+ var(--dtbl-parent-selected-bg, rgba(11, 58, 46, 0.08)));
93
+ }
94
+
95
+ /* Padding of list/tree items outside tables (unless the `noPadding` item prop is set) */
96
+ .dtbl-item-padding {
97
+ padding: var(--dtbl-item-padding, 6px 10px);
98
+ }
99
+
100
+ /* Overlay that grays out a read-only row */
101
+ .dtbl-readonly-overlay {
102
+ position: absolute;
103
+ left: 0; top: 0; right: 0; bottom: 0;
104
+ pointer-events: none;
105
+ opacity: 0.15;
106
+ background: #888;
107
+ filter: grayscale(80%);
108
+ }
109
+
110
+ /* Sort direction indicator in a column header */
111
+ .dtbl-sort-icon {
112
+ transform: translateY(-50%);
113
+ position: absolute;
114
+ top: 50%;
115
+ right: 0;
116
+ pointer-events: none;
117
+ display: inline-flex;
118
+ align-items: center;
119
+ }
120
+
121
+ /* Invisible drag handle at the right edge of a resizable column header */
122
+ .dtbl-resize-handle {
123
+ position: absolute;
124
+ bottom: 0;
125
+ width: 8px;
126
+ top: 0;
127
+ /* The fallback is `defaultResizeCursor` from theme.ts; keep the two in sync. */
128
+ cursor: var(--dtbl-resize-cursor, url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACgAAAAeCAYAAABe3VzdAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAGJoAABiaAZsyYXAAAAEbSURBVFhH7ZSLDsIgDAA3jf//wUZnb4GlEsbaTmUmuwSpPLobMMahI9M0zfUoSHUllPKU9kdqp+pHErwR6kJ7lr/Mvx0RkXuqFylNd8EtTsG9nIJ7aV40ta8qI3fU3Cljmjks4xiTn8Xdl8cSh1ZQJ7TA2CzqxS3olctEJV2CUblMRNJ8Bmty0paiNrV50rZM1rl1H7FJUCf4FIXIkr8U3Nzib8gBOcmd/q6yKcjb8Cafhpx5pVqYthhqK2kVr83Tcjq37iM2f8VMYkIJ7a2Shi1oAQuua4bENUkrXjlwCUJUMiIHbkHwSkblICQIPNDyUOu4NcKCv+IU3Ev4bESQs5iid+Qjkq6dF3UvDiPIalFKDrHFa/zBFg/DC/as2Q7ADo1PAAAAAElFTkSuQmCC) 30 15, col-resize);
129
+ }
130
+
131
+ /* Default tree-expander button (see TableConfig.renderIconButton to replace it) */
132
+ .dtbl-expander {
133
+ background: none;
134
+ border: none;
135
+ padding: 0;
136
+ margin: 0;
137
+ color: inherit;
138
+ font: inherit;
139
+ cursor: pointer;
140
+ display: inline-flex;
141
+ align-items: center;
142
+ justify-content: center;
143
+ align-self: center;
144
+ height: 2em;
145
+ border-radius: 1em;
146
+ }
147
+ .dtbl-expander:hover {
148
+ background-color: var(--dtbl-hover-unselected-bg, rgba(11, 58, 46, 0.08));
149
+ }
150
+
151
+ /* Default text glyph icons (see TableConfig.icons to replace them) */
152
+ .dtbl-glyph {
153
+ font-size: 0.72em;
154
+ line-height: 1;
155
+ user-select: none;
156
+ }
157
+ /* Default SVG chevron icons (sort direction, tree expander) */
158
+ .dtbl-glyph-svg {
159
+ display: block;
160
+ user-select: none;
161
+ }
package/src/hooks.ts ADDED
@@ -0,0 +1,138 @@
1
+ import { createContext, useContext, useEffect, useReducer, useRef, useState } from 'react';
2
+
3
+ import { Holder } from 'holders';
4
+
5
+ import { nully } from './util.js';
6
+
7
+ export function useForceUpdate(): () => void {
8
+ return useReducer(i => i + 1, 0)[1];
9
+ }
10
+
11
+ /** Returns an override if provided and `useState(initialState)[0]` otherwise. Typically
12
+ * `initialState` would be, or return, a collection type such as a Set. */
13
+ export function useStatefulObjectIfNotGivenBy<S>(override: S | nully, initialState: S | (() => S)): S;
14
+ export function useStatefulObjectIfNotGivenBy<S>(override: S | nully): S|undefined;
15
+ export function useStatefulObjectIfNotGivenBy<S>(override: S | nully, initialState?: S): S|undefined {
16
+ // Since hooks cannot be called conditionally, we call `useState` even if there's an override
17
+ return coalesce(override, useState(initialState)[0]);
18
+ }
19
+
20
+ /** This is the same as the `??` operator except that the second argument is evaluated
21
+ * unconditionally. Since hooks must be evaluated unconditionally, this makes it useful
22
+ * for using a hook's output as a default, e.g.
23
+ *
24
+ * const value = coalesce(p.parentState, useHeldState(''));
25
+ **/
26
+ export function coalesce<T>(override: T | nully, defaultValue: T): T {
27
+ return override ?? defaultValue;
28
+ }
29
+
30
+ /** Wraps `useState` in a `Holder`, an object with `get` and `set` methods (see the
31
+ * `holders` package). */
32
+ export function useHeldState<S>(initialState: S | (() => S)): Holder<S>;
33
+ export function useHeldState<S>(): Holder<S|undefined>;
34
+ export function useHeldState<S>(initialState?: S): Holder<S> {
35
+ let [state, setState] = useState(initialState);
36
+ return {
37
+ get() { return state!; },
38
+ set(s: S) {
39
+ setState(typeof s === 'function' ? () => s : s);
40
+ state = s;
41
+ }
42
+ };
43
+ }
44
+
45
+ /** Returns `override` if provided, or a `Holder` backed by `useState` otherwise. */
46
+ export function useHeldStateIfNotGivenBy<S>(override: Holder<S> | nully, initialState: S | (() => S)): Holder<S>;
47
+ export function useHeldStateIfNotGivenBy<S>(override: Holder<S|undefined> | nully): Holder<S|undefined>;
48
+ export function useHeldStateIfNotGivenBy<S>(override: Holder<S> | nully, initialState?: S): Holder<S> {
49
+ let [state, setState] = useState(initialState);
50
+
51
+ if (override) {
52
+ return override;
53
+ } else {
54
+ return {
55
+ get() { return state!; },
56
+ set(s: S) {
57
+ setState(typeof s === 'function' ? () => s : s);
58
+ state = s;
59
+ }
60
+ };
61
+ }
62
+ }
63
+
64
+ /** When a component's `readOnly` prop is undefined, the list/table components fall back on
65
+ * this context, so an app can mark a whole panel read-only with a single provider. */
66
+ export const ReadOnlyContext = createContext<boolean>(false);
67
+
68
+ export function useIsReadOnlyContext(): boolean {
69
+ return useContext(ReadOnlyContext);
70
+ }
71
+
72
+ // WORKAROUND: React 18 StrictMode causes fake cleanup events when the component
73
+ // is still visible. Specifically, `useEffect` calls your function twice, and it
74
+ // calls the cleanup function returned from your function before it calls your
75
+ // function a second time. Supposedly this is because they plan to add a feature
76
+ // someday such that components can be "hidden" without being destroyed.
77
+ // Unfortunately React won't tell us whether the component is being destroyed or
78
+ // not, so I wrote this helper function to bypass the StrictMode behavior of
79
+ // useEffect so that you can have an effect and cleanup handler that are called
80
+ // only once.
81
+ //
82
+ // Needs testing. It should support the `deps` array, but it's meant for the case
83
+ // that deps is [].
84
+ //
85
+ // It works by setting a temporary state during the first effect call and asking
86
+ // for a callback using `setTimeout()`. The temporary state will prevent the
87
+ // second `useEffect` call from doing anything except returning the destructor
88
+ // returned earlier by the effect handler, if any (and recording the fact that
89
+ // the second call happened, by clearing the temporary state). And if the second
90
+ // effect call doesn't happen, we know StrictMode is off.
91
+ //
92
+ // There's a slight difference in timing when `StrictMode` is off: the destructor
93
+ // is called in `setTimeout` rather than immediately.
94
+ export function useOneEffect(effect: () => (void | (() => void)), deps?: React.DependencyList)
95
+ {
96
+ let state = useRef<boolean | (() => void)>(false);
97
+ useEffect(() => {
98
+ if (state.current === false) {
99
+ // This is the first call to the effect, and it is unknown whether
100
+ // there will be a second call (we don't know if StrictMode is on)
101
+ let destructor = effect();
102
+ if (destructor) {
103
+ state.current = destructor;
104
+ return () => {
105
+ // At this point a fake or real cleanup is happening, and we must
106
+ // figure out which. If it's fake, then `state.current` will be
107
+ // false by the time the `setTimeout` handler is called. If it's real,
108
+ // then `state.current` should still point to the same `destructor`.
109
+ setTimeout(() => {
110
+ let destructor = state.current;
111
+ if (typeof destructor === 'function') {
112
+ state.current = false;
113
+ destructor();
114
+ }
115
+ });
116
+ };
117
+ } else {
118
+ // Helps detect if there is a second call to this effect handler
119
+ state.current = true;
120
+ setTimeout(() => {
121
+ state.current = false;
122
+ });
123
+ }
124
+ } else {
125
+ // This is the second call to the effect. This time we want to return
126
+ // the destructor from the first effect call, if there was one.
127
+ let destructor = state.current;
128
+ state.current = false;
129
+ if (typeof destructor === 'function') {
130
+ return destructor;
131
+ }
132
+ }
133
+ }, deps);
134
+ }
135
+
136
+ export function useUnmountHandler(destructor: () => void, deps: React.DependencyList = []) {
137
+ return useOneEffect(() => destructor, deps);
138
+ }
package/src/index.ts ADDED
@@ -0,0 +1,15 @@
1
+ // Note: the stylesheet is deliberately NOT imported here. A CSS import in the published
2
+ // entry point makes the package unloadable by anything without a CSS-aware bundler (Node,
3
+ // SSR, a consumer's vitest). Import 'datatable-tree/datatable.css' from your app instead.
4
+ export * from './config.js';
5
+ export * from './DataTable.js';
6
+ export * from './Draggable.js';
7
+ export * from './hooks.js';
8
+ export * from './interfaces.js';
9
+ export * from './ListBox.js';
10
+ export * from './ListVirtualizer.js';
11
+ export * from './reactivity.js';
12
+ export * from './styles.js';
13
+ export * from './theme.js';
14
+ export * from './TreeList.js';
15
+ export * from './util.js';
@@ -0,0 +1,309 @@
1
+ // Copied verbatim (documentation unchanged) from the 'sorted-btree' package by David Piepgrass,
2
+ // https://github.com/qwertie/btree-typescript (interfaces.d.ts), MIT license.
3
+
4
+ /** Read-only set interface (subinterface of IMapSource<K,any>).
5
+ * The word "set" usually means that each item in the collection is unique
6
+ * (appears only once, based on a definition of equality used by the
7
+ * collection.) Objects conforming to this interface aren't guaranteed not
8
+ * to contain duplicates, but as an example, BTree<K,V> implements this
9
+ * interface and does not allow duplicates. */
10
+ export interface ISetSource<K=any>
11
+ {
12
+ /** Returns the number of key/value pairs in the map object. */
13
+ size: number;
14
+ /** Returns a boolean asserting whether the key exists in the map object or not. */
15
+ has(key: K): boolean;
16
+ /** Returns a new iterator for iterating the items in the set (the order is implementation-dependent). */
17
+ keys(): IterableIterator<K>;
18
+ }
19
+
20
+ /** Read-only map interface (i.e. a source of key-value pairs). */
21
+ export interface IMapSource<K=any, V=any> extends ISetSource<K>
22
+ {
23
+ /** Returns the number of key/value pairs in the map object. */
24
+ size: number;
25
+ /** Returns the value associated to the key, or undefined if there is none. */
26
+ get(key: K): V|undefined;
27
+ /** Returns a boolean asserting whether the key exists in the map object or not. */
28
+ has(key: K): boolean;
29
+ /** Calls callbackFn once for each key-value pair present in the map object.
30
+ * The ES6 Map class sends the value to the callback before the key, so
31
+ * this interface must do likewise. */
32
+ forEach(callbackFn: (v:V, k:K, map:IMapSource<K,V>) => void, thisArg?: any): void;
33
+
34
+ /** Returns an iterator that provides all key-value pairs from the collection (as arrays of length 2). */
35
+ entries(): IterableIterator<[K,V]>;
36
+ /** Returns a new iterator for iterating the keys of each pair. */
37
+ keys(): IterableIterator<K>;
38
+ /** Returns a new iterator for iterating the values of each pair. */
39
+ values(): IterableIterator<V>;
40
+ // TypeScript compiler decided Symbol.iterator has type 'any'
41
+ //[Symbol.iterator](): IterableIterator<[K,V]>;
42
+ }
43
+
44
+ /** Write-only set interface (the set cannot be queried, but items can be added to it.)
45
+ * Note: BTree<K,V> does not officially implement this interface, but BTree<K>
46
+ * can be used as an instance of ISetSink<K>. */
47
+ export interface ISetSink<K=any>
48
+ {
49
+ /** Adds the specified item to the set, if it was not in the set already. */
50
+ add(key: K): any;
51
+ /** Returns true if an element in the map object existed and has been
52
+ * removed, or false if the element did not exist. */
53
+ delete(key: K): boolean;
54
+ /** Removes everything so that the set is empty. */
55
+ clear(): void;
56
+ }
57
+
58
+ /** Write-only map interface (i.e. a drain into which key-value pairs can be "sunk") */
59
+ export interface IMapSink<K=any, V=any>
60
+ {
61
+ /** Returns true if an element in the map object existed and has been
62
+ * removed, or false if the element did not exist. */
63
+ delete(key: K): boolean;
64
+ /** Sets the value for the key in the map object (the return value is
65
+ * boolean in BTree but Map returns the Map itself.) */
66
+ set(key: K, value: V): any;
67
+ /** Removes all key/value pairs from the IMap object. */
68
+ clear(): void;
69
+ }
70
+
71
+ /** Set interface.
72
+ * Note: BTree<K,V> does not officially implement this interface, but BTree<K>
73
+ * can be used as an instance of ISet<K>. */
74
+ export interface ISet<K=any> extends ISetSource<K>, ISetSink<K> { }
75
+
76
+ /** An interface compatible with ES6 Map and BTree. This interface does not
77
+ * describe the complete interface of either class, but merely the common
78
+ * interface shared by both. */
79
+ export interface IMap<K=any, V=any> extends IMapSource<K, V>, IMapSink<K, V> { }
80
+
81
+ /** An data source that provides read-only access to a set of items called
82
+ * "keys" in sorted order. This is a subinterface of ISortedMapSource. */
83
+ export interface ISortedSetSource<K=any> extends ISetSource<K>
84
+ {
85
+ /** Gets the lowest key in the collection. */
86
+ minKey(): K | undefined;
87
+ /** Gets the highest key in the collection. */
88
+ maxKey(): K | undefined;
89
+ /** Returns the next key larger than the specified key (or undefined if there is none).
90
+ * Also, nextHigherKey(undefined) returns the lowest key. */
91
+ nextHigherKey(key?: K): K|undefined;
92
+ /** Returns the next key smaller than the specified key (or undefined if there is none).
93
+ * Also, nextLowerKey(undefined) returns the highest key. */
94
+ nextLowerKey(key?: K): K|undefined;
95
+ /** Calls `callback` on the specified range of keys, in ascending order by key.
96
+ * @param low The first key scanned will be greater than or equal to `low`.
97
+ * @param high Scanning stops when a key larger than this is reached.
98
+ * @param includeHigh If the `high` key is present in the map, `onFound` is called
99
+ * for that final pair if and only if this parameter is true.
100
+ * @param onFound A function that is called for each key pair. Because this
101
+ * is a subinterface of ISortedMapSource, if there is a value
102
+ * associated with the key, it is passed as the second parameter.
103
+ * @param initialCounter Initial third argument of `onFound`. This value
104
+ * increases by one each time `onFound` is called. Default: 0
105
+ * @returns Number of pairs found and the number of times `onFound` was called.
106
+ */
107
+ forRange(low: K, high: K, includeHigh: boolean, onFound?: (k:K,v:any,counter:number) => void, initialCounter?: number): number;
108
+ /** Returns a new iterator for iterating the keys of each pair in ascending order.
109
+ * @param firstKey Minimum key to include in the output. */
110
+ keys(firstKey?: K): IterableIterator<K>;
111
+ }
112
+
113
+ /** An data source that provides read-only access to items in sorted order. */
114
+ export interface ISortedMapSource<K=any, V=any> extends IMapSource<K, V>, ISortedSetSource<K>
115
+ {
116
+ /** Returns the next pair whose key is larger than the specified key (or undefined
117
+ * if there is none). If key === undefined, this function returns the lowest pair. */
118
+ nextHigherPair(key?: K): [K,V]|undefined;
119
+ /** Returns the next pair whose key is smaller than the specified key (or undefined
120
+ * if there is none). If key === undefined, this function returns the highest pair. */
121
+ nextLowerPair(key?: K): [K,V]|undefined;
122
+ /** Builds an array of pairs from the specified range of keys, sorted by key.
123
+ * Each returned pair is also an array: pair[0] is the key, pair[1] is the value.
124
+ * @param low The first key in the array will be greater than or equal to `low`.
125
+ * @param high This method returns when a key larger than this is reached.
126
+ * @param includeHigh If the `high` key is present in the map, its pair will be
127
+ * included in the output if and only if this parameter is true. Note:
128
+ * if the `low` key is present, it is always included in the output.
129
+ * @param maxLength Maximum length of the returned array (default: unlimited)
130
+ * @remarks Computational complexity: O(result.length + log size)
131
+ */
132
+ getRange(low: K, high: K, includeHigh?: boolean, maxLength?: number): [K,V][];
133
+ /** Calls `callback` on the specified range of keys, in ascending order by key.
134
+ * @param low The first key scanned will be greater than or equal to `low`.
135
+ * @param high Scanning stops when a key larger than this is reached.
136
+ * @param includeHigh If the `high` key is present in the map, `onFound` is called
137
+ * for that final pair if and only if this parameter is true.
138
+ * @param onFound A function that is called for each key-value pair.
139
+ * @param initialCounter Initial third argument of onFound. This value
140
+ * increases by one each time `onFound` is called. Default: 0
141
+ * @returns Number of pairs found and the number of times `callback` was called.
142
+ */
143
+ forRange(low: K, high: K, includeHigh: boolean, onFound?: (k:K,v:V,counter:number) => void, initialCounter?: number): number;
144
+ /** Returns an iterator that provides items in order by key.
145
+ * @param firstKey Minimum key to include in the output. */
146
+ entries(firstKey?: K): IterableIterator<[K,V]>;
147
+ /** Returns a new iterator for iterating the keys of each pair in ascending order.
148
+ * @param firstKey Minimum key to include in the output. */
149
+ keys(firstKey?: K): IterableIterator<K>;
150
+ /** Returns a new iterator for iterating the values of each pair in order by key.
151
+ * @param firstKey Minimum key whose associated value is included in the output. */
152
+ values(firstKey?: K): IterableIterator<V>;
153
+
154
+ // This method should logically be in IMapSource but is not supported by ES6 Map
155
+ /** Performs a reduce operation like the `reduce` method of `Array`.
156
+ * It is used to combine all pairs into a single value, or perform conversions. */
157
+ reduce<R>(callback: (previous:R,currentPair:[K,V],counter:number,tree:IMapF<K,V>) => R, initialValue: R): R;
158
+ /** Performs a reduce operation like the `reduce` method of `Array`.
159
+ * It is used to combine all pairs into a single value, or perform conversions. */
160
+ reduce<R>(callback: (previous:R|undefined,currentPair:[K,V],counter:number,tree:IMapF<K,V>) => R): R|undefined;
161
+ }
162
+
163
+ /** An interface for a set of keys (the combination of ISortedSetSource<K> and ISetSink<K>) */
164
+ export interface ISortedSet<K=any> extends ISortedSetSource<K>, ISetSink<K> { }
165
+
166
+ /** An interface for a sorted map (dictionary),
167
+ * not including functional/persistent methods. */
168
+ export interface ISortedMap<K=any, V=any> extends IMap<K,V>, ISortedMapSource<K, V>
169
+ {
170
+ // All of the following methods should be in IMap but are left out of IMap
171
+ // so that IMap is compatible with ES6 Map.
172
+
173
+ /** Adds or overwrites a key-value pair in the sorted map.
174
+ * @param key the key is used to determine the sort order of data in the tree.
175
+ * @param value data to associate with the key
176
+ * @param overwrite Whether to overwrite an existing key-value pair
177
+ * (default: true). If this is false and there is an existing
178
+ * key-value pair then the call to this method has no effect.
179
+ * @returns true if a new key-value pair was added, false if the key
180
+ * already existed. */
181
+ set(key: K, value: V, overwrite?: boolean): boolean;
182
+ /** Adds all pairs from a list of key-value pairs.
183
+ * @param pairs Pairs to add to this tree. If there are duplicate keys,
184
+ * later pairs currently overwrite earlier ones (e.g. [[0,1],[0,7]]
185
+ * associates 0 with 7.)
186
+ * @param overwrite Whether to overwrite pairs that already exist (if false,
187
+ * pairs[i] is ignored when the key pairs[i][0] already exists.)
188
+ * @returns The number of pairs added to the collection.
189
+ */
190
+ setPairs(pairs: [K,V][], overwrite?: boolean): number;
191
+ /** Deletes a series of keys from the collection. */
192
+ deleteKeys(keys: K[]): number;
193
+ /** Removes a range of key-value pairs from the B+ tree.
194
+ * @param low The first key deleted will be greater than or equal to `low`.
195
+ * @param high Deleting stops when a key larger than this is reached.
196
+ * @param includeHigh Specifies whether the `high` key, if present, is deleted.
197
+ * @returns The number of key-value pairs that were deleted. */
198
+ deleteRange(low: K, high: K, includeHigh: boolean): number;
199
+
200
+ // TypeScript requires these methods of ISortedMapSource to be repeated
201
+ entries(firstKey?: K): IterableIterator<[K,V]>;
202
+ keys(firstKey?: K): IterableIterator<K>;
203
+ values(firstKey?: K): IterableIterator<V>;
204
+ }
205
+
206
+ /** An interface for a functional set, in which the set object could be read-only
207
+ * but new versions of the set can be created by calling "with" or "without"
208
+ * methods to add or remove keys. This is a subinterface of IMapF<K,V>,
209
+ * so the items in the set may be referred to as "keys". */
210
+ export interface ISetF<K=any> extends ISetSource<K> {
211
+ /** Returns a copy of the set with the specified key included.
212
+ * @remarks You might wonder why this method accepts only one key
213
+ * instead of `...keys: K[]`. The reason is that the derived interface
214
+ * IMapF expects the second parameter to be a value. Therefore
215
+ * withKeys() is provided to set multiple keys at once. */
216
+ with(key: K): ISetF<K>;
217
+ /** Returns a copy of the set with the specified key removed. */
218
+ without(key: K): ISetF<K>;
219
+ /** Returns a copy of the tree with all the keys in the specified array present.
220
+ * @param keys The keys to add.
221
+ * @param returnThisIfUnchanged If true, the method returns `this` when
222
+ * all of the keys are already present in the collection. The
223
+ * default value may be true or false depending on the concrete
224
+ * implementation of the interface (in BTree, the default is false.) */
225
+ withKeys(keys: K[], returnThisIfUnchanged?: boolean): ISetF<K>;
226
+ /** Returns a copy of the tree with all the keys in the specified array removed. */
227
+ withoutKeys(keys: K[], returnThisIfUnchanged?: boolean): ISetF<K>;
228
+ /** Returns a copy of the tree with items removed whenever the callback
229
+ * function returns false.
230
+ * @param callback A function to call for each item in the set.
231
+ * The second parameter to `callback` exists because ISetF
232
+ * is a subinterface of IMapF. If the object is a map, v
233
+ * is the value associated with the key, otherwise v could be
234
+ * undefined or another copy of the third parameter (counter). */
235
+ filter(callback: (k:K,v:any,counter:number) => boolean, returnThisIfUnchanged?: boolean): ISetF<K>;
236
+ }
237
+
238
+ /** An interface for a functional map, in which the map object could be read-only
239
+ * but new versions of the map can be created by calling "with" or "without"
240
+ * methods to add or remove keys or key-value pairs.
241
+ */
242
+ export interface IMapF<K=any, V=any> extends IMapSource<K, V>, ISetF<K> {
243
+ /** Returns a copy of the tree with the specified key set (the value is undefined). */
244
+ with(key: K): IMapF<K,V|undefined>;
245
+ /** Returns a copy of the tree with the specified key-value pair set. */
246
+ with<V2>(key: K, value: V2, overwrite?: boolean): IMapF<K,V|V2>;
247
+ /** Returns a copy of the tree with the specified key-value pairs set. */
248
+ withPairs<V2>(pairs: [K,V|V2][], overwrite: boolean): IMapF<K,V|V2>;
249
+ /** Returns a copy of the tree with all the keys in the specified array present.
250
+ * @param keys The keys to add. If a key is already present in the tree,
251
+ * neither the existing key nor the existing value is modified.
252
+ * @param returnThisIfUnchanged If true, the method returns `this` when
253
+ * all of the keys are already present in the collection. The
254
+ * default value may be true or false depending on the concrete
255
+ * implementation of the interface (in BTree, the default is false.) */
256
+ withKeys(keys: K[], returnThisIfUnchanged?: boolean): IMapF<K,V|undefined>;
257
+ /** Returns a copy of the tree with all values altered by a callback function. */
258
+ mapValues<R>(callback: (v:V,k:K,counter:number) => R): IMapF<K,R>;
259
+ /** Performs a reduce operation like the `reduce` method of `Array`.
260
+ * It is used to combine all pairs into a single value, or perform conversions. */
261
+ reduce<R>(callback: (previous:R,currentPair:[K,V],counter:number,tree:IMapF<K,V>) => R, initialValue: R): R;
262
+ /** Performs a reduce operation like the `reduce` method of `Array`.
263
+ * It is used to combine all pairs into a single value, or perform conversions. */
264
+ reduce<R>(callback: (previous:R|undefined,currentPair:[K,V],counter:number,tree:IMapF<K,V>) => R): R|undefined;
265
+
266
+ // Update return types in ISetF
267
+ without(key: K): IMapF<K,V>;
268
+ withoutKeys(keys: K[], returnThisIfUnchanged?: boolean): IMapF<K,V>;
269
+ /** Returns a copy of the tree with pairs removed whenever the callback
270
+ * function returns false. */
271
+ filter(callback: (k:K,v:V,counter:number) => boolean, returnThisIfUnchanged?: boolean): IMapF<K,V>;
272
+ }
273
+
274
+ /** An interface for a functional sorted set: a functional set in which the
275
+ * keys (items) are sorted. This is a subinterface of ISortedMapF. */
276
+ export interface ISortedSetF<K=any> extends ISetF<K>, ISortedSetSource<K>
277
+ {
278
+ // TypeScript requires this method of ISortedSetSource to be repeated
279
+ keys(firstKey?: K): IterableIterator<K>;
280
+ }
281
+
282
+ export interface ISortedMapF<K=any,V=any> extends ISortedSetF<K>, IMapF<K,V>, ISortedMapSource<K,V>
283
+ {
284
+ /** Returns a copy of the tree with the specified range of keys removed. */
285
+ withoutRange(low: K, high: K, includeHigh: boolean, returnThisIfUnchanged?: boolean): ISortedMapF<K,V>;
286
+
287
+ // TypeScript requires these methods of ISortedSetF and ISortedMapSource to be repeated
288
+ entries(firstKey?: K): IterableIterator<[K,V]>;
289
+ keys(firstKey?: K): IterableIterator<K>;
290
+ values(firstKey?: K): IterableIterator<V>;
291
+ forRange(low: K, high: K, includeHigh: boolean, onFound?: (k:K,v:V,counter:number) => void, initialCounter?: number): number;
292
+
293
+ // Update the return value of methods from base interfaces
294
+ with(key: K): ISortedMapF<K,V|undefined>;
295
+ with<V2>(key: K, value: V2, overwrite?: boolean): ISortedMapF<K,V|V2>;
296
+ withKeys(keys: K[], returnThisIfUnchanged?: boolean): ISortedMapF<K,V|undefined>;
297
+ withPairs<V2>(pairs: [K,V|V2][], overwrite: boolean): ISortedMapF<K,V|V2>;
298
+ mapValues<R>(callback: (v:V,k:K,counter:number) => R): ISortedMapF<K,R>;
299
+ without(key: K): ISortedMapF<K,V>;
300
+ withoutKeys(keys: K[], returnThisIfUnchanged?: boolean): ISortedMapF<K,V>;
301
+ filter(callback: (k:K,v:any,counter:number) => boolean, returnThisIfUnchanged?: boolean): ISortedMapF<K,V>;
302
+ }
303
+
304
+ export interface ISortedMapConstructor<K,V> {
305
+ new (entries?: [K,V][], compare?: (a: K, b: K) => number): ISortedMap<K,V>;
306
+ }
307
+ export interface ISortedMapFConstructor<K,V> {
308
+ new (entries?: [K,V][], compare?: (a: K, b: K) => number): ISortedMapF<K,V>;
309
+ }
@@ -0,0 +1,24 @@
1
+ // Optional entry point (`datatable-tree/mobx`): a ReactivityAdapter backed by MobX.
2
+ // Requires the optional peer dependencies `mobx` and `mobx-react` to be installed.
3
+ import { computed, observable, runInAction } from 'mobx';
4
+ import { observer } from 'mobx-react';
5
+
6
+ import { ReactivityAdapter } from './reactivity.js';
7
+
8
+ /** A `ReactivityAdapter` backed by MobX. Install it once at app startup, before rendering:
9
+ *
10
+ * import { setReactivityAdapter } from 'datatable-tree';
11
+ * import { mobxReactivityAdapter } from 'datatable-tree/mobx';
12
+ *
13
+ * setReactivityAdapter(mobxReactivityAdapter);
14
+ *
15
+ * With this adapter, all list/tree/table components behave as mobx-react `observer`s:
16
+ * rows re-render when observable row data mutates, and props like `selection`,
17
+ * `expandedParents` and `columnsToSortBy` can be MobX observable collections. */
18
+ export const mobxReactivityAdapter: ReactivityAdapter = {
19
+ observer: observer as ReactivityAdapter['observer'],
20
+ runInAction: f => runInAction(f),
21
+ observableSet: <K>() => observable.set<K>(),
22
+ observableArray: <T>() => observable.array<T>(),
23
+ computed: <T>(f: () => T) => computed(f),
24
+ };