| 149 | |
| 150 | /** Controls the state and interactions of a tree view. */ |
| 151 | export class TreePattern<V> implements TreeInputs<V> { |
| 152 | /** The tree behavior for the tree. */ |
| 153 | readonly treeBehavior: Tree<TreeItemPattern<V>, V>; |
| 154 | |
| 155 | /** Whether the tree has been interacted with. */ |
| 156 | readonly hasBeenInteracted = signal(false); |
| 157 | |
| 158 | /** The root level is 0. */ |
| 159 | readonly level = () => 0; |
| 160 | |
| 161 | /** The root is always expanded. */ |
| 162 | readonly expanded = () => true; |
| 163 | |
| 164 | /** The root is always visible. */ |
| 165 | readonly visible = () => true; |
| 166 | |
| 167 | /** The tab index of the tree. */ |
| 168 | readonly tabIndex: SignalLike<-1 | 0> = computed(() => this.treeBehavior.tabIndex()); |
| 169 | |
| 170 | /** The id of the current active item. */ |
| 171 | readonly activeDescendant = computed(() => this.treeBehavior.activeDescendant()); |
| 172 | |
| 173 | /** The direct children of the root (top-level tree items). */ |
| 174 | readonly children = computed(() => |
| 175 | this.inputs.items().filter(item => item.level() === this.level() + 1), |
| 176 | ); |
| 177 | |
| 178 | /** Whether the tree selection follows focus. */ |
| 179 | readonly followFocus = computed(() => this.inputs.selectionMode() === 'follow'); |
| 180 | |
| 181 | /** Whether the tree direction is RTL. */ |
| 182 | readonly isRtl = computed(() => this.textDirection() === 'rtl'); |
| 183 | |
| 184 | /** The key for navigating to the previous item. */ |
| 185 | readonly prevKey = computed(() => { |
| 186 | if (this.inputs.orientation() === 'vertical') { |
| 187 | return 'ArrowUp'; |
| 188 | } |
| 189 | return this.isRtl() ? 'ArrowRight' : 'ArrowLeft'; |
| 190 | }); |
| 191 | |
| 192 | /** The key for navigating to the next item. */ |
| 193 | readonly nextKey = computed(() => { |
| 194 | if (this.inputs.orientation() === 'vertical') { |
| 195 | return 'ArrowDown'; |
| 196 | } |
| 197 | return this.isRtl() ? 'ArrowLeft' : 'ArrowRight'; |
| 198 | }); |
| 199 | |
| 200 | /** The key for collapsing an item or moving to its parent. */ |
| 201 | readonly collapseKey = computed(() => { |
| 202 | if (this.inputs.orientation() === 'horizontal') { |
| 203 | return 'ArrowUp'; |
| 204 | } |
| 205 | return this.isRtl() ? 'ArrowRight' : 'ArrowLeft'; |
| 206 | }); |
| 207 | |
| 208 | /** The key for expanding an item or moving to its first child. */ |
nothing calls this directly
no test coverage detected