CalendarMonth.js
5.81 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
// components/CalendarMonth/CalendarMonth.js
import Calendar from './Calendar_Function'
import CalendarMonthOnly from './Calendar_Function_monthOnly'
import _component_ from './Static_Properties'
import { CLOSE_ICON_URL } from '~/utils/imageBase.js'
import {
getInitialDayType,
resolveMonthOnly,
resolveDayOnly,
buildMonthSetDataPayload,
shouldUseSingleMonthView,
shouldRebuildMonthListOnBoundsChange
} from './calendarMonth.helpers.js'
const CalendarFunction = Symbol('CalendarFunction')
Component({
properties: _component_.properties,
data: _component_.data,
methods: {
noScroll() {},
/** 确保月份数据已 lazy 构建 */
ensureMonthListReady() {
if (this._monthListReady || this._monthOnlyMode) return
this[CalendarFunction].setMonthList()
this._monthListReady = true
},
/** 按外部回显值重置当前选中日期 */
resetData(date) {
this.ensureMonthListReady()
let { monthList, current } = this.data
monthList.map(x => {
x.data.map(i => {
i.active = false
i.rangeLabel = ''
})
})
monthList.map((item, index) => {
item.data.map(ele => {
if (ele.data == date) {
ele.active = true
current = index
}
})
})
this.setData(buildMonthSetDataPayload(this.data, { monthList, current }))
},
setCalenderBodyHeight() {
this[CalendarFunction].setCalenderBodyHeight()
},
showCalendar(e) {
if (this.data.disabled) return false
let type = (e && e.currentTarget && e.currentTarget.dataset.type) || ''
let result = type === 'show'
if (!result) {
this.setData({ hideCalendar: true })
setTimeout(() => {
this.setData({
show: result,
hideCalendar: false
})
}, 300)
} else {
if (!this._monthOnlyMode) {
this.ensureMonthListReady()
}
this.setData({ show: result })
if (!this.data.calenderBodyHeight) {
this[CalendarFunction].setCalenderBodyHeight()
}
}
this.triggerEvent('tapCalendar')
},
dateChange(e) {
let { current, source } = e.detail
let { dayType, dayOnly } = this.data
if (source !== 'touch') return
if (dayType) {
if (!shouldUseSingleMonthView(dayOnly, false, false)) {
this[CalendarFunction].onSwiperDateChange(current)
}
} else {
this.setData({ yearCurrent: current })
}
},
bindDateChange: function (e) {
var date = e.detail.value
this.setData({ date: date })
this.post_data.year = date
this.renderHtml()
},
choose(e) {
let { dateType, minDate, maxDate } = this.data
let { data } = e.currentTarget.dataset
if (
(minDate && Date.parse(minDate) > Date.parse(data)) ||
(maxDate && Date.parse(maxDate) < Date.parse(data))
) {
return
}
this[CalendarFunction][dateType](e)
},
monChoose(e) {
const { data, type } = e.currentTarget.dataset
if (data.disable) return false
if (this._monthOnlyMode) {
this[CalendarFunction][this.properties.dateType](e)
return
}
this[CalendarFunction][type](e)
},
done() {
if (this._monthOnlyMode) {
this[CalendarFunction].done()
return
}
this[CalendarFunction].done(this.data.dateType)
},
changeDate(e) {
let { value } = e.detail
if (this._monthOnlyMode) {
this.setData({ current: value })
return
}
this[CalendarFunction].onPickerMonthChange(Number(value))
},
bindYearChange(e) {
let { value } = e.detail
this.setData({ yearCurrent: value })
},
showShortcutPicker() {
this.setData({ showShortcut: !this.data.showShortcut })
},
showShortcutPickerYear() {
this.setData({ showShortcutYear: !this.data.showShortcutYear })
},
chooseAll() {
this.ensureMonthListReady()
this[CalendarFunction].chooseAll()
},
changeModel() {
let { dayType } = this.data
this.setData({ dayType: dayType == 0 ? 1 : 0 })
},
/**
* minDate/maxDate 变更时失效 lazy monthList,弹层打开时立即重建
* @param {string} minDate
* @param {string} maxDate
*/
onCalendarBoundsChange(minDate, maxDate) {
if (!this[CalendarFunction]) return
const prevMin = this._lastBoundsMin
const prevMax = this._lastBoundsMax
this._lastBoundsMin = minDate
this._lastBoundsMax = maxDate
if (prevMin === undefined && prevMax === undefined) return
if (
!shouldRebuildMonthListOnBoundsChange(prevMin, prevMax, minDate, maxDate)
) {
return
}
this._monthListReady = false
if (this.data.show && !this._monthOnlyMode) {
this[CalendarFunction].setMonthList()
this._monthListReady = true
}
}
},
observers: {
'minDate, maxDate': function (minDate, maxDate) {
this.onCalendarBoundsChange(minDate, maxDate)
}
},
attached() {
const monthOnly = resolveMonthOnly(
this.properties.monthOnly,
this.properties.monType
)
const dayOnly = resolveDayOnly(this.properties.dayOnly, monthOnly)
this._monthOnlyMode = monthOnly
this._dayOnlyMode = dayOnly
this._monthListReady = false
if (monthOnly) {
this.setData({
dayType: getInitialDayType({ monthOnly: true }),
closeIconUrl: CLOSE_ICON_URL,
dayOnly: false
})
this[CalendarFunction] = new CalendarMonthOnly(this)
this._monthListReady = true
} else {
this.setData({
dayType: getInitialDayType({ monthOnly: false, dayOnly }),
closeIconUrl: CLOSE_ICON_URL,
dayOnly
})
this[CalendarFunction] = new Calendar(this)
}
}
})