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 | 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 8414x 8414x 8414x 8414x 8414x 8414x 8414x 8414x 8414x 8414x 8414x 8414x 8414x 8414x 8414x 8414x 8414x 8414x 8414x 8414x 8414x 8414x 8414x 8414x 8414x 8414x 8414x 8414x 8414x 8414x 8414x 8414x 8414x 8414x 8414x 1x 1x 8424x 8424x 8911x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 5472x 3439x 40x 40x 8911x 8424x 8424x 8424x 8424x 8424x 8424x 8424x 8911x 8911x 8911x 8911x 8911x 8424x 8424x 8424x 1583x 1583x 8424x 8424x 1x 1x 5x 5x 1x 1x 1x 20988x 20988x 20988x 20988x 20988x 1245x 19743x 20988x 1x 1x 1x 10494x 10494x 10494x 10494x 10494x 10494x 10494x 10494x 10494x 10494x 10494x 10494x 10494x 10494x 10494x 10494x 10494x 10494x 10494x 10494x 10494x 10494x 1x 1x 1x 20988x 20988x 20988x 20988x 20988x 20988x 20988x 20988x 20988x 20988x 20988x 20988x 20988x 20988x 1341x 1341x 1341x 1341x 1341x 1341x 1209x 132x 132x 1341x 1341x 20988x 20988x 20988x 1x | import {
ComponentRef,
DestroyRef,
Directive,
ElementRef,
EnvironmentInjector,
ViewContainerRef,
afterRenderEffect,
effect,
inject,
input,
inputBinding,
} from '@angular/core';
import { createWidgetKeySet } from '../../utils/utilities';
import { TooltipComponent } from './tooltip.component';
const ARIA_DESCRIBEDBY = 'aria-describedby';
const ARIA_LABELLEDBY = 'aria-labelledby';
export type ConnectType = 'aria-describedby' | 'aria-labelledby' | 'none';
type PreviousAriaConnectionData = Record<
typeof ARIA_DESCRIBEDBY | typeof ARIA_LABELLEDBY,
string | undefined
>;
@Directive({
selector: '[data-gc-tooltip]',
})
export class TooltipDirective {
public readonly connectAs = input<ConnectType>(ARIA_DESCRIBEDBY, {
alias: 'data-gc-tooltip-connect-as',
});
public readonly testId = input<string>(undefined, {
alias: 'data-gc-tooltip-testid',
});
public readonly offset = input(0, { alias: 'data-gc-tooltip-offset' });
public readonly orientation = input<'vertical' | 'horizontal'>('vertical', {
alias: 'data-gc-tooltip-orientation',
});
/**
* Set the text of the tooltip.
* Tooltip is not created if the text is empty.
*/
public readonly text = input.required<string>({ alias: 'data-gc-tooltip' });
public readonly focusAnchor = input<
ElementRef<HTMLElement> | HTMLElement | undefined
>(undefined, { alias: 'data-gc-tooltip-focus-anchor' });
public readonly permitTooltip = input<() => boolean>(undefined, {
alias: 'data-gc-tooltip-permit',
});
/** @ignore */
private readonly viewRef = inject(ViewContainerRef);
/** @ignore */
private readonly widgetKeys = createWidgetKeySet('gc-tooltip');
private previousAriaConnection?: PreviousAriaConnectionData;
private tooltipComponent?: ComponentRef<TooltipComponent>;
constructor() {
const injector = inject(EnvironmentInjector);
effect(() => {
if (this.text() && this.tooltipComponent === undefined) {
this.tooltipComponent = this.viewRef.createComponent(TooltipComponent, {
environmentInjector: injector,
bindings: [
inputBinding('text', this.text),
inputBinding('offset', this.offset),
inputBinding('orientation', this.orientation),
inputBinding('contentId', () => this.widgetKeys.baseKey),
inputBinding('invoker', () => this.viewRef.element),
inputBinding('focusInvoker', this.focusAnchor),
inputBinding('permitTooltip', this.permitTooltip),
inputBinding('testId', this.testId),
],
});
} else if (!this.text() && this.tooltipComponent !== undefined) {
this.tooltipComponent.destroy();
this.tooltipComponent = undefined;
}
});
// only aria attributes referenced via `connectAs` should be modified, other attributes which may be present on the host element
// should not be touched. Angular host bindings would also delete unrelated attributes (see https://gitlab.bestsolution.at/gefa/gefa-web-controls/-/merge_requests/2527/)
// -> manual implementation which only removes attributes if we wrote it ourselves before
afterRenderEffect({
write: () => {
this.previousAriaConnection = this.updateAriaAttributes(
this.connectAs(),
this.text(),
);
},
});
inject(DestroyRef).onDestroy(() => {
this.tooltipComponent?.destroy();
this.updateAriaAttributes('none', ''); // remove any attributes which have been set
});
}
public hide(): void {
this.tooltipComponent?.instance.hide();
}
/** @ignore */
private determineAriaAttributeValue(
attribute: typeof ARIA_DESCRIBEDBY | typeof ARIA_LABELLEDBY,
connectAs: ConnectType,
text: string,
): string | undefined {
return connectAs === attribute && text !== ''
? this.widgetKeys.baseKey
: undefined;
}
/** @ignore */
private updateAriaAttributes(
connectAs: ConnectType,
text: string,
): PreviousAriaConnectionData | undefined {
const tooltipOwner = this.viewRef.element;
if (!(tooltipOwner.nativeElement instanceof Element)) {
console.error('Tooltip owner element is not defined.');
return undefined;
}
const tooltipOwnerElement = tooltipOwner.nativeElement;
return {
[ARIA_DESCRIBEDBY]: this.updateAriaAttribute(
tooltipOwnerElement,
ARIA_DESCRIBEDBY,
connectAs,
text,
),
[ARIA_LABELLEDBY]: this.updateAriaAttribute(
tooltipOwnerElement,
ARIA_LABELLEDBY,
connectAs,
text,
),
};
}
/** @ignore */
private updateAriaAttribute(
element: Element,
attribute: typeof ARIA_DESCRIBEDBY | typeof ARIA_LABELLEDBY,
connectAs: ConnectType,
text: string,
): string | undefined {
const expectedPreviousValue: string | undefined =
this.previousAriaConnection?.[attribute];
const newValue = this.determineAriaAttributeValue(
attribute,
connectAs,
text,
);
if (expectedPreviousValue !== newValue) {
const presentValue = element.getAttribute(attribute) ?? undefined;
if (presentValue !== expectedPreviousValue) {
console.error(
`The value of the '${attribute}' attribute was modified externally / outside of the tooltip.`,
`Expected: "${expectedPreviousValue ?? '<not present>'}"`,
`Found: "${presentValue ?? '<not present>'}"`,
);
throw new Error('unexpected modification of tooltip aria attributes');
}
if (newValue) {
element.setAttribute(attribute, newValue);
} else {
element.removeAttribute(attribute);
}
}
return newValue;
}
}
|