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

89.85% Statements 673/749
90.37% Branches 122/135
88.88% Functions 32/36
89.85% Lines 673/749

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 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 7501x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 830x 830x 830x 830x 830x 830x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2819x 2819x 2819x 2819x 2819x 2819x 2819x 2819x 2819x 329x 329x 329x 329x 329x 329x 329x 329x 2490x 2490x 2490x 2490x 2490x 2490x 2490x 2490x 2490x 2490x 2490x 204x 2490x 2490x 2490x 4x 2819x 2819x 2819x 1x 1x 57447x 57447x 1x 1x 1x 1x 1x 2284x 2284x 2284x 1x 1x     1x 1x 1x 1x 1x 2313x 2313x 2313x 1x 1x     1x 1x 1x 1x 1x 1125x 1125x 1125x 1x 1x 394x 394x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2307x 2307x 2307x 2307x 2307x 2307x 2307x 2307x 1x 1x 1x 1x 1x 208x 208x 208x 208x 208x 1x 1x 1x 3346x 3346x 3346x 2633x 3346x 3346x 1x 1x 1x 3346x 1154x 2192x 2192x 3346x 3346x 1x 1x 1x 1x 1x 537x 537x 537x 537x 537x 534x 535x 332x 332x 332x 332x 332x 332x 332x 332x 332x 332x 332x 332x 332x 109x 332x 332x 537x 537x 537x 1x 1x 1x 1x 1x 3248x 758x 758x 758x 2490x 2490x 2490x 2490x 3248x 3248x 3248x 1x 1x 5x 2x 2x 2x 2x 2x 1x 5x 5x 1x 1x             1x 1x 109x 109x 109x 109x 109x 6x 6x 6x 109x 109x 1x 1x 332x 332x 332x 332x 332x 332x 332x 223x 109x 109x 109x 109x 109x 109x 109x 109x 4x                   109x 109x 109x 6x                   109x 109x 109x 332x 332x 223x 223x 171x 171x 171x 171x 52x 52x 52x 52x 52x 52x 52x 52x 52x 332x 1x 1x 1x 1x 1x 1033x 1033x 1033x 1033x 1033x 1033x 1033x 1033x 1033x 1033x 1033x 1033x 1033x 1033x 830x 1033x 173x 657x 1033x 1033x 1033x 1033x 1033x 1033x 1033x 1033x 1033x 1033x 1033x 1033x 830x 1033x 1033x 1x 1x 1x 1x 1x 173x 173x 173x 173x 173x 173x 173x 173x 173x 173x 173x 173x 60x 60x 60x 36x 36x 24x 24x 24x     24x 24x 113x 113x   113x 113x 173x 173x 1x 1x 1x 1x 1x 830x 830x 830x 830x 830x 830x 830x 830x 830x 208x 208x 208x 208x 412x 144x 144x 12x 12x 12x 132x 132x 144x 144x 144x 144x 418x 9x 9x 9x 9x 499x 409x 409x 409x 409x 463x 36x 36x 36x 36x 54x                 529x 24x 24x 24x 24x 830x 830x 1x 1x 1x 208x 208x 208x 208x 208x 208x 208x 208x 6x 202x 202x 208x 208x 208x 208x       208x 208x 208x 208x 208x 208x 4x 4x     208x 208x 208x 1x 1x 1x 132x   132x 132x 132x 132x 1x 1x 1x 1x 1x 830x 830x 830x 830x 830x 830x 830x 830x 830x 830x 830x 830x 60x 60x 60x 60x 770x 770x 770x 545x               770x 770x 770x 12x 12x 12x 12x 12x 12x 758x 758x 758x 758x 758x 758x 758x 758x 239x 12x 12x 12x 12x 12x 12x 746x 746x 830x 830x 830x 830x 830x 1x 1x 1x 12x 12x 12x 12x 12x 12x 12x 12x 12x           12x   12x 12x 12x 12x 12x 1x 1x 1x                                     1x 1x 1x 60x 60x 60x 60x 60x 24x 24x 36x 36x 36x 60x 60x 1x 1x 1x 2635x 2635x 2635x 1740x 1310x 8x 8x 8x 2635x 2635x 1x  
import {
  ChangeDetectorRef,
  Component,
  ElementRef,
  EventEmitter,
  HostBinding,
  Input,
  NgZone,
  OnDestroy,
  Optional,
  Output,
  ViewChild,
} from '@angular/core';
 
