This repository was archived by the owner on May 19, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 707
Expand file tree
/
Copy pathutils.js
More file actions
132 lines (124 loc) · 4.15 KB
/
utils.js
File metadata and controls
132 lines (124 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import classnames from 'classnames';
import {
startOfMonth,
endOfMonth,
startOfWeek,
endOfWeek,
differenceInCalendarDays,
differenceInCalendarMonths,
addDays,
subDays,
getWeekOfMonth,
nextSunday,
isSunday,
lastDayOfWeek,
} from 'date-fns';
export function calcFocusDate(currentFocusedDate, props) {
const { shownDate, date, months, ranges, focusedRange, displayMode } = props;
// find primary date according the props
let targetInterval;
if (displayMode === 'dateRange') {
const range = ranges[focusedRange[0]] || {};
targetInterval = {
start: range.startDate,
end: range.endDate,
};
} else {
targetInterval = {
start: date,
end: date,
};
}
targetInterval.start = startOfMonth(targetInterval.start || new Date());
targetInterval.end = endOfMonth(targetInterval.end || targetInterval.start);
const targetDate =
(focusedRange[1]
? targetInterval.end || targetInterval.start
: targetInterval.start || targetInterval.end) ||
shownDate ||
new Date();
// initial focus
if (!currentFocusedDate) return shownDate || targetDate;
// // just return targetDate for native scrolled calendars
// if (props.scroll.enabled) return targetDate;
if (differenceInCalendarMonths(targetInterval.start, targetInterval.end) > months) {
// don't change focused if new selection in view area
return currentFocusedDate;
}
return targetDate;
}
export function findNextRangeIndex(ranges, currentRangeIndex = -1) {
const nextIndex = ranges.findIndex(
(range, i) => i > currentRangeIndex && range.autoFocus !== false && !range.disabled
);
if (nextIndex !== -1) return nextIndex;
return ranges.findIndex(range => range.autoFocus !== false && !range.disabled);
}
export function getMonthDisplayRange(date, dateOptions, fixedHeight) {
const startDateOfMonth = startOfMonth(date, dateOptions);
const endDateOfMonth = endOfMonth(date, dateOptions);
const startDateOfCalendar = startOfWeek(startDateOfMonth, dateOptions);
let endDateOfCalendar = endOfWeek(endDateOfMonth, dateOptions);
if (fixedHeight && differenceInCalendarDays(endDateOfCalendar, startDateOfCalendar) <= 34) {
endDateOfCalendar = addDays(endDateOfCalendar, 7);
}
return {
start: startDateOfCalendar,
end: endDateOfCalendar,
startDateOfMonth,
endDateOfMonth,
};
}
export function generateStyles(sources) {
if (!sources.length) return {};
const generatedStyles = sources
.filter(source => Boolean(source))
.reduce((styles, styleSource) => {
Object.keys(styleSource).forEach(key => {
styles[key] = classnames(styles[key], styleSource[key]);
});
return styles;
}, {});
return generatedStyles;
}
export function calculateBroadcastWeekNumber(currentDate) {
const isDecember = (currentDate.getMonth() + 1) === 12;
if (isDecember && getWeekOfMonth(currentDate) === 6) {
return 1;
}
if (
isDecember
&& getWeekOfMonth(currentDate) === 5
&& !isSunday(currentDate)
&& (nextSunday(currentDate).getMonth() + 1) !== 12
) {
return 1;
};
const firstDayOfTheYearDate = new Date(currentDate.getFullYear(), 0, 1);
const firstDayOfTheYear = firstDayOfTheYearDate.getDay();
const yearStartsOnSunday = firstDayOfTheYear === 0;
const broadcastStartOfWeek1Day = subDays(firstDayOfTheYearDate, yearStartsOnSunday ? 6 : firstDayOfTheYear - 1);
const days = Math.floor((currentDate - broadcastStartOfWeek1Day) / (24 * 60 * 60 * 1000));
const weekNumber = Math.ceil((currentDate.getDay() + 1 + days) / 7);
return weekNumber;
}
export function shouldRenderBroadcastDay(calendarDate, calendarMonth) {
const weekOfMonth = getWeekOfMonth(calendarDate, { weekStartsOn: 1 });
if (weekOfMonth <= 4 && calendarDate.getMonth() === calendarMonth) {
return true;
}
if (weekOfMonth === 6 && calendarDate.getMonth() === calendarMonth) {
return false;
}
if (
weekOfMonth === 5
&& calendarDate.getMonth() === calendarMonth
&& lastDayOfWeek(calendarDate, { weekStartsOn: 1 }).getMonth() !== calendarMonth
) {
return false;
};
if (weekOfMonth === 1 && calendarDate.getMonth() !== calendarMonth) {
return false;
}
return true;
}