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