week.js
13.2 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/**
* @Author: drfu*
* @Description: 周视图
* @Date: 2020-10-08 21:22:09*
* @Last Modified by: drfu
* @Last Modified time: 2020-10-12 14:39:45
* */
import { renderCalendar } from '../render'
import {
getCalendarConfig,
getCalendarData,
logger,
dateUtil
} from '../utils/index'
import { calcJumpData } from '../core'
/**
* 当月第一周所有日期
*/
function firstWeekInMonth(
target = {},
calendarDates = [],
calendarConfig = {}
) {
const { firstDayOfWeek } = calendarConfig
const firstDayOfWeekIsMon = firstDayOfWeek === 'Mon'
const { year, month } = target
let firstDay = dateUtil.getDayOfWeek(year, month, 1)
if (firstDayOfWeekIsMon && firstDay === 0) {
firstDay = 7
}
const [, end] = [0, 7 - firstDay]
return calendarDates.slice(0, firstDayOfWeekIsMon ? end + 1 : end)
}
/**
* 当月最后一周所有日期
*/
function lastWeekInMonth(target = {}, calendarDates = [], calendarConfig = {}) {
const { firstDayOfWeek } = calendarConfig
const firstDayOfWeekIsMon = firstDayOfWeek === 'Mon'
const { year, month } = target
const lastDay = dateUtil.getDatesCountOfMonth(year, month)
let lastDayWeek = dateUtil.getDayOfWeek(year, month, lastDay)
if (firstDayOfWeekIsMon && lastDayWeek === 0) {
lastDayWeek = 7
}
const [start, end] = [lastDay - lastDayWeek, lastDay]
return calendarDates.slice(firstDayOfWeekIsMon ? start : start - 1, end)
}
/**
* 判断目标日期是否在某些指定日历内
*/
function dateIsInDatesRange(target, dates) {
if (!target || !dates || !dates.length) return false
const targetDateStr = dateUtil.toTimeStr(target)
let rst = false
for (let date of dates) {
const dateStr = dateUtil.toTimeStr(date)
if (dateStr === targetDateStr) {
rst = true
return rst
}
rst = false
}
return rst
}
function getDatesWhenTargetInFirstWeek(target, firstWeekDates) {
const { year, month } = target
const prevMonthInfo = dateUtil.getPrevMonthInfo({ year, month })
let lastMonthDatesCount = dateUtil.getDatesCountOfMonth(
prevMonthInfo.year,
prevMonthInfo.month
)
let dates = firstWeekDates
let firstWeekCount = firstWeekDates.length
for (let i = 0; i < 7 - firstWeekCount; i++) {
const week = dateUtil.getDayOfWeek(+year, +month, lastMonthDatesCount)
dates.unshift({
year: prevMonthInfo.year,
month: prevMonthInfo.month,
date: lastMonthDatesCount,
week
})
lastMonthDatesCount -= 1
}
return dates
}
function getDatesWhenTargetInLastWeek(target, lastWeekDates) {
const { year, month } = target
const prevMonthInfo = dateUtil.getNextMonthInfo({ year, month })
let dates = lastWeekDates
let lastWeekCount = lastWeekDates.length
for (let i = 0; i < 7 - lastWeekCount; i++) {
const week = dateUtil.getDayOfWeek(+year, +month, i + 1)
dates.push({
year: prevMonthInfo.year,
month: prevMonthInfo.month,
date: i + 1,
week
})
}
return dates
}
function getDates(target, calendarDates = [], calendarConfig = {}) {
const { year, month, date } = target
const targetDay = dateUtil.getDayOfWeek(year, month, date)
const { firstDayOfWeek } = calendarConfig
const firstDayOfWeekIsMon = firstDayOfWeek === 'Mon'
if (firstDayOfWeekIsMon) {
const startIdx = date - (targetDay || 7)
return calendarDates.splice(startIdx, 7)
} else {
const startIdx = date - targetDay - 1
return calendarDates.splice(startIdx, 7)
}
}
function getTargetWeekDates(target, calendarConfig) {
if (!target) return
const { year, month } = target
const calendarDates = dateUtil.calcDates(year, month)
const firstWeekDates = firstWeekInMonth(target, calendarDates, calendarConfig)
const lastWeekDates = lastWeekInMonth(target, calendarDates, calendarConfig)
if (dateIsInDatesRange(target, firstWeekDates)) {
return getDatesWhenTargetInFirstWeek(target, firstWeekDates)
} else if (dateIsInDatesRange(target, lastWeekDates)) {
return getDatesWhenTargetInLastWeek(target, lastWeekDates)
} else {
return getDates(target, calendarDates, calendarConfig)
}
}
/**
* 计算周视图下当前这一周最后一天
*/
function calculateLastDateOfCurrentWeek(calendarData = {}) {
const { dates = [] } = calendarData
return dates[dates.length - 1]
}
/**
* 计算周视图下当前这一周第一天
*/
function calculateFirstDateOfCurrentWeek(calendarData = {}) {
const { dates } = calendarData
return dates[0]
}
/**
* 计算下一周的日期
*/
function calculateNextWeekDates(calendarData = {}) {
let { curYear, curMonth } = calendarData
let calendarDates = []
let lastDateInThisWeek = calculateLastDateOfCurrentWeek(calendarData)
const { year: LYear, month: LMonth } = lastDateInThisWeek
if (curYear !== LYear || curMonth !== LMonth) {
calendarDates = dateUtil.calcDates(LYear, LMonth)
curYear = LYear
curMonth = LMonth
} else {
calendarDates = dateUtil.calcDates(curYear, curMonth)
}
const lastDateInThisMonth = dateUtil.getDatesCountOfMonth(curYear, curMonth)
const count = lastDateInThisMonth - lastDateInThisWeek.date
const lastDateIdx = calendarDates.findIndex(
date => dateUtil.toTimeStr(date) === dateUtil.toTimeStr(lastDateInThisWeek)
)
const startIdx = lastDateIdx + 1
if (count >= 7) {
return {
dates: calendarDates.splice(startIdx, 7),
year: curYear,
month: curMonth
}
} else {
const nextMonth = dateUtil.getNextMonthInfo({
year: curYear,
month: curMonth
})
const { year, month } = nextMonth || {}
const calendarDatesOfNextMonth = dateUtil.calcDates(year, month)
const remainDatesOfThisMonth = calendarDates.splice(startIdx)
const patchDatesOfNextMonth = calendarDatesOfNextMonth.splice(
0,
7 - remainDatesOfThisMonth.length
)
return {
dates: [...remainDatesOfThisMonth, ...patchDatesOfNextMonth],
...nextMonth
}
}
}
/**
* 计算上一周的日期
*/
function calculatePrevWeekDates(calendarData = {}) {
let { curYear, curMonth } = calendarData
let firstDateInThisWeek = calculateFirstDateOfCurrentWeek(calendarData)
let calendarDates = []
const { year: FYear, month: FMonth } = firstDateInThisWeek
if (curYear !== FYear || curMonth !== FMonth) {
calendarDates = dateUtil.calcDates(FYear, FMonth)
curYear = FYear
curMonth = FMonth
} else {
calendarDates = dateUtil.calcDates(curYear, curMonth)
}
const firstDateIdx = calendarDates.findIndex(
date => dateUtil.toTimeStr(date) === dateUtil.toTimeStr(firstDateInThisWeek)
)
if (firstDateIdx - 7 >= 0) {
const startIdx = firstDateIdx - 7
return {
dates: calendarDates.splice(startIdx, 7),
year: curYear,
month: curMonth
}
} else {
const prevMonth = dateUtil.getPrevMonthInfo({
year: curYear,
month: curMonth
})
const { year, month } = prevMonth || {}
const calendarDatesOfPrevMonth = dateUtil.calcDates(year, month)
const remainDatesOfThisMonth = calendarDates.splice(
0,
firstDateInThisWeek.date - 1
)
const patchDatesOfPrevMonth = calendarDatesOfPrevMonth.splice(
-(7 - remainDatesOfThisMonth.length)
)
return {
dates: [...patchDatesOfPrevMonth, ...remainDatesOfThisMonth],
...prevMonth
}
}
}
export default () => {
return {
name: 'week',
beforeRender(calendarData = {}, calendarConfig = {}, component) {
const { initializedWeekMode, selectedDates } = calendarData
if (calendarConfig.weekMode && !initializedWeekMode) {
const { defaultDate } = calendarConfig
const target =
(selectedDates && selectedDates[0]) ||
(defaultDate && dateUtil.transformDateRow2Dict(defaultDate)) ||
dateUtil.todayFMD()
const waitRenderData = this.methods(
component
).__calcDatesWhenSwitchView('week', target)
const { data, config } = waitRenderData || {}
const setSelectDates = this.methods(
component
).__selectTargetDateWhenJump(target, data.dates, config)
return {
calendarData: {
...data,
...setSelectDates,
weeksCh: dateUtil.getWeekHeader(calendarConfig.firstDayOfWeek),
initializedWeekMode: true
},
calendarConfig
}
}
return {
calendarData,
calendarConfig
}
},
onSwitchCalendar(target = {}, calendarData = {}, component) {
const { direction } = target
const { curYear, curMonth } = calendarData
const calendarConfig = getCalendarConfig(component)
let waitRenderData = {}
if (calendarConfig.weekMode) {
if (direction === 'left') {
waitRenderData = calculateNextWeekDates(calendarData)
} else {
waitRenderData = calculatePrevWeekDates(calendarData)
}
const { dates, year, month } = waitRenderData
return {
...calendarData,
dates,
curYear: year || curYear,
curMonth: month || curMonth
}
}
return calendarData
},
methods(component) {
return {
__selectTargetDateWhenJump: (target = {}, dates = [], config = {}) => {
let selectedDate = target
const weekDates = dates.map((date, idx) => {
const tmp = { ...date }
tmp.id = idx
const isTarget =
dateUtil.toTimeStr(target) === dateUtil.toTimeStr(tmp)
if (isTarget && !target.choosed && config.autoChoosedWhenJump) {
tmp.choosed = true
selectedDate = tmp
}
return tmp
})
return {
dates: weekDates,
selectedDates: [selectedDate]
}
},
__calcDatesForWeekMode(target, config = {}, calendarData = {}) {
const { year, month } = target || {}
const weekDates = getTargetWeekDates(target, config)
weekDates.forEach((date, idx) => (date.id = idx))
return {
data: {
...calendarData,
prevMonthGrids: null,
nextMonthGrids: null,
dates: weekDates,
curYear: year,
curMonth: month
},
config: {
...config,
weekMode: true
}
}
},
__calcDatesForMonthMode(target, config = {}, calendarData = {}) {
const { year, month } = target || {}
const waitRenderData = calcJumpData({
dateInfo: target,
config
})
return {
data: {
...calendarData,
...waitRenderData,
curYear: year,
curMonth: month
},
config: {
...config,
weekMode: false
}
}
},
/**
* 周、月视图切换
* @param {string} view 视图 [week, month]
* @param {object} target
*/
__calcDatesWhenSwitchView: (view, target) => {
const calendarConfig = getCalendarConfig(component)
if (calendarConfig.multi)
return logger.warn('多选模式不能切换周月视图')
const existCalendarData = getCalendarData('calendar', component) || {}
const {
selectedDates = [],
dates = [],
curYear,
curMonth
} = existCalendarData
const currentMonthSelected = selectedDates.filter(
item => curYear === +item.year || curMonth === +item.month
)
let jumpTarget = {}
if (target) {
jumpTarget = target
} else {
if (currentMonthSelected.length) {
jumpTarget = currentMonthSelected.pop()
} else {
jumpTarget = dates[0]
}
}
if (view === 'week') {
return this.methods(component).__calcDatesForWeekMode(
jumpTarget,
calendarConfig,
existCalendarData
)
} else {
return this.methods(component).__calcDatesForMonthMode(
jumpTarget,
calendarConfig,
existCalendarData
)
}
},
weekModeJump: dateInfo => {
const target = dateInfo || dateUtil.todayFMD()
const existCalendarData = getCalendarData('calendar', component) || {}
const waitRenderData = this.methods(
component
).__calcDatesWhenSwitchView('week', target)
const { data, config } = waitRenderData || {}
const setSelectDates = this.methods(
component
).__selectTargetDateWhenJump(target, data.dates, config)
return renderCalendar.call(
component,
{
...existCalendarData,
...data,
...setSelectDates
},
config
)
},
switchView: (view, target) => {
const waitRenderData = this.methods(
component
).__calcDatesWhenSwitchView(view, target)
const { data, config } = waitRenderData || {}
if (!data) return logger.warn('当前状态不能切换为周视图')
return renderCalendar.call(component, data, config)
}
}
}
}
}