todo.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import WxData from './wxData'
  2. import { Logger, uniqueArrayByDate, GetDate } from './utils'
  3. const logger = new Logger()
  4. const getDate = new GetDate()
  5. class Todo extends WxData {
  6. constructor(component) {
  7. super(component)
  8. this.Component = component
  9. }
  10. /**
  11. * 设置待办事项标志
  12. * @param {object} options 待办事项配置
  13. */
  14. setTodoLabels(options) {
  15. if (options) this.Component.todoConfig = options
  16. const calendar = this.getData('calendar')
  17. if (!calendar || !calendar.days) {
  18. return logger.warn('请等待日历初始化完成后再调用该方法')
  19. }
  20. const dates = [...calendar.days]
  21. const { curYear, curMonth } = calendar
  22. const {
  23. circle,
  24. dotColor = '',
  25. pos = 'bottom',
  26. showLabelAlways,
  27. days: todoDays = []
  28. } = options || this.Component.todoConfig || {}
  29. const { todoLabels = [] } = calendar
  30. const currentMonthTodoLabels = this.getTodoLabels({
  31. year: curYear,
  32. month: curMonth
  33. })
  34. let newTodoLabels = todoDays.filter(
  35. item => +item.year === +curYear && +item.month === +curMonth
  36. )
  37. if (this.Component.weekMode) {
  38. newTodoLabels = todoDays
  39. }
  40. const allTodos = currentMonthTodoLabels.concat(newTodoLabels)
  41. for (let todo of allTodos) {
  42. let target
  43. if (this.Component.weekMode) {
  44. target = dates.find(
  45. date =>
  46. +todo.year === +date.year &&
  47. +todo.month === +date.month &&
  48. +todo.day === +date.day
  49. )
  50. } else {
  51. target = dates[todo.day - 1]
  52. }
  53. if (!target) continue
  54. if (showLabelAlways) {
  55. target.showTodoLabel = true
  56. } else {
  57. target.showTodoLabel = !target.choosed
  58. }
  59. if (target.showTodoLabel) {
  60. target.todoText = todo.todoText
  61. }
  62. target.color = todo.color
  63. }
  64. const o = {
  65. 'calendar.days': dates,
  66. 'calendar.todoLabels': uniqueArrayByDate(todoLabels.concat(todoDays))
  67. }
  68. if (!circle) {
  69. o['calendar.todoLabelPos'] = pos
  70. o['calendar.todoLabelColor'] = dotColor
  71. }
  72. o['calendar.todoLabelCircle'] = circle || false
  73. o['calendar.showLabelAlways'] = showLabelAlways || false
  74. this.setData(o)
  75. }
  76. /**
  77. * 删除指定日期的待办事项
  78. * @param {array} todos 需要删除待办事项的日期
  79. */
  80. deleteTodoLabels(todos) {
  81. if (!(todos instanceof Array) || !todos.length) return
  82. const todoLabels = this.filterTodos(todos)
  83. const { days: dates, curYear, curMonth } = this.getData('calendar')
  84. const currentMonthTodoLabels = todoLabels.filter(
  85. item => curYear === +item.year && curMonth === +item.month
  86. )
  87. dates.forEach(item => {
  88. item.showTodoLabel = false
  89. })
  90. currentMonthTodoLabels.forEach(item => {
  91. dates[item.day - 1].showTodoLabel = !dates[item.day - 1].choosed
  92. })
  93. this.setData({
  94. 'calendar.days': dates,
  95. 'calendar.todoLabels': todoLabels
  96. })
  97. }
  98. /**
  99. * 清空所有待办事项
  100. */
  101. clearTodoLabels() {
  102. const { days = [] } = this.getData('calendar')
  103. const dates = [].concat(days)
  104. dates.forEach(item => {
  105. item.showTodoLabel = false
  106. })
  107. this.setData({
  108. 'calendar.days': dates,
  109. 'calendar.todoLabels': []
  110. })
  111. }
  112. /**
  113. * 获取所有待办事项
  114. * @param {object} target 指定年月
  115. * @param {number} [target.year] 年
  116. * @param {number} [target.month] 月
  117. */
  118. getTodoLabels(target) {
  119. const { todoLabels = [] } = this.getData('calendar')
  120. if (target) {
  121. const { year, month } = target
  122. const _todoLabels = todoLabels.filter(
  123. item => +item.year === +year && +item.month === +month
  124. )
  125. return _todoLabels
  126. }
  127. return todoLabels
  128. }
  129. /**
  130. * 过滤将删除的待办事项
  131. * @param {array} todos 需要删除待办事项
  132. */
  133. filterTodos(todos) {
  134. const todoLabels = this.getData('calendar.todoLabels') || []
  135. const deleteTodo = todos.map(item => getDate.toTimeStr(item))
  136. return todoLabels.filter(
  137. item => !deleteTodo.includes(getDate.toTimeStr(item))
  138. )
  139. }
  140. /**
  141. * 单选时显示待办事项
  142. * @param {array} todoDays
  143. * @param {array} days
  144. * @param {array} selectedDays
  145. */
  146. showTodoLabels(todoDays, days, selectedDays) {
  147. todoDays.forEach(item => {
  148. if (this.Component.weekMode) {
  149. days.forEach((_item, idx) => {
  150. if (+_item.day === +item.day) {
  151. const day = days[idx]
  152. day.hasTodo = true
  153. day.todoText = item.todoText
  154. if (
  155. selectedDays &&
  156. selectedDays.length &&
  157. +selectedDays[0].day === +item.day
  158. ) {
  159. day.showTodoLabel = true
  160. }
  161. }
  162. })
  163. } else {
  164. const day = days[item.day - 1]
  165. if (!day) return
  166. day.hasTodo = true
  167. day.todoText = item.todoText
  168. if (
  169. selectedDays &&
  170. selectedDays.length &&
  171. +selectedDays[0].day === +item.day
  172. ) {
  173. days[selectedDays[0].day - 1].showTodoLabel = true
  174. }
  175. }
  176. })
  177. }
  178. }
  179. export default component => new Todo(component)