|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {QueryList} from '@angular/core'; |
| 10 | +import {Observable, Subject} from 'rxjs'; |
| 11 | + |
| 12 | +// TODO(cassc): Temporarily disable tslint since this is just the raw API. |
| 13 | +// tslint:disable |
| 14 | + |
| 15 | +/** Represents an item within a tree that can be passed to a TreeKeyManager. */ |
| 16 | +export interface TreeKeyManagerItem { |
| 17 | + /** Whether the item is disabled. */ |
| 18 | + isDisabled?(): boolean; |
| 19 | + |
| 20 | + /** The user-facing label for this item. */ |
| 21 | + getLabel?(): string; |
| 22 | + |
| 23 | + /** Perform the main action (i.e. selection) for this item. */ |
| 24 | + activate(): void; |
| 25 | + |
| 26 | + /** Retrieves the parent for this item. This is `null` if there is no parent. */ |
| 27 | + getParent(): this | null; |
| 28 | + |
| 29 | + /** Retrieves the children for this item. */ |
| 30 | + getChildren(): this[] | Observable<this[]>; |
| 31 | + |
| 32 | + /** Determines if the item is currently expanded. */ |
| 33 | + isExpanded(): boolean; |
| 34 | + |
| 35 | + /** Collapses the item, hiding its children. */ |
| 36 | + collapse(): void; |
| 37 | + |
| 38 | + /** Expands the item, showing its children. */ |
| 39 | + expand(): void; |
| 40 | + |
| 41 | + /** |
| 42 | + * Focuses the item. This should provide some indication to the user that this item is focused. |
| 43 | + */ |
| 44 | + focus(): void; |
| 45 | +} |
| 46 | + |
| 47 | +export interface TreeKeyManagerOptions<T extends TreeKeyManagerItem> { |
| 48 | + items: Observable<T[]> | QueryList<T> | T[]; |
| 49 | + |
| 50 | + /** |
| 51 | + * Sets the predicate function that determines which items should be skipped by the tree key |
| 52 | + * manager. By default, disabled items are skipped. |
| 53 | + * |
| 54 | + * If the item is to be skipped, this function should return false. |
| 55 | + */ |
| 56 | + skipPredicate?: (item: T) => boolean; |
| 57 | + |
| 58 | + /** |
| 59 | + * If true, then the key manager will call `activate` in addition to calling `focus` when a |
| 60 | + * particular item is focused. By default, this is false. |
| 61 | + */ |
| 62 | + activationFollowsFocus?: boolean; |
| 63 | + |
| 64 | + /** |
| 65 | + * The direction in which the tree items are laid out horizontally. This influences which key |
| 66 | + * will be interpreted as expand or collapse. Defaults to 'ltr'. |
| 67 | + */ |
| 68 | + horizontalOrientation?: 'rtl' | 'ltr'; |
| 69 | + |
| 70 | + /** |
| 71 | + * If provided, determines how the key manager determines if two items are equivalent. |
| 72 | + * |
| 73 | + * It should provide a unique key for each unique tree item. If two tree items are equivalent, |
| 74 | + * then this function should return the same value. |
| 75 | + */ |
| 76 | + trackBy?: (treeItem: T) => unknown; |
| 77 | + |
| 78 | + /** |
| 79 | + * If a value is provided, enables typeahead mode, which allows users to set the active item |
| 80 | + * by typing the visible label of the item. |
| 81 | + * |
| 82 | + * If a number is provided, this will be the time to wait after the last keystroke before |
| 83 | + * setting the active item. If `true` is provided, the default interval of 200ms will be used. |
| 84 | + */ |
| 85 | + typeAheadDebounceInterval?: true | number; |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * This class manages keyboard events for trees. If you pass it a QueryList or other list of tree |
| 90 | + * items, it will set the active item, focus, handle expansion and typeahead correctly when |
| 91 | + * keyboard events occur. |
| 92 | + */ |
| 93 | +export class TreeKeyManager<T extends TreeKeyManagerItem> { |
| 94 | + constructor(options: TreeKeyManagerOptions<T>) {} |
| 95 | + |
| 96 | + /** |
| 97 | + * Stream that emits any time the TAB key is pressed, so components can react |
| 98 | + * when focus is shifted off of the list. |
| 99 | + */ |
| 100 | + readonly tabOut = new Subject<void>(); |
| 101 | + |
| 102 | + /** |
| 103 | + * Handles a keyboard event on the tree. |
| 104 | + * @param event Keyboard event that represents the user interaction with the tree. |
| 105 | + */ |
| 106 | + onKeydown(event: KeyboardEvent) {} |
| 107 | + |
| 108 | + /** |
| 109 | + * Handles a mouse click on a particular tree item. |
| 110 | + * @param treeItem The item that was clicked by the user. |
| 111 | + */ |
| 112 | + onClick(treeItem: T) {} |
| 113 | + |
| 114 | + /** Index of the currently active item. */ |
| 115 | + getActiveItemIndex(): number | null { |
| 116 | + return null; |
| 117 | + } |
| 118 | + |
| 119 | + /** The currently active item. */ |
| 120 | + getActiveItem(): T | null { |
| 121 | + return null; |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +// tslint:enable |
0 commit comments