2023-10-30 22:14:12 +08:00
|
|
|
import { createApp } from 'vue/dist/vue.esm-bundler.js';
|
|
|
|
import DateTimePicker from '../../../vue/shared/date_time_picker.vue';
|
2023-11-09 02:32:43 +08:00
|
|
|
import { handleTurbolinks } from '../helpers/turbolinks.js';
|
2023-10-30 22:14:12 +08:00
|
|
|
/*
|
|
|
|
<div id="date-time-picker" class="vue-date-time-picker">
|
|
|
|
<input ref="input" type="hidden" v-model="date" id="legacy-id" data-default="" />
|
|
|
|
<date-time-picker ref="vueDateTime" @change="updateDate" :mode="date"></date-time-picker>
|
|
|
|
</div>
|
|
|
|
*/
|
|
|
|
|
|
|
|
window.initDateTimePickerComponent = (id) => {
|
|
|
|
const app = createApp({
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
date: null,
|
|
|
|
onChange: null
|
|
|
|
};
|
|
|
|
},
|
|
|
|
mounted() {
|
|
|
|
if (this.$refs.input.dataset.default) {
|
2023-11-08 20:52:24 +08:00
|
|
|
const defaultDate = new Date(this.$refs.input.dataset.default.replace(/-/g, '/')); // Safari fix
|
2023-11-07 18:34:34 +08:00
|
|
|
this.date = this.formatDate(defaultDate);
|
2023-10-30 22:14:12 +08:00
|
|
|
this.$refs.vueDateTime.datetime = defaultDate;
|
2023-11-06 17:09:18 +08:00
|
|
|
} else if (this.date) {
|
|
|
|
this.$refs.vueDateTime.datetime = new Date(this.date);
|
2023-10-30 22:14:12 +08:00
|
|
|
}
|
2023-11-06 17:09:18 +08:00
|
|
|
|
|
|
|
|
2023-10-30 22:14:12 +08:00
|
|
|
$(this.$refs.input).data('dateTimePicker', this);
|
|
|
|
$(this.$el.parentElement).parent().trigger('dp:ready');
|
|
|
|
},
|
|
|
|
methods: {
|
2023-11-07 18:34:34 +08:00
|
|
|
formatDate(date) {
|
|
|
|
if (this.$refs.input.dataset.simpleFormat) {
|
|
|
|
const y = date.getFullYear();
|
|
|
|
const m = date.getMonth() + 1;
|
|
|
|
const d = date.getDate();
|
|
|
|
const hours = date.getHours();
|
|
|
|
const mins = date.getMinutes();
|
|
|
|
return `${y}/${m}/${d} ${hours}:${mins}`;
|
|
|
|
}
|
|
|
|
return date.toISOString();
|
|
|
|
},
|
2023-10-30 22:14:12 +08:00
|
|
|
updateDate(date) {
|
2023-11-07 18:34:34 +08:00
|
|
|
this.date = this.formatDate(date);
|
2023-10-30 22:14:12 +08:00
|
|
|
this.$nextTick(() => {
|
|
|
|
if (this.onChange) this.onChange(date);
|
|
|
|
});
|
|
|
|
|
|
|
|
},
|
|
|
|
setDate(date) {
|
2023-11-07 18:34:34 +08:00
|
|
|
this.date = this.formatDate(date);
|
2023-10-30 22:14:12 +08:00
|
|
|
this.$refs.vueDateTime.datetime = date;
|
|
|
|
this.$nextTick(() => {
|
|
|
|
if (this.onChange) this.onChange(date);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
clearDate() {
|
|
|
|
this.date = null;
|
|
|
|
this.$refs.vueDateTime.datetime = null;
|
|
|
|
this.$nextTick(() => {
|
|
|
|
if (this.onChange) this.onChange(null);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
app.component('DateTimePicker', DateTimePicker);
|
|
|
|
app.config.globalProperties.i18n = window.I18n;
|
|
|
|
app.mount(id);
|
2023-11-09 02:32:43 +08:00
|
|
|
handleTurbolinks(app);
|
2023-10-30 22:14:12 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
document.addEventListener('turbolinks:load', () => {
|
|
|
|
const dateTimePickers = document.querySelectorAll('.vue-date-time-picker');
|
|
|
|
dateTimePickers.forEach((dateTimePicker) => {
|
|
|
|
window.initDateTimePickerComponent(`#${dateTimePicker.id}`);
|
|
|
|
});
|
|
|
|
})
|