UNPKG

@taiga-ui/core

Version:

Core library for creating Angular components and applications using Taiga UI

95 lines (90 loc) 3.91 kB
import * as i0 from '@angular/core'; import { inject, Pipe } from '@angular/core'; import { DAYS_IN_WEEK, TuiDay } from '@taiga-ui/cdk/date-time'; import { TUI_FIRST_DAY_OF_WEEK } from '@taiga-ui/core/tokens'; import { tuiInRange } from '@taiga-ui/cdk/utils/math'; /// <reference types="@taiga-ui/tsconfig/ng-dev-mode" /> /** * Computes day of week offset of the beginning of the month */ const getMonthStartDaysOffset = (month, firstDayOfWeek) => { const startMonthOffsetFromSunday = new Date(month.year, month.month, 1).getDay(); return startMonthOffsetFromSunday >= firstDayOfWeek ? startMonthOffsetFromSunday - firstDayOfWeek : DAYS_IN_WEEK - (firstDayOfWeek - startMonthOffsetFromSunday); }; /** * Calculated day on a calendar grid * @return resulting day on these coordinates (could exceed passed month) */ const getDayFromMonthRowCol = ({ month, rowIndex, colIndex, firstDayOfWeek, }) => { ngDevMode && console.assert(Number.isInteger(rowIndex)); ngDevMode && console.assert(tuiInRange(rowIndex, 0, 6)); ngDevMode && console.assert(Number.isInteger(colIndex)); ngDevMode && console.assert(tuiInRange(colIndex, 0, DAYS_IN_WEEK)); let day = rowIndex * DAYS_IN_WEEK + colIndex - getMonthStartDaysOffset(month, firstDayOfWeek) + 1; if (day > month.daysCount) { day -= month.daysCount; month = month.append({ month: 1 }); } if (day <= 0) { month = month.append({ month: -1 }); day = month.daysCount + day; } return new TuiDay(month.year, month.month, day); }; const CALENDAR_ROWS_COUNT = 6; class TuiCalendarSheetPipe { constructor() { this.firstDayOfWeek = inject(TUI_FIRST_DAY_OF_WEEK); this.currentMonth = null; this.currentSheet = []; } transform(month, showAdjacentDays = false) { if (this.currentMonth?.monthSame(month)) { return this.currentSheet; } const sheet = []; for (let rowIndex = 0; rowIndex < CALENDAR_ROWS_COUNT; rowIndex++) { const row = []; for (let colIndex = 0; colIndex < DAYS_IN_WEEK; colIndex++) { const day = getDayFromMonthRowCol({ month, rowIndex, colIndex, firstDayOfWeek: this.firstDayOfWeek, }); const isPrevMonthDay = (day, relativeToMonth = month) => day.year < relativeToMonth.year || day.month < relativeToMonth.month; const isNextMonthDay = (day, relativeToMonth = month) => day.year > relativeToMonth.year || day.month > relativeToMonth.month; if (isPrevMonthDay(day) && !showAdjacentDays) { continue; } if (isNextMonthDay(day) && !showAdjacentDays) { break; } row.push(day); } sheet.push(row); } this.currentSheet = sheet.filter((row) => row.length); this.currentMonth = month; return this.currentSheet; } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: TuiCalendarSheetPipe, deps: [], target: i0.ɵɵFactoryTarget.Pipe }); } static { this.ɵpipe = i0.ɵɵngDeclarePipe({ minVersion: "14.0.0", version: "16.2.12", ngImport: i0, type: TuiCalendarSheetPipe, isStandalone: true, name: "tuiCalendarSheet" }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.2.12", ngImport: i0, type: TuiCalendarSheetPipe, decorators: [{ type: Pipe, args: [{ standalone: true, name: 'tuiCalendarSheet', }] }] }); /** * Generated bundle index. Do not edit. */ export { TuiCalendarSheetPipe }; //# sourceMappingURL=taiga-ui-core-pipes-calendar-sheet.mjs.map