All files / lib/internal/label-container label-container.component.ts

99.06% Statements 528/533
95.2% Branches 119/125
95.83% Functions 46/48
99.06% Lines 528/533

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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 5341x 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 27375x 27375x 27375x 1x 1x 27375x 27375x 27375x 27375x 27375x 27375x 27375x 1x 1x 49885x 49885x 49885x 49885x 49885x 49885x 49885x 49885x 49885x 49885x 49885x 1x 1x 1459x 1459x 1459x 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 5195x 5195x 5195x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3865x 3865x 3865x 3865x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2828x     2828x 2828x 1x 1x 3865x 3865x 1x 1x 3865x 3865x 1x 1x 65717x 65717x 1x 1x 1x 69582x 69582x 69582x 69582x 69582x 69582x 69582x 69582x 28x 28x 69582x 69582x 69582x 69582x 27867x 69582x 69582x 28834x 28834x   28834x 5169x 23665x   28834x 28834x 28834x 5169x 23665x 23665x 28834x 28834x 69582x 69582x 69582x 28834x 28834x 69582x 27281x 15450x 27281x 27281x 27281x 27281x 27281x 90x 90x 27281x 27281x 27281x 27281x 27281x 27281x 4677x 4677x 4677x 22604x 22604x 22604x 10773x 11861x 11861x 11861x 11861x 27281x 27281x 27281x 69582x 69582x 5169x 69582x 69582x 69582x   69582x 69582x 1x 1x 3865x 3865x 3865x 3865x 3865x 3865x 3865x 3865x 3865x 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 908x 908x 908x 1x 1x 18915x 18915x 1x 1x 1x 1x 1x 37237x 37237x 37237x 1x 1x 1484x 459x 459x 459x 459x 1484x 1484x 1484x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 923x 923x 923x 1x 1x 1x 923x 923x 923x 923x 923x 923x 1x 1x 1x 54x 54x 1x 1x 1x 1640x 1640x 1640x 1640x 1640x 1640x 1640x 1640x 1607x 702x 1336x 1336x 1336x 1336x 1336x 1336x 1336x 1336x 1336x 1336x 1336x 1336x 1336x 1336x 1336x 1336x 1640x 1640x 1640x 1640x 1640x 1640x 1x 1x 1x 18970x 18970x 1x 1x 1x 18970x 18970x 1x 1x 1x 18970x 18970x 1x 1x 1x 18970x 18970x 1x 1x 1x 18970x 18970x 1x 1x 1x 18970x 18970x 1x 1x 1x 18970x 18970x 1x 1x 1x 18915x 1165x 17750x 17750x 18915x 18915x 1x 1x 1x 18915x 18915x 1x 1x 1x 18970x 6308x 12662x 12662x 18970x 18970x 1x 1x 1x 18970x 18533x 437x 437x 18970x 18970x 1x 1x 1x 75880x 74316x 1564x 1564x 75880x 75880x 1x 1x 1x 18915x 18322x 593x 593x 18915x 18915x 1x 1x 1x 18970x 6623x 12347x 12347x 18970x 18970x 1x 1x 1x 230x 230x 1x  
import {
  AfterContentChecked,
  AfterContentInit,
  AfterViewInit,
  Component,
  ContentChild,
  ContentChildren,
  Directive,
  ElementRef,
  EventEmitter,
  HostBinding,
  Input,
  NgZone,
  OnChanges,
  OnDestroy,
  OnInit,
  Output,
  QueryList,
  SimpleChanges,
  ViewChild,
} from '@angular/core';
 
import { observeSizeOutsideAngular } from '../../utils/internal-utils';
import { Callback } from '../../utils/util.types';
 
let NEXT_AUTO_ID = 0;
 
// TODO rename directive attributes to add prefix
 
@Directive({
  selector: 'label[data-gc-auto-id-label]',
  standalone: false,
})
export class AutoIdLabelDirective {
  constructor(public el: ElementRef<HTMLElement>) {}
}
 
@Directive({
  selector: '[data-gc-auto-id-label]:not(label)',
  standalone: false,
})
export class AutoIdGenericLabelDirective {
  @Input('auto-id-label-active')
  public active = true;
 
  @Input('auto-id-label-order')
  public order?: number; // note: dynamic updates not fully supported at the moment
 
