todo.js
5.05 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import WxData from './wxData'
import { Logger, uniqueArrayByDate, GetDate } from './utils'
const logger = new Logger()
const getDate = new GetDate()
class Todo extends WxData {
constructor(component) {
super(component)
this.Component = component
}
/**
* 设置待办事项标志
* @param {object} options 待办事项配置
*/
setTodoLabels(options) {
if (options) this.Component.todoConfig = options
const calendar = this.getData('calendar')
if (!calendar || !calendar.days) {
return logger.warn('请等待日历初始化完成后再调用该方法')
}
const dates = [...calendar.days]
const { curYear, curMonth } = calendar
const {
circle,
dotColor = '',
pos = 'bottom',
showLabelAlways,
days: todoDays = []
} = options || this.Component.todoConfig || {}
const { todoLabels = [] } = calendar
const currentMonthTodoLabels = this.getTodoLabels({
year: curYear,
month: curMonth
})
let newTodoLabels = todoDays.filter(
item => +item.year === +curYear && +item.month === +curMonth
)
if (this.Component.weekMode) {
newTodoLabels = todoDays
}
const allTodos = currentMonthTodoLabels.concat(newTodoLabels)
for (let todo of allTodos) {
let target
if (this.Component.weekMode) {
target = dates.find(
date =>
+todo.year === +date.year &&
+todo.month === +date.month &&
+todo.day === +date.day
)
} else {
target = dates[todo.day - 1]
}
if (!target) continue
if (showLabelAlways) {
target.showTodoLabel = true
} else {
target.showTodoLabel = !target.choosed
}
if (target.showTodoLabel) {
target.todoText = todo.todoText
}
target.color = todo.color
}
const o = {
'calendar.days': dates,
'calendar.todoLabels': uniqueArrayByDate(todoLabels.concat(todoDays))
}
if (!circle) {
o['calendar.todoLabelPos'] = pos
o['calendar.todoLabelColor'] = dotColor
}
o['calendar.todoLabelCircle'] = circle || false
o['calendar.showLabelAlways'] = showLabelAlways || false
this.setData(o)
}
/**
* 删除指定日期的待办事项
* @param {array} todos 需要删除待办事项的日期
*/
deleteTodoLabels(todos) {
if (!(todos instanceof Array) || !todos.length) return
const todoLabels = this.filterTodos(todos)
const { days: dates, curYear, curMonth } = this.getData('calendar')
const currentMonthTodoLabels = todoLabels.filter(
item => curYear === +item.year && curMonth === +item.month
)
dates.forEach(item => {
item.showTodoLabel = false
})
currentMonthTodoLabels.forEach(item => {
dates[item.day - 1].showTodoLabel = !dates[item.day - 1].choosed
})
this.setData({
'calendar.days': dates,
'calendar.todoLabels': todoLabels
})
}
/**
* 清空所有待办事项
*/
clearTodoLabels() {
const { days = [] } = this.getData('calendar')
const dates = [].concat(days)
dates.forEach(item => {
item.showTodoLabel = false
})
this.setData({
'calendar.days': dates,
'calendar.todoLabels': []
})
}
/**
* 获取所有待办事项
* @param {object} target 指定年月
* @param {number} [target.year] 年
* @param {number} [target.month] 月
*/
getTodoLabels(target) {
const { todoLabels = [] } = this.getData('calendar')
if (target) {
const { year, month } = target
const _todoLabels = todoLabels.filter(
item => +item.year === +year && +item.month === +month
)
return _todoLabels
}
return todoLabels
}
/**
* 过滤将删除的待办事项
* @param {array} todos 需要删除待办事项
*/
filterTodos(todos) {
const todoLabels = this.getData('calendar.todoLabels') || []
const deleteTodo = todos.map(item => getDate.toTimeStr(item))
return todoLabels.filter(
item => !deleteTodo.includes(getDate.toTimeStr(item))
)
}
/**
* 单选时显示待办事项
* @param {array} todoDays
* @param {array} days
* @param {array} selectedDays
*/
showTodoLabels(todoDays, days, selectedDays) {
todoDays.forEach(item => {
if (this.Component.weekMode) {
days.forEach((_item, idx) => {
if (+_item.day === +item.day) {
const day = days[idx]
day.hasTodo = true
day.todoText = item.todoText
if (
selectedDays &&
selectedDays.length &&
+selectedDays[0].day === +item.day
) {
day.showTodoLabel = true
}
}
})
} else {
const day = days[item.day - 1]
if (!day) return
day.hasTodo = true
day.todoText = item.todoText
if (
selectedDays &&
selectedDays.length &&
+selectedDays[0].day === +item.day
) {
days[selectedDays[0].day - 1].showTodoLabel = true
}
}
})
}
}
export default component => new Todo(component)