import {
  observeElementOutsideAngular,
  observeElementSize,
  scrollIntoViewWithSpacingSmooth,
  waitForRender,
} from '../../utils/internal-utils';
import { Callback } from '../../utils/util.types';
 
import { ElementInsertionContextService } from '../element-insertion-context.service';
 
type StaticAlignment =
  | 'start' // Popup starts at anker left-position
  | 'end' // Popup end at anker right-position
  | 'fill' // Popup starts and anker left-position and ends at anker right-position
  | 'middle' // Middle of popup is aligned to left position of anker
  | 'right' // Popup starts at anker-right position
  | 'left' // Popup ends at anker-left position
  | 'max-right'; // Popup is aligned to the right position of window
 
export type Alignment =
  | StaticAlignment
  | 'auto' // Sets the alignment to start or end depending on the available space
  | 'submenu'; // Sets the alignment to right-inline, left-inline or max-right and the vertical alignment depending on the available space
 
export type VAlignment =
  | 'top' // Popup ends at the top-position of the anker
  | 'bottom' // Popup starts at the bottom-position of the anker
  | 'inline'; //Popup starts the at top-position of the anker
 
export type VerticalFlipComputer = (above: number, below: number) => boolean;
 
export type VerticalFlipStrategy = 'auto' | 'none' | VerticalFlipComputer;
 
export type OverlayLocation = `${StaticAlignment}-${VAlignment}`;
 
function toPosition(
  alignment: StaticAlignment,
  vAlignment: VAlignment,
): OverlayLocation {
  return (alignment + '-' + vAlignment) as OverlayLocation;
}
 
@Component({
  selector: 'gc-overlay-container',
  templateUrl: './overlay-container.component.html',
  styleUrls: ['./overlay-container.component.css'],
})
export class OverlayContainerComponent implements OnDestroy {
  @Input()
  public offset = 0;
 
  @Input()
  public margin = 0;
 
  @Output()
  public readonly onVisible: EventEmitter<void> = new EventEmitter<void>();
 
  @Output()
  public readonly onDetectHide: EventEmitter<void> = new EventEmitter<void>();
 
  @Output()
  public readonly onLocationChange: EventEmitter<OverlayLocation> =
    new EventEmitter<OverlayLocation>();
 
  @ViewChild('container')
  public containerElement?: ElementRef<HTMLDivElement>;
 
  @HostBinding('style.--gc-extended-scroll-container-height.px')
  public extendedScrollContainerHeight = 0;
 
  @HostBinding('style.--gc-extended-scroll-container-top.px')
  public extendedScrollContainerTop = 0;
 
  /**
   * Minimum space which is guaranteed to the overlay container to be available
   * below the `anker` (or `scrollTargetElement` if one is set)
   */
  @Input()
  public minBottomOffset = 0;
 
  /**
   * Decides how much the overlay container will be offset horizontally from the anker.
   * Supports positive and negative numbers.
   */
  @Input()
  public horizontalOffset = 0;
 
  /**
   * Decides if the overlay container is allowed to be "detached" from the anker
   * if not enough space is available to position it relative to the anker.
   * The overlay container is then positioned absolutely to the start of the document.
   */
  @Input()
  public unboundWhenConstrained = false;
 
  /**
   * When set to `menu` there is a different scroll strategy, when `minBottomOffset` is set and the page height gets automatically extended,
   * if there is not enough space to show the full menu
   */
  @Input()
  public responsiveStrategy: 'default' | 'menu' = 'default';
 
  @Input()
  public verticalFlipStrategy: VerticalFlipStrategy = 'auto';
 
  /**
   * Space left between the popup element and the viewPort bounding box
   */
  @Input()
  public viewPortOffset = 10;
 
