Calendar_Function.js
10.6 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
import {
buildAllMonthsMeta,
buildYearListMeta,
buildMonthSetDataPayload,
getMonthDifference,
getDayNumber,
shouldBuildYearList,
applyRangeLabels
} from './calendarMonth.helpers.js'
const _CalendarObject_ = Symbol('_CalendarObject_')
const _nowDate_ = Symbol('_nowDate_')
const _separator_ = Symbol('_separator_')
class Calendar {
[_CalendarObject_] = {}
[_nowDate_] = new Date()
[_separator_] = '-'
constructor(calendarObject) {
this[_CalendarObject_] = calendarObject
this[_separator_] = calendarObject.data.separator
this.setCalenderHeight()
let { weekdays, firstDayOfWeek } = calendarObject.data
let firstDay = weekdays[firstDayOfWeek - 1]
let [last, first] = weekdays.join('').split(firstDay)
calendarObject.setData({
weekdays: [firstDay, ...first, ...last]
})
}
/** 首次打开弹层时构建月份数据(lazy init) */
setMonthList() {
let _this = this[_CalendarObject_]
let {
separator,
previous,
minDate,
maxDate,
next,
firstDayOfWeek,
allowSameDay,
dateType,
value,
rangeText,
dayOnly
} = _this.data
const built = buildAllMonthsMeta({
separator,
previous,
next,
minDate,
maxDate,
firstDayOfWeek,
allowSameDay,
dateType,
value,
nowDate: this[_nowDate_],
rangeText
})
const payload = buildMonthSetDataPayload(_this.data, {
multipleDate: built.multipleDate,
date: built.date,
rangeDate: built.rangeDate,
monthList: built.monthList,
dateList: built.dateList,
monPickList: built.monPickList,
current: built.current,
minDate: built.minDate,
maxDate: built.maxDate,
swiperCurrent: 1
})
if (shouldBuildYearList({ dayOnly, monthOnly: false, monType: false })) {
const yearMeta = buildYearListMeta({
minDate: built.minDate,
maxDate: built.maxDate,
separator,
nowDate: this[_nowDate_]
})
payload.yearList = yearMeta.yearList
payload.rangeMonth = yearMeta.rangeMonth
}
_this.setData(payload)
}
/** setData 时同步三窗 displayMonths */
syncMonthView(partial = {}) {
const _this = this[_CalendarObject_]
if (partial.monthList && _this.data.rangeText) {
applyRangeLabels(partial.monthList, _this.data.rangeText)
}
_this.setData(buildMonthSetDataPayload(_this.data, partial))
}
/** shortcut picker 切月 */
onPickerMonthChange(current) {
this.syncMonthView({ current })
}
/** 三窗 swiper 滑月 */
shiftMonth(delta) {
const _this = this[_CalendarObject_]
const { current, monthList } = _this.data
const next = current + delta
if (next < 0 || next >= monthList.length) {
_this.setData({ swiperCurrent: 1 })
return
}
this.syncMonthView({ current: next })
}
/** swiper 三窗模式:touch 滑到 0/2 时换月 */
onSwiperDateChange(swiperIndex) {
if (swiperIndex === 0) this.shiftMonth(-1)
else if (swiperIndex === 2) this.shiftMonth(1)
}
setCalenderHeight() {
let _this = this[_CalendarObject_]
let { data: calenderHeight } = _this
let height = '80%'
if (typeof calenderHeight === 'number') {
wx.$util.getSystemInfo(res => {
let { windowHeight } = res
if (calenderHeight > windowHeight) calenderHeight = windowHeight
if (calenderHeight < windowHeight * 0.5) calenderHeight = windowHeight * 0.5
height = `${calenderHeight}px`
})
}
if (typeof calenderHeight === 'string') {
if (calenderHeight.includes('%')) {
height = calenderHeight
let [num] = calenderHeight.split('%')
if (num < 50) height = '50%'
if (num > 100) height = '80%'
} else {
if (calenderHeight < 50) calenderHeight = '50'
if (calenderHeight > 100) calenderHeight = '80'
height = `${calenderHeight}%`
}
}
_this.setData({
isFullSucreen: wx.$util.isFullSucreen(_this.data.isComponents),
height
})
}
setCalenderBodyHeight() {
let _this = this[_CalendarObject_]
wx.$util.getViewInfo(
'.calender-header',
header => {
wx.$util.getViewInfo(
'.calender-footer',
footer => {
let headerHeight = header.height
let footerHeight = (footer && footer.height) || 0
_this.setData({
calenderBodyHeight: `${headerHeight + footerHeight}px`
})
},
_this
)
},
_this
)
}
getMonthDifference(date1, date2) {
return getMonthDifference(date1, date2, this[_separator_])
}
single(e) {
let _this = this[_CalendarObject_]
let { index, data, ind } = e.currentTarget.dataset
let { showConfirm, monthList } = _this.data
monthList.map(x => {
x.data.map(i => {
i.active = false
i.rangeLabel = ''
})
})
monthList[index].data[ind].active = true
this.syncMonthView({
monthList,
date: data
})
if (!showConfirm) this.done()
}
multiple(e) {
let _this = this[_CalendarObject_]
let { index, ind } = e.currentTarget.dataset
let { monthList } = _this.data
let arr = []
monthList[index].data[ind].active = !monthList[index].data[ind].active
monthList.map(x =>
x.data.map(i => {
if (i.active) arr.push(i.data)
})
)
_this.setData({
[`monthList[${index}].data`]: monthList[index].data,
multipleDate: arr
})
}
range(e) {
const _this = this[_CalendarObject_]
let { index, data, ind } = e.currentTarget.dataset
let { allowSameDay, showConfirm, monthList, rangeDate, rangeText } = _this.data
const start = Date.parse(rangeDate.start)
if (rangeDate.end || start > Date.parse(data)) {
monthList = this.clearStatus({
monthList,
index,
ind,
rangeText
})
rangeDate.start = data
rangeDate.end = ''
} else {
if (!allowSameDay && Date.parse(data) == Date.parse(rangeDate.start)) return
const { maxDayRange, maxDayRangeType } = _this.data
if (maxDayRangeType == 'day') {
if (
Date.parse(data) >
Date.parse(rangeDate.start) + (maxDayRange - 1) * 24 * 60 * 60 * 1000
) {
return wx.showToast({
title: `选择范围不能超过${maxDayRange}天`,
icon: 'none'
})
}
}
if (maxDayRangeType == 'month') {
let startDate = new Date(rangeDate.start)
let endDate = new Date(data)
let months =
(endDate.getFullYear() - startDate.getFullYear()) * 12 +
(endDate.getMonth() - startDate.getMonth())
if (startDate.getDate() > endDate.getDate()) {
months--
}
if (months >= maxDayRange) {
return wx.showToast({
title: `选择范围不能超过${maxDayRange}个月`,
icon: 'none'
})
}
}
monthList[index].data[ind].active = true
monthList[index].data[ind].end = true
monthList[index].data[ind].rangeLabel = rangeText.end
rangeDate.end = data
monthList.map(x => {
x.data.map(i => {
let day = Date.parse(i.data)
i.middle = day > start && day < Date.parse(data)
})
})
}
applyRangeLabels(monthList, rangeText)
this.syncMonthView({
monthList,
rangeDate
})
if (!showConfirm && rangeDate.end) this.done()
}
month(e) {
console.log(e)
const _this = this[_CalendarObject_]
let { rangeMonth, yearList, showConfirm } = _this.data
let { index, yearindex, data } = e.currentTarget.dataset
const start = Date.parse(rangeMonth.start)
let falseObj = {
end: false,
start: false,
select: false
}
console.log(rangeMonth.end || start > Date.parse(data.date))
console.log(rangeMonth.end)
console.log(start)
console.log(data)
console.log(Date.parse(data.date))
if (rangeMonth.end || start > Date.parse(data.date)) {
yearList = yearList.map(item => {
return item.map(ele => {
return {
...ele,
...falseObj
}
})
})
yearList[yearindex][index]['start'] = true
rangeMonth.start = data.date
rangeMonth.end = ''
} else {
yearList[yearindex][index]['end'] = true
rangeMonth.end = data.date
yearList.map(x => {
x.map(i => {
let day = Date.parse(i.date)
i.select = day > start && day < Date.parse(data.date)
})
})
}
_this.setData({
yearList,
rangeMonth
})
if (!showConfirm && rangeMonth.end) this.done()
}
done() {
let _this = this[_CalendarObject_]
let { multipleDate, rangeDate, dateType, date, rangeMonth, dayType } = _this.data
let targer = {
single: date,
multiple: multipleDate,
range: rangeDate
}
if (!dayType) {
if (!rangeMonth.end) return
_this.triggerEvent('select', {
date: rangeMonth,
type: 'dayType'
})
} else {
if ((dateType === 'multiple' && multipleDate.length < 1) || (dateType === 'range' && !rangeDate.end)) return
_this.triggerEvent('select', {
date: targer[dateType]
})
}
_this.showCalendar()
}
clearStatus(params) {
let { monthList, index, ind, rangeText } = params
monthList.map(x =>
x.data.map(i => {
i.active = false
i.start = false
i.end = false
i.middle = false
i.rangeLabel = ''
})
)
monthList[index].data[ind].active = true
monthList[index].data[ind].start = true
monthList[index].data[ind].rangeLabel = rangeText ? rangeText.start : '开始'
return monthList
}
getDayNumber(year, month) {
return getDayNumber(year, month)
}
chooseAll() {
let _this = this[_CalendarObject_]
let { monthList, minDate, maxDate } = _this.data
monthList.map((e, index) => {
e.data.map((i, ind) => {
let obj = {
currentTarget: {
dataset: {
index,
ind,
data: i.data
}
}
}
if (i.data == minDate) this.range(obj)
if (i.data == maxDate) this.range(obj)
})
})
}
}
export default Calendar