mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
643 lines
19 KiB
643 lines
19 KiB
9 months ago
|
<script lang="ts" setup>
|
||
9 months ago
|
import dayjs from 'dayjs'
|
||
9 months ago
|
import type { Row } from '~/lib'
|
||
9 months ago
|
import { ref } from '#imports'
|
||
9 months ago
|
|
||
|
const emits = defineEmits(['expand-record'])
|
||
9 months ago
|
|
||
9 months ago
|
const { selectedDateRange, formattedData, formattedSideBarData, calendarRange, selectedDate, displayField, updateRowProperty } =
|
||
|
useCalendarViewStoreOrThrow()
|
||
9 months ago
|
|
||
9 months ago
|
const container = ref<null | HTMLElement>(null)
|
||
9 months ago
|
|
||
|
const { width: containerWidth } = useElementSize(container)
|
||
9 months ago
|
|
||
9 months ago
|
const { isUIAllowed } = useRoles()
|
||
|
|
||
9 months ago
|
const meta = inject(MetaInj, ref())
|
||
|
|
||
9 months ago
|
const weekDates = computed(() => {
|
||
9 months ago
|
const startOfWeek = new Date(selectedDateRange.value.start!)
|
||
|
const endOfWeek = new Date(selectedDateRange.value.end!)
|
||
9 months ago
|
const datesArray = []
|
||
9 months ago
|
while (startOfWeek.getTime() <= endOfWeek.getTime()) {
|
||
9 months ago
|
datesArray.push(new Date(startOfWeek))
|
||
|
startOfWeek.setDate(startOfWeek.getDate() + 1)
|
||
|
}
|
||
|
return datesArray
|
||
|
})
|
||
|
|
||
9 months ago
|
function getRandomNumbers() {
|
||
|
const typedArray = new Uint8Array(10)
|
||
|
const randomValues = window.crypto.getRandomValues(typedArray)
|
||
|
return randomValues.join('')
|
||
|
}
|
||
|
|
||
9 months ago
|
const findFirstSuitableColumn = (recordsInDay: any, startDayIndex: number, spanDays: number) => {
|
||
|
let column = 0
|
||
|
while (true) {
|
||
|
let isColumnSuitable = true
|
||
|
for (let i = 0; i < spanDays; i++) {
|
||
|
const dayIndex = startDayIndex + i
|
||
9 months ago
|
if (!recordsInDay[dayIndex]) {
|
||
|
recordsInDay[dayIndex] = {}
|
||
|
}
|
||
9 months ago
|
if (recordsInDay[dayIndex][column]) {
|
||
|
isColumnSuitable = false
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (isColumnSuitable) {
|
||
|
return column
|
||
|
}
|
||
|
column++
|
||
|
}
|
||
|
}
|
||
|
|
||
9 months ago
|
const calendarData = computed(() => {
|
||
|
if (!formattedData.value || !calendarRange.value) return []
|
||
9 months ago
|
const recordsInDay: {
|
||
|
[key: number]: {
|
||
|
[key: number]: boolean
|
||
|
}
|
||
|
} = {
|
||
9 months ago
|
0: {},
|
||
|
1: {},
|
||
|
2: {},
|
||
|
3: {},
|
||
|
4: {},
|
||
|
5: {},
|
||
|
6: {},
|
||
9 months ago
|
}
|
||
|
|
||
9 months ago
|
const recordsInRange: Array<Row> = []
|
||
9 months ago
|
const perDayWidth = containerWidth.value / 7
|
||
|
|
||
9 months ago
|
calendarRange.value.forEach((range) => {
|
||
9 months ago
|
const fromCol = range.fk_from_col
|
||
|
const toCol = range.fk_to_col
|
||
|
if (fromCol && toCol) {
|
||
9 months ago
|
for (const record of [...formattedData.value].filter((r) => {
|
||
9 months ago
|
const startDate = dayjs(r.row[fromCol.title!])
|
||
|
const endDate = dayjs(r.row[toCol.title!])
|
||
9 months ago
|
if (!startDate.isValid() || !endDate.isValid()) return false
|
||
9 months ago
|
return !endDate.isBefore(startDate)
|
||
|
})) {
|
||
9 months ago
|
const id = record.row.id ?? getRandomNumbers()
|
||
9 months ago
|
let startDate = dayjs(record.row[fromCol.title!])
|
||
9 months ago
|
const ogStartDate = startDate.clone()
|
||
9 months ago
|
const endDate = dayjs(record.row[toCol.title!])
|
||
9 months ago
|
|
||
9 months ago
|
if (startDate.isBefore(selectedDateRange.value.start)) {
|
||
|
startDate = dayjs(selectedDateRange.value.start)
|
||
|
}
|
||
9 months ago
|
|
||
9 months ago
|
const startDaysDiff = startDate.diff(selectedDateRange.value.start, 'day')
|
||
9 months ago
|
|
||
|
let spanDays = Math.max(Math.min(endDate.diff(startDate, 'day'), 6) + 1, 1)
|
||
|
|
||
|
if (endDate.isAfter(startDate, 'month')) {
|
||
|
spanDays = 7 - startDaysDiff
|
||
|
}
|
||
|
|
||
|
if (startDaysDiff > 0) {
|
||
|
spanDays = Math.min(spanDays, 7 - startDaysDiff)
|
||
|
}
|
||
9 months ago
|
const widthStyle = `calc(max(${spanDays} * ${perDayWidth}px, ${perDayWidth}px))`
|
||
9 months ago
|
|
||
9 months ago
|
let suitableColumn = -1
|
||
|
for (let i = 0; i < spanDays; i++) {
|
||
|
const dayIndex = startDaysDiff + i
|
||
9 months ago
|
|
||
9 months ago
|
if (!recordsInDay[dayIndex]) {
|
||
|
recordsInDay[dayIndex] = {}
|
||
9 months ago
|
}
|
||
|
|
||
9 months ago
|
if (suitableColumn === -1) {
|
||
|
suitableColumn = findFirstSuitableColumn(recordsInDay, dayIndex, spanDays)
|
||
9 months ago
|
}
|
||
9 months ago
|
}
|
||
9 months ago
|
|
||
9 months ago
|
// 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
|
||
9 months ago
|
}
|
||
9 months ago
|
|
||
9 months ago
|
let position = 'none'
|
||
9 months ago
|
|
||
|
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({
|
||
|
...record,
|
||
|
rowMeta: {
|
||
|
...record.rowMeta,
|
||
9 months ago
|
range: range as any,
|
||
9 months ago
|
position,
|
||
9 months ago
|
id,
|
||
9 months ago
|
style: {
|
||
|
width: widthStyle,
|
||
|
left: `${startDaysDiff * perDayWidth}px`,
|
||
9 months ago
|
top: `${suitableColumn * 40}px`,
|
||
9 months ago
|
},
|
||
|
},
|
||
|
})
|
||
9 months ago
|
}
|
||
9 months ago
|
} else if (fromCol) {
|
||
9 months ago
|
for (const record of formattedData.value) {
|
||
9 months ago
|
const id = record.row.id ?? getRandomNumbers()
|
||
|
|
||
9 months ago
|
const startDate = dayjs(record.row[fromCol.title!])
|
||
9 months ago
|
const startDaysDiff = Math.max(startDate.diff(selectedDateRange.value.start, 'day'), 0)
|
||
|
const suitableColumn = findFirstSuitableColumn(recordsInDay, startDaysDiff, 1)
|
||
|
recordsInDay[startDaysDiff][suitableColumn] = true
|
||
9 months ago
|
|
||
|
recordsInRange.push({
|
||
|
...record,
|
||
|
rowMeta: {
|
||
|
...record.rowMeta,
|
||
9 months ago
|
range: range as any,
|
||
9 months ago
|
id,
|
||
9 months ago
|
position: 'rounded',
|
||
|
style: {
|
||
|
width: `calc(${perDayWidth}px)`,
|
||
9 months ago
|
left: `${startDaysDiff * perDayWidth}px`,
|
||
9 months ago
|
top: `${suitableColumn * 40}px`,
|
||
9 months ago
|
},
|
||
9 months ago
|
},
|
||
|
})
|
||
9 months ago
|
}
|
||
|
}
|
||
|
})
|
||
|
|
||
|
return recordsInRange
|
||
|
})
|
||
9 months ago
|
|
||
9 months ago
|
const dragElement = ref<HTMLElement | null>(null)
|
||
|
|
||
9 months ago
|
const draggingId = ref<string | null>(null)
|
||
9 months ago
|
|
||
9 months ago
|
const resizeInProgress = ref(false)
|
||
9 months ago
|
|
||
9 months ago
|
const dragTimeout = ref<string | number | null | NodeJS.Timeout>(null)
|
||
9 months ago
|
|
||
9 months ago
|
const isDragging = ref(false)
|
||
|
const dragRecord = ref<Row>()
|
||
9 months ago
|
|
||
9 months ago
|
const resizeDirection = ref<'right' | 'left' | null>()
|
||
|
const resizeRecord = ref<Row | null>(null)
|
||
9 months ago
|
|
||
9 months ago
|
const useDebouncedRowUpdate = useDebounceFn((row: Row, updateProperty: string[], isDelete: boolean) => {
|
||
|
updateRowProperty(row, updateProperty, isDelete)
|
||
|
}, 500)
|
||
|
|
||
|
const onResize = (event: MouseEvent) => {
|
||
9 months ago
|
if (!isUIAllowed('dataEdit')) return
|
||
9 months ago
|
if (!container.value || !resizeRecord.value) return
|
||
9 months ago
|
const { width, left } = container.value.getBoundingClientRect()
|
||
|
|
||
|
const percentX = (event.clientX - left - window.scrollX) / width
|
||
|
|
||
9 months ago
|
const fromCol = resizeRecord.value.rowMeta.range?.fk_from_col
|
||
|
const toCol = resizeRecord.value.rowMeta.range?.fk_to_col
|
||
9 months ago
|
if (!fromCol || !toCol) return
|
||
|
|
||
|
const ogEndDate = dayjs(resizeRecord.value.row[toCol.title!])
|
||
|
const ogStartDate = dayjs(resizeRecord.value.row[fromCol.title!])
|
||
9 months ago
|
|
||
9 months ago
|
const day = Math.floor(percentX * 7)
|
||
|
|
||
9 months ago
|
if (resizeDirection.value === 'right') {
|
||
|
let newEndDate = dayjs(selectedDateRange.value.start).add(day, 'day')
|
||
9 months ago
|
const updateProperty = [toCol.title!]
|
||
9 months ago
|
|
||
9 months ago
|
if (dayjs(newEndDate).isBefore(ogStartDate, 'day')) {
|
||
|
newEndDate = ogStartDate.clone()
|
||
9 months ago
|
}
|
||
|
|
||
9 months ago
|
if (!newEndDate.isValid()) return
|
||
9 months ago
|
|
||
|
const newRow = {
|
||
9 months ago
|
...resizeRecord.value,
|
||
9 months ago
|
row: {
|
||
9 months ago
|
...resizeRecord.value.row,
|
||
9 months ago
|
[toCol.title!]: newEndDate.format('YYYY-MM-DD'),
|
||
9 months ago
|
},
|
||
|
}
|
||
9 months ago
|
|
||
9 months ago
|
formattedData.value = formattedData.value.map((r) => {
|
||
9 months ago
|
const pk = extractPkFromRow(r.row, meta.value!.columns!)
|
||
9 months ago
|
|
||
9 months ago
|
if (pk === extractPkFromRow(newRow.row, meta.value!.columns!)) {
|
||
9 months ago
|
return newRow
|
||
9 months ago
|
}
|
||
9 months ago
|
return r
|
||
|
})
|
||
|
useDebouncedRowUpdate(newRow, updateProperty, false)
|
||
|
} else if (resizeDirection.value === 'left') {
|
||
|
let newStartDate = dayjs(selectedDateRange.value.start).add(day, 'day')
|
||
9 months ago
|
const updateProperty = [fromCol.title!]
|
||
9 months ago
|
|
||
9 months ago
|
if (dayjs(newStartDate).isAfter(ogEndDate)) {
|
||
9 months ago
|
newStartDate = dayjs(dayjs(ogEndDate)).clone()
|
||
9 months ago
|
}
|
||
9 months ago
|
if (!newStartDate) return
|
||
9 months ago
|
|
||
9 months ago
|
const newRow = {
|
||
|
...resizeRecord.value,
|
||
|
row: {
|
||
|
...resizeRecord.value.row,
|
||
9 months ago
|
[fromCol.title!]: dayjs(newStartDate).format('YYYY-MM-DD'),
|
||
9 months ago
|
},
|
||
|
}
|
||
9 months ago
|
|
||
9 months ago
|
formattedData.value = formattedData.value.map((r) => {
|
||
9 months ago
|
const pk = extractPkFromRow(r.row, meta.value!.columns!)
|
||
9 months ago
|
|
||
9 months ago
|
if (pk === extractPkFromRow(newRow.row, meta.value!.columns!)) {
|
||
9 months ago
|
return newRow
|
||
|
}
|
||
|
return r
|
||
|
})
|
||
|
useDebouncedRowUpdate(newRow, updateProperty, false)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
const onResizeEnd = () => {
|
||
|
resizeInProgress.value = false
|
||
|
resizeDirection.value = null
|
||
|
resizeRecord.value = null
|
||
|
document.removeEventListener('mousemove', onResize)
|
||
|
document.removeEventListener('mouseup', onResizeEnd)
|
||
|
}
|
||
|
|
||
|
const onResizeStart = (direction: 'right' | 'left', event: MouseEvent, record: Row) => {
|
||
9 months ago
|
if (!isUIAllowed('dataEdit')) return
|
||
9 months ago
|
resizeInProgress.value = true
|
||
|
resizeDirection.value = direction
|
||
|
resizeRecord.value = record
|
||
|
document.addEventListener('mousemove', onResize)
|
||
|
document.addEventListener('mouseup', onResizeEnd)
|
||
|
}
|
||
|
|
||
|
const onDrag = (event: MouseEvent) => {
|
||
9 months ago
|
if (!isUIAllowed('dataEdit')) return
|
||
9 months ago
|
if (!container.value || !dragRecord.value) return
|
||
9 months ago
|
const { width, left } = container.value.getBoundingClientRect()
|
||
|
|
||
|
const percentX = (event.clientX - left - window.scrollX) / width
|
||
|
|
||
|
const fromCol = dragRecord.value.rowMeta.range?.fk_from_col
|
||
|
const toCol = dragRecord.value.rowMeta.range?.fk_to_col
|
||
|
|
||
9 months ago
|
if (!fromCol) return
|
||
|
|
||
9 months ago
|
const day = Math.floor(percentX * 7)
|
||
|
|
||
|
const newStartDate = dayjs(selectedDateRange.value.start).add(day, 'day')
|
||
|
if (!newStartDate) return
|
||
|
|
||
|
let endDate
|
||
|
|
||
|
const newRow = {
|
||
|
...dragRecord.value,
|
||
|
row: {
|
||
|
...dragRecord.value.row,
|
||
9 months ago
|
[fromCol.title!]: dayjs(newStartDate).format('YYYY-MM-DD'),
|
||
9 months ago
|
},
|
||
|
}
|
||
|
|
||
|
if (toCol) {
|
||
9 months ago
|
const fromDate = dragRecord.value.row[fromCol.title!] ? dayjs(dragRecord.value.row[fromCol.title!]) : null
|
||
|
const toDate = dragRecord.value.row[toCol.title!] ? dayjs(dragRecord.value.row[toCol.title!]) : null
|
||
9 months ago
|
|
||
|
if (fromDate && toDate) {
|
||
|
endDate = dayjs(newStartDate).add(toDate.diff(fromDate, 'day'), 'day')
|
||
|
} else if (fromDate && !toDate) {
|
||
|
endDate = dayjs(newStartDate).endOf('day')
|
||
|
} else if (!fromDate && toDate) {
|
||
|
endDate = dayjs(newStartDate).endOf('day')
|
||
9 months ago
|
} else {
|
||
9 months ago
|
endDate = newStartDate.clone()
|
||
|
}
|
||
|
|
||
9 months ago
|
newRow.row[toCol.title!] = dayjs(endDate).format('YYYY-MM-DD')
|
||
9 months ago
|
}
|
||
|
|
||
|
formattedData.value = formattedData.value.map((r) => {
|
||
9 months ago
|
const pk = extractPkFromRow(r.row, meta.value!.columns!)
|
||
9 months ago
|
|
||
9 months ago
|
if (pk === extractPkFromRow(newRow.row, meta.value!.columns!)) {
|
||
9 months ago
|
return newRow
|
||
9 months ago
|
}
|
||
9 months ago
|
return r
|
||
|
})
|
||
|
}
|
||
|
|
||
|
const stopDrag = (event: MouseEvent) => {
|
||
|
event.preventDefault()
|
||
9 months ago
|
clearTimeout(dragTimeout.value!)
|
||
9 months ago
|
|
||
9 months ago
|
if (!isUIAllowed('dataEdit')) return
|
||
9 months ago
|
if (!isDragging.value || !container.value || !dragRecord.value) return
|
||
9 months ago
|
|
||
9 months ago
|
const { width, left } = container.value.getBoundingClientRect()
|
||
|
|
||
|
const percentX = (event.clientX - left - window.scrollX) / width
|
||
|
|
||
|
const fromCol = dragRecord.value.rowMeta.range?.fk_from_col
|
||
|
const toCol = dragRecord.value.rowMeta.range?.fk_to_col
|
||
|
|
||
|
const day = Math.floor(percentX * 7)
|
||
|
|
||
|
const newStartDate = dayjs(selectedDateRange.value.start).add(day, 'day')
|
||
9 months ago
|
if (!newStartDate || !fromCol) return
|
||
9 months ago
|
|
||
|
let endDate
|
||
|
|
||
|
const newRow = {
|
||
|
...dragRecord.value,
|
||
|
row: {
|
||
|
...dragRecord.value.row,
|
||
9 months ago
|
[fromCol.title!]: dayjs(newStartDate).format('YYYY-MM-DD'),
|
||
9 months ago
|
},
|
||
|
}
|
||
|
|
||
9 months ago
|
const updateProperty = [fromCol.title!]
|
||
9 months ago
|
|
||
|
if (toCol) {
|
||
9 months ago
|
const fromDate = dragRecord.value.row[fromCol.title!] ? dayjs(dragRecord.value.row[fromCol.title!]) : null
|
||
|
const toDate = dragRecord.value.row[toCol.title!] ? dayjs(dragRecord.value.row[toCol.title!]) : null
|
||
9 months ago
|
|
||
|
if (fromDate && toDate) {
|
||
|
endDate = dayjs(newStartDate).add(toDate.diff(fromDate, 'day'), 'day')
|
||
|
} else if (fromDate && !toDate) {
|
||
|
endDate = dayjs(newStartDate).endOf('day')
|
||
|
} else if (!fromDate && toDate) {
|
||
|
endDate = dayjs(newStartDate).endOf('day')
|
||
|
} else {
|
||
|
endDate = newStartDate.clone()
|
||
9 months ago
|
}
|
||
9 months ago
|
|
||
9 months ago
|
newRow.row[toCol.title!] = dayjs(endDate).format('YYYY-MM-DD')
|
||
9 months ago
|
|
||
9 months ago
|
updateProperty.push(toCol.title!)
|
||
9 months ago
|
}
|
||
9 months ago
|
|
||
|
if (!newRow) return
|
||
|
|
||
|
if (dragElement.value) {
|
||
|
formattedData.value = formattedData.value.map((r) => {
|
||
9 months ago
|
const pk = extractPkFromRow(r.row, meta.value!.columns!)
|
||
9 months ago
|
|
||
9 months ago
|
if (pk === extractPkFromRow(newRow.row, meta.value!.columns!)) {
|
||
9 months ago
|
return newRow
|
||
|
}
|
||
|
return r
|
||
|
})
|
||
|
} else {
|
||
|
formattedData.value = [...formattedData.value, newRow]
|
||
|
formattedSideBarData.value = formattedSideBarData.value.filter((r) => {
|
||
9 months ago
|
const pk = extractPkFromRow(r.row, meta.value!.columns!)
|
||
9 months ago
|
|
||
9 months ago
|
return pk !== extractPkFromRow(newRow.row, meta.value!.columns!)
|
||
9 months ago
|
})
|
||
|
}
|
||
|
|
||
|
const allRecords = document.querySelectorAll('.draggable-record')
|
||
|
allRecords.forEach((el) => {
|
||
|
el.style.visibility = ''
|
||
|
el.style.opacity = '100%'
|
||
|
})
|
||
|
|
||
|
if (dragElement.value) {
|
||
|
dragElement.value.style.boxShadow = 'none'
|
||
|
dragElement.value.classList.remove('hide')
|
||
9 months ago
|
// isDragging.value = false
|
||
9 months ago
|
draggingId.value = null
|
||
|
dragElement.value = null
|
||
|
}
|
||
|
|
||
|
updateRowProperty(newRow, updateProperty, false)
|
||
|
|
||
|
document.removeEventListener('mousemove', onDrag)
|
||
|
document.removeEventListener('mouseup', stopDrag)
|
||
|
}
|
||
|
|
||
|
const dragStart = (event: MouseEvent, record: Row) => {
|
||
9 months ago
|
if (!isUIAllowed('dataEdit')) return
|
||
9 months ago
|
if (resizeInProgress.value) return
|
||
|
let target = event.target as HTMLElement
|
||
|
|
||
9 months ago
|
isDragging.value = false
|
||
9 months ago
|
|
||
9 months ago
|
dragTimeout.value = setTimeout(() => {
|
||
|
isDragging.value = true
|
||
|
while (!target.classList.contains('draggable-record')) {
|
||
|
target = target.parentElement as HTMLElement
|
||
9 months ago
|
}
|
||
|
|
||
9 months ago
|
const allRecords = document.querySelectorAll('.draggable-record')
|
||
|
allRecords.forEach((el) => {
|
||
|
if (!el.getAttribute('data-unique-id').includes(record.rowMeta.id!)) {
|
||
|
// el.style.visibility = 'hidden'
|
||
|
el.style.opacity = '30%'
|
||
|
}
|
||
|
})
|
||
|
|
||
|
dragRecord.value = record
|
||
9 months ago
|
|
||
9 months ago
|
isDragging.value = true
|
||
|
dragElement.value = target
|
||
|
draggingId.value = record.rowMeta.id!
|
||
|
dragRecord.value = record
|
||
9 months ago
|
|
||
9 months ago
|
document.addEventListener('mousemove', onDrag)
|
||
|
document.addEventListener('mouseup', stopDrag)
|
||
|
}, 200)
|
||
|
|
||
|
const onMouseUp = () => {
|
||
9 months ago
|
clearTimeout(dragTimeout.value!)
|
||
9 months ago
|
document.removeEventListener('mouseup', onMouseUp)
|
||
|
if (!isDragging.value) {
|
||
9 months ago
|
emits('expand-record', record)
|
||
9 months ago
|
}
|
||
|
}
|
||
|
|
||
|
document.addEventListener('mouseup', onMouseUp)
|
||
9 months ago
|
}
|
||
9 months ago
|
|
||
|
const dropEvent = (event: DragEvent) => {
|
||
|
if (!isUIAllowed('dataEdit')) return
|
||
|
event.preventDefault()
|
||
9 months ago
|
|
||
9 months ago
|
const data = event.dataTransfer?.getData('text/plain')
|
||
|
if (data) {
|
||
|
const {
|
||
|
record,
|
||
|
}: {
|
||
|
record: Row
|
||
|
} = JSON.parse(data)
|
||
|
const { width, left } = container.value.getBoundingClientRect()
|
||
|
|
||
|
const percentX = (event.clientX - left - window.scrollX) / width
|
||
|
|
||
|
const fromCol = record.rowMeta.range?.fk_from_col
|
||
|
const toCol = record.rowMeta.range?.fk_to_col
|
||
|
|
||
|
if (!fromCol) return
|
||
|
|
||
|
const day = Math.floor(percentX * 7)
|
||
|
|
||
|
const newStartDate = dayjs(selectedDateRange.value.start).add(day, 'day')
|
||
|
|
||
|
let endDate
|
||
|
|
||
|
const newRow = {
|
||
|
...record,
|
||
|
row: {
|
||
|
...record.row,
|
||
|
[fromCol.title!]: dayjs(newStartDate).format('YYYY-MM-DD'),
|
||
|
},
|
||
|
}
|
||
|
|
||
|
const updateProperty = [fromCol.title!]
|
||
|
|
||
|
if (toCol) {
|
||
|
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 (fromDate && toDate) {
|
||
|
endDate = dayjs(newStartDate).add(toDate.diff(fromDate, 'day'), 'day')
|
||
|
} else if (fromDate && !toDate) {
|
||
|
endDate = dayjs(newStartDate).endOf('day')
|
||
|
} else if (!fromDate && toDate) {
|
||
|
endDate = dayjs(newStartDate).endOf('day')
|
||
|
} else {
|
||
|
endDate = newStartDate.clone()
|
||
|
}
|
||
|
newRow.row[toCol.title!] = dayjs(endDate).format('YYYY-MM-DD')
|
||
|
updateProperty.push(toCol.title!)
|
||
|
}
|
||
|
|
||
|
if (!newRow) return
|
||
|
|
||
|
if (dragElement.value) {
|
||
|
formattedData.value = formattedData.value.map((r) => {
|
||
|
const pk = extractPkFromRow(r.row, meta.value!.columns!)
|
||
|
|
||
|
if (pk === extractPkFromRow(newRow.row, meta.value!.columns!)) {
|
||
|
return newRow
|
||
|
}
|
||
|
return r
|
||
|
})
|
||
|
} else {
|
||
|
formattedData.value = [...formattedData.value, newRow]
|
||
|
formattedSideBarData.value = formattedSideBarData.value.filter((r) => {
|
||
|
const pk = extractPkFromRow(r.row, meta.value!.columns!)
|
||
|
|
||
|
return pk !== extractPkFromRow(newRow.row, meta.value!.columns!)
|
||
|
})
|
||
|
}
|
||
|
|
||
|
if (dragElement.value) {
|
||
|
dragElement.value.style.boxShadow = 'none'
|
||
|
dragElement.value.classList.remove('hide')
|
||
|
|
||
|
dragElement.value = null
|
||
|
}
|
||
|
updateRowProperty(newRow, updateProperty, false)
|
||
|
}
|
||
|
}
|
||
9 months ago
|
</script>
|
||
|
|
||
|
<template>
|
||
9 months ago
|
<div class="flex relative flex-col prevent-select" @drop="dropEvent">
|
||
9 months ago
|
<div class="flex">
|
||
|
<div
|
||
|
v-for="date in weekDates"
|
||
9 months ago
|
:key="date.toISOString()"
|
||
9 months ago
|
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"
|
||
9 months ago
|
>
|
||
|
{{ dayjs(date).format('DD ddd') }}
|
||
|
</div>
|
||
|
</div>
|
||
9 months ago
|
<div ref="container" class="flex h-[calc(100vh-11.6rem)]">
|
||
9 months ago
|
<div
|
||
|
v-for="date in weekDates"
|
||
|
:key="date.toISOString()"
|
||
|
:class="{
|
||
|
'!border-2 border-brand-500': dayjs(date).isSame(selectedDate, 'day'),
|
||
|
}"
|
||
9 months ago
|
class="flex flex-col border-r-1 min-h-[100vh] last:border-r-0 items-center w-1/7"
|
||
9 months ago
|
@click="selectedDate = date"
|
||
|
></div>
|
||
|
</div>
|
||
9 months ago
|
<div class="absolute nc-scrollbar-md overflow-y-auto mt-9 pointer-events-none inset-0">
|
||
9 months ago
|
<div
|
||
|
v-for="(record, id) in calendarData"
|
||
|
:key="id"
|
||
9 months ago
|
: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',
|
||
|
}"
|
||
9 months ago
|
class="absolute group draggable-record pointer-events-auto"
|
||
9 months ago
|
@mousedown="dragStart($event, record)"
|
||
9 months ago
|
>
|
||
|
<LazySmartsheetRow :row="record">
|
||
|
<LazySmartsheetCalendarRecordCard
|
||
9 months ago
|
:date="dayjs(record.row[record.rowMeta.range!.fk_from_col.title!]).format('DD-MM-YYYY')"
|
||
9 months ago
|
:name="record.row[displayField!.title!]"
|
||
9 months ago
|
:position="record.rowMeta.position"
|
||
9 months ago
|
:record="record"
|
||
9 months ago
|
:resize="!!record.rowMeta.range?.fk_to_col && isUIAllowed('dataEdit')"
|
||
9 months ago
|
color="blue"
|
||
9 months ago
|
@dblclick="emits('expand-record', record)"
|
||
9 months ago
|
@resize-start="onResizeStart"
|
||
9 months ago
|
/>
|
||
|
</LazySmartsheetRow>
|
||
|
</div>
|
||
9 months ago
|
</div>
|
||
|
</div>
|
||
|
</template>
|
||
|
|
||
9 months ago
|
<style lang="scss" scoped>
|
||
|
.hide {
|
||
|
transition: 0.01s;
|
||
|
transform: translateX(-9999px);
|
||
|
}
|
||
9 months ago
|
.prevent-select {
|
||
|
-webkit-user-select: none; /* Safari */
|
||
|
-ms-user-select: none; /* IE 10 and IE 11 */
|
||
|
user-select: none; /* Standard syntax */
|
||
|
}
|
||
9 months ago
|
</style>
|