All files / lib/selection-footer selection-footer.component.ts

96.3% Statements 365/379
93.44% Branches 57/61
85.71% Functions 12/14
96.3% Lines 365/379

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 3801x 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 22x 4x 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 24x 24x 3x 3x 3x 20x 18x 24x 1x 1x 1x 1x 1x 1x 1x 24x 6x 1x 1x 1x 1x 1x 1x 19x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 19x 19x 1x 1x 1x 1x 1x 1x 19x 1x 1x 1x 1x 76x 76x 76x 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 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x     18x 18x 18x 20x 20x 20x 20x   20x 20x 20x 20x 20x 20x 18x 18x 18x 22x 22x 18x 18x 4x 18x 22x 18x 18x 18x 24x 3x 3x 3x 3x 3x 24x 18x 18x 1x 1x 1x             1x 1x 1x 1515x 1515x 1x 1x 1x     1x 1x 1x 1510x 1510x 1x 1x 1x 90x 90x 1x 1x 1x 60x 2x 2x 2x 2x 2x 60x 60x 58x 58x 58x 58x 58x 58x 60x 252x 252x 252x 252x       252x 252x 252x 252x 237x 237x 15x 15x 252x 252x 60x 58x 58x 60x 1x 1x 1x 58x 58x 58x 58x 58x 1x 1x 1x 18x 18x 18x 18x 18x 18x 19x 19x 19x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 1x 1x 1x 4x 4x 4x 4x 4x 1x  
/*******************************************************************************
 * Copyright bei
 * Entwicklungs- und Pflegeverbund für das gemeinsame Fachverfahren gefa
 *
 *******************************************************************************/
import {
  ChangeDetectionStrategy,
  Component,
  DestroyRef,
  ElementRef,
  NgZone,
  computed,
  effect,
  inject,
  input,
  output,
  signal,
  untracked,
  viewChild,
  viewChildren,
} from '@angular/core';
 
import { observeElementSize } from '../utils/internal-utils';
import { Callback, Item } from '../utils/util.types';
import { SoundType, itemTrack, playSound } from '../utils/utilities';
 
import { ButtonContentDisplay } from '../button/button.component';
 
/**
 * Types of the status which the selection footer can have.
 */
export type SelectionFooterStatus = 'success' | 'in-progress' | 'default';
 
const DESELECT_BUTTON_TEXT = 'Auswahl aufheben';
 
/**
 * `gc-selection-footer`  is control tool, which can be used to manage actions in tables for selected items.
 */
@Component({
  selector: 'gc-selection-footer',
  templateUrl: './selection-footer.component.html',
  styleUrls: [
    '../utils/styles/reset-list.css',
    './selection-footer.component.css',
  ],
  host: {
    '[class.gc-not-in-viewport]': 'isNotInViewPort()',
    '[class.gc-in-viewport]': 'isInViewPort()',
  },
  changeDetection: ChangeDetectionStrategy.Default,
  standalone: false,
})
export class SelectionFooterComponent {
  /**
   * Reference to the parent element, on which this component sticks or float.
   */
  public readonly observeForSticky = input<
    ElementRef<HTMLElement> | undefined,
    ElementRef<HTMLElement> | HTMLElement | undefined
  >(undefined, {
    transform: owner =>
      owner === undefined || 'nativeElement' in owner
        ? owner
        : new ElementRef(owner),
  });
 
  /**
   * Number of selected entries.
   */
  public readonly selectedEntries = input(0);
 
  /**
   * Actions shown in the selection footer.
   */
  public readonly actions = input<readonly Item[]>([]);
 
  /**
   * Keys of all disabled actions. If `selectedEntries` is zero, all actions disabled automatically.
   */
  public readonly disabledActionKeys = input<
    ReadonlySet<string>,
    ReadonlySet<string> | undefined
  >(new Set<string>(), {
    transform: value => value ?? new Set<string>(),
  });
 
  /**
   * Current status of the form.
   */
  public readonly status = input<SelectionFooterStatus>('default');
 