  /**
   * Show the popup
   */
  @Input()
  public set visible(value: boolean) {
    const mutObserverOptions: MutationObserverInit = {
      attributes: true,
      characterData: true,
      subtree: true,
      childList: true,
    };
 
    if (value) {
      this._visible = value;
      window.addEventListener('scroll', this.scrollHandler, true);
      window.addEventListener('resize', this.resizeHandler);
      this.mutationObserver = observeElementOutsideAngular(
        document.documentElement,
        mutObserverOptions,
        this.zone,
        this.mutationObserverCallback.bind(this),
      );
    } else {
      this._positioned = false;
      this._top = 0;
      this._left = 0;
      this._visible = value;
      window.removeEventListener('scroll', this.scrollHandler, true);
      window.removeEventListener('resize', this.resizeHandler);
      this.updateMaxHeightCssProperty(undefined);
 
      if (this.mutationObserver) {
        this.mutationObserver();
      }
 
      if (this.extendedScrollContainerHeight > 0) {
        this.extendedScrollContainerHeight = 0;
      }
    }
  }
 
  public get visible(): boolean {
    return this._visible;
  }
 
  /**
   * The anker the popup is positioned relative to
   */
  @Input()
  public set anker(value: ElementRef<HTMLElement> | HTMLElement | undefined) {
    this._anker = value instanceof ElementRef ? value.nativeElement : value;
  }
 
  public get anker(): ElementRef<HTMLElement> | HTMLElement | undefined {
    return this._anker;
  }
 
  /**
   * To what edge of the element the item is aligned to
   */
  @Input()
  public set alignment(value: Alignment) {
    this._alignment = value;
  }
 
  public get alignment(): Alignment {
    return this._alignment;
  }
 
  /**
   * Define the node minBottomOffset is computed against, in case of undefined it it the anker
   */
  @Input()
  public set scrollTargetElement(value: ElementRef<HTMLElement> | undefined) {
    this._scrollTargetElement = value;
  }
 
  public get scrollTargetElement(): ElementRef<HTMLElement> | undefined {
    return this._scrollTargetElement;
  }
 
  /**
   * @ignore
   */
  public _top = 0;
 
  /**
   * @ignore
   */
  public _left = 0;
 
  /** @ignore */
  public _width?: number = undefined;
 
  /**
   * @ignore
   */
  public _positioned = false;
 
  /**
   * @ignore
   */
  private _visible = false;
 
  /**
   * @ignore
   */
  private _anker?: HTMLElement;
 
  /** @ignore */
  private _ankerPositionTop = 0;
 
  /**
   * @ignore
   */
  private _alignment: Alignment = 'start';
 
  private popupResizeObserverCleanup?: Callback;
 
  /**
   * @ignore
   */
  private _scrollTargetElement?: ElementRef<HTMLElement>;
 
  private scrollHandler = this.onscroll.bind(this);
 
  private resizeHandler = this.onresize.bind(this);
 
  private mutationObserver?: Callback;
 
  private parentElement: HTMLElement;
 
  constructor(
    private elementRef: ElementRef<HTMLElement>,
    private zone: NgZone,
    private cdr: ChangeDetectorRef,
    @Optional() eic: ElementInsertionContextService | null,
  ) {
    this.parentElement = eic?.parent ?? document.body;
    this.parentElement.appendChild(elementRef.nativeElement);
  }
 
  /**
   * @ignore
   */
  ngOnDestroy(): void {
    this.parentElement.removeChild(this.elementRef.nativeElement);
    window.removeEventListener('scroll', this.scrollHandler, true);
    window.removeEventListener('resize', this.resizeHandler);
    this.popupResizeObserverCleanup?.();
  }
 
  /** @ignore */
  public _leftVal(): string {
    const hOffset =
      this._alignment === 'end'
        ? this.horizontalOffset * -1
        : this.horizontalOffset;
    return Math.ceil(this._left + hOffset).toString() + 'px';
  }
 
  /** @ignore */
  public _widthVal(): string {
    if (this._width != undefined) {
      return Math.ceil(this._width).toString() + 'px';
    } else {
      return 'auto';
    }
  }
 
  /**
   * @ignore
   */
  public async _handleAttach() {
    this.popupResizeObserverCleanup?.();
    const popupNode = this.elementRef.nativeElement.firstElementChild;
    const el = this._anker;
 
    await waitForRender();
 
    if (popupNode && el) {
      this.popupResizeObserverCleanup = observeElementSize(
        popupNode,
        this.zone,
        () => this.updatePopupPosition(),
      );
 
      if (this.minBottomOffset >= 0) {
        await this.scrollIntoView(popupNode, el);
      }
      this._positioned = this.updatePopupPosition() !== undefined;
 
      if (this._positioned) {
        if (this.responsiveStrategy === 'menu') {
          this.resizeScrollContainer(popupNode);
        }
        this.onVisible.next();
      }
    }
  }
 
