core.js
3.84 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
133
134
135
136
137
138
139
140
141
142
143
144
import { dateUtil, getCalendarConfig } from './utils/index'
/**
* 计算当前月份前后两月应占的格子
* @param {number} year 年份
* @param {number} month 月份
*/
function calculateEmptyGrids(year, month, config) {
const prevMonthGrids = calculatePrevMonthGrids(year, month, config)
const nextMonthGrids = calculateNextMonthGrids(year, month, config)
return {
prevMonthGrids,
nextMonthGrids
}
}
/**
* 计算上月应占的格子
* @param {number} year 年份
* @param {number} month 月份
*/
function calculatePrevMonthGrids(year, month, config) {
let emptyGrids = []
const prevMonthDays = dateUtil.getDatesCountOfMonth(year, month - 1)
let firstDayOfWeek = dateUtil.firstDayOfWeek(year, month)
if (config.firstDayOfWeek === 'Mon') {
if (firstDayOfWeek === 0) {
firstDayOfWeek = 6
} else {
firstDayOfWeek -= 1
}
}
if (firstDayOfWeek > 0) {
const len = prevMonthDays - firstDayOfWeek
const { onlyShowCurrentMonth } = config
const YMInfo = dateUtil.getPrevMonthInfo({ year, month })
for (let i = prevMonthDays; i > len; i--) {
if (onlyShowCurrentMonth) {
emptyGrids.push('')
} else {
const week = dateUtil.getDayOfWeek(+year, +month, i)
emptyGrids.push({
...YMInfo,
date: i,
week
})
}
}
emptyGrids.reverse()
}
return emptyGrids
}
/**
* 计算下一月日期是否需要多展示的日期
* 某些月份日期为5排,某些月份6排,统一为6排
* @param {number} year
* @param {number} month
* @param {object} config
*/
function calculateExtraEmptyDate(year, month, config) {
let extDate = 0
if (+month === 2) {
extDate += 7
let firstDayofMonth = dateUtil.getDayOfWeek(year, month, 1)
if (config.firstDayOfWeek === 'Mon') {
if (+firstDayofMonth === 1) extDate += 7
} else {
if (+firstDayofMonth === 0) extDate += 7
}
} else {
let firstDayofMonth = dateUtil.getDayOfWeek(year, month, 1)
if (config.firstDayOfWeek === 'Mon') {
if (firstDayofMonth !== 0 && firstDayofMonth < 6) {
extDate += 7
}
} else {
if (firstDayofMonth <= 5) {
extDate += 7
}
}
}
return extDate
}
/**
* 计算下月应占的格子
* @param {number} year 年份
* @param {number} month 月份
*/
function calculateNextMonthGrids(year, month, config) {
let emptyGrids = []
const datesCount = dateUtil.getDatesCountOfMonth(year, month)
let lastDayWeek = dateUtil.getDayOfWeek(year, month, datesCount)
if (config.firstDayOfWeek === 'Mon') {
if (lastDayWeek === 0) {
lastDayWeek = 6
} else {
lastDayWeek -= 1
}
}
let len = 7 - (lastDayWeek + 1)
const { onlyShowCurrentMonth } = config
if (!onlyShowCurrentMonth) {
len = len + calculateExtraEmptyDate(year, month, config)
}
const YMInfo = dateUtil.getNextMonthInfo({ year, month })
for (let i = 1; i <= len; i++) {
const week = dateUtil.getDayOfWeek(+year, +month, i)
if (onlyShowCurrentMonth) {
emptyGrids.push('')
} else {
emptyGrids.push({
id: i - 1,
...YMInfo,
date: i,
week: week || 7
})
}
}
return emptyGrids
}
/**
* 设置日历面板数据
* @param {number} year 年份
* @param {number} month 月份
* @param {number} curDate 日期
*/
function calculateCurrentMonthDates(year, month) {
return dateUtil.calcDates(year, month)
}
export function calcJumpData({ dateInfo, config, component }) {
dateInfo = dateInfo || dateUtil.todayFMD()
const { year, month, date } = dateInfo
const calendarConfig = config || getCalendarConfig(component)
const emptyGrids = calculateEmptyGrids(year, month, calendarConfig)
const calendar = {
curYear: year,
curMonth: month,
curDate: date,
dates: calculateCurrentMonthDates(year, month),
...emptyGrids
}
return calendar
}