  /**
   * Text that can be used as a descriptive message for the default status.
   */
  public readonly statusTextDefault = input('');
 
  /**
   * Text that can be used as a descriptive message for the success status.
   */
  public readonly statusTextSuccess = input('');
 
  /**
   * Text that can be used as a descriptive message for the in-progress status.
   */
  public readonly statusTextInProgress = input('');
 
  /**
   * Key of the primary action.
   */
  public readonly primaryActionKey = input('');
 
  /**
   * Event emitted when an item is selected.
   */
  public readonly onItemAction = output<Item>();
 
  /**
   * Event emitted when items are deselected.
   */
  public readonly onDeselectAction = output<Item | undefined>();
 
  /** @ignore */
  protected readonly statusText = computed(() => {
    switch (this.status()) {
      case 'in-progress':
        return this.statusTextInProgress();
      case 'success':
        return this.statusTextSuccess();
      case 'default':
        return this.statusTextDefault();
    }
  });
 
  /**
   * ButtonContentDisplay type for the primary action button to show status icon
   */
  protected readonly primaryActionButtonContentDisplay =
    computed<ButtonContentDisplay>(() =>
      this.status() === 'success' || this.status() === 'in-progress'
        ? 'status'
        : 'text-only',
    );
 
  /** ignore */
  protected readonly isNotInViewPort = computed<boolean>(
    () =>
      !this.isParentElementInViewport() &&
      this.observeForSticky() !== undefined,
  );
 
  /** ignore */
  protected readonly isInViewPort = computed<boolean>(
    () =>
      this.isParentElementInViewport() && this.observeForSticky() !== undefined,
  );
 
  /** @ignore */
  protected readonly menuItems = computed<readonly Item[]>(() => {
    let items: readonly Item[] = this.actions().flat().reverse();
    if (!this.isDeselectButtonVisible()) {
      const deselectItem: Item = {
        key: this.actions().length.toString(),
        label: this.deselectText,
      };
      items = items.concat(deselectItem);
    }
    return items;
  });
 
  /** @ignore */
  protected readonly notVisibleMenuItems = computed<Item[]>(() => {
    return this.actions()
      .filter(i => !this.isItemVisible(i))
      .reverse();
  });
 
  /** @ignore */
  protected readonly visibleActions = signal<ReadonlySet<string>>(new Set());
 
  /** @ignore */
  protected readonly isDeselectButtonWithText = signal(true);
 
  /** @ignore */
  protected readonly isDeselectButtonVisible = signal(true);
 
  /** @ignore */
  protected readonly isParentElementInViewport = signal(true);
 
  /** @ignore */
  protected readonly notVisibleActionKeys = computed<ReadonlySet<string>>(
    () => {
      return new Set(this.notVisibleMenuItems().map(nva => nva.key));
    },
  );
 
  /** @ignore */
  protected readonly deselectText = DESELECT_BUTTON_TEXT;
 
  /** @ignore */
  protected readonly actionsText = 'Aktionen';
 
  /** @ignore */
  protected readonly menuText = 'Weitere Aktionen';
 
  /** @ignore */
  private readonly previousActionsLength = computed(
    () => this.actions().length,
  );
 
  /** @ignore */
  private readonly renderedItems =
    viewChildren<ElementRef<HTMLElement>>('actionItem');
 
  /** @ignore */
  private readonly menuItem =
    viewChild.required<ElementRef<HTMLElement>>('collapsedMenuItem');
 
  /** @ignore */
  private intersectionObserver: IntersectionObserver | undefined;
 
  /** @ignore */
  private resizeObserver: Callback;
 
