Skip to content

Commit

Permalink
Merge pull request #78 from uvarov-frontend/hotfix/update_method
Browse files Browse the repository at this point in the history
hotfix/update method
  • Loading branch information
uvarov-frontend authored Apr 26, 2023
2 parents 139100c + 649790a commit 8326d73
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 49 deletions.
9 changes: 4 additions & 5 deletions docs/en/reference/main/initialize-and-update.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ calendar.init();

You can also update the calendar with the new settings at any time using the `update()` method.

The `update()` method, unlike `reset()`, saves the user's selected dates, month, and year after the update.

```js
calendar.settings.lang = 'de-AT';
calendar.settings.iso8601 = false;
calendar.update();
```

Or reset using the `reset()` method.

```js
calendar.update();
// or
calendar.reset();
```
11 changes: 5 additions & 6 deletions docs/ru/reference/main/initialize-and-update.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ const calendar = new VanillaCalendar(element, params);
calendar.init();
```

Вы также можете обновить календарь с новыми настройками в любое время, используя метод `update()`.
Вы также можете обновить календарь с новыми настройками в любое время, используя метод `update()` или `reset()`.

Метод `update()`, в отличие от `reset()`, сохраняет выбранные пользователем даты, месяц и год после обновления.

```js
calendar.settings.lang = 'de-AT';
calendar.settings.iso8601 = false;
calendar.update();
```

Или сбросить, используя метод `reset()`.

```js
calendar.update();
// or
calendar.reset();
```
10 changes: 5 additions & 5 deletions package/src/scripts/methods/clickCalendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import createDays from './createDays';
import createMonths from './createMonths';
import createYears from './createYears';
import generateDate from '../helpers/generateDate';
import update from './updateCalendar';
import mainMethod from './mainMethod';
import handlerMultipleRanged from './handlerMultipleRanged';

const clickCalendar = (self: IVanillaCalendar) => {
Expand Down Expand Up @@ -154,7 +154,7 @@ const clickCalendar = (self: IVanillaCalendar) => {
createYears(self);
} else if (self.currentType === 'year' && yearHeaderEl) {
self.currentType = self.type;
update(self);
mainMethod(self);
} else if (yearItemEl) {
if (self.selectedMonth === undefined || !self.dateMin || !self.dateMax) return;
self.selectedYear = Number(yearItemEl.dataset.calendarYear);
Expand All @@ -166,7 +166,7 @@ const clickCalendar = (self: IVanillaCalendar) => {
self.selectedMonth = self.dateMax.getMonth();
}
if (self.actions.clickYear) self.actions.clickYear(e, self.selectedYear);
update(self);
mainMethod(self);
}
};

Expand All @@ -176,12 +176,12 @@ const clickCalendar = (self: IVanillaCalendar) => {
createMonths(self);
} else if (self.currentType === 'month' && monthHeaderEl) {
self.currentType = self.type;
update(self);
mainMethod(self);
} else if (monthItemEl) {
self.selectedMonth = Number(monthItemEl.dataset.calendarMonth);
self.currentType = self.type;
if (self.actions.clickMonth) self.actions.clickMonth(e, self.selectedMonth);
update(self);
mainMethod(self);
}
};

Expand Down
4 changes: 2 additions & 2 deletions package/src/scripts/methods/handlerMultipleRanged.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IVanillaCalendar } from 'src/types';
import generateDate from '../helpers/generateDate';
import update from './updateCalendar';
import mainMethod from './mainMethod';

let currentSelf: null | IVanillaCalendar = null;

Expand Down Expand Up @@ -63,7 +63,7 @@ const cancelSelectionDays = (e: KeyboardEvent) => {
currentSelf.selectedDates = [];
(currentSelf.HTMLElement as HTMLElement).removeEventListener('mousemove', hoverDaysEvent);
document.removeEventListener('keydown', cancelSelectionDays);
update(currentSelf);
mainMethod(currentSelf);
};

const setDisabledDates = () => {
Expand Down
6 changes: 2 additions & 4 deletions package/src/scripts/methods/initCalendar.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { IVanillaCalendar } from 'src/types';
import setVariablesDates from './setVariablesDates';
import updateCalendar from './updateCalendar';
import resetCalendar from './resetCalendar';
import handlerInput from './handlerInput';
import clickCalendar from './clickCalendar';
import createCalendarToInput from './createCalendarToInput';

const initCalendar = (self: IVanillaCalendar) => {
if (!self.HTMLElement) return;
createCalendarToInput(self);
setVariablesDates(self);
updateCalendar(self);
resetCalendar(self);
handlerInput(self);
clickCalendar(self);
};
Expand Down
33 changes: 33 additions & 0 deletions package/src/scripts/methods/mainMethod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { IVanillaCalendar } from 'src/types';
import controlArrows from './controlArrows';
import createDays from './createDays';
import createDOM from './createDOM';
import showMonth from './showMonth';
import showYear from './showYear';
import createMonths from './createMonths';
import createTime from './createTime';
import createWeek from './createWeek';
import createYears from './createYears';
import getLocale from './getLocale';
import setTheme from './setTheme';

const mainMethod = (self: IVanillaCalendar) => {
setTheme(self);
getLocale(self);
createDOM(self);
showMonth(self);
showYear(self);
controlArrows(self);
createTime(self);

if (self.currentType === 'default' || self.currentType === 'multiple') {
createWeek(self);
createDays(self);
} else if (self.currentType === 'month') {
createMonths(self);
} else if (self.currentType === 'year') {
createYears(self);
}
};

export default mainMethod;
4 changes: 2 additions & 2 deletions package/src/scripts/methods/resetCalendar.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { IVanillaCalendar } from 'src/types';
import setVariablesDates from './setVariablesDates';
import updateCalendar from './updateCalendar';
import mainMethod from './mainMethod';

const resetCalendar = (self: IVanillaCalendar) => {
setVariablesDates(self);
updateCalendar(self);
mainMethod(self);
};

export default resetCalendar;
46 changes: 21 additions & 25 deletions package/src/scripts/methods/updateCalendar.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
import { IVanillaCalendar } from 'src/types';
import controlArrows from './controlArrows';
import createDays from './createDays';
import createDOM from './createDOM';
import showMonth from './showMonth';
import showYear from './showYear';
import createMonths from './createMonths';
import createTime from './createTime';
import createWeek from './createWeek';
import createYears from './createYears';
import getLocale from './getLocale';
import setTheme from './setTheme';
import setVariablesDates from './setVariablesDates';
import mainMethod from './mainMethod';

const updateCalendar = (self: IVanillaCalendar) => {
setTheme(self);
getLocale(self);
createDOM(self);
showMonth(self);
showYear(self);
controlArrows(self);
createTime(self);
let tempSelectedDates = null;
let tempSelectedMonth = null;
let tempSelectedYear = null;

if (self.currentType === 'default' || self.currentType === 'multiple') {
createWeek(self);
createDays(self);
} else if (self.currentType === 'month') {
createMonths(self);
} else if (self.currentType === 'year') {
createYears(self);
if (!self.settings.selected.dates?.[0]) {
tempSelectedDates = self.settings.selected.dates;
self.settings.selected.dates = self.selectedDates;
}
if (!self.settings.selected.month) {
tempSelectedMonth = self.settings.selected.month;
self.settings.selected.month = self.selectedMonth as number;
}
if (!self.settings.selected.year) {
tempSelectedYear = self.settings.selected.year;
self.settings.selected.year = self.selectedYear as number;
}
setVariablesDates(self);
mainMethod(self);
self.settings.selected.dates = tempSelectedDates;
self.settings.selected.month = tempSelectedMonth;
self.settings.selected.year = tempSelectedYear;
};

export default updateCalendar;

0 comments on commit 8326d73

Please sign in to comment.