Skip to content

Commit

Permalink
add utilities and methods
Browse files Browse the repository at this point in the history
  • Loading branch information
uvarov-frontend committed Dec 5, 2023
1 parent 159d4b9 commit 707e881
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 9 deletions.
4 changes: 4 additions & 0 deletions package/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down
3 changes: 2 additions & 1 deletion package/src/scripts/handles/handleClickDay.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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));
Expand Down
17 changes: 9 additions & 8 deletions package/src/scripts/handles/handleDayRangedSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
19 changes: 19 additions & 0 deletions package/src/vanilla-calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T.IOptions>) {
Expand All @@ -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?: {
Expand Down
12 changes: 12 additions & 0 deletions package/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -212,6 +222,8 @@ export interface IVanillaCalendar {
locale: ILocale;
actions: IActions;
popups: IPopups;
methods: IMethods;
utilities: IUtilities;
CSSClasses: ICSSClasses;
DOMTemplates: IDOMTemplates;

Expand Down

0 comments on commit 707e881

Please sign in to comment.