calendarMonth.helpers.js
12 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
/**
* @file CalendarMonth 纯逻辑 — 模式解析、月份构建、可见窗口(不调 setData)
* 供 CalendarMonth 组件 attached / Calendar_Function 与单测共用。
*/
/**
* 解析 monthOnly(兼容历史 monType 属性名)
* @param {boolean} monthOnly
* @param {boolean} monType
* @returns {boolean}
*/
export function resolveMonthOnly(monthOnly, monType) {
return Boolean(monthOnly || monType)
}
/**
* 解析 dayOnly;monthOnly 优先,启用 monthOnly 时不走 dayOnly
* @param {boolean} dayOnly
* @param {boolean} monthOnly
* @returns {boolean}
*/
export function resolveDayOnly(dayOnly, monthOnly) {
if (monthOnly) return false
return Boolean(dayOnly)
}
/**
* 初始 dayType(0=月份视图,1=日期视图)
* @param {{ monthOnly?: boolean, dayOnly?: boolean }} options
* @returns {number}
*/
export function getInitialDayType(options = {}) {
const { monthOnly = false } = options
if (monthOnly) return 0
return 1
}
/**
* 是否展示「按日期 / 按月份」切换
* @param {boolean} monthOnly
* @param {boolean} dayOnly
* @param {boolean} [monType]
* @returns {boolean}
*/
export function shouldShowDayMonthToggle(monthOnly, dayOnly, monType = false) {
return !resolveMonthOnly(monthOnly, monType) && !resolveDayOnly(dayOnly, monthOnly)
}
/**
* 是否渲染月份 swiper 面板(与 WXML wx:if 表达式一致)
* @param {{ dayOnly?: boolean, dayType?: number, monthOnly?: boolean, monType?: boolean }} options
* @returns {boolean}
*/
export function shouldShowMonthPanel({ dayOnly = false, dayType = 1, monthOnly = false, monType = false } = {}) {
if (resolveMonthOnly(monthOnly, monType)) return true
if (resolveDayOnly(dayOnly, monthOnly)) return false
return !dayType
}
/**
* dayOnly 日选择是否走单月渲染(无全量 swiper)
* @param {boolean} dayOnly
* @param {boolean} [monthOnly]
* @param {boolean} [monType]
* @returns {boolean}
*/
export function shouldUseSingleMonthView(dayOnly, monthOnly = false, monType = false) {
return resolveDayOnly(dayOnly, resolveMonthOnly(monthOnly, monType))
}
/**
* dayOnly 模式下是否跳过 yearList 构建
* @param {{ dayOnly?: boolean, monthOnly?: boolean, monType?: boolean }} options
* @returns {boolean}
*/
export function shouldBuildYearList({ dayOnly = false, monthOnly = false, monType = false } = {}) {
return !resolveDayOnly(dayOnly, resolveMonthOnly(monthOnly, monType))
}
/**
* @param {number} n
* @returns {string}
*/
function pad2(n) {
return n < 10 ? `0${n}` : String(n)
}
/**
* @param {Date} date
* @param {string} separator
* @returns {string}
*/
export function formatYMD(date, separator = '-') {
const d = new Date(date)
return `${d.getFullYear()}${separator}${pad2(d.getMonth() + 1)}${separator}${pad2(d.getDate())}`
}
/**
* @param {Date} date
* @param {string} separator
* @returns {string}
*/
export function formatYM(date, separator = '-') {
const d = new Date(date)
return `${d.getFullYear()}${separator}${pad2(d.getMonth() + 1)}`
}
/**
* 获取月份天数
* @param {number|string} year
* @param {number} month
* @returns {number}
*/
export function getDayNumber(year, month) {
const y = Number(year)
let days = 31
switch (month) {
case 2:
days = (y % 4 === 0 && y % 100 !== 0) || y % 400 === 0 ? 29 : 28
break
case 4:
case 6:
case 9:
case 11:
days = 30
break
default:
break
}
return days
}
/**
* 两日期相差月数
* @param {string} date1
* @param {string} date2
* @param {string} [separator]
* @returns {number}
*/
export function getMonthDifference(date1, date2, separator = '-') {
const [year1, month1] = date1.split(separator)
const [year2, month2] = date2.split(separator)
return (parseInt(year2, 10) - parseInt(year1, 10)) * 12 + (parseInt(month2, 10) - parseInt(month1, 10))
}
/**
* 为 range 模式写入 rangeLabel,减少 WXML 嵌套
* @param {Array<{ data: object[] }>} monthList
* @param {{ start?: string, end?: string }} [rangeText]
* @returns {Array<{ data: object[] }>}
*/
export function applyRangeLabels(monthList, rangeText = { start: '开始', end: '结束' }) {
monthList.forEach(month => {
month.data.forEach(day => {
if (day.start) day.rangeLabel = rangeText.start
else if (day.end) day.rangeLabel = rangeText.end
else day.rangeLabel = ''
})
})
return monthList
}
/**
* 构建单个月份的日格数据(纯函数,不 setData)
* @param {number} dateMs 该月任意时刻时间戳
* @param {object} ctx
* @returns {{ month: { mark: number, data: object[] }, monthKey: string, year: string }}
*/
export function buildMonthEntry(dateMs, ctx) {
const {
separator,
firstDayOfWeek,
allowSameDay,
minDate,
maxDate,
dateType,
today,
rangeDate,
multipleDate,
nextDay,
rangeText
} = ctx
const [year, month] = formatYM(dateMs, separator).split(separator)
const days = getDayNumber(year, Number(month))
const week = new Date(Number(year), Number(month) - 1, 1).getDay()
const [now_year, now_month, now_day] = today.split(separator)
const [next_year, next_month, next_day] = allowSameDay
? today.split(separator)
: (nextDay || today).split(separator)
const arr = []
let fdw = firstDayOfWeek === 7 ? 0 : firstDayOfWeek
for (let i = 1; i <= days; i++) {
const todayStr = `${year}${separator}${month}${separator}${now_day}`
const dayStr = `${year}${separator}${month}${separator}${i < 10 ? `0${i}` : i}`
const obj = {
day: i,
data: dayStr,
active: now_year === year && now_month === month && i === Number(now_day),
rangeLabel: ''
}
if ((minDate && Date.parse(minDate) > Date.parse(dayStr)) || (maxDate && Date.parse(maxDate) < Date.parse(dayStr))) {
obj.disable = true
}
if (dateType === 'multiple') multipleDate.push(todayStr)
if (nextDay && dateType === 'range') {
if (now_year === year && now_month === month && i === Number(now_day)) {
obj.start = true
rangeDate.start = todayStr
obj.rangeLabel = rangeText.start
}
if (next_year === year && next_month === month && i === Number(next_day)) {
obj.active = true
obj.end = true
rangeDate.end = `${year}${separator}${next_month}${separator}${next_day}`
obj.rangeLabel = rangeText.end
}
}
if (i === 1) obj.week = `week-${week - fdw}`
arr.push(obj)
}
return {
month: { data: arr, mark: Number(month) },
monthKey: `${year}${separator}${month}`,
year
}
}
/**
* 一次性构建 monthList / dateList 等(替代循环 setData)
* @param {object} options
* @returns {object}
*/
export function buildAllMonthsMeta(options = {}) {
const {
separator = '-',
previous: previousProp = 3,
next: nextProp = 3,
minDate: minDateIn,
maxDate: maxDateIn,
firstDayOfWeek = 7,
allowSameDay = false,
dateType = 'single',
value = '',
nowDate = new Date(),
rangeText = { start: '开始', end: '结束' }
} = options
let minDate = minDateIn
let maxDate = maxDateIn
let previous = previousProp
let next = nextProp
const nowMonth = nowDate.getMonth()
const today = value || formatYMD(nowDate, separator)
if (maxDate || minDate) {
if (!minDate) {
minDate = formatYMD(new Date(nowDate.getFullYear(), nowMonth - previousProp, 1), separator)
}
if (!maxDate) {
maxDate = formatYMD(nowDate, separator)
}
previous = getMonthDifference(minDate, formatYMD(nowDate, separator), separator)
next = getMonthDifference(formatYMD(nowDate, separator), maxDate, separator)
} else {
previous = previous < 0 ? 0 : previous
next = next < 0 ? 0 : next
}
const monthList = []
const dateList = []
const monPickList = []
const rangeDate = {}
const multipleDate = []
const now = new Date(nowDate.getFullYear(), nowDate.getMonth(), 1).getTime()
let nextDay = ''
if (dateType === 'range') {
const t = new Date(today)
t.setDate(t.getDate() + 1)
nextDay = formatYMD(t, separator)
}
for (let i = nowMonth - previous; i <= nowMonth + next; i++) {
const monthDate = new Date(now)
monthDate.setMonth(i)
const entry = buildMonthEntry(monthDate.getTime(), {
separator,
firstDayOfWeek,
allowSameDay,
minDate,
maxDate,
dateType,
today,
rangeDate,
multipleDate,
nextDay,
rangeText
})
monthList.push(entry.month)
dateList.push(entry.monthKey)
if (!monPickList.includes(entry.year)) monPickList.unshift(entry.year)
}
monPickList.sort((a, b) => Number(b) - Number(a))
const uniqueMonPickList = [...new Set(monPickList)]
const [nowYear, nowMonthStr] = today.split(separator)
let current = dateList.findIndex(e => e === `${nowYear}${separator}${nowMonthStr}`)
if (current < 0) current = 0
return {
monthList,
dateList,
monPickList: uniqueMonPickList,
current,
date: today,
rangeDate,
multipleDate,
minDate,
maxDate
}
}
/**
* 构建 yearList(全功能日/月切换用)
* @param {object} options
* @returns {{ yearList: object[][], rangeMonth: { start: string, end: string } }}
*/
export function buildYearListMeta({ minDate, maxDate, separator = '-', nowDate = new Date() }) {
const [minYear, minMonth] = minDate.split(separator)
const [maxYear, maxMonth] = maxDate.split(separator)
const yearList = []
const now_Year = nowDate.getFullYear()
const now_Month = nowDate.getMonth()
for (let index = minYear; index <= maxYear; index++) {
const mothObj = []
for (let MonIndex = 0; MonIndex < 12; MonIndex++) {
const month = MonIndex + 1 >= 10 ? MonIndex + 1 : `0${MonIndex + 1}`
const obj = {
label: `${MonIndex + 1}月`,
disable: false,
start: false,
end: false,
select: false,
rangeDate: `${index}年${month + 1}月`,
date: `${index}-${month}`
}
if ((MonIndex + 1 < minMonth && minYear == index) || (maxYear == index && maxMonth < MonIndex + 1)) {
obj.disable = true
}
if (index == now_Year && MonIndex == now_Month) {
obj.start = true
obj.end = true
}
mothObj.push(obj)
}
yearList.unshift(mothObj)
}
return {
yearList,
rangeMonth: {
start: formatYM(nowDate, separator),
end: formatYM(nowDate, separator)
}
}
}
/**
* 三窗 swiper 可见月份(prev / center / next)
* @param {Array<{ mark: number, data: object[] }>} monthList
* @param {number} current
* @returns {Array<{ monthIndex: number, mark: number, data: object[] }>}
*/
export function buildDisplayMonthWindow(monthList, current) {
if (!monthList || !monthList.length) return []
const total = monthList.length
const clamp = i => Math.max(0, Math.min(total - 1, i))
const prev = clamp(current - 1)
const center = clamp(current)
const next = clamp(current + 1)
return [
{ monthIndex: prev, mark: monthList[prev].mark, data: monthList[prev].data },
{ monthIndex: center, mark: monthList[center].mark, data: monthList[center].data },
{ monthIndex: next, mark: monthList[next].mark, data: monthList[next].data }
]
}
/**
* 合并 setData payload:全功能模式附带 displayMonths
* @param {object} componentData
* @param {object} partial
* @returns {object}
*/
export function buildMonthSetDataPayload(componentData, partial = {}) {
const { monthList, current, dayOnly } = componentData
const nextCurrent = partial.current !== undefined ? partial.current : current
const list = partial.monthList || monthList
const payload = { ...partial }
if (!shouldUseSingleMonthView(dayOnly, false, false) && list && list.length) {
payload.displayMonths = buildDisplayMonthWindow(list, nextCurrent)
payload.swiperCurrent = 1
}
return payload
}
/**
* min/max 边界是否变化,需重建 monthList
* @param {string} prevMin
* @param {string} prevMax
* @param {string} nextMin
* @param {string} nextMax
* @returns {boolean}
*/
export function shouldRebuildMonthListOnBoundsChange(
prevMin,
prevMax,
nextMin,
nextMax
) {
return (prevMin || '') !== (nextMin || '') || (prevMax || '') !== (nextMax || '')
}