core.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. console.log('lastDayWeek', lastDayWeek);
  92. if (config.firstDayOfWeek === 'Mon') {
  93. if (lastDayWeek === 0) {
  94. lastDayWeek = 6
  95. } else {
  96. lastDayWeek -= 1
  97. }
  98. }
  99. let len = 7 - (lastDayWeek + 1)
  100. const { onlyShowCurrentMonth } = config
  101. if (!onlyShowCurrentMonth) {
  102. len = len + calculateExtraEmptyDate(year, month, config)
  103. }
  104. const YMInfo = dateUtil.getNextMonthInfo({ year, month })
  105. for (let i = 1; i <= len; i++) {
  106. const week = dateUtil.getDayOfWeek(+year, +month, i)
  107. if (onlyShowCurrentMonth) {
  108. emptyGrids.push('')
  109. } else {
  110. emptyGrids.push({
  111. id: i - 1,
  112. ...YMInfo,
  113. date: i,
  114. week: week || 7
  115. })
  116. }
  117. }
  118. console.log('emptyGrids', emptyGrids);
  119. return emptyGrids
  120. }
  121. /**
  122. * 设置日历面板数据
  123. * @param {number} year 年份
  124. * @param {number} month 月份
  125. * @param {number} curDate 日期
  126. */
  127. function calculateCurrentMonthDates(year, month) {
  128. return dateUtil.calcDates(year, month)
  129. }
  130. export function calcJumpData({ dateInfo, config, component }) {
  131. dateInfo = dateInfo || dateUtil.todayFMD()
  132. const { year, month, date } = dateInfo
  133. const calendarConfig = config || getCalendarConfig(component)
  134. const emptyGrids = calculateEmptyGrids(year, month, calendarConfig)
  135. const calendar = {
  136. curYear: year,
  137. curMonth: month,
  138. curDate: date,
  139. dates: calculateCurrentMonthDates(year, month),
  140. ...emptyGrids
  141. }
  142. return calendar
  143. }