Browse Source

feat(nc-gui): week view date time partial support

pull/7611/head
DarkPhoenix2704 9 months ago
parent
commit
b8746dd5bd
  1. 371
      packages/nc-gui/components/smartsheet/calendar/WeekView/DateTimeField.vue

371
packages/nc-gui/components/smartsheet/calendar/WeekView/DateTimeField.vue

@ -1,11 +1,11 @@
<script lang="ts" setup>
import dayjs from 'dayjs'
import type { Row } from '~/lib'
import { ref } from '#imports'
import { computed, ref } from '#imports'
const emits = defineEmits(['expand-record'])
const { selectedDateRange, formattedData, formattedSideBarData, calendarRange, selectedDate, displayField, updateRowProperty } =
const { selectedDateRange, formattedData, formattedSideBarData, calendarRange, displayField, selectedTime, updateRowProperty } =
useCalendarViewStoreOrThrow()
const container = ref<null | HTMLElement>(null)
@ -16,15 +16,29 @@ const { isUIAllowed } = useRoles()
const meta = inject(MetaInj, ref())
const weekDates = computed(() => {
const datesHours = computed(() => {
const datesHours: Array<Array<dayjs.Dayjs>> = []
const startOfWeek = new Date(selectedDateRange.value.start!)
const endOfWeek = new Date(selectedDateRange.value.end!)
const datesArray = []
while (startOfWeek.getTime() <= endOfWeek.getTime()) {
datesArray.push(new Date(startOfWeek))
const hours: Array<dayjs.Dayjs> = []
for (let i = 0; i < 24; i++) {
hours.push(
dayjs()
.hour(i)
.minute(0)
.second(0)
.millisecond(0)
.year(startOfWeek.getFullYear() || dayjs().year())
.month(startOfWeek.getMonth() || dayjs().month())
.date(startOfWeek.getDate() || dayjs().date()),
)
}
datesHours.push(hours)
startOfWeek.setDate(startOfWeek.getDate() + 1)
}
return datesArray
return datesHours
})
function getRandomNumbers() {
@ -55,146 +69,210 @@ const findFirstSuitableColumn = (recordsInDay: any, startDayIndex: number, spanD
}
}
const calendarData = computed(() => {
if (!formattedData.value || !calendarRange.value) return []
const recordsInDay: {
[key: number]: {
[key: number]: boolean
const recordsAcrossAllRange = computed<{
records: Array<Row>
count: {
[key: string]: {
[key: string]: {
overflow: boolean
count: number
overflowCount: number
}
}
}
}>(() => {
if (!formattedData.value || !calendarRange.value || !container.value) return []
const { scrollHeight } = container.value
const perWidth = containerWidth.value / 7
const perHeight = scrollHeight / 24
const scheduleStart = dayjs(selectedDateRange.value.start).startOf('day')
const scheduleEnd = dayjs(selectedDateRange.value.end).endOf('day')
const perRecordHeight = 40
const spaceBetweenRecords = 5
const recordsInDayHour: {
[key: string]: {
[key: string]: {
overflow: boolean
count: number
overflowCount: number
}
} = {
0: {},
1: {},
2: {},
3: {},
4: {},
5: {},
6: {},
}
} = {}
if (!calendarRange.value) return []
const recordsInRange: Array<Row> = []
const perDayWidth = containerWidth.value / 7
const recordsToDisplay: Array<Row> = []
calendarRange.value.forEach((range) => {
const fromCol = range.fk_from_col
const toCol = range.fk_to_col
const sortedFormattedData = [...formattedData.value].filter((record) => {
const fromDate = record.row[fromCol.title!] ? dayjs(record.row[fromCol.title!]) : null
if (fromCol && toCol) {
for (const record of [...formattedData.value].filter((r) => {
const startDate = dayjs(r.row[fromCol.title!])
const endDate = dayjs(r.row[toCol.title!])
if (!startDate.isValid() || !endDate.isValid()) return false
return !endDate.isBefore(startDate)
})) {
const id = record.row.id ?? getRandomNumbers()
let startDate = dayjs(record.row[fromCol.title!])
const ogStartDate = startDate.clone()
const endDate = dayjs(record.row[toCol.title!])
const fromDate = record.row[fromCol.title!] ? dayjs(record.row[fromCol.title!]) : null
const toDate = record.row[toCol.title!] ? dayjs(record.row[toCol.title!]) : null
if (startDate.isBefore(selectedDateRange.value.start)) {
startDate = dayjs(selectedDateRange.value.start)
return fromDate && toDate && !toDate.isBefore(fromDate)
} else if (fromCol && !toCol) {
return !!fromDate
}
return false
})
const startDaysDiff = startDate.diff(selectedDateRange.value.start, 'day')
let spanDays = Math.max(Math.min(endDate.diff(startDate, 'day'), 6) + 1, 1)
sortedFormattedData.forEach((record: Row) => {
if (!toCol && fromCol) {
const startDate = record.row[fromCol.title!] ? dayjs(record.row[fromCol.title!]) : null
const dateKey = startDate?.format('YYYY-MM-DD')
const hourKey = startDate?.format('HH:mm')
if (endDate.isAfter(startDate, 'month')) {
spanDays = 7 - startDaysDiff
if (dateKey && hourKey) {
if (!recordsInDayHour[dateKey]) {
recordsInDayHour[dateKey] = {}
}
if (startDaysDiff > 0) {
spanDays = Math.min(spanDays, 7 - startDaysDiff)
if (!recordsInDayHour[dateKey][hourKey]) {
recordsInDayHour[dateKey][hourKey] = {
overflow: false,
count: 0,
overflowCount: 0,
}
}
recordsInDayHour[dateKey][hourKey].count++
}
const widthStyle = `calc(max(${spanDays} * ${perDayWidth}px, ${perDayWidth}px))`
let suitableColumn = -1
for (let i = 0; i < spanDays; i++) {
const dayIndex = startDaysDiff + i
const id = record.rowMeta.id ?? getRandomNumbers()
if (!recordsInDay[dayIndex]) {
recordsInDay[dayIndex] = {}
}
const dayIndex = dayjs(dateKey).day() - 1
const hourIndex = datesHours.value[dayIndex].findIndex((h) => h.format('HH:mm') === hourKey)
if (suitableColumn === -1) {
suitableColumn = findFirstSuitableColumn(recordsInDay, dayIndex, spanDays)
}
const style = {
left: `${dayIndex * perWidth}px`,
top: `${hourIndex * perHeight}px`,
}
// Mark the suitable column as occupied for the entire span
for (let i = 0; i < spanDays; i++) {
const dayIndex = startDaysDiff + i
recordsInDay[dayIndex][suitableColumn] = true
}
let position = 'none'
const isStartInRange =
ogStartDate && ogStartDate.isBetween(selectedDateRange.value.start, selectedDateRange.value.end, 'day', '[]')
const isEndInRange = endDate && endDate.isBetween(selectedDateRange.value.start, selectedDateRange.value.end, 'day', '[]')
if (isStartInRange && isEndInRange) {
position = 'rounded'
} else if (
startDate &&
endDate &&
startDate.isBefore(selectedDateRange.value.start) &&
endDate.isAfter(selectedDateRange.value.end)
) {
position = 'none'
} else if (
startDate &&
endDate &&
(startDate.isAfter(selectedDateRange.value.end) || endDate.isBefore(selectedDateRange.value.start))
) {
position = 'none'
} else if (isStartInRange) {
position = 'leftRounded'
} else if (isEndInRange) {
position = 'rightRounded'
}
recordsInRange.push({
const recordIndex = recordsInDayHour[dateKey][hourKey].count
const top = hourIndex * perHeight + (recordIndex - 1) * (perRecordHeight + spaceBetweenRecords)
const heightRequired = perRecordHeight * recordIndex + spaceBetweenRecords
if (heightRequired + 20 > perHeight) {
style.display = 'none'
recordsInDayHour[dateKey][hourKey].overflow = true
recordsInDayHour[dateKey][hourKey].overflowCount++
} else {
style.height = `${perRecordHeight}px`
style.top = `${top}px`
}
recordsToDisplay.push({
...record,
rowMeta: {
...record.rowMeta,
range: range as any,
position,
id,
style: {
width: widthStyle,
left: `${startDaysDiff * perDayWidth}px`,
top: `${suitableColumn * 40}px`,
},
style,
range,
},
})
} else if (fromCol && toCol) {
const id = record.rowMeta.id ?? getRandomNumbers()
let startDate = record.row[fromCol.title!] ? dayjs(record.row[fromCol.title!]) : null
let endDate = record.row[toCol.title!] ? dayjs(record.row[toCol.title!]) : null
if (!startDate?.isValid() || !endDate?.isValid()) return
if (startDate.isBefore(scheduleStart, 'minutes')) {
startDate = scheduleStart
}
} else if (fromCol) {
for (const record of formattedData.value) {
const id = record.row.id ?? getRandomNumbers()
if (endDate.isAfter(scheduleEnd, 'minutes')) {
endDate = scheduleEnd
}
let currentStartDate: dayjs.Dayjs = startDate.clone()
while (currentStartDate.isSameOrBefore(endDate!, 'day')) {
const currentEndDate = currentStartDate.clone().endOf('day')
const recordStart = currentEndDate.isSameOrBefore(startDate, 'day') ? startDate : currentStartDate
const recordEnd = currentEndDate.isAfter(endDate, 'day') ? endDate : currentEndDate
const dateKey = recordStart.format('YYYY-MM-DD')
let hour = recordStart?.clone().startOf('hour')
while (hour.isSameOrBefore(recordEnd, 'hour')) {
const hourKey = hour.format('HH:mm')
if (!recordsInDayHour[dateKey]) {
recordsInDayHour[dateKey] = {}
}
if (!recordsInDayHour[dateKey][hourKey]) {
recordsInDayHour[dateKey][hourKey] = {
overflow: false,
count: 0,
overflowCount: 0,
}
}
recordsInDayHour[dateKey][hourKey].count++
hour = hour.add(1, 'hour')
}
const dayIndex = dayjs(dateKey).day() - 1
let maxRecordCount = 0
for (let i = 0; i < (datesHours.value[dayIndex] ?? []).length; i++) {
const hourKey = datesHours.value[dayIndex][i].format('HH:mm')
if (recordsInDayHour[dateKey]?.[hourKey]?.count > maxRecordCount) {
maxRecordCount = recordsInDayHour[dateKey][hourKey].count
}
}
const startHourIndex = (datesHours.value[dayIndex] ?? []).findIndex(
(h) => h.format('HH:mm') === recordStart.format('HH:mm'),
)
const startDate = dayjs(record.row[fromCol.title!])
const startDaysDiff = Math.max(startDate.diff(selectedDateRange.value.start, 'day'), 0)
const suitableColumn = findFirstSuitableColumn(recordsInDay, startDaysDiff, 1)
recordsInDay[startDaysDiff][suitableColumn] = true
const endHourIndex = (datesHours.value[dayIndex] ?? []).findIndex(
(h) => h.format('HH:mm') === recordEnd?.startOf('hour').format('HH:mm'),
)
recordsInRange.push({
const spanHours = endHourIndex - startHourIndex + 1
console.log(record.row[displayField.value.title], startHourIndex, endHourIndex)
const style: Partial<CSSStyleDeclaration> = {
left: `${dayIndex * perWidth}px`,
top: `${startHourIndex * perHeight}px`,
height: `${(endHourIndex - startHourIndex + 1) * perHeight - spanHours - 5}px`,
}
recordsToDisplay.push({
...record,
rowMeta: {
...record.rowMeta,
range: range as any,
id,
position: 'rounded',
style: {
width: `calc(${perDayWidth}px)`,
left: `${startDaysDiff * perDayWidth}px`,
top: `${suitableColumn * 40}px`,
},
style,
range,
},
})
currentStartDate = currentStartDate.add(1, 'day')
}
}
})
return recordsInRange
console.log(recordsInDayHour)
})
return {
records: recordsToDisplay,
count: recordsInDayHour,
}
})
const dragElement = ref<HTMLElement | null>(null)
@ -480,56 +558,69 @@ const dropEvent = (event: DragEvent) => {
</script>
<template>
<div class="flex relative flex-col prevent-select" @drop="dropEvent">
<div class="flex">
<div class="w-full relative prevent-select" @drop="dropEvent">
<div class="flex absolute w-full top-0">
<div
v-for="date in weekDates"
:key="date.toISOString()"
v-for="date in datesHours"
:key="date[0].toISOString()"
class="w-1/7 text-center text-sm text-gray-500 w-full py-1 border-gray-200 border-b-1 border-r-1 bg-gray-50"
>
{{ dayjs(date).format('DD ddd') }}
{{ dayjs(date[0]).format('DD ddd') }}
</div>
</div>
<div ref="container" class="flex h-[calc(100vh-11.6rem)]">
<div ref="container" class="h-[calc(100vh-11.7rem)] relative flex w-full mt-7.5 overflow-y-auto nc-scrollbar-md">
<div v-for="(date, index) in datesHours" :key="index" class="h-full w-1/7">
<div
v-for="date in weekDates"
:key="date.toISOString()"
v-for="(hour, hourIndex) in date"
:key="hourIndex"
:class="{
'!border-2 border-brand-500': dayjs(date).isSame(selectedDate, 'day'),
'border-2 !border-brand-500': hour.isSame(selectedTime, 'hour'),
}"
class="flex flex-col border-r-1 min-h-[100vh] last:border-r-0 items-center w-1/7"
@click="selectedDate = date"
></div>
class="text-center relative h-56 text-sm text-gray-500 w-full py-1 border-gray-200 border-1 border-r-white border-t-white last:border-r-white bg-gray-50"
@click="selectedTime = hour.toDate()"
>
<span v-if="date[0].day() === selectedDateRange.start?.getDay()" class="absolute left-1">
{{ hour.format('h A') }}
</span>
<div
v-if="
recordsAcrossAllRange.count?.[dayjs(hour).format('YYYY-MM-DD')]?.[dayjs(hour).format('HH:mm')]?.overflow &&
!draggingId
"
class="text-xs absolute bottom-2 text-center inset-x-0 !z-[90] text-gray-500"
>
+
{{ recordsAcrossAllRange.count[dayjs(hour).format('YYYY-MM-DD')]?.[dayjs(hour).format('HH:mm')]?.overflowCount }}
more
</div>
</div>
<div class="absolute nc-scrollbar-md overflow-y-auto mt-9 pointer-events-none inset-0">
</div>
<div class="absolute pointer-events-none inset-0 !mt-[20px]">
<div
v-for="(record, id) in calendarData"
:key="id"
:data-unique-id="record.rowMeta.id"
:style="{
...record.rowMeta.style,
boxShadow:
record.rowMeta.id === draggingId
? '0px 8px 8px -4px rgba(0, 0, 0, 0.04), 0px 20px 24px -4px rgba(0, 0, 0, 0.10)'
: 'none',
}"
class="absolute group draggable-record pointer-events-auto"
v-for="(record, rowIndex) in recordsAcrossAllRange.records"
:key="rowIndex"
:data-unique-id="record.rowMeta!.id"
:style="record.rowMeta!.style"
class="absolute draggable-record w-1/7 cursor-pointer pointer-events-auto"
@mousedown="dragStart($event, record)"
@dragover.prevent
>
<LazySmartsheetRow :row="record">
<LazySmartsheetCalendarRecordCard
:date="dayjs(record.row[record.rowMeta.range!.fk_from_col.title!]).format('DD-MM-YYYY HH:mm')"
:name="record.row[displayField!.title!]"
:position="record.rowMeta.position"
:date="dayjs(record.row![record.rowMeta!.range!.fk_from_col.title!]).format('HH:mm')"
:name="record.row![displayField!.title!]"
:position="record.rowMeta!.position"
:record="record"
:resize="!!record.rowMeta.range?.fk_to_col && isUIAllowed('dataEdit')"
:resize="false"
color="blue"
@dblclick="emits('expand-record', record)"
size="auto"
/>
</LazySmartsheetRow>
</div>
</div>
</div>
</div>
</template>
<style lang="scss" scoped>
@ -538,8 +629,8 @@ const dropEvent = (event: DragEvent) => {
transform: translateX(-9999px);
}
.prevent-select {
-webkit-user-select: none; /* Safari */
-ms-user-select: none; /* IE 10 and IE 11 */
user-select: none; /* Standard syntax */
-webkit-user-select: none;
-ms-user-select: none;
user-select: none;
}
</style>

Loading…
Cancel
Save