helper.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import {
  2. weekMap, CHINESE_MODEL, PREV_DAY, CURRENT_DAY, NEXT_DAY,
  3. } from './const'
  4. import {
  5. getWeekOfMonth,
  6. getCurrentYear,
  7. getCurrentMonth,
  8. getDaysCountOfMonth,
  9. formatMonthOrDay,
  10. isCurrentDay,
  11. formatDate,
  12. getCurrentDate,
  13. } from './utils'
  14. export const getWeekSort = (model = CHINESE_MODEL) => {
  15. const values = [...weekMap.values()]
  16. if (model === CHINESE_MODEL) {
  17. values.splice(0, 1)
  18. values.push(weekMap.get(0))
  19. }
  20. return values
  21. }
  22. const getPrevLeftDays = (firstDay, model) => {
  23. let leftCount = firstDay
  24. // 如果是星期日
  25. // 正常中方日历以周日结尾
  26. // 西方以星期日作为第一天
  27. if (+leftCount === 0) {
  28. leftCount = model !== CHINESE_MODEL ? 0 : 6
  29. } else {
  30. leftCount = model !== CHINESE_MODEL ? leftCount : leftCount - 1
  31. }
  32. return leftCount
  33. }
  34. const getFullDays = (year, month, day, tag = CURRENT_DAY) => {
  35. const current = isCurrentDay(year, month, day)
  36. return {
  37. tag: tag,
  38. day: day,
  39. full: `${year}-${formatMonthOrDay(month)}-${formatMonthOrDay(day)}`,
  40. current: current,
  41. selected: current,
  42. }
  43. }
  44. export const getPrevYearAndMonth = (year, month) => {
  45. let prevYear = year
  46. let prevMonth = month
  47. if (+month === 1) {
  48. prevYear -= 1
  49. prevMonth = 12
  50. } else {
  51. prevMonth -= 1
  52. }
  53. return { year: prevYear, month: prevMonth }
  54. }
  55. export const getNextYearAndMonth = (year, month) => {
  56. let nextYear = year
  57. let nextMonth = month
  58. if (+month === 12) {
  59. nextYear += 1
  60. nextMonth = 1
  61. } else {
  62. nextMonth += 1
  63. }
  64. return { year: nextYear, month: nextMonth }
  65. }
  66. const getPrevMonthLeftDays = (year, month, firstDay, model) => {
  67. const yearAndMonth = getPrevYearAndMonth(year, month)
  68. const prevYear = yearAndMonth.year
  69. const prevMonth = yearAndMonth.month
  70. const leftCount = getPrevLeftDays(firstDay, model)
  71. const prevDays = []
  72. const prevMonthDays = getDaysCountOfMonth(prevMonth, prevYear)
  73. for (let i = 0; i < leftCount; i++) {
  74. prevDays.unshift(getFullDays(prevYear, prevMonth, prevMonthDays - i, PREV_DAY))
  75. }
  76. return prevDays
  77. }
  78. const getNextMonthLeftDays = (year, month, days, firstDay, model) => {
  79. const yearAndMonth = getNextYearAndMonth(year, month)
  80. const nextYear = yearAndMonth.year
  81. const nextMonth = yearAndMonth.month
  82. const leftCount = getPrevLeftDays(firstDay, model)
  83. const nextDays = []
  84. const nextLefts = 6 * 7 - (leftCount + days)
  85. for (let i = 0; i < nextLefts; i++) {
  86. nextDays.push(getFullDays(nextYear, nextMonth, i + 1, NEXT_DAY))
  87. }
  88. return nextDays
  89. }
  90. export const getDaysOfMonth = (year = getCurrentYear(),
  91. month = getCurrentMonth(), model = CHINESE_MODEL) => {
  92. const firstDayOfMonth = getWeekOfMonth(month, year)
  93. const days = getDaysCountOfMonth(month, year)
  94. const currentDaysArr = []
  95. const prevDaysArr = getPrevMonthLeftDays(year, month, firstDayOfMonth, model)
  96. const nextDaysArr = getNextMonthLeftDays(year, month, days, firstDayOfMonth, model)
  97. for (let i = 1; i <= days; i++) {
  98. currentDaysArr.push(getFullDays(year, month, i))
  99. }
  100. return prevDaysArr.concat(currentDaysArr).concat(nextDaysArr)
  101. }
  102. export const selectDayByIndex = (days, index) => days.map((day, idx) => {
  103. const tempDay = day
  104. tempDay.selected = index === idx
  105. return tempDay
  106. })
  107. export const setSelectedDays = (days, selectedDay) => {
  108. const fDate = formatDate(selectedDay)
  109. return days.map(day => {
  110. const tempDay = day
  111. tempDay.selected = day.full === fDate.format
  112. return tempDay
  113. })
  114. }
  115. export const getDaysAfterchangedYearOrMonth = (year = getCurrentYear(),
  116. month = getCurrentMonth(), model = CHINESE_MODEL) => (
  117. getDaysOfMonth(year, month, model)
  118. )
  119. export const isInCurrentMonth = date => {
  120. const currentDate = getCurrentDate()
  121. return date.substr(0, 7) === currentDate.substr(0, 7)
  122. }