2021-12-10 21:12:06 +08:00
|
|
|
<template>
|
2023-08-28 19:10:02 +08:00
|
|
|
<div class="date-time-picker grow">
|
2023-10-25 19:16:34 +08:00
|
|
|
<VueDatePicker
|
2023-11-15 22:28:25 +08:00
|
|
|
ref="datetimePicker"
|
2023-11-14 17:58:38 +08:00
|
|
|
:class="{
|
|
|
|
'only-time': mode == 'time',
|
|
|
|
}"
|
2023-11-25 02:16:32 +08:00
|
|
|
@closed="closedHandler"
|
|
|
|
@cleared="clearedHandler"
|
2023-11-08 20:52:24 +08:00
|
|
|
v-model="compDatetime"
|
2023-11-07 22:15:37 +08:00
|
|
|
:teleport="teleport"
|
2023-11-08 20:52:24 +08:00
|
|
|
:no-today="true"
|
2023-11-07 22:15:37 +08:00
|
|
|
:clearable="clearable"
|
2023-11-08 20:52:24 +08:00
|
|
|
:format="format"
|
|
|
|
:month-change-on-scroll="false"
|
|
|
|
:six-weeks="true"
|
2023-11-13 21:13:29 +08:00
|
|
|
:auto-apply="true"
|
|
|
|
:partial-flow="true"
|
2023-11-08 20:52:24 +08:00
|
|
|
:markers="markers"
|
2023-11-15 22:28:25 +08:00
|
|
|
week-start="0"
|
2023-11-08 20:52:24 +08:00
|
|
|
:enable-time-picker="mode == 'datetime'"
|
|
|
|
:time-picker="mode == 'time'"
|
|
|
|
:placeholder="placeholder" >
|
|
|
|
<template #arrow-right>
|
|
|
|
<img class="slot-icon" src="/images/calendar/navigate_next.svg"/>
|
|
|
|
</template>
|
|
|
|
<template #arrow-left>
|
|
|
|
<img class="slot-icon" src="/images/calendar/navigate_before.svg"/>
|
|
|
|
</template>
|
2023-11-14 17:58:38 +08:00
|
|
|
<template v-if="mode == 'time'" #input-icon>
|
|
|
|
<img class="input-slot-image" src="/images/calendar/clock.svg"/>
|
|
|
|
</template>
|
|
|
|
<template v-else #input-icon>
|
2023-11-08 20:52:24 +08:00
|
|
|
<img class="input-slot-image" src="/images/calendar/calendar.svg"/>
|
|
|
|
</template>
|
2023-11-13 21:13:29 +08:00
|
|
|
<template #clock-icon>
|
|
|
|
<img class="slot-icon" src="/images/calendar/clock.svg"/>
|
|
|
|
</template>
|
2023-11-14 17:58:38 +08:00
|
|
|
<template #calendar-icon>
|
|
|
|
<img class="slot-icon" src="/images/calendar/calendar.svg"/>
|
|
|
|
</template>
|
|
|
|
<template #arrow-up>
|
|
|
|
<img class="slot-icon" src="/images/calendar/up.svg"/>
|
|
|
|
</template>
|
|
|
|
<template #arrow-down>
|
|
|
|
<img class="slot-icon" src="/images/calendar/down.svg"/>
|
|
|
|
</template>
|
2023-11-08 20:52:24 +08:00
|
|
|
</VueDatePicker>
|
2021-12-10 21:12:06 +08:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2023-11-24 19:55:22 +08:00
|
|
|
import VueDatePicker from '@vuepic/vue-datepicker';
|
2021-12-15 19:22:24 +08:00
|
|
|
|
2023-11-24 19:55:22 +08:00
|
|
|
export default {
|
|
|
|
name: 'DateTimePicker',
|
|
|
|
props: {
|
|
|
|
mode: { type: String, default: 'datetime' },
|
|
|
|
clearable: { type: Boolean, default: false },
|
|
|
|
teleport: { type: Boolean, default: true },
|
|
|
|
defaultValue: { type: Date, required: false },
|
|
|
|
placeholder: { type: String },
|
|
|
|
standAlone: { type: Boolean, default: false, required: false },
|
|
|
|
dateClassName: { type: String, default: '' },
|
|
|
|
timeClassName: { type: String, default: '' },
|
|
|
|
disabled: { type: Boolean, default: false }
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
manualUpdate: false,
|
|
|
|
datetime: this.defaultValue,
|
|
|
|
time: null,
|
|
|
|
markers: [
|
|
|
|
{
|
|
|
|
date: new Date(),
|
|
|
|
type: 'dot',
|
|
|
|
color: '#3B99FD',
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
created() {
|
2023-11-07 18:34:34 +08:00
|
|
|
if (this.defaultValue) {
|
|
|
|
this.time = {
|
2023-11-24 19:55:22 +08:00
|
|
|
hours: this.defaultValue.getHours(),
|
|
|
|
minutes:this.defaultValue.getMinutes(),
|
|
|
|
}
|
2023-10-30 22:14:12 +08:00
|
|
|
}
|
|
|
|
},
|
2023-11-24 19:55:22 +08:00
|
|
|
components: {
|
|
|
|
VueDatePicker
|
|
|
|
},
|
|
|
|
watch: {
|
|
|
|
defaultValue: function () {
|
|
|
|
this.datetime = this.defaultValue;
|
2023-11-25 02:16:32 +08:00
|
|
|
if (this.defaultValue instanceof Date) {
|
2023-11-24 19:55:22 +08:00
|
|
|
this.time = {
|
|
|
|
hours: this.defaultValue.getHours(),
|
|
|
|
minutes: this.defaultValue.getMinutes()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
datetime: function () {
|
|
|
|
if (this.mode == 'time') {
|
2023-11-14 22:18:02 +08:00
|
|
|
|
2023-11-24 19:55:22 +08:00
|
|
|
this.time = null;
|
|
|
|
|
2023-11-25 02:16:32 +08:00
|
|
|
if (this.datetime instanceof Date) {
|
2023-11-07 18:34:34 +08:00
|
|
|
this.time = {
|
2023-11-20 18:38:41 +08:00
|
|
|
hours: this.datetime.getHours(),
|
2023-11-24 19:55:22 +08:00
|
|
|
minutes: this.datetime.getMinutes()
|
|
|
|
}
|
2023-11-20 18:38:41 +08:00
|
|
|
}
|
|
|
|
|
2023-11-24 19:55:22 +08:00
|
|
|
return
|
|
|
|
}
|
2023-11-07 18:34:34 +08:00
|
|
|
|
2023-11-24 19:55:22 +08:00
|
|
|
if (this.manualUpdate) {
|
|
|
|
this.manualUpdate = false;
|
|
|
|
return;
|
|
|
|
}
|
2023-11-14 22:18:02 +08:00
|
|
|
|
2023-11-24 19:55:22 +08:00
|
|
|
if ( this.datetime == null) {
|
|
|
|
this.$emit('cleared');
|
|
|
|
}
|
2023-11-02 20:35:10 +08:00
|
|
|
|
2023-11-24 19:55:22 +08:00
|
|
|
if (this.defaultValue != this.datetime) {
|
|
|
|
this.$emit('change', this.datetime);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
time: function () {
|
|
|
|
if (this.manualUpdate) {
|
|
|
|
this.manualUpdate = false;
|
|
|
|
return;
|
|
|
|
}
|
2023-11-14 17:58:38 +08:00
|
|
|
|
2023-11-24 19:55:22 +08:00
|
|
|
if (this.mode != 'time') return;
|
2023-11-07 18:34:34 +08:00
|
|
|
|
2023-11-24 19:55:22 +08:00
|
|
|
let newDate;
|
2023-11-07 18:34:34 +08:00
|
|
|
|
2023-11-24 19:55:22 +08:00
|
|
|
if (this.time) {
|
|
|
|
newDate = new Date();
|
|
|
|
newDate.setHours(this.time.hours);
|
|
|
|
newDate.setMinutes(this.time.minutes);
|
|
|
|
} else {
|
|
|
|
newDate = {
|
|
|
|
hours: null,
|
|
|
|
minutes: null
|
|
|
|
};
|
|
|
|
this.$emit('cleared');
|
|
|
|
}
|
2023-11-07 18:34:34 +08:00
|
|
|
|
2023-11-24 19:55:22 +08:00
|
|
|
if (this.defaultValue != newDate) {
|
|
|
|
this.$emit('change', newDate);
|
|
|
|
}
|
2021-12-10 21:12:06 +08:00
|
|
|
}
|
2023-10-25 19:16:34 +08:00
|
|
|
},
|
2023-11-24 19:55:22 +08:00
|
|
|
computed: {
|
|
|
|
compDatetime: {
|
|
|
|
get () {
|
|
|
|
if (this.mode == 'time') {
|
|
|
|
return this.time
|
|
|
|
} else {
|
|
|
|
return this.datetime
|
|
|
|
}
|
|
|
|
},
|
|
|
|
set (val) {
|
|
|
|
if (this.mode == 'time') {
|
|
|
|
this.time = val
|
|
|
|
} else {
|
|
|
|
this.datetime = val
|
|
|
|
}
|
2023-11-08 20:52:24 +08:00
|
|
|
}
|
2023-11-07 18:34:34 +08:00
|
|
|
},
|
2023-11-24 19:55:22 +08:00
|
|
|
format() {
|
|
|
|
if (this.mode == 'time') return 'HH:mm'
|
|
|
|
if (this.mode == 'date') return document.body.dataset.datetimePickerFormatVue
|
|
|
|
return `${document.body.dataset.datetimePickerFormatVue} HH:mm`
|
|
|
|
}
|
2023-11-15 22:28:25 +08:00
|
|
|
},
|
2023-11-24 19:55:22 +08:00
|
|
|
mounted() {
|
|
|
|
window.addEventListener('resize', this.close);
|
2023-11-15 22:28:25 +08:00
|
|
|
},
|
2023-11-24 19:55:22 +08:00
|
|
|
unmounted() {
|
|
|
|
window.removeEventListener('resize', this.close);
|
2023-11-15 22:28:25 +08:00
|
|
|
},
|
2023-11-24 19:55:22 +08:00
|
|
|
methods: {
|
|
|
|
close() {
|
|
|
|
this.$refs.datetimePicker.closeMenu();
|
|
|
|
},
|
2023-11-25 02:16:32 +08:00
|
|
|
closedHandler() {
|
|
|
|
this.$emit('closed');
|
|
|
|
},
|
|
|
|
clearedHandler() {
|
|
|
|
this.$emit('cleared');
|
|
|
|
}
|
2023-11-24 19:55:22 +08:00
|
|
|
}
|
|
|
|
}
|
2022-02-25 22:27:31 +08:00
|
|
|
</script>
|