core.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import { dateUtil, getCalendarConfig } from './utils/index'
  2. /**
  3. * 计算当前月份前后两月应占的格子
  4. * @param {number} year 年份
  5. * @param {number} month 月份
  6. */
  7. function calculateEmptyGrids(year, month, config) {
  8. const prevMonthGrids = calculatePrevMonthGrids(year, month, config)
  9. const nextMonthGrids = calculateNextMonthGrids(year, month, config)
  10. return {
  11. prevMonthGrids,
  12. nextMonthGrids
  13. }
  14. }
  15. /**
  16. * 计算上月应占的格子
  17. * @param {number} year 年份
  18. * @param {number} month 月份
  19. */
  20. function calculatePrevMonthGrids(year, month, config) {
  21. let emptyGrids = []
  22. const prevMonthDays = dateUtil.getDatesCountOfMonth(year, month - 1)
  23. let firstDayOfWeek = dateUtil.firstDayOfWeek(year, month)
  24. if (config.firstDayOfWeek === 'Mon') {
  25. if (firstDayOfWeek === 0) {
  26. firstDayOfWeek = 6
  27. } else {
  28. firstDayOfWeek -= 1
  29. }
  30. }
  31. if (firstDayOfWeek > 0) {
  32. const len = prevMonthDays - firstDayOfWeek
  33. const { onlyShowCurrentMonth } = config
  34. const YMInfo = dateUtil.getPrevMonthInfo({ year, month })
  35. for (let i = prevMonthDays; i > len; i--) {
  36. if (onlyShowCurrentMonth) {
  37. emptyGrids.push('')
  38. } else {
  39. const week = dateUtil.getDayOfWeek(+year, +month, i)
  40. emptyGrids.push({
  41. ...YMInfo,
  42. date: i,
  43. week
  44. })
  45. }
  46. }
  47. emptyGrids.reverse()
  48. }
  49. return emptyGrids
  50. }
  51. /**
  52. * 计算下一月日期是否需要多展示的日期
  53. * 某些月份日期为5排,某些月份6排,统一为6排
  54. * @param {number} year
  55. * @param {number} month
  56. * @param {object} config
  57. */
  58. function calculateExtraEmptyDate(year, month, config) {
  59. let extDate = 0
  60. if (+month === 2) {
  61. extDate += 7
  62. let firstDayofMonth = dateUtil.getDayOfWeek(year, month, 1)
  63. if (config.firstDayOfWeek === 'Mon') {
  64. if (+firstDayofMonth === 1) extDate += 7
  65. } else {
  66. if (+firstDayofMonth === 0) extDate += 7
  67. }
  68. } else {
  69. let firstDayofMonth = dateUtil.getDayOfWeek(year, month, 1)
  70. if (config.firstDayOfWeek === 'Mon') {
  71. if (firstDayofMonth !== 0 && firstDayofMonth < 6) {
  72. extDate += 7
  73. }
  74. } else {
  75. if (firstDayofMonth <= 5) {
  76. extDate += 7
  77. }
  78. }
  79. }
  80. return extDate
  81. }
  82. /**
  83. * 计算下月应占的格子
  84. * @param {number} year 年份
  85. * @param {number} month 月份
  86. */
  87. function calculateNextMonthGrids(year, month, config) {
  88. let emptyGrids = []
  89. const datesCount = dateUtil.getDatesCountOfMonth(year, month)
  90. let lastDayWeek = dateUtil.getDayOfWeek(year, month, datesCount)
  91. if (config.firstDayOfWeek === 'Mon') {
  92. if (lastDayWeek === 0) {
  93. lastDayWeek = 6
  94. } else {
  95. lastDayWeek -= 1
  96. }
  97. }
  98. let len = 7 - (lastDayWeek + 1)
  99. const { onlyShowCurrentMonth } = config
  100. if (!onlyShowCurrentMonth) {
  101. len = len + calculateExtraEmptyDate(year, month, config)
  102. }
  103. const YMInfo = dateUtil.getNextMonthInfo({ year, month })
  104. for (let i = 1; i <= len; i++) {
  105. const week = dateUtil.getDayOfWeek(+year, +month, i)
  106. if (onlyShowCurrentMonth) {
  107. emptyGrids.push('')
  108. } else {
  109. emptyGrids.push({
  110. id: i - 1,
  111. ...YMInfo,
  112. date: i,
  113. week: week || 7
  114. })
  115. }
  116. }
  117. return emptyGrids
  118. }
  119. /**
  120. * 设置日历面板数据
  121. * @param {number} year 年份
  122. * @param {number} month 月份
  123. * @param {number} curDate 日期
  124. */
  125. function calculateCurrentMonthDates(year, month) {
  126. return dateUtil.calcDates(year, month)
  127. }
  128. export function calcJumpData({ dateInfo, config, component }) {
  129. dateInfo = dateInfo || dateUtil.todayFMD()
  130. const { year, month, date } = dateInfo
  131. const calendarConfig = config || getCalendarConfig(component)
  132. const emptyGrids = calculateEmptyGrids(year, month, calendarConfig)
  133. const calendar = {
  134. curYear: year,
  135. curMonth: month,
  136. curDate: date,
  137. dates: calculateCurrentMonthDates(year, month),
  138. ...emptyGrids
  139. }
  140. return calendar
  141. }