  constructor(public el: ElementRef<HTMLElement>) {}
}
 
@Directive({
  selector: '[data-gc-auto-id-input]',
  standalone: false,
})
export class AutoIdInputDirective {
  @Input('data-gc-labeledbystrategy')
  public labeledByStrategy?: 'label+self';
 
  @HostBinding('attr.aria-invalid')
  @Input('data-gc-error-state')
  public errorState = false;
 
  @HostBinding('attr.id')
  public get id() {
    return this.mainDirective.inputId;
  }
 
  @HostBinding('attr.aria-errormessage')
  public get ariaErrorId() {
    if (this.errorState) {
      return this.mainDirective.errorId;
    } else {
      return undefined;
    }
  }
 
  public get isLabelable(): boolean {
    // https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Content_categories#labelable
    return [
      'button',
      'input',
      'meter',
      'output',
      'progress',
      'select',
      'textarea',
    ].includes(this.el.nativeElement.tagName.toLowerCase());
  }
 
  constructor(
    public el: ElementRef<HTMLElement>,
    private mainDirective: AutoIdDirective,
  ) {}
}
 
@Directive({
  selector: '[data-gc-auto-id-desc]',
  standalone: false,
})
export class AutoIdDescDirective {
  constructor(public el: ElementRef<HTMLElement>) {}
}
 
@Directive({
  selector: '[data-gc-auto-id-sec-desc]',
  standalone: false,
})
export class AutoIdSecondaryDescDirective {
  constructor(public el: ElementRef<HTMLElement>) {}
}
 
@Directive({
  selector: '[data-gc-auto-id-error]',
  host: {
    role: 'log',
    'aria-live': 'assertive',
  },
  standalone: false,
})
export class AutoIdErrorDirective {
  @HostBinding('attr.id')
  public get id() {
    return this.mainDirective.errorId;
  }
 
  constructor(private mainDirective: AutoIdDirective) {}
}
 
@Directive({
  selector: '[auto-id]', // eslint-disable-line @angular-eslint/directive-selector -- old occurence, needs to be updated
  exportAs: 'autoId',
  standalone: false,
})
export class AutoIdDirective
  implements OnInit, AfterContentInit, AfterContentChecked, OnChanges
{
  @ContentChild(AutoIdLabelDirective) contentLabel?: AutoIdLabelDirective;
  @ContentChildren(AutoIdGenericLabelDirective, { descendants: true })
  contentLabelsGeneric!: QueryList<AutoIdGenericLabelDirective>;
  @ContentChild(AutoIdInputDirective) contentInput?: AutoIdInputDirective;
  @ContentChild(AutoIdDescDirective) contentDesc?: AutoIdDescDirective;
  @ContentChild(AutoIdSecondaryDescDirective)
  contentSecDesc?: AutoIdSecondaryDescDirective;
 
  @Input('auto-id')
  public id?: string;
 
  autoId!: string;
  labelId!: string;
  inputId!: string;
  descId!: string;
  secDescId!: string;
  errorId!: string;
 
  private nextScopedId = 0;
  constructor(public el: ElementRef) {}
 
  ngOnChanges(changes: SimpleChanges): void {
    if ('id' in changes && !changes.id.firstChange) {
      this.initIds();
      this.updateIds();
    }
  }
 
  ngOnInit(): void {
    this.initIds();
  }
 
  ngAfterContentInit(): void {
    this.updateIds();
  }
 
  ngAfterContentChecked(): void {
    this.updateIds();
  }
 
  // eslint-disable-next-line sonarjs/cognitive-complexity
  updateIds(): void {
    // NOSONAR ignoring cognitive complexity report
    const labelEl = this.contentLabel?.el;
    const inputEl = this.contentInput?.el;
    const descEl = this.contentDesc?.el;
    const secDescEl = this.contentSecDesc?.el;
 
    for (const i of this.contentLabelsGeneric) {
      if (!i.el.nativeElement.id) {
        i.el.nativeElement.id =
          this.labelId + '-' + (this.nextScopedId++).toString();
      }
    }
 
    if (labelEl) {
      labelEl.nativeElement.id = this.labelId;
    }
    if (inputEl && this.contentInput) {
      let describedBy: string | undefined = undefined;
      if (descEl && secDescEl) {
        describedBy = this.descId + ' ' + this.secDescId;
      } else if (descEl) {
        describedBy = this.descId;
      } else if (secDescEl) {
        describedBy = this.secDescId;
      }
 
      if (describedBy !== undefined) {
        inputEl.nativeElement.setAttribute('aria-describedby', describedBy);
      } else {
        inputEl.nativeElement.removeAttribute('aria-describedby');
      }
    }
 
    if (
      this.contentInput &&
      inputEl &&
      (labelEl !== undefined || this.contentLabelsGeneric.length > 0)
    ) {
      if (labelEl && this.contentInput.isLabelable) {
        labelEl.nativeElement.setAttribute('for', this.inputId);
      }
 
      const genericLabelIDs = this.contentLabelsGeneric
        .filter(entry => entry.active)
        .map((entry, index) => ({
          id: entry.el.nativeElement.id,
          order: entry.order ?? index,
        }))
        .sort((a, b) => a.order - b.order)
        .map(entry => entry.id);
 
      // we only connect with aria-labelledby if it is different to 'for'
      if (this.contentInput.labeledByStrategy === 'label+self') {
        inputEl.nativeElement.setAttribute(
          'aria-labelledby',
          [this.labelId, this.inputId].concat(genericLabelIDs).join(' '),
        );
      } else if (
        !this.contentInput.isLabelable ||
        this.contentLabelsGeneric.length > 0
      ) {
        inputEl.nativeElement.setAttribute(
          'aria-labelledby',
          (labelEl ? [this.labelId] : []).concat(genericLabelIDs).join(' '),
        );
      }
    }
 
    if (descEl) {
      descEl.nativeElement.id = this.descId;
    }
 
    if (secDescEl) {
      secDescEl.nativeElement.id = this.secDescId;
    }
  }
 
  private initIds() {
    const id = this.id ?? 'widget-' + (NEXT_AUTO_ID++).toString();
 
    this.autoId = id;
    this.labelId = id + '-label';
    this.descId = id + '-desc';
    this.secDescId = id + '-sec-desc';
    this.inputId = id + '-control';
    this.errorId = id + '-error';
  }
}
 
