helper.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import {
  2. weekMap, CHINESE_MODEL, PREV_DAY, CURRENT_DAY, NEXT_DAY, monthMap,
  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. const compareDate = (d1, d2) => {
  108. let dNum1 = d1
  109. let dNum2 = d2
  110. if (typeof d1 === 'string') {
  111. dNum1 = +d1.split(/[/\-\\:]/).join('')
  112. }
  113. if (typeof d2 === 'string') {
  114. dNum2 = +d2.split(/[/\-\\:]/).join('')
  115. }
  116. if (dNum1 > dNum2) {
  117. return 1
  118. }
  119. if (dNum1 < dNum2) {
  120. return -1
  121. }
  122. return 0
  123. }
  124. export const setSelectedDaysAndRange = (days, selectedDay, disabledRange) => {
  125. const fDate = formatDate(selectedDay)
  126. let range = disabledRange
  127. if (!range) {
  128. range = [0]
  129. }
  130. if (!range[1]) {
  131. range.unshift(0)
  132. }
  133. return days.map(day => {
  134. const tempDay = day
  135. tempDay.selected = day.full === fDate.format
  136. tempDay.disabled = compareDate(day.full, range[0]) >= 0 && compareDate(day.full, range[1]) <= 0
  137. return tempDay
  138. })
  139. }
  140. export const getDaysAfterchangedYearOrMonth = (year = getCurrentYear(),
  141. month = getCurrentMonth(), model = CHINESE_MODEL) => (
  142. getDaysOfMonth(year, month, model)
  143. )
  144. export const isInCurrentMonth = (date, current = getCurrentDate()) => (
  145. date.substring(0, 7) === current.substring(0, 7)
  146. )
  147. export const resetCalendarFromSpecialDay = (originDays, date,
  148. current = getCurrentDate(), model = CHINESE_MODEL, disabledRange) => {
  149. let days = originDays
  150. if (!isInCurrentMonth(date, current)) {
  151. const year = date.substring(0, 4)
  152. const month = date.substring(5, 7)
  153. days = getDaysAfterchangedYearOrMonth(year, month, model)
  154. const afterDays = setSelectedDaysAndRange(days, date, disabledRange)
  155. return {
  156. afterDays: afterDays,
  157. changeYear: year,
  158. changeMonth: month,
  159. }
  160. }
  161. const afterDays = setSelectedDaysAndRange(days, date, disabledRange)
  162. return { afterDays }
  163. }
  164. export const getDecadeByGivenYear = (decade, year) => {
  165. const tempDecade = +decade
  166. const factor = Math.floor(tempDecade / 10)
  167. const result = Array(12).fill({})
  168. result[0] = {
  169. flag: 'prev',
  170. value: 10 * factor - 1,
  171. code: 10 * factor - 1,
  172. }
  173. result[11] = {
  174. flag: 'next',
  175. value: 10 * (factor + 1),
  176. code: 10 * (factor + 1),
  177. }
  178. for (let i = 0; i <= 9; i++) {
  179. const r = 10 * factor + i
  180. /* eslint-disable eqeqeq */
  181. if (r === tempDecade && year == tempDecade) {
  182. /* eslint-enable eqeqeq */
  183. result[i + 1] = {
  184. flag: 'current',
  185. value: r,
  186. code: r,
  187. }
  188. } else {
  189. result[i + 1] = {
  190. flag: 'normal',
  191. value: r,
  192. code: r,
  193. }
  194. }
  195. }
  196. return result
  197. }
  198. export const getChineseMonth = month => {
  199. const months = [...monthMap.values()]
  200. return months.map((m, i) => {
  201. const code = i + 1
  202. if (code === +month) {
  203. return {
  204. flag: 'current',
  205. value: m,
  206. code: code,
  207. }
  208. }
  209. return {
  210. flag: 'normal',
  211. value: m,
  212. code: code,
  213. }
  214. })
  215. }