All files / lib/calendar/calendar-header calendar-header.component.ts

22.66% Statements 46/203
100% Branches 0/0
0% Functions 0/8
22.66% Lines 46/203

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 2041x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                                                                                                                                                                                                   1x 1x 1x 1x 1x 1x                 1x 1x 1x           1x 1x 1x     1x 1x 1x                                                                           1x  
/*******************************************************************************
 * Copyright bei
 * Entwicklungs- und Pflegeverbund für das gemeinsame Fachverfahren gefa
 *
 *******************************************************************************/
import {
  AfterViewInit,
  Component,
  ElementRef,
  EventEmitter,
  HostBinding,
  Input,
  NgZone,
  OnChanges,
  OnDestroy,
  Output,
  SimpleChanges,
  ViewChild,
  inject,
} from '@angular/core';
 
import { ButtonComponent } from '../../button/button.component';
import { ToggleButtonGroupComponent } from '../../toggle-button-group/toggle-button-group.component';
 
export type CalendarHeaderAction = 'next' | 'previous' | 'today';
export type CalendarView = 'day' | 'week' | 'month';
 
/**
 * Header component for the main Calendar component.
 */
@Component({
  selector: 'gc-calendar-header',
  templateUrl: './calendar-header.component.html',
  styleUrls: ['./calendar-header.component.css'],
})
export class CalendarHeaderComponent
  implements OnChanges, AfterViewInit, OnDestroy
{
  /**
   * Decides which calenar view is currently active.
   */
  @Input()
  public view: CalendarView = 'day';

  /**
   * Label that will be shown in the calendar header (date).
   * If only a start property is set then the hyphen will not be rendered.
   */
  @Input({ required: true })
  public label: { start: string; end?: string } = { start: '' };

  /**
   * Fires upon a calendar-header action press and emits an action.
   */
  @Output()
  public readonly onAction: EventEmitter<CalendarHeaderAction> =
    new EventEmitter<CalendarHeaderAction>();

  /**
   * Fires upon a calendar-header view change and emits the current view.
   */
  @Output()
  public readonly viewChange: EventEmitter<CalendarView> =
    new EventEmitter<CalendarView>();

  /** @ignore */
  @ViewChild('container')
  private containerEl!: ElementRef<HTMLElement>;

  /** @ignore */
  @ViewChild('previousButton')
  private previousButton!: ButtonComponent;

  /** @ignore */
  @ViewChild(ToggleButtonGroupComponent, { read: ElementRef<HTMLElement> })
  private toggleButtonGroupComponent!: ElementRef<HTMLElement>;

  /** @ignore */
  @HostBinding('style.height.px')
  private height = 0;

  /** @ignore */
  protected get nextButtonLabel(): string {
    const toggleButtonText = this.selectedToggleButton.key;
    switch (toggleButtonText) {
      case 'day':
        return 'Nächster Tag';
      case 'week':
        return 'Nächste Woche';
      case 'month':
        return 'Nächster Monat';
      default:
        toggleButtonText satisfies never;
        return '';
    }
  }

  /** @ignore */
  protected get previousButtonLabel(): string {
    const toggleButtonText = this.selectedToggleButton.key;
    switch (toggleButtonText) {
      case 'day':
        return 'Vorheriger Tag';
      case 'week':
        return 'Vorherige Woche';
      case 'month':
        return 'Vorheriger Monat';
      default:
        toggleButtonText satisfies never;
        return '';
    }
  }

  /** @ignore */
  protected toggleButtonItems: readonly { key: CalendarView; label: string }[] =
    [
      { key: 'day', label: 'Tag' },
      { key: 'week', label: 'Woche' },
      { key: 'month', label: 'Monat' },
    ];

  /**
   * @ignore
   * the value is overriden in the first ngOnChanges run.
   */
  protected selectedToggleButton = this.toggleButtonItems[0];

  /** @ignore */
  protected condensedToggleButtonGroup = false;

  /** @ignore */
  private zone: NgZone = inject(NgZone);

  /** @ignore */
  private tbgHasActiveElement = false;
 
  /** @ignore */
  private resizeObserver?: ResizeObserver;
 
  /** @ignore */
  ngOnChanges(changes: SimpleChanges): void {
    if ('view' in changes) {
      const foundButton = this.toggleButtonItems.find(b => b.key === this.view);

      if (foundButton) {
        this.selectedToggleButton = foundButton;
      }
    }
  }
 
  /** @ignore */
  ngAfterViewInit(): void {
    this.zone.runOutsideAngular(() => {
      this.resizeObserver = new ResizeObserver(this.onResizeHandler.bind(this));
      this.resizeObserver.observe(this.containerEl.nativeElement);
    });
  }
 
  /** @ignore */
  ngOnDestroy(): void {
    this.resizeObserver?.disconnect();
  }
 
  /** @ignore */
  protected onResizeHandler() {
    NgZone.assertNotInAngularZone();

    const containerEl = this.containerEl.nativeElement;

    // needs to be set manually due to 'container-type' css property
    if (this.height !== containerEl.getBoundingClientRect().height) {
      this.zone.run(() => {
        this.height = containerEl.getBoundingClientRect().height;
      });
    }

    if (containerEl.clientWidth > 420) {
      this.tbgHasActiveElement =
        this.toggleButtonGroupComponent.nativeElement.contains(
          document.activeElement,
        );
    }

    if (this.condensedToggleButtonGroup !== containerEl.clientWidth < 840) {
      this.zone.run(() => {
        this.condensedToggleButtonGroup = containerEl.clientWidth < 840;
      });
    }

    if (containerEl.clientWidth < 420) {
      if (this.selectedToggleButton !== this.toggleButtonItems[0]) {
        this.zone.run(() => {
          this.viewChange.emit('day');
          this.selectedToggleButton = this.toggleButtonItems[0];
        });
      }

      if (this.tbgHasActiveElement) {
        this.previousButton.focusChild();
      }
    }
  }
}