  /**
   * @ignore
   */
  public updateMaxHeightCssProperty(value: number | undefined) {
    if (value !== undefined) {
      this.elementRef.nativeElement.style.setProperty(
        '--gc-overlay-max-height',
        `${value.toFixed(0)}px`,
      );
    } else {
      this.elementRef.nativeElement.style.removeProperty(
        '--gc-overlay-max-height',
      );
    }
  }
 
  private onscroll(event: Event): void {
    if (!this._positioned) {
      return;
    }
    const target = event.target as HTMLElement;
 
    if (this._anker && target.contains(this._anker)) {
      this.updatePopupPosition();
    }
  }
 
  private onresize(): void {
    if (!this._positioned) {
      return;
    }
    this.updatePopupPosition();
    this.onDetectHide.next();
  }
 
  private resizeScrollContainer(popupNode: Element) {
    const totalPageHeight = document.documentElement.scrollHeight;
    const scrollTopHeight = document.documentElement.scrollTop;
    if (
      totalPageHeight - this._top - scrollTopHeight <
      popupNode.clientHeight
    ) {
      this.extendedScrollContainerTop = this._top + scrollTopHeight;
      this.extendedScrollContainerHeight = popupNode.clientHeight + 16;
    }
  }
 
  private async scrollIntoView(popupNode: Element, anker: HTMLElement) {
    let bottomOffset = 0;
    let verticalAlignment = undefined;
 
    const prefHeight = Math.ceil(popupNode.getBoundingClientRect().height);
 
    if (this.responsiveStrategy === 'default') {
      bottomOffset =
        Math.min(this.minBottomOffset, prefHeight) + this.viewPortOffset;
    } else {
      const pageHeight = document.documentElement.clientHeight;
      /**
       * updatePopupPosition() is needed, because menu needs its correct top position and the vertical alignment to calculate scroll amount
       */
      verticalAlignment = this.updatePopupPosition();
      if (
        verticalAlignment === 'top' &&
        this._top < 0 &&
        this._top + prefHeight < this.minBottomOffset
      ) {
        bottomOffset = Math.max(
          this.viewPortOffset,
          pageHeight -
            (prefHeight +
              anker.getBoundingClientRect().height +
              this.offset +
              this.viewPortOffset),
        );
      } else if (
        pageHeight - this._top < prefHeight &&
        pageHeight - this._top < this.minBottomOffset
      ) {
        bottomOffset = Math.min(
          pageHeight -
            anker.getBoundingClientRect().height -
            this.viewPortOffset,
          prefHeight -
            anker.getBoundingClientRect().bottom +
            this._top +
            this.viewPortOffset,
        );
      }
    }
 
    if (bottomOffset > 0) {
      const negativeScroll = verticalAlignment === 'top';
      if (this.scrollTargetElement) {
        await scrollIntoViewWithSpacingSmooth(
          this.scrollTargetElement.nativeElement,
          bottomOffset,
          negativeScroll,
        );
      } else {
        await scrollIntoViewWithSpacingSmooth(
          anker,
          bottomOffset,
          negativeScroll,
        );
      }
    }
  }
 
  /**
   * @ignore
   */
  private updatePopupPosition(): VAlignment | undefined {
    const ankerNode = this._anker;
 
    if (ankerNode === undefined) {
      return undefined;
    }
 
    const ankerBounds = ankerNode.getBoundingClientRect();
    const popupNodes = this.elementRef.nativeElement.children;
 
    if (popupNodes.length === 0) {
      return undefined;
    }
 
    const popupNode = popupNodes[0] as HTMLElement;
    const staticAlignment: StaticAlignment =
      this._alignment === 'auto' || this._alignment === 'submenu'
        ? this.autoPosition(ankerBounds, popupNode)
        : this._alignment;
    const verticalPosition = this.computeVerticalAlignment(
      ankerBounds,
      popupNode,
      staticAlignment,
    );
 
    this._ankerPositionTop = ankerNode.offsetTop;
    const positioned = this.computeAlignment(
      ankerBounds,
      popupNode,
      verticalPosition,
      staticAlignment,
    );
    return positioned ? verticalPosition : undefined;
  }
 