@Component({
  selector: 'gc-label-container',
  templateUrl: './label-container.component.html',
  styleUrls: ['./label-container.component.css'],
  standalone: false,
})
export class LabelContainerComponent implements AfterViewInit, OnDestroy {
  /**
   * manual required state
   */
  @Input()
  public required?: boolean;
 
  /**
   * manual readonly state
   */
  @Input()
  public readonly?: boolean;
 
  /**
   * manual disabled state
   */
  @Input()
  public disabled?: boolean;
 
  /**
   * manual active state
   */
  @Input()
  public active?: boolean;
 
  /**
   * Decides if the label container is resizable
   */
  @Input() resizable?: boolean;
 
  /** @ignore */
  @ViewChild('drawerSlot', { static: true })
  public drawerSlot!: ElementRef<HTMLElement>;
 
  @Output()
  public readonly boxClick: EventEmitter<MouseEvent> =
    new EventEmitter<MouseEvent>();
 
  /** @ignore */
  @ViewChild('box', { static: true })
  public boxEl!: ElementRef<HTMLDivElement>;
 
  /** @ignore */
  @ViewChild('lastCharacterPositionEl')
  public lastCharacterPositionEl!: ElementRef<HTMLSpanElement>;
 
  /** @ignore */
  @ViewChild('topBorderStartEl')
  public topBorderStartEl!: ElementRef<HTMLSpanElement>;
 
  /** @ignore */
  @ViewChild('label')
  public labelEl!: ElementRef<HTMLDivElement>;
 
  /** @ignore */
  @ViewChild('fontLoadHelperEl')
  public fontLoadHelperEl!: ElementRef<HTMLDivElement>;
 
  /**
   * manual error state
   */
  @Input()
  set error(value: boolean) {
    this._error = value;
  }
 
  get error(): boolean {
    return this._error;
  }
 
  /**
   * manual focused state
   */
  @Input()
  public get focused() {
    return this._focused;
  }
 
  public set focused(value: boolean | undefined) {
    if (value === true && this._focused !== value) {
      this.el.nativeElement.scrollIntoView({
        block: 'nearest',
        inline: 'nearest',
      });
    }
    this._focused = value;
  }
 
