From 707e88154cc56225b7c58fce61985309197d0f8a Mon Sep 17 00:00:00 2001 From: Yury Uvarov Date: Tue, 5 Dec 2023 16:15:38 +0300 Subject: [PATCH] add utilities and methods --- package/index.d.ts | 4 ++++ package/src/scripts/handles/handleClickDay.ts | 3 ++- .../handles/handleDayRangedSelection.ts | 17 +++++++++-------- package/src/vanilla-calendar.ts | 19 +++++++++++++++++++ package/types.ts | 12 ++++++++++++ 5 files changed, 46 insertions(+), 9 deletions(-) diff --git a/package/index.d.ts b/package/index.d.ts index a2bdc09d..65893c8a 100644 --- a/package/index.d.ts +++ b/package/index.d.ts @@ -10,6 +10,8 @@ export type Visibility = T.IVisibility; export type Locale = T.ILocale; export type Actions = T.IActions; export type Popups = T.IPopups; +export type Utilities = T.IUtilities; +export type Methods = T.IMethods; export type CSSClasses = T.ICSSClasses; export type DOMTemplates = T.IDOMTemplates; export type FormatDateString = T.FormatDateString; @@ -33,6 +35,8 @@ export default class VanillaCalendar implements T.IVanillaCalendar { locale: T.ILocale; actions: T.IActions; popups: T.IPopups; + utilities: T.IUtilities; + methods: T.IMethods; CSSClasses: T.ICSSClasses; DOMTemplates: T.IDOMTemplates; diff --git a/package/src/scripts/handles/handleClickDay.ts b/package/src/scripts/handles/handleClickDay.ts index 8e43eb89..33b70b27 100644 --- a/package/src/scripts/handles/handleClickDay.ts +++ b/package/src/scripts/handles/handleClickDay.ts @@ -1,4 +1,5 @@ import VanillaCalendar from '@src/vanilla-calendar'; +import { FormatDateString } from '@package/types'; import actionsInput from '@scripts/helpers/actionsInput'; import changeMonth from '@scripts/methods/changeMonth'; import createDays from '@scripts/methods/createDays'; @@ -15,7 +16,7 @@ const handleClickDay = (self: VanillaCalendar, event: MouseEvent) => { const daySelectionActions = { single: () => handleDaySelection(self, dayBtnEl, false), multiple: () => handleDaySelection(self, dayBtnEl, true), - 'multiple-ranged': () => handleDayRangedSelection(self, dayBtnEl), + 'multiple-ranged': () => handleDayRangedSelection(self, dayBtnEl.dataset.calendarDay as FormatDateString), }; daySelectionActions[self.settings.selection.day](); self.selectedDates?.sort((a, b) => +new Date(a) - +new Date(b)); diff --git a/package/src/scripts/handles/handleDayRangedSelection.ts b/package/src/scripts/handles/handleDayRangedSelection.ts index f7f8b632..997b5e2e 100644 --- a/package/src/scripts/handles/handleDayRangedSelection.ts +++ b/package/src/scripts/handles/handleDayRangedSelection.ts @@ -75,14 +75,15 @@ const resetDisabledDates = () => { current.self.rangeMax = current.rangeMax as FormatDateString; }; -const handleDayRangedSelection = (self: VanillaCalendar, dayBtnEl: HTMLElement) => { - const formattedDate = dayBtnEl.dataset.calendarDay as FormatDateString; - const selectedDateExists = self.selectedDates.length === 1 && self.selectedDates[0].includes(formattedDate); - self.selectedDates = selectedDateExists && !self.settings.selection.cancelableDay - ? [formattedDate] - : selectedDateExists && self.settings.selection.cancelableDay - ? [] - : self.selectedDates.length > 1 ? [formattedDate] : [...self.selectedDates, formattedDate]; +const handleDayRangedSelection = (self: VanillaCalendar, formattedDate?: FormatDateString) => { + if (formattedDate) { + const selectedDateExists = self.selectedDates.length === 1 && self.selectedDates[0].includes(formattedDate); + self.selectedDates = selectedDateExists && !self.settings.selection.cancelableDay + ? [formattedDate] + : selectedDateExists && self.settings.selection.cancelableDay + ? [] + : self.selectedDates.length > 1 ? [formattedDate] : [...self.selectedDates, formattedDate]; + } if (self.settings.range.disableGaps) { current.rangeMin = current.rangeMin ? current.rangeMin : self.rangeMin; diff --git a/package/src/vanilla-calendar.ts b/package/src/vanilla-calendar.ts index 371d3b5d..8e9206b7 100644 --- a/package/src/vanilla-calendar.ts +++ b/package/src/vanilla-calendar.ts @@ -4,6 +4,11 @@ import init from '@scripts/init'; import update from '@scripts/update'; import destroy from '@scripts/destroy'; import messages from '@scripts/helpers/getMessages'; +import generateDate from '@scripts/helpers/generateDate'; +import getDate from '@scripts/helpers/getDate'; +import parseDates from '@scripts/helpers/parseDates'; +import handleDayRangedSelection from '@scripts/handles/handleDayRangedSelection'; +import createDays from '@scripts/methods/createDays'; export default class VanillaCalendar extends DefaultOptionsCalendar implements T.IVanillaCalendar { constructor(selector: HTMLElement | string, options?: Partial) { @@ -27,6 +32,20 @@ export default class VanillaCalendar extends DefaultOptionsCalendar implements T replaceProperties(this, options); } + methods = { + forceSelectOnlyFirstDay: () => { + this.selectedDates = this.selectedDates?.[0] ? [this.selectedDates[0]] : []; + createDays(this); + handleDayRangedSelection(this); + }, + }; + + utilities = { + getDateString: (date: Date) => generateDate(date), + getDate: (date: T.FormatDateString) => getDate(date), + parseDates: (dates: string[]) => parseDates(dates), + }; + init = () => init(this); update = (reset?: { diff --git a/package/types.ts b/package/types.ts index a3004587..86746d21 100644 --- a/package/types.ts +++ b/package/types.ts @@ -66,6 +66,16 @@ export interface ILocale { weekday: string[] | []; } +export interface IMethods { + forceSelectOnlyFirstDay: () => void; +} + +export interface IUtilities { + getDateString: (date: Date) => void; + getDate: (date: FormatDateString) => void; + parseDates: (dates: string[]) => void; +} + export interface IActions { clickDay: ((e: MouseEvent, self: IVanillaCalendar) => void) | null; clickWeekNumber: ((e: MouseEvent, number: number, days: HTMLElement[], year: number, self: IVanillaCalendar) => void) | null; @@ -212,6 +222,8 @@ export interface IVanillaCalendar { locale: ILocale; actions: IActions; popups: IPopups; + methods: IMethods; + utilities: IUtilities; CSSClasses: ICSSClasses; DOMTemplates: IDOMTemplates;