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

92.73% Statements 332/358
92.3% Branches 48/52
83.33% Functions 20/24
92.73% Lines 332/358

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 3591x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 20x 20x 20x 20x 20x 4x 16x 20x 1x 1x     1x 1x 1x 1x 1x 1145x 1145x 1145x 1x 1x 18x 18x 18x 18x 18x 18x 18x 18x 18x 18x 1x 1x 1x 1x 1x 1129x 1129x 1129x   1129x 1x 1x 1x 16x 16x 1x 1x 1x 192x 192x 192x 192x 192x 1x 1x 1x 192x 192x 192x 192x 192x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 16x 16x 16x 16x 16x 16x 16x 16x 16x 1x 1x 1x 1x 1x       1x 1x 1x 23x 20x 16x 4x 4x 23x 23x 23x 1x 1x 1x             1x 1x 1x 1880x 1880x 1x 1x 1x     1x 1x 1x 2061x 2061x 1x 1x 1x 1x 1x 7x 7x 1x 1x 1x 1x 1x 1x 7x 7x 1x 1x 1x 1x 1x 188x 188x 188x 188x 188x 188x 1x 1x 1x 1x 1x 56x 56x 56x 56x 56x 56x 56x 56x 54x 54x 54x 54x 54x 54x 56x 232x 232x 232x 232x       232x 232x 232x 232x 217x 217x 15x 15x 232x 232x 56x 54x 54x 56x 1x 1x 1x 1x 1x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 54x 1x 1x 1x 1x 1x 16x           17x 17x         16x 16x 16x 16x 16x 16x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 1x  
/*******************************************************************************
 * Copyright bei
 * Entwicklungs- und Pflegeverbund für das gemeinsame Fachverfahren gefa
 *
 *******************************************************************************/
import {
  Component,
  ElementRef,
  EventEmitter,
  HostBinding,
  Input,
  NgZone,
  OnChanges,
  OnDestroy,
  Output,
  QueryList,
  SimpleChanges,
  ViewChild,
  ViewChildren,
} from '@angular/core';
 
import { observeElementSize } from '../utils/internal-utils';
 
import type { Callback, Item } from '../../public-api';
 
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',
  ],
})
export class SelectionFooterComponent implements OnDestroy, OnChanges {
  /**
   * Number of selected entries.
   */
  @Input()
  public selectedEntries = 0;
 
  /**
   * Event emitted when an item is selected.
   */
  @Output()
  public readonly onItemAction: EventEmitter<Item> = new EventEmitter<Item>();
 
  /**
   * Key of the primary action.
   */
  @Input()
  public primaryActionKey = '';
 
  /**
   * Event emitted when items are deselected.
   */
  @Output()
  public readonly onDeselectAction: EventEmitter<Item> =
    new EventEmitter<Item>();
 
  /** @ignore */
  @ViewChildren('actionItem')
  private renderedItems?: QueryList<ElementRef<HTMLElement>>;
 
  /** @ignore */
  @ViewChild('collapsedMenuItem')
  private menuItem!: ElementRef<HTMLElement>;
 
  /**
   * Reference to the parent element, on which this component sticks or float.
   */
  @Input()
  public set observeForSticky(
    owner: ElementRef<HTMLElement> | HTMLElement | undefined,
  ) {
    this._observeForSticky =
      owner === undefined || 'nativeElement' in owner
        ? owner
        : new ElementRef(owner);
  }
 
  public get observeForSticky(): ElementRef<HTMLElement> | undefined {
    return this._observeForSticky;
  }
 
  /**
   * Actions shown in the selection footer.
   */
  @Input()
  public get actions(): readonly Item[] {
    return this._actions;
  }
 
  public set actions(actions: readonly Item[]) {
    if (this._actions.length === this.visibleActions.size) {
      this.visibleActions = new Set(actions.map(i => i.key));
    }
 
    this._actions = actions;
 
    setTimeout(() => {
      this._computeVisibleItems();
    });
  }
 
  /**
   * Keys of all disabled actions. If `selectedEntries` is zero, all actions disabled automatically.
   */
  @Input()
  public get disabledActionKeys(): ReadonlySet<string> {
    return this._disabledActionKeys
      ? this._disabledActionKeys
      : new Set<string>();
  }
 
  /** @ignore */
  public set disabledActionKeys(value: ReadonlySet<string> | undefined) {
    this._disabledActionKeys = value ?? new Set<string>();
  }
 