  constructor(
    private el: ElementRef<HTMLElement>,
    zone: NgZone,
  ) {
    this.resizeObserver = observeElementSize(
      el.nativeElement,
      zone,
      this.computeVisibleItems.bind(this),
    );
 
    inject(DestroyRef).onDestroy(() => {
      this.resizeObserver();
      this.destroyScrollingObserver();
    });
 
    effect(() => {
      const currentActions = this.actions();
 
      untracked(() => {
        if (this.previousActionsLength() === this.visibleActions().size) {
          this.visibleActions.set(new Set(currentActions.map(i => i.key)));
        }
 
        setTimeout(() => {
          this.computeVisibleItems();
        });
      });
    });
 
    effect(onCleanup => {
      const observeForSticky = this.observeForSticky();
      if (observeForSticky !== undefined) {
        this.createScrollingObserver();
        onCleanup(() => {
          this.destroyScrollingObserver();
        });
      }
    });
 
    effect(() => {
      if (this.status() === 'success') {
        void playSound(SoundType.SUCCESS, [
          'SELECTION_FOOTER',
          'STATE_SUCCESS',
          'SUCCESS',
        ]);
      }
    });
  }
 
  /** @ignore */
  protected onMenuAction(item: Item): void {
    if (item.label === this.deselectText) {
      this.onDeselectAction.emit(item);
    } else {
      this.onItemAction.emit(item);
    }
  }
 
  /** @ignore */
  protected isItemVisible(item: Item): boolean {
    return this.visibleActions().has(item.key);
  }
 
  /** @ignore */
  protected isLastItem(item: Item): boolean {
    return this.actions()[this.actions().length - 1]?.key === item.key;
  }
 
  /** @ignore */
  protected isPrimaryButton(item: Item): boolean {
    return this.primaryActionKey() === item.key;
  }
 
  /** @ignore */
  protected _itemTrackBy(item: Item): string {
    return itemTrack(item);
  }
 
  /** @ignore */
  private computeVisibleItems(): void {
    if (this.actions().length === 0 || this.renderedItems().length === 0) {
      this.visibleActions.set(new Set());
      this.isDeselectButtonVisible.set(true);
      this.isDeselectButtonWithText.set(true);
      return;
    }
 
    const hostBounds = this.el.nativeElement.getBoundingClientRect();
    const collapsedMenuItemWidth =
      this.menuItem().nativeElement.getBoundingClientRect().width;
    let availableWidth = hostBounds.width - 280;
 
    const visibleItems: Set<string> = new Set();
 
    for (let i = this.actions().length - 1; i >= 0 && availableWidth > 0; i--) {
      const itemWidth = this.renderedItems()
        .at(i)
        ?.nativeElement.getBoundingClientRect().width;
      if (itemWidth === undefined) {
        console.warn('failed to find rendered item for item index', i);
        break;
      }
 
      const requiredWidth = itemWidth + (i > 0 ? collapsedMenuItemWidth : 0);
 
      if (availableWidth >= requiredWidth) {
        visibleItems.add(this.actions()[i].key);
        availableWidth -= requiredWidth;
      } else {
        availableWidth = 0;
      }
    }
    this.visibleActions.set(visibleItems);
 
    this.calculateDeselectVisibility(hostBounds.width);
  }
 
  /** @ignore */
  private calculateDeselectVisibility(hostBoundsWidth: number) {
    if (this.actions().length > 0 && this.renderedItems().length > 0) {
      this.isDeselectButtonWithText.set(hostBoundsWidth >= 470);
      this.isDeselectButtonVisible.set(hostBoundsWidth >= 340);
    }
  }
 
  /** @ignore */
  private createScrollingObserver() {
    if (!this.intersectionObserver) {
      const options = {
        rootMargin: '0px',
        threshold: 0,
      };
      this.intersectionObserver = new IntersectionObserver(entries => {
        this.isParentElementInViewport.set(
          entries.some(entry => entry.isIntersecting),
        );
      }, options);
    } else {
      this.intersectionObserver.disconnect();
    }
 
    const observeForSticky = this.observeForSticky();
    if (observeForSticky) {
      this.intersectionObserver.observe(observeForSticky.nativeElement);
    }
  }
 
  /** @ignore */
  private destroyScrollingObserver() {
    if (this.intersectionObserver) {
      this.intersectionObserver.disconnect();
      this.intersectionObserver = undefined;
    }
  }
}