All files / common/helpers overlay-helper.ts

92.3% Statements 36/39
64.51% Branches 20/31
100% Functions 7/7
92.3% Lines 36/39

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                                                                18x       18x 18x             18x     18x               18x 18x 18x 18x 18x 2x 2x   16x   18x                     17x 17x           17x               56x 18x 18x 18x 18x   38x 38x 38x 38x   56x       7x                                   18x   5x 5x 5x       5x         13x 13x           13x    
/**
 * Copyright (c) Siemens 2016 - 2025
 * SPDX-License-Identifier: MIT
 */
/* eslint-disable prefer-arrow/prefer-arrow-functions */
import {
  ConnectedOverlayPositionChange,
  ConnectionPositionPair,
  FlexibleConnectedPositionStrategy,
  Overlay,
  OverlayConfig,
  OverlayRef,
  PositionStrategy
} from '@angular/cdk/overlay';
import { ElementRef } from '@angular/core';
 
import {
  positionBottomEnd,
  positionBottomStart,
  positions,
  positionTopEnd,
  positionTopStart
} from '../models/positions.model';
import { isRTL } from './rtl';
 
export function makePositionStrategy(
  elementRef: ElementRef<any> | undefined,
  overlay: Overlay,
  placement: keyof typeof positions | ConnectionPositionPair[],
  constrain = false,
  center = true
): PositionStrategy {
  Iif (!elementRef?.nativeElement) {
    return overlay.position().global().centerHorizontally().centerVertically();
  }
 
  const popoverPositions = getOverlayPositions(elementRef, placement, center);
  const positionStrategy = overlay
    .position()
    .flexibleConnectedTo(elementRef)
    .withPush(false)
    .withGrowAfterOpen(true)
    .withFlexibleDimensions(constrain)
    .withPositions(popoverPositions);
  Iif (constrain) {
    positionStrategy.withViewportMargin(8);
  }
  return positionStrategy;
}
 
export function makeOverlay(
  positionStrategy: PositionStrategy,
  overlay: Overlay,
  hasBackdrop: boolean
): OverlayRef {
  const config = new OverlayConfig();
  config.positionStrategy = positionStrategy;
  config.scrollStrategy = overlay.scrollStrategies.reposition();
  config.direction = isRTL() ? 'rtl' : 'ltr';
  if (hasBackdrop) {
    config.hasBackdrop = true;
    config.backdropClass = 'cdk-overlay-transparent-backdrop';
  } else {
    config.hasBackdrop = false;
  }
  return overlay.create(config);
}
 
export function getOverlay(
  elementRef: ElementRef<any>,
  overlay: Overlay,
  hasBackdrop: boolean,
  placement: keyof typeof positions | ConnectionPositionPair[],
  constrain = false,
  center = true
): OverlayRef {
  const positionStrategy = makePositionStrategy(elementRef, overlay, placement, constrain, center);
  return makeOverlay(positionStrategy, overlay, hasBackdrop);
}
 
export function getPositionStrategy(
  overlayref: OverlayRef
): FlexibleConnectedPositionStrategy | undefined {
  return overlayref.getConfig().positionStrategy as FlexibleConnectedPositionStrategy;
}
 
export function getOverlayPositions(
  elementRef: ElementRef<any>,
  placement: keyof typeof positions | ConnectionPositionPair[],
  center = true
): ConnectionPositionPair[] {
  if (elementRef.nativeElement && center) {
    const rtl = isRTL();
    const halfWidth = Math.round(elementRef.nativeElement.offsetWidth / 2);
    positionTopStart.offsetX = positionBottomStart.offsetX = halfWidth * (rtl ? 1 : -1);
    positionTopEnd.offsetX = positionBottomEnd.offsetX = halfWidth * (rtl ? -1 : 1);
  } else {
    positionTopStart.offsetX = undefined;
    positionTopEnd.offsetX = undefined;
    positionBottomStart.offsetX = undefined;
    positionBottomEnd.offsetX = undefined;
  }
  return typeof placement === 'string' ? positions[placement] : placement;
}
 
export function hasTrigger(trigger: string, triggers?: string): boolean {
  return (triggers?.split(/\s+/) ?? []).includes(trigger);
}
 
export interface OverlayArrowPosition {
  left?: number;
  right?: number;
}
/**
 * calculates the arrow position from left/right for tooltips, popovers, etc
 * @param change - the event from the position strategy
 * @param overlay - ElementRef for the overlay content, i.e. the popover/tooltip component
 * @param anchor - ElementRef for the anchoring element, i.e. the trigger of the popover/tooltip
 */
export function calculateOverlayArrowPosition(
  change: ConnectedOverlayPositionChange,
  overlay: ElementRef,
  anchor?: ElementRef
): OverlayArrowPosition {
  if (anchor && ['bottom', 'top'].includes(change.connectionPair.originY)) {
    // Calculate offset to the anchor element center
    const anchorRect = anchor.nativeElement.getBoundingClientRect();
    const overlayRect = overlay.nativeElement.getBoundingClientRect();
    const center = anchorRect.left + anchorRect.width / 2;
    // Position arrow centered to the anchor element
 
    // prettier-ignore
    return isRTL()
      ? { right: overlayRect.right - center }
      : { left: center - overlayRect.left };
  }
 
  const offsetX = change.connectionPair.offsetX;
  Iif (offsetX) {
    // prettier-ignore
    return offsetX < 0
      ? { left: -offsetX }
      : { right: offsetX };
  }
  return {};
}