todo.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /**
  2. * @Author: drfu*
  3. * @Description: 代办事项
  4. * @Date: 2020-10-08 21:22:09*
  5. * @Last Modified by: drfu
  6. * @Last Modified time: 2020-10-11 14:23:02
  7. * */
  8. import { getCalendarData, dateUtil } from '../utils/index'
  9. import { renderCalendar } from '../render'
  10. function filterTodos({ curYear, curMonth, exsitedTodos, toSetTodos }) {
  11. const exsitedCurrentMonthTodos = dateUtil.filterDatesByYM(
  12. {
  13. year: curYear,
  14. month: curMonth
  15. },
  16. exsitedTodos
  17. )
  18. const toSetTodosOfThisMonth = dateUtil.filterDatesByYM(
  19. {
  20. year: curYear,
  21. month: curMonth
  22. },
  23. toSetTodos
  24. )
  25. const allTodosOfThisMonths = dateUtil.uniqueArrayByDate(
  26. exsitedCurrentMonthTodos.concat(toSetTodosOfThisMonth)
  27. )
  28. return allTodosOfThisMonths
  29. }
  30. function updateDatePropertyOfTodoLabel(todos, dates, showLabelAlways) {
  31. const datesInfo = [...dates]
  32. for (let todo of todos) {
  33. let targetIdx = datesInfo.findIndex(
  34. item => dateUtil.toTimeStr(item) === dateUtil.toTimeStr(todo)
  35. )
  36. let target = datesInfo[targetIdx]
  37. if (!target) continue
  38. if (showLabelAlways) {
  39. target.showTodoLabel = true
  40. } else {
  41. target.showTodoLabel = !target.choosed
  42. }
  43. if (target.showTodoLabel) {
  44. target.todoText = todo.todoText
  45. }
  46. target.color = todo.color
  47. }
  48. return datesInfo
  49. }
  50. export default () => {
  51. return {
  52. name: 'todo',
  53. methods(component) {
  54. return {
  55. setTodos: (options = {}) => {
  56. const calendar = getCalendarData('calendar', component)
  57. if (!calendar || !calendar.dates) {
  58. return Promise.reject('请等待日历初始化完成后再调用该方法')
  59. }
  60. let dates = [...calendar.dates]
  61. const { curYear, curMonth } = calendar
  62. const {
  63. circle,
  64. dotColor = '',
  65. pos = 'bottom',
  66. showLabelAlways,
  67. dates: todoDates = []
  68. } = options
  69. const { todos = [] } = calendar
  70. const allTodosOfThisMonths = filterTodos({
  71. curYear,
  72. curMonth,
  73. exsitedTodos: todos,
  74. toSetTodos: todoDates
  75. })
  76. dates = updateDatePropertyOfTodoLabel(
  77. allTodosOfThisMonths,
  78. dates,
  79. showLabelAlways
  80. )
  81. const calendarData = {
  82. dates,
  83. todos: dateUtil.uniqueArrayByDate(
  84. todos.concat(
  85. todoDates.map(date => dateUtil.tranformStr2NumOfDate(date))
  86. )
  87. )
  88. }
  89. if (!circle) {
  90. calendarData.todoLabelPos = pos
  91. calendarData.todoLabelColor = dotColor
  92. }
  93. calendarData.todoLabelCircle = circle || false
  94. calendarData.showLabelAlways = showLabelAlways || false
  95. const existCalendarData = getCalendarData('calendar', component)
  96. return renderCalendar.call(component, {
  97. ...existCalendarData,
  98. ...calendarData
  99. })
  100. },
  101. deleteTodos(todos = []) {
  102. if (!(todos instanceof Array) || !todos.length)
  103. return Promise.reject('deleteTodos()应为入参为非空数组')
  104. const existCalendarData = getCalendarData('calendar', component)
  105. const allTodos = existCalendarData.todos || []
  106. const toDeleteTodos = todos.map(item => dateUtil.toTimeStr(item))
  107. const remainTodos = allTodos.filter(
  108. item => !toDeleteTodos.includes(dateUtil.toTimeStr(item))
  109. )
  110. const { dates, curYear, curMonth } = existCalendarData
  111. const _dates = [...dates]
  112. const currentMonthTodos = dateUtil.filterDatesByYM(
  113. {
  114. year: curYear,
  115. month: curMonth
  116. },
  117. remainTodos
  118. )
  119. _dates.forEach(item => {
  120. item.showTodoLabel = false
  121. })
  122. currentMonthTodos.forEach(item => {
  123. _dates[item.date - 1].showTodoLabel = !_dates[item.date - 1].choosed
  124. })
  125. return renderCalendar.call(component, {
  126. ...existCalendarData,
  127. dates: _dates,
  128. todos: remainTodos
  129. })
  130. },
  131. clearTodos() {
  132. const existCalendarData = getCalendarData('calendar', component)
  133. const _dates = [...existCalendarData.dates]
  134. _dates.forEach(item => {
  135. item.showTodoLabel = false
  136. })
  137. return renderCalendar.call(component, {
  138. ...existCalendarData,
  139. dates: _dates,
  140. todos: []
  141. })
  142. },
  143. getTodos() {
  144. return getCalendarData('calendar.todos', component) || []
  145. }
  146. }
  147. }
  148. }
  149. }