helper.js 2.9 KB

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