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 | 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 28x 28x 28x 28x 28x 28x 1x 1x 6893x 6893x 6893x 1x 1x 238x 238x 238x 1x 1x 1x 1x 1x 1x 8477x 8477x 8477x 8477x 1x 1x 10x 10x 10x 10x 10x 1x 1x 22078x 22078x 22078x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8059x 8059x 8059x 8059x 8059x 8059x 8059x 8059x 8059x 8059x 8059x 8059x 67x 8059x 8059x 8059x 8059x 1x 1x 1x 27875x 8477x 8477x 8477x 8477x 2902x 2902x 8361x 49x 49x 49x 5526x 5526x 5171x 5171x 8477x 8477x 8477x 27875x 27875x 8477x 27875x 27875x 1x 1x 1x 1797x 1797x 1x 1x 1x 17790x 17790x 17790x 17790x 17790x 1468x 16322x 17790x 1x 1x 1x 8477x 8477x 8477x 8477x 8477x 8477x 8477x 8477x 8477x 8477x 8477x 8477x 8477x 8477x 8477x 8477x 8477x 8477x 8477x 8477x 1x 1x 1x 16954x 16954x 16954x 16954x 16954x 16954x 16954x 16954x 16954x 16954x 16954x 1456x 836x 836x 836x 836x 30x 806x 836x 836x 836x 16954x 16954x 16954x 16954x 16954x 15528x 1426x 16954x 16954x 16954x 16954x 16954x 16954x 1318x 1318x 1318x 1318x 1318x 1318x 1316x 2x 2x 1318x 1318x 16954x 1x | import {
ComponentRef,
Directive,
ElementRef,
Input,
OnChanges,
OnDestroy,
Optional,
SimpleChange,
SimpleChanges,
ViewContainerRef,
} from '@angular/core';
import { createWidgetKeySet } from '../../utils/utilities';
import { ElementInsertionContextService } from '../element-insertion-context.service';
import { OverlayContainerComponent } from '../overlay-container/overlay-container.component';
import { ConnectType, TooltipComponent } from './tooltip.component';
const ARIA_DESCRIBEDBY = 'aria-describedby';
const ARIA_LABELLEDBY = 'aria-labelledby';
@Directive({
selector: '[data-gc-tooltip]',
standalone: false,
})
export class TooltipDirective implements OnDestroy, OnChanges {
@Input('data-gc-tooltip-connect-as')
public connectAs: ConnectType = ARIA_DESCRIBEDBY;
@Input('data-gc-tooltip-testid')
public set testId(value: string) {
(this.componentRef.location.nativeElement as HTMLElement).setAttribute(
'data-testid',
value,
);
}
@Input('data-gc-tooltip-offset')
public set offset(value: number) {
this.componentRef.setInput('offset', value);
}
@Input('data-gc-tooltip-orientation')
public set orientation(value: 'vertical' | 'horizontal') {
this.componentRef.setInput('orientation', value);
}
/**
* Set the text of the tooltip.
* Tooltip is not created if the text is empty.
*/
@Input({ alias: 'data-gc-tooltip', required: true })
public set text(value: string) {
this._text = value;
this.componentRef.setInput('text', value);
}
@Input('data-gc-tooltip-focus-anchor')
public set focusAnchor(
value: ElementRef<HTMLElement> | HTMLElement | undefined,
) {
this.componentRef.setInput('focusAnchor', value);
}
@Input('data-gc-tooltip-permit')
public set permitTooltip(value: () => boolean) {
this.componentRef.setInput('permitTooltip', value);
}
/** @ignore */
private readonly _widgetKeys = createWidgetKeySet('gc-tooltip');
/** @ignore */
private _text = '';
/** @ignore */
private readonly componentRef: ComponentRef<TooltipComponent>;
/** @ignore */
private readonly parentElement: HTMLElement;
constructor(
private readonly viewRef: ViewContainerRef,
@Optional() eic: ElementInsertionContextService | null,
@Optional() parent: OverlayContainerComponent | null,
) {
this.componentRef = this.viewRef.createComponent(TooltipComponent);
this.componentRef.setInput('tooltipId', this._widgetKeys.baseKey);
this.componentRef.setInput(
'tooltipOwner',
this.viewRef.element.nativeElement,
);
let zIndex = 1000;
if (parent) {
zIndex = parent.zIndex + 900;
}
this.componentRef.setInput('zIndex', zIndex);
this.parentElement = eic?.parent ?? document.body;
}
/** @ignore */
ngOnChanges(changes: SimpleChanges): void {
if ('text' in changes) {
const change = changes.text;
const componentAttached =
change.firstChange || change.previousValue !== '';
if (change.currentValue === '' && componentAttached) {
const componentIndex = this.viewRef.indexOf(this.componentRef.hostView);
this.viewRef.detach(componentIndex);
} else if (change.currentValue && !componentAttached) {
this.viewRef.insert(this.componentRef.hostView);
this.parentElement.appendChild(
this.componentRef.location.nativeElement,
);
} else if (change.firstChange) {
this.parentElement.appendChild(
this.componentRef.location.nativeElement,
);
}
}
if ('connectAs' in changes || 'text' in changes) {
this.updateAriaAttributes(changes.connectAs, changes.text);
}
}
/** @ignore */
ngOnDestroy(): void {
this.componentRef.destroy();
}
/** @ignore */
private determineAriaAttributeValue(
attribute: Exclude<ConnectType, 'none'>,
connectAs: ConnectType,
text: string,
): string | undefined {
return connectAs === attribute && text !== ''
? this.componentRef.instance.tooltipContentId
: undefined;
}
/** @ignore */
private updateAriaAttributes(
connectAs: SimpleChange | undefined,
text: SimpleChange | undefined,
) {
const tooltipOwner = this.viewRef.element;
if (!(tooltipOwner.nativeElement instanceof Element)) {
console.error('Tooltip owner element is not defined.');
return;
}
const tooltipOwnerElement = tooltipOwner.nativeElement;
this.updateAriaAttribute(
tooltipOwnerElement,
ARIA_DESCRIBEDBY,
connectAs,
text,
);
this.updateAriaAttribute(
tooltipOwnerElement,
ARIA_LABELLEDBY,
connectAs,
text,
);
}
/** @ignore */
private updateAriaAttribute(
element: Element,
attribute: typeof ARIA_DESCRIBEDBY | typeof ARIA_LABELLEDBY,
connectAsChange: SimpleChange | undefined,
textChange: SimpleChange | undefined,
) {
type ConnectAsPropType = typeof this.connectAs;
type TextPropType = typeof this.text;
let expectedPreviousValue: string | undefined = undefined;
if (
connectAsChange?.firstChange !== true &&
textChange?.firstChange !== true
) {
expectedPreviousValue = this.determineAriaAttributeValue(
attribute,
connectAsChange !== undefined
? (connectAsChange.previousValue as ConnectAsPropType)
: this.connectAs,
textChange !== undefined
? (textChange.previousValue as TextPropType)
: this._text,
);
}
const newValue = this.determineAriaAttributeValue(
attribute,
connectAsChange !== undefined
? (connectAsChange.currentValue as ConnectAsPropType)
: this.connectAs,
textChange !== undefined
? (textChange.currentValue as TextPropType)
: this._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);
}
}
}
}
|