Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2712x 2712x 1x 1x 156x 156x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 114x 114x 1x 1x 142x 142x 142x 142x 142x 1x 1x 284x 284x 1x 1x 50x 50x 1x 1x 156x 156x 156x 156x 118x 118x 117x 118x 118x 156x 156x 156x 156x 156x 154x 154x 154x 2x 2x 156x 156x 1x 1x 1x 46x 46x 18x 46x 46x 46x 46x 46x 46x 46x 46x 46x 46x 1x 1x 1x 20x 20x 1x 1x 1x 67x 67x 67x 19x 19x 19x 49x 8x 8x 8x 51x 17x 17x 17x 56x 2x 2x 2x 2x 2x 2x 2x 67x 19x 2x 2x 17x 10x 10x 67x 67x 67x 67x 1x 1x 1x 2x 2x 2x 2x 2x 1x 1x 1x 19x 19x 18x 19x 19x 1x 1x 1x 17x 17x 17x 17x 17x 1x 1x 1x 8x 8x 8x 8x 8x 1x 1x 2x 2x 1x 1x 142x 142x 1095x 1095x 142x 142x 142x 1x 1x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 44x 18x 18x 18x 18x 18x 10x 10x 10x 10x 1x 2x 2x 2x 2x 2x 1x 1x 10x 10x 10x 4x 10x 10x 1x 1x 10x 7x 7x 10x 10x 1x | import { ChangeDetectorRef, EventEmitter, Injectable } from '@angular/core';
import { isPrintableCharacter } from '../utils/internal-utils';
import { Converter, Item, Key } from '../utils/util.types';
import { VirtualListBoxItemDirective } from './virtual-listbox-item.directive';
import type { ListboxSelectEvent } from './virtual-listbox.directive';
@Injectable()
export class VirtualListboxService {
public get activeDescendant(): string | null {
return this.hasFocus ? this._activeDescendant : null; // Only render active descendant if it has focus otherwise FF & NVDA thinks the list has the focus
}
public set activeDescendant(value: string | null) {
this._activeDescendant = value;
}
public value?: Key;
public readonly onSelect: EventEmitter<ListboxSelectEvent> =
new EventEmitter<ListboxSelectEvent>();
public readonly onKeypress: EventEmitter<KeyboardEvent> =
new EventEmitter<KeyboardEvent>();
private hasFocus = false;
private focusedListIndex = -1;
private _activeDescendant: string | null = null;
private itemSearchText = '';
private clearTimer?: ReturnType<typeof setTimeout>;
private children: readonly VirtualListBoxItemDirective[] = [];
constructor(private change: ChangeDetectorRef) {}
public itemIdComputer: Converter<Item, string> = i => 'item-' + i.key;
public setItems(viewChildren: readonly VirtualListBoxItemDirective[]): void {
this.children = viewChildren;
this.focusedListIndex = -1;
this._activeDescendant = null;
this.connectMenuItems();
}
public getItems(): readonly VirtualListBoxItemDirective[] {
return this.children;
}
public selectItem(item: Item, open: boolean): void {
this.onSelect.emit({ key: item.key, open });
}
public updateFocusedIndex(
idx: number,
scrollContainer: HTMLElement | 'global',
) {
if (this.focusedListIndex !== -1) {
const prevItem = this.children.at(this.focusedListIndex);
if (prevItem) {
prevItem.focused = false;
}
}
this.focusedListIndex = idx;
const item = this.children.at(idx);
if (item) {
item.focused = true;
item.scrollIntoView(scrollContainer);
this.activeDescendant = this.itemIdComputer(item.item);
} else {
this.activeDescendant = null;
}
}
/** @ignore */
public onFocus(): void {
this.hasFocus = true;
if (this.focusedListIndex === -1 && this.children.length > 0) {
this.updateFocusedIndex(0, 'global');
}
// Do not sync with selection - eg windows explorer does not do it either
if (this.focusedListIndex !== -1) {
const item = this.children.at(this.focusedListIndex);
if (item) {
item.scrollIntoView('global');
}
}
}
/** @ignore */
public onBlur(): void {
this.hasFocus = false;
}
/** @ignore */
public onKeyDown(event: KeyboardEvent): void {
const key = event.key;
switch (key) {
case 'Enter':
this._impl_onEnter();
event.preventDefault();
break;
case 'ArrowUp':
this._impl_onUp();
event.preventDefault();
break;
case 'ArrowDown':
this._impl_onDown();
event.preventDefault();
break;
case 'End':
this._impl_onEnd();
event.preventDefault();
break;
case 'Home':
this.updateFocusedIndex(0, 'global');
event.preventDefault();
break;
default:
if (key === ' ' && this.itemSearchText === '') {
this._impl_onSpace();
event.preventDefault();
} else if (key === ' ' || isPrintableCharacter(key)) {
this.moveByKey(key);
event.preventDefault();
}
}
this.onKeypress.emit(event);
}
/** @ignore */
protected _impl_onSpace(): void {
const el = this.children.at(this.focusedListIndex);
if (el && !el.ignoreSelection) {
this.selectItem(el.item.item, false);
}
}
/** @ignore */
protected _impl_onEnter(): void {
const el = this.children.at(this.focusedListIndex);
if (el && !el.ignoreSelection) {
this.selectItem(el.item.item, true);
}
}
/** @ignore */
protected _impl_onDown(): void {
if (this.focusedListIndex === -1) {
this.updateFocusedIndex(0, 'global');
} else if (this.focusedListIndex + 1 < this.children.length) {
this.updateFocusedIndex(this.focusedListIndex + 1, 'global');
}
}
/** @ignore */
protected _impl_onUp(): void {
if (this.focusedListIndex === -1) {
this.updateFocusedIndex(this.children.length - 1, 'global');
} else if (this.focusedListIndex - 1 >= 0) {
this.updateFocusedIndex(this.focusedListIndex - 1, 'global');
}
}
protected _impl_onEnd(): void {
this.updateFocusedIndex(this.children.length - 1, 'global');
}
private connectMenuItems(): void {
const size = this.getItems().length;
this.getItems().forEach((e, index) => {
e.updateItem(index, size);
e.focused = false;
});
this.change.detectChanges();
}
private moveByKey(char: string): void {
const curIdx = this.focusedListIndex === -1 ? 0 : this.focusedListIndex;
const firstSearch = this.itemSearchText === '';
this.itemSearchText += char;
this.clearSearchTimer();
this.clearTimer = setTimeout(() => {
this.itemSearchText = '';
}, 1000);
let indx = this.children.findIndex((itemDirective, idx) => {
const indexCheck = firstSearch ? idx > curIdx : idx >= curIdx;
return (
indexCheck &&
itemDirective.item.label
.toLocaleLowerCase()
.includes(this.itemSearchText.toLocaleLowerCase())
);
});
if (indx === -1) {
indx = this.children.findIndex((itemDirective, idx) => {
return (
idx <= curIdx &&
itemDirective.item.label
.toLocaleLowerCase()
.includes(this.itemSearchText.toLocaleLowerCase())
);
});
}
if (indx !== -1 && indx !== curIdx) {
this.updateFocusedIndex(indx, 'global');
}
}
private clearSearchTimer() {
if (this.clearTimer !== undefined) {
clearTimeout(this.clearTimer);
this.clearTimer = undefined;
}
}
}
|