todo.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 target = datesInfo[todo.date - 1]
  34. if (!target) continue
  35. if (showLabelAlways) {
  36. target.showTodoLabel = true
  37. } else {
  38. target.showTodoLabel = !target.choosed
  39. }
  40. if (target.showTodoLabel) {
  41. target.todoText = todo.todoText
  42. }
  43. target.color = todo.color
  44. }
  45. return datesInfo
  46. }
  47. export default () => {
  48. return {
  49. name: 'todo',
  50. methods(component) {
  51. return {
  52. setTodos: (options = {}) => {
  53. const calendar = getCalendarData('calendar', component)
  54. if (!calendar || !calendar.dates) {
  55. return Promise.reject('请等待日历初始化完成后再调用该方法')
  56. }
  57. let dates = [...calendar.dates]
  58. const { curYear, curMonth } = calendar
  59. const {
  60. circle,
  61. dotColor = '',
  62. pos = 'bottom',
  63. showLabelAlways,
  64. dates: todoDates = []
  65. } = options
  66. const { todos = [] } = calendar
  67. const allTodosOfThisMonths = filterTodos({
  68. curYear,
  69. curMonth,
  70. exsitedTodos: todos,
  71. toSetTodos: todoDates
  72. })
  73. dates = updateDatePropertyOfTodoLabel(
  74. allTodosOfThisMonths,
  75. dates,
  76. showLabelAlways
  77. )
  78. const calendarData = {
  79. dates,
  80. todos: dateUtil.uniqueArrayByDate(
  81. todos.concat(
  82. todoDates.map(date => dateUtil.tranformStr2NumOfDate(date))
  83. )
  84. )
  85. }
  86. if (!circle) {
  87. calendarData.todoLabelPos = pos
  88. calendarData.todoLabelColor = dotColor
  89. }
  90. calendarData.todoLabelCircle = circle || false
  91. calendarData.showLabelAlways = showLabelAlways || false
  92. const existCalendarData = getCalendarData('calendar', component)
  93. return renderCalendar.call(component, {
  94. ...existCalendarData,
  95. ...calendarData
  96. })
  97. },
  98. deleteTodos(todos = []) {
  99. if (!(todos instanceof Array) || !todos.length)
  100. return Promise.reject('deleteTodos()应为入参为非空数组')
  101. const existCalendarData = getCalendarData('calendar', component)
  102. const allTodos = existCalendarData.todos || []
  103. const toDeleteTodos = todos.map(item => dateUtil.toTimeStr(item))
  104. const remainTodos = allTodos.filter(
  105. item => !toDeleteTodos.includes(dateUtil.toTimeStr(item))
  106. )
  107. const { dates, curYear, curMonth } = existCalendarData
  108. const _dates = [...dates]
  109. const currentMonthTodos = dateUtil.filterDatesByYM(
  110. {
  111. year: curYear,
  112. month: curMonth
  113. },
  114. remainTodos
  115. )
  116. _dates.forEach(item => {
  117. item.showTodoLabel = false
  118. })
  119. currentMonthTodos.forEach(item => {
  120. _dates[item.date - 1].showTodoLabel = !_dates[item.date - 1].choosed
  121. })
  122. return renderCalendar.call(component, {
  123. ...existCalendarData,
  124. dates: _dates,
  125. todos: remainTodos
  126. })
  127. },
  128. clearTodos() {
  129. const existCalendarData = getCalendarData('calendar', component)
  130. const _dates = [...existCalendarData.dates]
  131. _dates.forEach(item => {
  132. item.showTodoLabel = false
  133. })
  134. return renderCalendar.call(component, {
  135. ...existCalendarData,
  136. dates: _dates,
  137. todos: []
  138. })
  139. },
  140. getTodos() {
  141. return getCalendarData('calendar.todos', component) || []
  142. }
  143. }
  144. }
  145. }
  146. }