2022-06-15 12:00:32 +00:00
|
|
|
<!--
|
|
|
|
// Copyright © 2022 Hardcore Engineering Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Eclipse Public License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License. You may
|
|
|
|
// obtain a copy of the License at https://www.eclipse.org/legal/epl-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
//
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
-->
|
|
|
|
<script lang="ts">
|
2023-07-10 15:50:29 +00:00
|
|
|
import { Calendar, generateEventId } from '@hcengineering/calendar'
|
2023-08-04 18:06:21 +00:00
|
|
|
import { Employee, PersonAccount } from '@hcengineering/contact'
|
2023-03-22 02:48:57 +00:00
|
|
|
import { UserBoxList } from '@hcengineering/contact-resources'
|
2023-07-28 03:46:26 +00:00
|
|
|
import { Class, DateRangeMode, Doc, Ref, getCurrentAccount } from '@hcengineering/core'
|
2023-07-10 15:50:29 +00:00
|
|
|
import { Card, getClient } from '@hcengineering/presentation'
|
|
|
|
import ui, { DateRangePresenter, EditBox, ToggleWithLabel } from '@hcengineering/ui'
|
|
|
|
import { createEventDispatcher, tick } from 'svelte'
|
2022-06-15 12:00:32 +00:00
|
|
|
import calendar from '../plugin'
|
2023-07-28 03:46:26 +00:00
|
|
|
import { saveUTC } from '../utils'
|
2022-06-15 12:00:32 +00:00
|
|
|
|
|
|
|
export let attachedTo: Ref<Doc> = calendar.ids.NoAttached
|
|
|
|
export let attachedToClass: Ref<Class<Doc>> = calendar.class.Event
|
|
|
|
export let title: string = ''
|
|
|
|
export let date: Date | undefined = undefined
|
|
|
|
export let withTime = false
|
|
|
|
|
|
|
|
const now = new Date()
|
2023-07-25 10:50:55 +00:00
|
|
|
const defaultDuration = 60 * 60 * 1000
|
|
|
|
const allDayDuration = 24 * 60 * 60 * 1000 - 1
|
2022-06-15 12:00:32 +00:00
|
|
|
|
2022-10-24 06:44:08 +00:00
|
|
|
let startDate =
|
2022-06-15 12:00:32 +00:00
|
|
|
date === undefined ? now.getTime() : withTime ? date.getTime() : date.setHours(now.getHours(), now.getMinutes())
|
2022-10-24 06:44:08 +00:00
|
|
|
let duration = defaultDuration
|
|
|
|
let dueDate = startDate + duration
|
|
|
|
let dueDateRef: DateRangePresenter
|
2023-07-10 15:50:29 +00:00
|
|
|
let allDay = false
|
2022-10-24 06:44:08 +00:00
|
|
|
|
2023-08-04 18:06:21 +00:00
|
|
|
const currentUser = getCurrentAccount() as PersonAccount
|
|
|
|
let participants: Ref<Employee>[] = [currentUser.person as Ref<Employee>]
|
2022-06-15 12:00:32 +00:00
|
|
|
|
|
|
|
const dispatch = createEventDispatcher()
|
|
|
|
const client = getClient()
|
|
|
|
|
|
|
|
export function canClose (): boolean {
|
|
|
|
return title !== undefined && title.trim().length === 0 && participants.length === 0
|
|
|
|
}
|
|
|
|
|
|
|
|
async function saveEvent () {
|
|
|
|
let date: number | undefined
|
2022-10-24 06:44:08 +00:00
|
|
|
if (startDate != null) date = startDate
|
2022-06-15 12:00:32 +00:00
|
|
|
if (date === undefined) return
|
2023-07-10 15:50:29 +00:00
|
|
|
const space = `${getCurrentAccount()._id}_calendar` as Ref<Calendar>
|
|
|
|
await client.addCollection(calendar.class.Event, space, attachedTo, attachedToClass, 'events', {
|
|
|
|
eventId: generateEventId(),
|
2023-07-25 10:50:55 +00:00
|
|
|
date: allDay ? saveUTC(date) : date,
|
|
|
|
dueDate: allDay ? saveUTC(dueDate) : dueDate,
|
2022-06-15 12:00:32 +00:00
|
|
|
description: '',
|
|
|
|
participants,
|
2023-07-10 15:50:29 +00:00
|
|
|
title,
|
|
|
|
allDay,
|
|
|
|
access: 'owner'
|
2022-06-15 12:00:32 +00:00
|
|
|
})
|
|
|
|
}
|
2022-10-24 06:44:08 +00:00
|
|
|
|
|
|
|
const handleNewStartDate = async (newStartDate: number | null) => {
|
|
|
|
if (newStartDate !== null) {
|
2023-07-25 10:50:55 +00:00
|
|
|
startDate = allDay ? new Date(newStartDate).setHours(0, 0, 0, 0) : newStartDate
|
2023-07-10 15:50:29 +00:00
|
|
|
dueDate = startDate + (allDay ? allDayDuration : duration)
|
2022-10-24 06:44:08 +00:00
|
|
|
await tick()
|
|
|
|
dueDateRef.adaptValue()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleNewDueDate = async (newDueDate: number | null) => {
|
|
|
|
if (newDueDate !== null) {
|
|
|
|
const diff = newDueDate - startDate
|
|
|
|
if (diff > 0) {
|
2023-07-25 10:50:55 +00:00
|
|
|
dueDate = allDay ? new Date(newDueDate).setHours(23, 59, 59, 999) : newDueDate
|
|
|
|
duration = dueDate - startDate
|
2022-10-24 06:44:08 +00:00
|
|
|
} else {
|
2023-07-10 15:50:29 +00:00
|
|
|
dueDate = startDate + (allDay ? allDayDuration : duration)
|
2022-10-24 06:44:08 +00:00
|
|
|
}
|
|
|
|
await tick()
|
|
|
|
dueDateRef.adaptValue()
|
|
|
|
}
|
|
|
|
}
|
2023-07-10 15:50:29 +00:00
|
|
|
|
2023-07-11 07:33:46 +00:00
|
|
|
async function allDayChangeHandler () {
|
|
|
|
if (allDay) {
|
2023-07-25 10:50:55 +00:00
|
|
|
startDate = new Date(startDate).setHours(0, 0, 0, 0)
|
|
|
|
if (dueDate - startDate < allDayDuration) dueDate = allDayDuration + startDate
|
|
|
|
else dueDate = new Date(dueDate).setHours(23, 59, 59, 999)
|
2023-07-11 07:33:46 +00:00
|
|
|
} else {
|
|
|
|
dueDate = startDate + defaultDuration
|
|
|
|
}
|
|
|
|
await tick()
|
|
|
|
dueDateRef.adaptValue()
|
|
|
|
}
|
|
|
|
|
2023-07-10 15:50:29 +00:00
|
|
|
$: mode = allDay ? DateRangeMode.DATE : DateRangeMode.DATETIME
|
2022-06-15 12:00:32 +00:00
|
|
|
</script>
|
|
|
|
|
|
|
|
<Card
|
|
|
|
label={calendar.string.CreateEvent}
|
|
|
|
okAction={saveEvent}
|
2022-10-24 06:44:08 +00:00
|
|
|
canSave={title !== undefined && title.trim().length > 0 && participants.length > 0 && startDate !== undefined}
|
2022-06-15 12:00:32 +00:00
|
|
|
on:close={() => {
|
|
|
|
dispatch('close')
|
|
|
|
}}
|
2023-04-07 16:59:18 +00:00
|
|
|
on:changeContent
|
2022-06-15 12:00:32 +00:00
|
|
|
>
|
2023-05-30 09:57:41 +00:00
|
|
|
<EditBox bind:value={title} placeholder={calendar.string.Title} kind={'large-style'} autoFocus />
|
2022-06-15 12:00:32 +00:00
|
|
|
<svelte:fragment slot="pool">
|
2023-07-11 07:33:46 +00:00
|
|
|
<ToggleWithLabel bind:on={allDay} label={calendar.string.AllDay} on:change={allDayChangeHandler} />
|
2022-10-24 06:44:08 +00:00
|
|
|
<DateRangePresenter
|
|
|
|
value={startDate}
|
|
|
|
labelNull={ui.string.SelectDate}
|
|
|
|
on:change={async (event) => await handleNewStartDate(event.detail)}
|
2023-07-06 07:01:27 +00:00
|
|
|
kind={'regular'}
|
2023-07-10 15:50:29 +00:00
|
|
|
{mode}
|
2023-04-25 02:59:07 +00:00
|
|
|
size={'large'}
|
2022-10-24 06:44:08 +00:00
|
|
|
editable
|
|
|
|
/>
|
|
|
|
<DateRangePresenter
|
|
|
|
bind:this={dueDateRef}
|
|
|
|
value={dueDate}
|
|
|
|
labelNull={calendar.string.DueTo}
|
|
|
|
on:change={async (event) => await handleNewDueDate(event.detail)}
|
2023-07-06 07:01:27 +00:00
|
|
|
kind={'regular'}
|
2023-07-10 15:50:29 +00:00
|
|
|
{mode}
|
2023-04-25 02:59:07 +00:00
|
|
|
size={'large'}
|
2022-10-24 06:44:08 +00:00
|
|
|
editable
|
|
|
|
/>
|
2023-07-06 07:01:27 +00:00
|
|
|
<UserBoxList bind:items={participants} label={calendar.string.Participants} kind={'regular'} size={'large'} />
|
2022-06-15 12:00:32 +00:00
|
|
|
</svelte:fragment>
|
|
|
|
</Card>
|