All files / datepicker/components si-month-selection.component.ts

94.28% Statements 66/70
82.75% Branches 24/29
91.66% Functions 11/12
94.2% Lines 65/69

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                                                                                    1x           38x       38x   38x   38x                       38x 38x   38x           38x 68x 68x 68x             38x 68x 68x 68x       77x             74x         10x 10x   2x 2x   3x 3x   2x 2x   1x 1x   2x 2x 2x 2x                 8x       8x 8x 8x 8x   8x 8x             4x 4x 4x 4x 4x       3x         11x 11x       12x 6x         4x             74x 74x         74x 74x 74x 74x 888x 370x 370x   888x 888x 888x 888x           888x                     74x      
/**
 * Copyright (c) Siemens 2016 - 2025
 * SPDX-License-Identifier: MIT
 */
import { DatePipe } from '@angular/common';
import {
  ChangeDetectionStrategy,
  Component,
  computed,
  HostListener,
  input,
  model,
  OnChanges,
  output,
  SimpleChanges
} from '@angular/core';
import { isRTL } from '@siemens/element-ng/common';
 
import {
  addMonthsInRange,
  createDate,
  getFirstDateInYear,
  isAfterYear,
  isAnotherYear,
  isSameYear,
  today as getToday
} from '../date-time-helper';
import { Cell, SiCalendarBodyComponent } from './si-calendar-body.component';
import { SiCalendarDirectionButtonComponent } from './si-calendar-direction-button.component';
import { MonthCompareAdapter } from './si-compare-adapter';
import { SiInitialFocusComponent } from './si-initial-focus.component';
 
/**
 * Show months of a single year as table and handles the keyboard interactions.
 * The focus and focusedDate is handled according the keyboard interactions.
 */
@Component({
  selector: 'si-month-selection',
  imports: [SiCalendarDirectionButtonComponent, SiCalendarBodyComponent, DatePipe],
  templateUrl: './si-month-selection.component.html',
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class SiMonthSelectionComponent extends SiInitialFocusComponent implements OnChanges {
  /**
   * The translated list of months.
   *
   * @defaultValue []
   */
  readonly months = input<string[]>([]);
  /**
   * The active date, the cell which will receive the focus.
   */
  readonly focusedDate = model.required<Date>();
  /** Emits when the active focused date is changed to another month / year, typically during keyboard navigation. */
  readonly activeMonthChange = output<Date>();
  /** Emits when the user requests a different to show a different view. */
  readonly viewChange = output<'year'>();
  /** Listen Escape event to switch view back */
  @HostListener('keydown.Escape', ['$event'])
  triggerEsc(event: Event): void {
    this.selectedValueChange.emit(null);
    event.preventDefault();
    event.stopPropagation(); // Prevents the overlay from closing.
  }
  /**
   * The current visible list of calendar months.
   * Every time the focusedDate changes to another year the list will update.
   */
  protected monthCells: Cell[][] = [];
  protected compareAdapter = new MonthCompareAdapter();
  /** Number of column before the row is wrapped */
  private readonly columnCount = 2;
 
  /**
   * Indicate the previous button shall be disabled.
   * This happens when the focusedDate is equal or before the minDate.
   */
  protected readonly isPreviousButtonDisabled = computed(() => {
    const minDate = this.minDate();
    const focusedDate = this.focusedDate();
    return minDate && (isSameYear(focusedDate, minDate) || isAfterYear(minDate, focusedDate));
  });
 
  /**
   * Indicate the next button shall be disabled.
   * This happens when the focusedDate is equal or after the maxDate.
   */
  protected readonly isNextButtonDisabled = computed(() => {
    const maxDate = this.maxDate();
    const focusedDate = this.focusedDate();
    return maxDate && (isSameYear(focusedDate, maxDate) || isAfterYear(focusedDate, maxDate));
  });
 
  ngOnChanges(changes: SimpleChanges): void {
    if (
      changes.maxDate ||
      changes.minDate ||
      changes.maxMonth ||
      changes.minMonth ||
      changes.focusedDate
    ) {
      this.initView();
    }
  }
 
  protected calendarBodyKeyDown(event: KeyboardEvent): void {
    const isRtl = isRTL();
    switch (event.key) {
      case 'ArrowLeft':
        this.updateFocusedDate(isRtl ? 1 : -1);
        break;
      case 'ArrowRight':
        this.updateFocusedDate(isRtl ? -1 : 1);
        break;
      case 'ArrowUp':
        this.updateFocusedDate(-1 * this.columnCount);
        break;
      case 'ArrowDown':
        this.updateFocusedDate(this.columnCount);
        break;
      case 'Escape':
        this.selectedValueChange.emit(null);
        event.preventDefault();
        event.stopPropagation(); // Prevents the overlay from closing.
        return;
      case 'Enter':
      case 'Space':
      default:
        // Don't prevent default or focus active cell on keys that we don't explicitly handle.
        return;
    }
 
    // Prevent unexpected default actions such as form submission.
    event.preventDefault();
  }
 
  protected updateFocusedDate(offset: number): void {
    const prevDate = this.focusedDate();
    const newDate = addMonthsInRange(prevDate, offset, this.minDate(), this.maxDate());
    this.focusedDate.set(newDate);
    if (!this.compareAdapter.isEqual(prevDate, newDate)) {
      // Synchronize focusedDate with year view
      this.focusActiveCell();
      this.emitActiveMonthChange(newDate, prevDate);
    }
  }
  /**
   * Add offset to year and update focusedDate.
   */
  protected setYearOffset(offset: number): void {
    const prevDate = this.focusedDate();
    const newActive = createDate(prevDate);
    newActive.setFullYear(newActive.getFullYear() + offset);
    this.focusedDate.set(newActive);
    this.emitActiveMonthChange(newActive, prevDate);
  }
 
  protected emitSelectedValue(selected: Date): void {
    this.selectedValueChange.emit(selected);
  }
 
  protected emitFocusedDate(focused: Date): void {
    // Take over the current day on month changes
    focused.setDate(this.focusedDate().getDate());
    this.focusedDate.set(focused);
  }
 
  protected emitActiveMonthChange(focus: Date, prevFocus: Date): void {
    if (isAnotherYear(focus, prevFocus)) {
      this.activeMonthChange.emit(focus);
    }
  }
 
  protected emitViewChange(): void {
    this.viewChange.emit('year');
  }
 
  /**
   * Initialize view based on the focusedDate.
   */
  private initView(): void {
    this.monthCells = [];
    let row: Cell[] = [];
 
    // The cell date object needs to be the first to prevent that we jump to the next month when
    // setting the month. For example the focusedDate is 31. setting february would result in the
    // 3. March.
    const startDate = getFirstDateInYear(this.focusedDate());
    const today = getToday();
    const months = this.months();
    for (let i = 0; i <= 11; i++) {
      if (i > 0 && i % this.columnCount === 0) {
        this.monthCells.push(row);
        row = [];
      }
      const date = new Date(startDate);
      date.setMonth(i);
      const isToday = this.compareAdapter.isEqual(date, today);
      const isDisabled = !this.compareAdapter.isEqualOrBetween(
        date,
        this.minDate(),
        this.maxDate()
      );
 
      row.push({
        value: date.getDate(),
        disabled: isDisabled,
        ariaLabel: `${months[date.getMonth()]} ${this.focusedDate().getFullYear()}`,
        displayValue: months[date.getMonth()],
        isPreview: false,
        isToday,
        valueRaw: createDate(date),
        cssClasses: ['month', 'si-h4', 'text-truncate']
      });
    }
    this.monthCells.push(row);
  }
}