CollapsibleCalendar.vue
18.3 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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
<!--
* @Date: 2025-01-25 15:34:17
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-12-18 23:38:02
* @FilePath: /mlaj/src/components/ui/CollapsibleCalendar.vue
* @Description: 可折叠日历组件
-->
<template>
<div class="collapsible-calendar ">
<!-- 折叠状态显示 -->
<div class="calendar-collapsed" :class="{ 'is-compact': isHeaderHidden }">
<div class="calendar-header">
<div class="calendar-icon">
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M19 3H18V1H16V3H8V1H6V3H5C3.89 3 3.01 3.9 3.01 5L3 19C3 20.1 3.89 21 5 21H19C20.1 21 21 20.1 21 19V5C21 3.9 20.1 3 19 3ZM19 19H5V8H19V19Z" fill="#4caf50"/>
<path d="M7 10H9V12H7V10ZM11 10H13V12H11V10ZM15 10H17V12H15V10Z" fill="#4caf50"/>
</svg>
</div>
<!-- <div class="calendar-date-display">
<div class="calendar-date-main">{{ formattedCurrentDate }}</div>
<div class="calendar-weekday">{{ formattedWeekday }}</div>
</div> -->
<div class="calendar-title-wrapper">
<div class="calendar-title">{{ title }}</div>
<!-- <div class="text-xs text-gray-500 mt-1">点击查看打卡规则</div> -->
</div>
</div>
<div class="calendar-content">
<div class="calendar-date-display" @click="expandCalendar">
<div class="calendar-date-main">{{ formattedCurrentDate }}</div>
<div class="calendar-weekday">{{ formattedWeekday }}</div>
<div class="text-xs text-gray-500 mt-1 collapsible-text">切换日期</div>
</div>
<!-- <div class="calendar-action">
<div class="calendar-action-text">指定日期</div>
<div class="calendar-arrow">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M7 10L12 15L17 10H7Z" fill="#4caf50"/>
</svg>
</div>
</div> -->
<div class="calendar-actions-wrapper">
<!-- 作业筛选 -->
<div class="calendar-action course-filter" @click.stop="openCoursePicker">
<div class="calendar-action-text">{{ selectedCourseText }}</div>
<div class="calendar-arrow">
<svg width="16" height="16" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M7 10L12 15L17 10H7Z" fill="#4caf50"/>
</svg>
</div>
</div>
<div v-if="selectedSubtask" class="text-xs text-gray-500 mt-1 cursor-pointer hover:text-green-600 transition-colors collapsible-text" @click.stop="openRulesPopup">作业信息</div>
</div>
</div>
</div>
<!-- 展开状态的弹窗 -->
<van-popup
v-model:show="isExpanded"
position="bottom"
round
:style="{ height: '55%' }"
@close="collapseCalendar"
>
<div class="calendar-popup-content">
<van-calendar
ref="calendarRef"
:poppable="false"
:show-confirm="false"
switch-mode="year-month"
color="#4caf50"
:formatter="formatter"
row-height="50"
:show-mark="false"
@select="onSelectDay"
@click-subtitle="onClickSubtitle"
@panel-change="onPanelChange"
>
</van-calendar>
</div>
</van-popup>
<!-- 作业选择弹窗 -->
<van-popup
v-model:show="showCoursePicker"
round
position="bottom"
:style="{ minHeight: '30%', maxHeight: '80%', width: '100%' }"
>
<van-picker
title="作业选择"
:columns="courseColumns"
@confirm="onConfirmCourse"
@cancel="showCoursePicker = false"
:default-index="0"
/>
</van-popup>
<!-- 打卡规则弹窗 -->
<van-popup
v-model:show="showRulesPopup"
round
position="bottom"
closeable
:style="{ maxHeight: '50%' }"
>
<div class="p-4">
<div class="text-lg font-bold mb-4 text-center text-gray-800">作业信息</div>
<div class="details text-sm text-gray-600 pl-4 border-l-4 border-green-500" v-if="formattedRules">
<div class="mb-2">周期:{{ formattedRules.cycle }}</div>
<div class="mb-2">频次:{{ formattedRules.frequency }}</div>
<div v-if="formattedRules.begin_date" class="mb-2">开始时间:{{ formattedRules.begin_date }}</div>
<div v-if="formattedRules.end_date" class="mb-2">截止时间:{{ formattedRules.end_date }}</div>
<div v-if="formattedRules.attachment_type" class="mb-2">附件类型:{{ formattedRules.attachment_type }}</div>
</div>
</div>
</van-popup>
</div>
</template>
<script setup>
import { ref, computed, watch, onMounted } from 'vue'
import { useScroll } from '@vueuse/core'
import dayjs from 'dayjs'
// Props定义
const props = defineProps({
title: {
type: String,
default: '选择日期'
},
formatter: {
type: Function,
required: true
},
modelValue: {
type: [String, Date],
default: null
},
subtaskList: {
type: Array,
default: () => []
}
})
// Emits定义
const emit = defineEmits(['update:modelValue', 'select', 'click-subtitle', 'panel-change', 'select-course'])
// 响应式数据
const isExpanded = ref(false)
const calendarRef = ref(null)
const currentDate = ref(props.modelValue || new Date())
// 作业筛选相关
const showCoursePicker = ref(false)
const selectedCourseText = ref('全部作业')
const selectedCourseId = ref('')
const showRulesPopup = ref(false)
// 存储键名
const STORAGE_KEY = 'collapsible_calendar_filter_state'
// 是否已恢复状态
const isRestored = ref(false)
// 尝试从 sessionStorage 恢复状态
try {
const savedState = sessionStorage.getItem(STORAGE_KEY)
if (savedState) {
const { selectedCourseId: savedId, selectedCourseText: savedText } = JSON.parse(savedState)
// 只有当保存的值存在时才恢复 (savedId 可以是空字符串,表示全部作业)
if (savedId !== undefined) {
selectedCourseId.value = savedId
selectedCourseText.value = savedText || '全部作业'
isRestored.value = true
}
}
} catch (e) {
console.error('Failed to restore calendar filter', e)
}
const { y } = useScroll(window)
const isHeaderHidden = ref(false)
const lastY = ref(0)
watch(y, (newY) => {
// 忽略弹性滚动造成的负值
if (newY < 0) return
// 获取文档高度和窗口高度,用于判断是否触底
const windowHeight = window.innerHeight
const documentHeight = document.documentElement.scrollHeight
const bottomThreshold = 20 // 触底阈值
// 如果接近底部,不执行隐藏逻辑(避免回弹导致的显示)
if (newY + windowHeight >= documentHeight - bottomThreshold) {
// 如果已经在底部,保持之前的状态,或者强制隐藏(根据需求,通常保持当前状态更稳妥,或者强制隐藏因为是在向下浏览)
// 这里选择保持状态,但更新lastY防止下次误判
lastY.value = newY
return
}
// 滚动方向判定
const direction = newY > lastY.value ? 'down' : 'up'
const scrollThreshold = 50 // 滚动阈值
if (newY > scrollThreshold && direction === 'down') {
isHeaderHidden.value = true
} else if (direction === 'up') {
// 只有当向上滚动超过一定距离或者不在底部反弹区域时才显示
// 这里的判断其实已经被上面的触底检测覆盖了一部分,但为了保险,可以加一个最小变动阈值
if (Math.abs(newY - lastY.value) > 5) {
isHeaderHidden.value = false
}
}
lastY.value = newY
})
const courseColumns = computed(() => {
return [
{ text: '全部作业', value: '' },
...props.subtaskList.map(item => ({
text: item.title,
value: item.id
}))
]
})
const selectedSubtask = computed(() => {
if (!selectedCourseId.value) return null
return props.subtaskList.find(item => item.id === selectedCourseId.value)
})
const formattedRules = computed(() => {
if (!selectedSubtask.value) return null
const data = selectedSubtask.value
const cycleMap = {
'0': '本周期',
'30': '每月',
'7': '每周',
'1': '每日'
}
const cycleText = cycleMap[data.cycle] || data.cycle || '每日'
let attachmentText = '文本'
if (Array.isArray(data.attachment_type)) {
const typeMap = {
'text': '文本',
'image': '图片',
'video': '视频',
'audio': '音频'
}
attachmentText = data.attachment_type.map(t => typeMap[t] || t).join('/')
} else if (data.attachment_type) {
attachmentText = data.attachment_type
}
return {
cycle: cycleText,
frequency: `每周期${data.frequency || 1}次`,
begin_date: data.begin_date ? dayjs(data.begin_date).format('YYYY-MM-DD') : '',
end_date: data.end_date ? dayjs(data.end_date).format('YYYY-MM-DD') : '',
attachment_type: attachmentText
}
})
const openCoursePicker = () => {
showCoursePicker.value = true
}
const openRulesPopup = () => {
showRulesPopup.value = true
}
const onConfirmCourse = (result) => {
// 兼容 Vant 3/4
const option = result.selectedOptions ? result.selectedOptions[0] : result
selectedCourseText.value = option.text
selectedCourseId.value = option.value
showCoursePicker.value = false
emit('select-course', option.value)
// 保存状态
sessionStorage.setItem(STORAGE_KEY, JSON.stringify({
selectedCourseId: selectedCourseId.value,
selectedCourseText: selectedCourseText.value
}))
}
// 监听 subtaskList 变化,用于在恢复状态后同步筛选
watch(() => props.subtaskList, (newList) => {
if (newList && newList.length > 0 && isRestored.value) {
// 尝试在列表中找到对应的项,更新文本
const target = newList.find(item => item.id === selectedCourseId.value)
if (target) {
selectedCourseText.value = target.title
} else if (selectedCourseId.value) {
// 如果有ID但在列表中找不到(可能是作业被删除),则重置为全部
selectedCourseId.value = ''
selectedCourseText.value = '全部作业'
}
// 通知父组件筛选变更,确保数据与筛选状态一致
emit('select-course', selectedCourseId.value)
// 重置标记,确保只执行一次
isRestored.value = false
}
}, { immediate: true })
// 计算属性:格式化当前日期显示
const formattedCurrentDate = computed(() => {
return dayjs(currentDate.value).format('YYYY年MM月DD日')
})
// 计算属性:格式化星期几显示
const formattedWeekday = computed(() => {
const weekdays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
return weekdays[dayjs(currentDate.value).day()]
})
/**
* 展开日历
*/
const expandCalendar = () => {
isExpanded.value = true
}
/**
* 折叠日历
*/
const collapseCalendar = () => {
isExpanded.value = false
}
/**
* 日期选择处理
* @param {Date} day - 选中的日期
*/
const onSelectDay = (day) => {
currentDate.value = day
emit('update:modelValue', day)
emit('select', day)
// 选择日期后自动关闭弹窗
setTimeout(() => {
collapseCalendar()
}, 200)
}
/**
* 点击副标题处理
* @param {Event} evt - 点击事件
*/
const onClickSubtitle = (evt) => {
emit('click-subtitle', evt)
}
/**
* 面板切换处理
* @param {Object} panel - 切换后的面板信息
*/
const onPanelChange = (panel) => {
emit('panel-change', panel)
}
// 监听modelValue变化
watch(() => props.modelValue, (newValue) => {
if (newValue) {
currentDate.value = newValue
}
})
// 暴露方法给父组件
defineExpose({
expand: expandCalendar,
collapse: collapseCalendar,
reset: (date) => {
currentDate.value = date || new Date()
if (calendarRef.value && calendarRef.value.reset) {
calendarRef.value.reset(date || new Date())
}
}
})
</script>
<style lang="less" scoped>
.collapsible-calendar {
width: 100%;
}
.calendar-collapsed {
// background: linear-gradient(135deg, #ffffff 0%, #f8fffe 100%);
// border-radius: 16px;
padding: 20px;
box-shadow: 0 4px 20px rgba(76, 175, 80, 0.08), 0 2px 8px rgba(0, 0, 0, 0.06);
cursor: pointer;
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
border: 1px solid rgba(76, 175, 80, 0.1);
position: relative;
overflow: hidden;
&::before {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
height: 3px;
background: linear-gradient(90deg, #4caf50 0%, #66bb6a 100%);
border-radius: 16px 16px 0 0;
}
// &:hover {
// transform: translateY(-2px);
// box-shadow: 0 8px 30px rgba(76, 175, 80, 0.12), 0 4px 16px rgba(0, 0, 0, 0.08);
// border-color: rgba(76, 175, 80, 0.2);
// }
&:active {
transform: translateY(-1px);
transition: all 0.1s ease;
}
// 紧凑模式下的样式控制
&.is-compact {
.calendar-header {
max-height: 0;
margin-bottom: 0;
opacity: 0;
pointer-events: none;
}
.collapsible-text {
max-height: 0;
margin-top: 0;
opacity: 0;
overflow: hidden;
}
}
.calendar-header {
display: flex;
align-items: center;
margin-bottom: 16px;
gap: 12px;
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
max-height: 100px;
opacity: 1;
overflow: hidden;
&.is-hidden {
max-height: 0;
margin-bottom: 0;
opacity: 0;
pointer-events: none;
}
.calendar-icon {
display: flex;
align-items: center;
justify-content: center;
width: 40px;
height: 40px;
background: linear-gradient(135deg, rgba(76, 175, 80, 0.1) 0%, rgba(76, 175, 80, 0.05) 100%);
border-radius: 12px;
flex-shrink: 0;
}
.calendar-title-wrapper {
flex: 1;
min-width: 0;
.calendar-title {
font-size: 18px;
font-weight: 600;
color: #2c3e50;
margin-bottom: 2px;
line-height: 1.3;
// overflow: hidden;
// text-overflow: ellipsis;
// white-space: nowrap;
}
}
.calendar-date-display {
flex: 0.8;
.calendar-date-main {
font-size: 16px;
font-weight: 600;
color: #34495e;
margin-bottom: 4px;
line-height: 1.2;
}
.calendar-weekday {
font-size: 13px;
color: #4caf50;
font-weight: 500;
}
.calendar-subtitle {
font-size: 12px;
color: #7f8c8d;
font-weight: 400;
}
}
}
.calendar-content {
display: flex;
align-items: center;
justify-content: end;
gap: 16px;
.calendar-date-display {
flex: 1;
.calendar-date-main {
font-size: 16px;
font-weight: 600;
color: #34495e;
margin-bottom: 4px;
line-height: 1.2;
}
.calendar-weekday {
font-size: 13px;
color: #4caf50;
font-weight: 500;
}
}
.calendar-actions-wrapper {
display: flex;
flex-direction: column;
align-items: flex-end;
gap: 8px;
flex-shrink: 0;
}
.calendar-action {
display: flex;
align-items: center;
justify-content: end;
background: linear-gradient(135deg, rgba(76, 175, 80, 0.1) 0%, rgba(76, 175, 80, 0.05) 100%);
padding: 8px;
border-radius: 20px;
border: 1px solid rgba(76, 175, 80, 0.2);
flex-shrink: 0;
.calendar-action-text {
font-size: 13px;
color: #4caf50;
font-weight: 600;
white-space: nowrap;
max-width: 10rem;
overflow: hidden;
text-overflow: ellipsis;
}
.calendar-arrow {
display: flex;
align-items: center;
transition: transform 0.2s ease;
}
}
}
&:hover .calendar-action .calendar-arrow {
transform: translateY(2px);
}
}
.collapsible-text {
transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
max-height: 20px; // 假设文字高度不超过20px
opacity: 1;
}
.calendar-popup-content {
height: 100%;
overflow: hidden;
:deep(.van-calendar) {
height: 100%;
.van-calendar__header {
box-shadow: none;
border-bottom: 1px solid #eee;
}
.van-calendar__body {
height: calc(100% - 44px);
overflow-y: auto;
}
}
}
// 自定义日历样式
:deep(.van-popup) {
// border-radius: 0 0 12px 12px;
}
</style>