diff --git a/src/plugins/events/components/management/EditEvent.vue b/src/plugins/events/components/management/EditEvent.vue index 2358874..f66a951 100644 --- a/src/plugins/events/components/management/EditEvent.vue +++ b/src/plugins/events/components/management/EditEvent.vue @@ -82,9 +82,12 @@ @@ -117,13 +120,13 @@ export default defineComponent({ components: { IsoDateInput, Job, RecurrenceRule }, props: { modelValue: { - default: () => emptyEvent, + default: () => emptyEvent(), type: Object as PropType, }, date: { required: false, default: () => new Date(), - type: [Object, String, Number] as PropType, + type: [Object, Number] as PropType, }, }, emits: { @@ -131,20 +134,19 @@ export default defineComponent({ }, setup(props, { emit }) { const store = useScheduleStore(); - - const date = computed(() => new Date(props.date)); - const event = ref( - props.modelValue.id - ? props.modelValue - : Object.assign({}, props.modelValue, { start: date.value }) - ); + const active = ref(0); + const activeRef = ref(Job); const eventtypes = computed(() => store.eventTypes); const form = ref(); - const jobDeleteDisabled = computed(() => event.value.jobs.length < 2); const recurrent = ref(false); const recurrenceRule = ref({ frequency: 'daily', interval: 1 }); const templates = computed(() => store.templates); const template = ref(); + const event = ref( + props.modelValue.id + ? props.modelValue + : Object.assign({}, props.modelValue, { start: new Date(props.date) }) + ); onBeforeMount(() => { void store.getEventTypes(); @@ -152,25 +154,34 @@ export default defineComponent({ void store.getTemplates(); }); - function addJob() { - event.value.jobs.push(Object.assign({}, emptyJob)); + async function addJob() { + if (event.value.jobs.length > 0) { + // eslint-disable-next-line @typescript-eslint/no-unsafe-call + if (!(await activeRef.value?.validate())) return; + event.value.jobs.sort((a, b) => { + if (a.end && b.end) + if (a.end < b.end) return -1; + else if (a.end > b.end) return 1; + + return a.start.getTime() - b.start.getTime(); + }); + } + event.value.jobs.push( + emptyJob(event.value.jobs[event.value.jobs.length - 1]?.end || new Date(props.date)) + ); + active.value = event.value.jobs.length - 1; } function removeJob(index: number) { event.value.jobs.splice(index, 1); + active.value = index == active.value ? event.value.jobs.length - 1 : index; } function fromTemplate(tpl: FG.Event) { template.value = tpl; event.value = Object.assign({}, tpl); - event.value.start = new Date( - date.value.getUTCFullYear(), - date.value.getUTCMonth(), - date.value.getUTCDate() - ); + event.value.start = qdate.buildDate({ hours: 0, minutes: 0, seconds: 0 }); const diff = event.value.start.getTime() - tpl.start.getTime(); - console.log(event.value); - console.log(diff); if (event.value.end) event.value.end = new Date(event.value.end.getTime() + diff); event.value.jobs.forEach((job) => { @@ -231,12 +242,13 @@ export default defineComponent({ } function reset() { - event.value = Object.assign({}, props.modelValue || emptyEvent); + event.value = Object.assign({}, props.modelValue || emptyEvent()); template.value = undefined; } return { - jobDeleteDisabled, + active, + activeRef, addJob, eventtypes, templates, diff --git a/src/plugins/events/components/management/Job.vue b/src/plugins/events/components/management/Job.vue index e0d16bf..d3d9287 100644 --- a/src/plugins/events/components/management/Job.vue +++ b/src/plugins/events/components/management/Job.vue @@ -1,6 +1,6 @@