  /** @ignore */
  @HostBinding('class.gc-not-in-viewport')
  protected get isNotInViewPort(): boolean {
    return (
      !this.isParentElementInViewport && this._observeForSticky !== undefined
    );
  }
 
  /** @ignore */
  @HostBinding('class.gc-in-viewport')
  protected get isInViewPort(): boolean {
    return (
      this.isParentElementInViewport && this._observeForSticky !== undefined
    );
  }
 
  /** @ignore */
  protected visibleActions: ReadonlySet<string> = new Set();
 
  /** @ignore */
  protected notVisibleActionKeys: ReadonlySet<string> = new Set();
 
  /** @ignore */
  protected IsDeselectButtonWithText = true;
 
  /** @ignore */
  protected isDeselectButtonVisible = true;
 
  /** @ignore */
  protected deselectText = DESELECT_BUTTON_TEXT;
 
  /** @ignore */
  protected isParentElementInViewport = true;
 
  /** @ignore */
  protected actionsText = 'Aktionen';
 
  /** @ignore */
  protected menuText = 'Weitere Aktionen';
 
  /** @ignore */
  private _observeForSticky?: ElementRef<HTMLElement>;
 
  /** @ignore */
  private _actions: readonly Item[] = [];
 
  /**
   * @ignore
   */
  private _resizeObserver: Callback;
 
  /**
   * @ignore
   */
  private _intersectionObserver: IntersectionObserver | undefined;
 
  /**
   * @ignore
   */
  private _disabledActionKeys?: ReadonlySet<string> = new Set<string>();
 
  constructor(
    private el: ElementRef<HTMLElement>,
    zone: NgZone,
  ) {
    this._resizeObserver = observeElementSize(
      el.nativeElement,
      zone,
      this._computeVisibleItems.bind(this),
    );
  }
 
  /**
   * @ignore
   */
  ngOnDestroy(): void {
    this._resizeObserver();
    this._destroyScrollingObserver();
  }
 
  /** @ignore */
  ngOnChanges(changes: SimpleChanges) {
    if ('observeForSticky' in changes) {
      if (changes.observeForSticky.currentValue !== undefined) {
        this._createScrollingObserver();
      } else {
        this._destroyScrollingObserver();
      }
    }
  }
 
  /** @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 getMenuItems(): 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 getNotVisibleMenuItems(): Item[] {
    const notVisibleActions = this.actions
      .filter(i => !this.isItemVisible(i))
      .reverse();
    this.notVisibleActionKeys = new Set(notVisibleActions.map(nva => nva.key));
    return notVisibleActions;
  }
 
  /**
   * @ignore
   */
  private _computeVisibleItems(): void {
    if (this.actions.length === 0 || this.renderedItems === undefined) {
      this.visibleActions = new Set();
      this.isDeselectButtonVisible = true;
      this.IsDeselectButtonWithText = 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
        .get(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 = visibleItems;
 
    this._calculateDeselectVisibility(hostBounds.width);
  }
 
  /**
   * @ignore
   */
  private _calculateDeselectVisibility(hostBoundsWidth: number) {
    if (this.actions.length > 0 && this.renderedItems !== undefined) {
      if (hostBoundsWidth < 470) {
        this.IsDeselectButtonWithText = false;
      } else {
        this.IsDeselectButtonWithText = true;
      }
 
      if (hostBoundsWidth < 340) {
        this.isDeselectButtonVisible = false;
      } else {
        this.isDeselectButtonVisible = true;
      }
    }
  }
 
  /**
   * @ignore
   */
  private _createScrollingObserver() {
    if (!this._intersectionObserver) {
      const options = {
        rootMargin: '0px',
        threshold: 0,
      };
      this._intersectionObserver = new IntersectionObserver(entries => {
        this.isParentElementInViewport = entries.some(
          entry => entry.isIntersecting,
        );
      }, options);
    } else {
      this._intersectionObserver.disconnect();
    }
 
    if (this._observeForSticky) {
      this._intersectionObserver.observe(this._observeForSticky.nativeElement);
    }
  }
 
  /**
   * @ignore
   */
  private _destroyScrollingObserver() {
    if (this._intersectionObserver) {
      this._intersectionObserver.disconnect();
      this._intersectionObserver = undefined;
    }
  }
}