From 53d2d591c7d2cbf973d4a74bdf6047cb6a2863f9 Mon Sep 17 00:00:00 2001 From: Yury Uvarov Date: Tue, 3 Oct 2023 11:39:51 +0300 Subject: [PATCH 1/4] fix selectedYear --- package/src/scripts/methods/createMonths.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/package/src/scripts/methods/createMonths.ts b/package/src/scripts/methods/createMonths.ts index 61a321d5..1bddbfd0 100644 --- a/package/src/scripts/methods/createMonths.ts +++ b/package/src/scripts/methods/createMonths.ts @@ -4,14 +4,17 @@ import showMonth from './showMonth'; import showYear from './showYear'; const createMonths = (self: IVanillaCalendar, target?: HTMLElement) => { - const selectedMonth = target?.dataset.calendarSelectedMonth ? Number(target?.dataset.calendarSelectedMonth) : self.selectedMonth as number; + const selectedMonth = target?.dataset.calendarSelectedMonth ? Number(target.dataset.calendarSelectedMonth) : self.selectedMonth as number; + const yearEl = target?.closest(`.${self.CSSClasses.column}`)?.querySelector(`.${self.CSSClasses.year}`) as HTMLElement; + const selectedYear = yearEl ? Number(yearEl.dataset.calendarSelectedYear) : self.selectedYear as number; + self.currentType = 'month'; createDOM(self, target); showMonth(self); showYear(self); const monthsEl = (self.HTMLElement as HTMLElement).querySelector(`.${self.CSSClasses.months}`); - if (self.selectedYear === undefined || !self.dateMin || !self.dateMax || !monthsEl) return; + if (!self.dateMin || !self.dateMax || !monthsEl) return; if (self.settings.selection.month) monthsEl.classList.add(self.CSSClasses.monthsSelecting); @@ -31,8 +34,8 @@ const createMonths = (self: IVanillaCalendar, target?: HTMLElement) => { monthEl.classList.add(self.CSSClasses.monthsMonthSelected); } - if ((i < self.dateMin.getMonth() && self.selectedYear === self.dateMin.getFullYear()) - || (i > self.dateMax.getMonth() && self.selectedYear === self.dateMax.getFullYear()) + if ((i < self.dateMin.getMonth() && selectedYear <= self.dateMin.getFullYear()) + || (i > self.dateMax.getMonth() && selectedYear >= self.dateMax.getFullYear()) || (i !== selectedMonth && !activeMonthsID.includes(i))) { monthEl.classList.add(self.CSSClasses.monthsMonthDisabled); monthEl.tabIndex = -1; From 6a336e7cda461d2c5b1a276d2e199c1641ec286e Mon Sep 17 00:00:00 2001 From: Yury Uvarov Date: Tue, 3 Oct 2023 12:04:46 +0300 Subject: [PATCH 2/4] fix selectedMonth and selectedYear --- package/src/scripts/methods/clickCalendar.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/package/src/scripts/methods/clickCalendar.ts b/package/src/scripts/methods/clickCalendar.ts index 59ad66e8..bfa567f6 100644 --- a/package/src/scripts/methods/clickCalendar.ts +++ b/package/src/scripts/methods/clickCalendar.ts @@ -164,11 +164,13 @@ const clickCalendar = (self: IVanillaCalendar) => { ? getColumnID(self, self.CSSClasses.columnYear, self.CSSClasses.year, Number(yearItemEl.dataset.calendarYear), 'data-calendar-selected-year') : Number(yearItemEl.dataset.calendarYear); self.currentType = self.type; - if (self.selectedMonth < self.dateMin.getMonth() && self.selectedYear === self.dateMin.getFullYear()) { + if ((self.selectedMonth < self.dateMin.getMonth() && self.selectedYear <= self.dateMin.getFullYear()) || self.selectedYear <= self.dateMin.getFullYear()) { self.selectedMonth = self.dateMin.getMonth(); + self.selectedYear = self.dateMin.getFullYear(); } - if (self.selectedMonth > self.dateMax.getMonth() && self.selectedYear === self.dateMax.getFullYear()) { + if ((self.selectedMonth > self.dateMax.getMonth() && self.selectedYear >= self.dateMax.getFullYear()) || self.selectedYear >= self.dateMax.getFullYear()) { self.selectedMonth = self.dateMax.getMonth(); + self.selectedYear = self.dateMax.getFullYear(); } if (self.actions.clickYear) self.actions.clickYear(e, self.selectedYear); mainMethod(self); @@ -186,6 +188,7 @@ const clickCalendar = (self: IVanillaCalendar) => { self.currentType = self.type; mainMethod(self); } else if (monthItemEl) { + if (self.selectedMonth === undefined || !self.dateMin || !self.dateMax) return; self.selectedMonth = self.type === 'multiple' ? getColumnID(self, self.CSSClasses.columnMonth, self.CSSClasses.month, Number(monthItemEl.dataset.calendarMonth), 'data-calendar-selected-month') : Number(monthItemEl.dataset.calendarMonth); @@ -193,6 +196,12 @@ const clickCalendar = (self: IVanillaCalendar) => { const column = monthItemEl.closest(`.${self.CSSClasses.columnMonth}`) as HTMLElement; const year = column.querySelector(`.${self.CSSClasses.year}`) as HTMLElement; self.selectedYear = Number(year.dataset.calendarSelectedYear); + if (self.selectedMonth < self.dateMin.getMonth() && self.selectedYear <= self.dateMin.getFullYear()) { + self.selectedMonth = self.dateMin.getMonth(); + } + if (self.selectedMonth > self.dateMax.getMonth() && self.selectedYear >= self.dateMax.getFullYear()) { + self.selectedMonth = self.dateMax.getMonth(); + } } self.currentType = self.type; if (self.actions.clickMonth) self.actions.clickMonth(e, self.selectedMonth); From 1c534234b3a038b0d6616e95bef45cd17d407122 Mon Sep 17 00:00:00 2001 From: Yury Uvarov Date: Tue, 3 Oct 2023 15:29:45 +0300 Subject: [PATCH 3/4] fix selectedYear and add columnID --- package/src/scripts/methods/createMonths.ts | 11 +++++++++-- package/src/scripts/methods/createYears.ts | 14 ++++++++------ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/package/src/scripts/methods/createMonths.ts b/package/src/scripts/methods/createMonths.ts index 1bddbfd0..8940e70d 100644 --- a/package/src/scripts/methods/createMonths.ts +++ b/package/src/scripts/methods/createMonths.ts @@ -26,6 +26,13 @@ const createMonths = (self: IVanillaCalendar, target?: HTMLElement) => { templateMonthEl.type = 'button'; templateMonthEl.className = self.CSSClasses.monthsMonth; + const columnID = () => { + if (self.type !== 'multiple') return 0; + const columnEls = (self.HTMLElement as HTMLElement).querySelectorAll(`.${self.CSSClasses.column}`) as NodeListOf; + const indexColumn = [...columnEls].findIndex((column) => column.classList.contains(`${self.CSSClasses.columnMonth}`)); + return indexColumn > 0 ? indexColumn : 0; + }; + for (let i = 0; i < self.locale.months.length; i++) { const month = self.locale.months[i]; const monthEl = templateMonthEl.cloneNode(true) as HTMLButtonElement; @@ -34,8 +41,8 @@ const createMonths = (self: IVanillaCalendar, target?: HTMLElement) => { monthEl.classList.add(self.CSSClasses.monthsMonthSelected); } - if ((i < self.dateMin.getMonth() && selectedYear <= self.dateMin.getFullYear()) - || (i > self.dateMax.getMonth() && selectedYear >= self.dateMax.getFullYear()) + if ((i < self.dateMin.getMonth() + columnID() && selectedYear <= self.dateMin.getFullYear()) + || (i > self.dateMax.getMonth() + columnID() && selectedYear >= self.dateMax.getFullYear()) || (i !== selectedMonth && !activeMonthsID.includes(i))) { monthEl.classList.add(self.CSSClasses.monthsMonthDisabled); monthEl.tabIndex = -1; diff --git a/package/src/scripts/methods/createYears.ts b/package/src/scripts/methods/createYears.ts index 58e765ca..db054d2e 100644 --- a/package/src/scripts/methods/createYears.ts +++ b/package/src/scripts/methods/createYears.ts @@ -6,7 +6,7 @@ import showYear from './showYear'; const createYears = (self: IVanillaCalendar, target?: HTMLElement) => { if (self.viewYear === undefined || !self.dateMin || !self.dateMax) return; - const selectedYear = target?.dataset.calendarSelectedYear ? Number(target?.dataset.calendarSelectedYear) : self.selectedYear; + const selectedYear = target?.dataset.calendarSelectedYear ? Number(target?.dataset.calendarSelectedYear) : self.selectedYear as number; self.currentType = 'year'; createDOM(self, target); showMonth(self); @@ -20,6 +20,11 @@ const createYears = (self: IVanillaCalendar, target?: HTMLElement) => { templateYearEl.type = 'button'; templateYearEl.className = self.CSSClasses.yearsYear; + const relationshipID = () => { + if (self.type !== 'multiple') return 0; + return self.selectedYear === selectedYear ? 0 : 1; + }; + for (let i = self.viewYear - 7; i < self.viewYear + 8; i++) { const year = i; const yearEl = templateYearEl.cloneNode(true) as HTMLButtonElement; @@ -27,11 +32,8 @@ const createYears = (self: IVanillaCalendar, target?: HTMLElement) => { if (year === selectedYear) { yearEl.classList.add(self.CSSClasses.yearsYearSelected); } - if (year < self.dateMin.getFullYear()) { - yearEl.classList.add(self.CSSClasses.yearsYearDisabled); - yearEl.tabIndex = -1; - } - if (year > self.dateMax.getFullYear()) { + + if (year < self.dateMin.getFullYear() + relationshipID() || year > self.dateMax.getFullYear()) { yearEl.classList.add(self.CSSClasses.yearsYearDisabled); yearEl.tabIndex = -1; } From 725c253111df8e74cd8650539c3c07c16bbd4e2e Mon Sep 17 00:00:00 2001 From: Yury Uvarov Date: Tue, 3 Oct 2023 15:35:03 +0300 Subject: [PATCH 4/4] fix comparison --- package/src/scripts/methods/clickCalendar.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package/src/scripts/methods/clickCalendar.ts b/package/src/scripts/methods/clickCalendar.ts index bfa567f6..4fecc500 100644 --- a/package/src/scripts/methods/clickCalendar.ts +++ b/package/src/scripts/methods/clickCalendar.ts @@ -164,11 +164,11 @@ const clickCalendar = (self: IVanillaCalendar) => { ? getColumnID(self, self.CSSClasses.columnYear, self.CSSClasses.year, Number(yearItemEl.dataset.calendarYear), 'data-calendar-selected-year') : Number(yearItemEl.dataset.calendarYear); self.currentType = self.type; - if ((self.selectedMonth < self.dateMin.getMonth() && self.selectedYear <= self.dateMin.getFullYear()) || self.selectedYear <= self.dateMin.getFullYear()) { + if ((self.selectedMonth < self.dateMin.getMonth() && self.selectedYear <= self.dateMin.getFullYear()) || self.selectedYear < self.dateMin.getFullYear()) { self.selectedMonth = self.dateMin.getMonth(); self.selectedYear = self.dateMin.getFullYear(); } - if ((self.selectedMonth > self.dateMax.getMonth() && self.selectedYear >= self.dateMax.getFullYear()) || self.selectedYear >= self.dateMax.getFullYear()) { + if ((self.selectedMonth > self.dateMax.getMonth() && self.selectedYear >= self.dateMax.getFullYear()) || self.selectedYear > self.dateMax.getFullYear()) { self.selectedMonth = self.dateMax.getMonth(); self.selectedYear = self.dateMax.getFullYear(); }