  /** @ignore */
  private _error = false;
 
  /** @ignore */
  private _focused?: boolean;
 
  /** @ignore */
  private resizeObserver?: Callback;
 
  /** @ignore */
  private labelPositionOffset = 0;
 
  /** @ignore */
  private labelLineHeight = 0;
 
  /** @ignore */
  private labelTopBorderLeftPos = 0;
 
  constructor(
    private readonly zone: NgZone,
    private readonly el: ElementRef<HTMLElement>,
  ) {}
 
  /** @ignore */
  ngAfterViewInit(): void {
    this.resizeObserver = observeSizeOutsideAngular(
      this.zone,
      [this.fontLoadHelperEl.nativeElement, this.labelEl.nativeElement],
      this.computeLabelPositioning.bind(this),
    );
  }
 
  /** @ignore */
  ngOnDestroy(): void {
    this.resizeObserver?.();
  }
 
  /** @ignore */
  computeLabelPositioning(): undefined {
    const style = getComputedStyle(this.labelEl.nativeElement);
    const lineHeight = Number.parseFloat(style.lineHeight);
    const labelVerticalOffset = Number.parseFloat(style.height) - lineHeight;
    const lastCharacterOffsetLeft =
      this.lastCharacterPositionEl.nativeElement.offsetLeft;
 
    if (
      this.labelPositionOffset !== labelVerticalOffset ||
      this.labelLineHeight !== lineHeight ||
      this.labelTopBorderLeftPos !== lastCharacterOffsetLeft
    ) {
      this.labelPositionOffset = labelVerticalOffset;
      this.labelLineHeight = lineHeight;
      this.labelTopBorderLeftPos = lastCharacterOffsetLeft;
 
      this.el.nativeElement.style.setProperty(
        '--gc-label-position-offset',
        `${this.labelPositionOffset.toFixed()}px`,
      );
      this.el.nativeElement.style.setProperty(
        '--gc-label-line-height',
        `${this.labelLineHeight.toFixed()}px`,
      );
      this.el.nativeElement.style.setProperty(
        '--gc-label-top-border-left-pos',
        `${this.labelTopBorderLeftPos.toFixed()}px`,
      );
    }
    // We always return undefined as we don't want to execute
    // something in the zone
    return undefined;
  }
 
  /** @ignore */
  public isActive(): boolean {
    return !this.internal_disabled() && this.internal_active();
  }
 
  /** @ignore */
  public isError(): boolean {
    return !this.internal_disabled() && this.internal_error();
  }
 
  /** @ignore */
  public isReadonly(): boolean {
    return this.internal_readonly();
  }
 
  /** @ignore */
  public isDisabled(): boolean {
    return this.internal_disabled();
  }
 
  /** @ignore */
  public isRequired(): boolean {
    return this.internal_required();
  }
 
  /** @ignore */
  public isFocused(): boolean {
    return !this.internal_disabled() && this.internal_focused();
  }
 
  /** @ignore */
  isResizable(): boolean {
    return this.internal_resizable();
  }
 
  /** @ignore */
  public internal_active(): boolean {
    if (this.active != undefined) {
      return this.active;
    } else {
      return false;
    }
  }
 
  /** @ignore */
  public internal_error(): boolean {
    return this.error;
  }
 
  /** @ignore */
  public internal_readonly(): boolean {
    if (this.readonly != undefined) {
      return this.readonly;
    } else {
      return false;
    }
  }
 
  /** @ignore */
  public internal_required(): boolean {
    if (this.required != undefined) {
      return this.required;
    } else {
      return false;
    }
  }
 
  /** @ignore */
  public internal_disabled(): boolean {
    if (this.disabled != undefined) {
      return this.disabled;
    } else {
      return false;
    }
  }
 
  /** @ignore */
  public internal_focused(): boolean {
    if (this.focused != undefined) {
      return this.focused;
    } else {
      return false;
    }
  }
 
  /** @ignore */
  internal_resizable(): boolean {
    if (this.resizable != undefined) {
      return this.resizable;
    } else {
      return false;
    }
  }
 
  /** @ignore */
  public onBoxClick(event: MouseEvent): void {
    this.boxClick.emit(event);
  }
}