  /**
   * @ignore
   */
  private autoPosition(
    ankerBounds: DOMRect,
    popupNode: HTMLElement,
  ): StaticAlignment {
    const popupWidth = popupNode.clientWidth + this.margin;
    const documentRightEdge =
      document.documentElement.getBoundingClientRect().right;
    const ankerLeftEdge = ankerBounds.left;
    const innerMargin = 4;
    const outerMargin = 16;
    const submenuMinWidth = 140;
 
    if (this._alignment === 'submenu') {
      if (
        documentRightEdge - ankerBounds.right - innerMargin >
        submenuMinWidth + outerMargin
      ) {
        return 'right';
      } else if (
        ankerBounds.left + innerMargin >
        submenuMinWidth + outerMargin
      ) {
        return 'left';
      } else {
        return 'max-right';
      }
    } else if (popupWidth + ankerLeftEdge > documentRightEdge) {
      return 'end';
    } else {
      return 'start';
    }
  }
 
  /**
   * @ignore
   */
  private computeAlignment(
    ankerBounds: DOMRect,
    popupNode: HTMLElement,
    verticalPosition: VAlignment,
    alignment: StaticAlignment,
  ): boolean {
    const popupWidth = popupNode.clientWidth;
 
    switch (alignment) {
      case 'end':
        this.calculateLeftForEndAlignment(ankerBounds, popupNode, popupWidth);
        this._width = undefined;
        this.onLocationChange.emit(toPosition(alignment, verticalPosition));
        return true;
      case 'start':
        if (
          this.unboundWhenConstrained &&
          window.innerWidth - ankerBounds.right >= popupWidth
        ) {
          this._left = ankerBounds.left;
        } else {
          this.computeStartAlignment(ankerBounds);
        }
        this._width = undefined;
        this.onLocationChange.emit(toPosition(alignment, verticalPosition));
        return true;
      case 'middle':
        this._left = ankerBounds.left - popupWidth / 2;
        this._width = undefined;
        this.onLocationChange.emit(toPosition(alignment, verticalPosition));
        return true;
      case 'fill':
        this._left = ankerBounds.left;
        this._width = ankerBounds.width;
        this.onLocationChange.emit(toPosition(alignment, verticalPosition));
        return true;
      case 'right':
        this._left = ankerBounds.left + ankerBounds.width;
        this._width = undefined;
        this.onLocationChange.emit(toPosition(alignment, verticalPosition));
        return true;
      case 'left':
        this._left = ankerBounds.left - popupNode.clientWidth;
        this._width = undefined;
        if (this._left <= 0) {
          this._left = 0;
          this._width = ankerBounds.left;
        }
        this.onLocationChange.emit(toPosition(alignment, verticalPosition));
        return true;
      case 'max-right':
        this._left = window.innerWidth - popupNode.clientWidth;
        this._width = undefined;
        this.onLocationChange.emit(toPosition(alignment, verticalPosition));
        return true;
    }
  }
 
  /** @ignore */
  private calculateLeftForEndAlignment(
    ankerBounds: DOMRect,
    popupNode: HTMLElement,
    popupWidth: number,
  ): void {
    const innerMargin = 4;
 
    const calculateLeft = () => {
      if (ankerBounds.right + innerMargin > window.innerWidth) {
        this._left = window.innerWidth - popupWidth - innerMargin;
      } else {
        this._left = ankerBounds.left + ankerBounds.width - popupWidth;
      }
    };
 
    if (this.unboundWhenConstrained) {
      // do only if there is not enough space
      if (window.innerWidth < popupNode.offsetLeft + popupNode.clientWidth) {
        calculateLeft();
      }
    } else {
      calculateLeft();
    }
 
    if (this._left < 0) {
      if (window.innerWidth >= popupNode.offsetLeft + popupNode.clientWidth) {
        this._left = this.horizontalOffset + this.margin;
      } else {
        this._left = this.margin / 2;
      }
    }
  }
 
