UNPKG

v4web-components

Version:
73 lines (72 loc) 2.62 kB
export class Calendar { constructor(year, month) { this.daysInCalendarWithFiveRows = 42; this.daysInCalendarWithFourRows = 35; this.daysInCalendarWithThreeRows = 28; this.daysInCalendar = this.daysInCalendarWithFourRows; this.fillStartCount = 0; this.fillEndCount = 0; this.fillCount = [0, 1, 2, 3, 4, 5, 6]; this.year = year; this.month = month; } getCalendarDays() { const daysOfCurrentMonth = this.getDaysOfCurrentMonth(); const fillStartCount = this.fillCount[this.getFirstDayOfMonth()]; const fillEndCount = this.daysInCalendarWithFourRows - (daysOfCurrentMonth.length + fillStartCount); this.currentMonthCount = daysOfCurrentMonth.length; this.fillStartCount = fillStartCount; this.fillEndCount = fillEndCount; const fillStart = fillStartCount > 0 ? this.getDaysOfLastMonth(fillStartCount) : []; const fillEnd = this.getDaysOfNextMonth(fillEndCount); return fillStart.concat(daysOfCurrentMonth).concat(fillEnd); } getDaysOfCurrentMonth() { return this.getDaysOfMonth(this.month); } getDaysOfLastMonth(fillStartCount) { const daysOfMonth = this.getDaysOfMonth(this.month - 1); return daysOfMonth.slice(-fillStartCount); } getDaysOfNextMonth(endCount) { const daysOfMonth = this.getDaysOfMonth(this.month + 1); let slicedDays; if (endCount <= -1) { endCount = this.daysInCalendarWithFiveRows - (this.currentMonthCount + this.fillStartCount); slicedDays = daysOfMonth.slice(0, endCount); this.daysInCalendar = this.daysInCalendarWithFiveRows; this.fillEndCount = endCount; } else if (endCount === 7 && this.currentMonthCount + this.fillStartCount === 28) { endCount = this.daysInCalendarWithThreeRows - (this.currentMonthCount + this.fillStartCount); slicedDays = daysOfMonth.slice(0, endCount); this.daysInCalendar = this.daysInCalendarWithThreeRows; this.fillEndCount = endCount; } else { slicedDays = daysOfMonth.slice(0, endCount); } return slicedDays; } getDaysOfMonth(month) { const daysOfMonth = new Date(this.year, month, 0).getDate(); return Array.from({ length: daysOfMonth }, (_, i) => i + 1); } getFirstDayOfMonth() { return new Date(this.year, this.month - 1, 1).getDay(); } getFillStartCount() { return this.fillStartCount; } getFillEndCount() { return this.fillEndCount; } static getToday() { return { day: new Date().getDate(), month: new Date().getMonth() + 1, year: new Date().getFullYear(), }; } } //# sourceMappingURL=calendar.js.map