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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import {
DestroyRef,
Directive,
ElementRef,
Injectable,
inject,
input,
} from '@angular/core';
import { FocusableElementOwner } from './util.types';
import { isElementShowing } from './utilities';
export interface RegionNavigationTarget {
name: string;
readonly domElement: HTMLElement;
transferFocus: (direction: 'FORWARDS' | 'BACKWARDS') => boolean;
}
function findGlobalScope(el: HTMLElement | null) {
let cur = el;
while (cur !== null) {
if (cur.tagName === 'DIALOG') {
return cur;
}
cur = cur.parentElement;
}
return document;
}
function findNextRegion(
direction: 'FORWARDS' | 'BACKWARDS',
): HTMLElement | undefined {
const currentActive = document.activeElement as HTMLElement | null;
const scope = findGlobalScope(currentActive);
if (
currentActive === null ||
currentActive.tagName === 'BODY' ||
currentActive.tagName === 'DIALOG'
) {
return findAllVisibleRegionNavTargets(scope).at(0);
}
const region = findContainingScope(currentActive);
if (region) {
const allFields = findAllVisibleRegionNavTargets(scope);
// Only of there's more than one search field
// we can focus a next one
if (allFields.length > 1) {
const nextIdx =
allFields.indexOf(region) + (direction === 'BACKWARDS' ? -1 : 1);
if (nextIdx >= 0) {
if (nextIdx < allFields.length) {
return allFields[nextIdx];
}
return allFields[0];
} else {
return allFields.at(-1);
}
}
}
const all = findAllFocusableControlsWithRegionNavs(scope);
const idx = all.indexOf(currentActive);
if (idx !== -1) {
if (direction === 'FORWARDS') {
const el = all.find((v, curIdx) => {
// Skip all ahead of us
if (curIdx < idx) {
return false;
}
return isScopeElement(v);
});
if (el === undefined) {
return all.find(isScopeElement);
}
return el;
} else {
for (let i = idx; i >= 0; i--) {
if (isScopeElement(all[i])) {
return all[i];
}
}
}
}
return undefined;
}
function findAllFocusableControlsWithRegionNavs(scope: ParentNode) {
const els = scope.querySelectorAll(
'[data-gc-region-navigation-target], a[href], button, input, textarea, select, details,[tabindex]',
);
const result: HTMLElement[] = [];
for (let i = 0; i < els.length; i++) {
const el = els.item(i);
if (isElementShowing(el)) {
result.push(el as HTMLElement);
}
}
return result;
}
function findContainingScope(el: HTMLElement) {
let cur: HTMLElement | null = el;
while (cur !== null) {
if (isScopeElement(cur)) {
return cur;
}
cur = cur.parentElement;
}
return undefined;
}
function findAllVisibleRegionNavTargets(scope: ParentNode) {
const searchFields = scope.querySelectorAll(
'[data-gc-region-navigation-target]',
);
const result: HTMLElement[] = [];
for (let i = 0; i < searchFields.length; i++) {
const el = searchFields.item(i);
if (isElementShowing(el)) {
result.push(el as HTMLElement);
}
}
return result;
}
function isScopeElement(el: HTMLElement) {
return el.hasAttribute('data-gc-region-navigation-target');
}
@Injectable({ providedIn: 'root' })
export class RegionNavigationTargetService {
public get targets(): readonly RegionNavigationTarget[] {
return [...this._targets];
}
private _targets: RegionNavigationTarget[] = [];
public register(target: RegionNavigationTarget) {
this._targets.push(target);
return () => {
this._targets = this._targets.filter(r => r !== target);
};
}
public hasNextRegion(direction: 'FORWARDS' | 'BACKWARDS') {
return findNextRegion(direction) !== undefined;
}
public focusNextRegion(direction: 'FORWARDS' | 'BACKWARDS') {
const domElement = findNextRegion(direction);
const target = this.targets.find(e => e.domElement === domElement);
if (target) {
// Hack as it looks like focus transfer fails when done in a synchronous way
// tracked in https://gitlab.bestsolution.at/gefa/gefa-web-controls/-/work_items/3509
setTimeout(() => target.transferFocus(direction));
return true;
}
return false;
}
}
@Directive({
selector: '[data-gc-region-navigation-target]',
standalone: false,
})
export class RegionNavigationTargetDirective implements RegionNavigationTarget {
get domElement() {
return this.el.nativeElement;
}
get name() {
return this.focusRegionName();
}
public readonly focusRegionName = input.required<string>();
public readonly focusTransferHandler =
input<(direction: 'FORWARDS' | 'BACKWARDS') => boolean>();
public readonly focusTransferElement = input<FocusableElementOwner>();
private readonly el: ElementRef<HTMLElement> = inject<
ElementRef<HTMLElement>
>(ElementRef<HTMLElement>);
private readonly destroyRef = inject(DestroyRef);
private readonly registry = inject(RegionNavigationTargetService);
constructor() {
const unregister = this.registry.register(this);
this.destroyRef.onDestroy(() => {
unregister();
});
}
transferFocus(direction: 'FORWARDS' | 'BACKWARDS') {
const handler = this.focusTransferHandler();
if (handler) {
return handler(direction);
}
const owner = this.focusTransferElement();
if (owner) {
return owner.focusChild();
}
return false;
}
}
|