  /** @ignore */
  private computeStartAlignment(ankerBounds: DOMRect) {
    if (this.unboundWhenConstrained) {
      this._left = this.horizontalOffset + this.margin;
    } else {
      this._left = ankerBounds.left;
    }
  }
 
  /**
   * @ignore
   */
  private computeVerticalAlignment(
    ankerBounds: DOMRect,
    popupNode: HTMLElement,
    staticAlignment: StaticAlignment,
  ): VAlignment {
    let verticalPosition: VAlignment = 'bottom';
    this._top = this.offset + ankerBounds.top + ankerBounds.height;
 
    const below =
      document.documentElement.clientHeight - this._top - this.margin;
    const above = this._top + this.margin;
 
    if (this._alignment === 'submenu') {
      verticalPosition = this.computeVerticalAlignmentForSubmenu(
        staticAlignment,
        ankerBounds,
        verticalPosition,
      );
    } else if (
      this.verticalFlipStrategy !== 'auto' &&
      this.verticalFlipStrategy !== 'none'
    ) {
      verticalPosition = this.computeVerticalAlignmentUsingFlipComputer(
        above,
        below,
        verticalPosition,
        ankerBounds,
        popupNode,
      );
    } else {
      if (this.unboundWhenConstrained) {
        verticalPosition = this.computeVerticalAlignmentWhenUnbound(
          popupNode,
          below,
          above,
          ankerBounds,
          verticalPosition,
        );
      } else {
        /**
         * There must be enough space available on top of the anker, because the page is only allowed to extend to the bottom if needed
         * And there should be some space between the top of the current window and the anker to ensure scrollIntoView() is working correctly
         */
        if (
          this.verticalFlipStrategy === 'auto' &&
          popupNode.clientHeight > below &&
          above + document.documentElement.scrollTop > popupNode.clientHeight &&
          ankerBounds.top > this.viewPortOffset
        ) {
          verticalPosition = 'top';
          this._top = ankerBounds.top - popupNode.clientHeight - this.offset;
          this.updateMaxHeightCssProperty(above);
        } else {
          this.updateMaxHeightCssProperty(below);
        }
      }
    }
    return verticalPosition;
  }
 
  /** @ignore */
  private computeVerticalAlignmentWhenUnbound(
    popupNode: HTMLElement,
    below: number,
    above: number,
    ankerBounds: DOMRect,
    verticalPosition: VAlignment,
  ): VAlignment {
    if (
      this.verticalFlipStrategy === 'auto' &&
      popupNode.clientHeight > below &&
      above + document.documentElement.scrollTop > popupNode.clientHeight &&
      ankerBounds.top > this.viewPortOffset
    ) {
      this._top = ankerBounds.top - popupNode.clientHeight - this.offset;
      return 'top';
    } else if (popupNode.clientHeight > below) {
      this._top = this.margin;
      return verticalPosition;
    } else {
      return verticalPosition;
    }
  }
 
  /** @ignore */
  private computeVerticalAlignmentUsingFlipComputer(
    above: number,
    below: number,
    verticalPosition: VAlignment,
    ankerBounds: DOMRect,
    popupNode: HTMLElement,
  ): VAlignment {
    if (
      typeof this.verticalFlipStrategy !== 'string' &&
      this.verticalFlipStrategy(above, below)
    ) {
      this._top = ankerBounds.top - popupNode.clientHeight;
      this.updateMaxHeightCssProperty(above);
      return 'top';
    } else {
      this.updateMaxHeightCssProperty(below);
      return verticalPosition;
    }
  }
 
  /** @ignore */
  private computeVerticalAlignmentForSubmenu(
    staticAlignment: string,
    ankerBounds: DOMRect,
    verticalPosition: VAlignment,
  ): VAlignment {
    if (staticAlignment === 'max-right') {
      this._top = ankerBounds.top + ankerBounds.height;
      return verticalPosition;
    } else {
      this._top = ankerBounds.top;
      return 'inline';
    }
  }
 
  /** @ignore */
  private mutationObserverCallback() {
    // only update popup position if anker element has moved from previous position
    if (
      this._positioned &&
      this._ankerPositionTop !== 0 &&
      this._anker?.offsetTop !== this._ankerPositionTop
    ) {
      this.updatePopupPosition();
      this.cdr.detectChanges();
    }
  }
}