import * as React from 'react'; import { BookingCalendarClasses, bookingCalendarDefaultClasses, } from './classes'; import classNames from 'classnames'; import { BookingCalendarDateRange, BookingCalendarItemT, defaultGetMomentFormatFunction, GetNewMomentFunctionType, defaultGetNewMomentFunction, defaultGridAvailableSteps, GetMomentFormatFunctionType, ZoomLevel, } from './common'; import { BookingCalendarControls as DefaultBookingCalendarControls, BookingCalendarControlsProps, } from './bookingCalendarControls/BookingCalendarControls'; import { generateGridItems, LaneSourceData, splitBookingsToLanes, } from './utils'; import { BookingCalendarLane as DefaultBookingCalendarLane, BookingCalendarLaneProps, } from './bookingCalendarLane/BookingCalendarLane'; import moment, { Moment, Duration } from 'moment'; import { BookingCalendarItemProps, bookingCalendarItemDefaultClasses, } from './bookingCalendarItem'; import { BookingCalendarRenderItemProps } from './bookingCalendarRenderItem'; import { BookingCalendarGrid as DefaultBookingCalendarGrid, BookingCalendarGridProps, } from './bookingCalendarGrid/BookingCalendarGrid'; import { BookingCalendarLaneHeader as DefaultBookingCalendarLaneHeader, BookingCalendarLaneHeaderProps, } from './bookingCalendarLaneHeader'; import { BookingCalendarLanesHeader as DefaultBookingCalendarLanesHeader, BookingCalendarLanesHeaderProps, bookingCalendarLanesHeaderDefaultClasses, } from './bookingCalendarLanesHeader'; import { BookingCalendarDatePicker as DefaultBookingCalendarDatePicker, BookingCalendarDatePickerProps, } from './bookingCalendarDatePicker'; import { BookingCalendarTimeAxis } from './bookingCalendarTimeAxis/BookingCalendarTimeAxis'; import useDimensions from 'react-cool-dimensions'; import BookingCalendarSelection, { BookingCalendarSelectionData, } from './bookingCalendarSelection/BookingCalendarSelection'; import { bookingCalendarLaneDefaultClasses } from './bookingCalendarLane'; import { bookingCalendarSelectionDefaultClasses } from './bookingCalendarSelection/classes'; import { BookingCalendarNowSectionProps, BookingCalendarNowSection as DefaultBookingCalendarNowSection, } from './bookingCalendarNowSection'; export interface BookingCalendarProps extends BookingCalendarClasses { getMoment?: () => Moment; bookings: T[]; from: Moment; till: Moment; getMomentFormatFunction?: GetMomentFormatFunctionType; getNewMomentFunction?: GetNewMomentFunctionType; onRangeChange?: (range: BookingCalendarDateRange) => void; step?: Duration; showZoomAllButton?: boolean; showGrid?: boolean; gridAvailableSteps?: Duration[]; goalGridWidthPx?: number; minLanesCount?: number; lanesSource?: LaneSourceData[]; unmatchedLanesToFront?: boolean; children?: React.ReactNode; bookingCalendarTopLeftHeader?: React.ReactNode; filteringButton?: React.ReactNode; zoomLevels?: ZoomLevel[]; filterBookingsToZoom?: (booking: T) => boolean; showSelection?: boolean; minSelectionSize?: number; onSelection?: ( items: T[], from: Moment, till: Moment, e: React.MouseEvent ) => void; selectionContent?: React.ReactNode; showHeader?: boolean; showTimeAxis?: boolean; showNowSection?: boolean; alwaysShowNowSections?: boolean; headersClickEnabled?: boolean; onHeaderClick?: (range: BookingCalendarDateRange) => void; BookingCalendarItem?: React.ComponentType>; BookingCalendarRenderItem?: React.ComponentType< BookingCalendarRenderItemProps >; BookingCalendarLane?: React.ComponentType>; BookingCalendarControls?: React.ComponentType< BookingCalendarControlsProps >; BookingCalendarGrid?: React.ComponentType; BookingCalendarLaneHeader?: React.ComponentType< BookingCalendarLaneHeaderProps >; BookingCalendarLanesHeader?: React.ComponentType< BookingCalendarLanesHeaderProps >; BookingCalendarDatePicker?: | React.ComponentType> | React.ReactNode; BookingCalendarNowSection?: React.ComponentType; } const defaultStep = moment.duration(1, 'day'); export function BookingCalendar( props: BookingCalendarProps ) { const { getMoment = () => moment(), bookings, className, laneContainerClassName, laneHeaderContainerClassName, lanesHeaderContainerClassName, lanesHeaderHeaderContainerClassName, controlsClasses, tableClassName, from, till, onRangeChange, step = defaultStep, showGrid = true, showHeader = true, showSelection = true, showNowSection = true, alwaysShowNowSections = false, onSelection, minSelectionSize = 10, selectionContent, showZoomAllButton = true, gridAvailableSteps = defaultGridAvailableSteps, getMomentFormatFunction = defaultGetMomentFormatFunction, getNewMomentFunction = defaultGetNewMomentFunction, goalGridWidthPx = 60, minLanesCount, lanesSource, unmatchedLanesToFront = true, filterBookingsToZoom, zoomLevels, BookingCalendarControls = DefaultBookingCalendarControls, BookingCalendarItem, BookingCalendarLane = DefaultBookingCalendarLane, BookingCalendarRenderItem, BookingCalendarGrid = DefaultBookingCalendarGrid, BookingCalendarLaneHeader = DefaultBookingCalendarLaneHeader, BookingCalendarLanesHeader = DefaultBookingCalendarLanesHeader, BookingCalendarDatePicker = DefaultBookingCalendarDatePicker, BookingCalendarNowSection = DefaultBookingCalendarNowSection, bookingCalendarTopLeftHeader, filteringButton, headersClickEnabled = true, onHeaderClick, children, } = props; const { showTimeAxis = Boolean(bookings?.length) || Boolean(lanesSource?.length), } = props; const lanes = React.useMemo( () => splitBookingsToLanes( bookings, from, till, minLanesCount, lanesSource, unmatchedLanesToFront ), [bookings, from, till, minLanesCount, lanesSource, unmatchedLanesToFront] ); const { observe, entry } = useDimensions(); const width = entry?.target?.scrollWidth ?? 0; const { items: gridItems, bestStep } = React.useMemo( () => generateGridItems( from, till, step, width, gridAvailableSteps, goalGridWidthPx, from?.clone()?.startOf('day') ), [from, till, step, width, gridAvailableSteps, goalGridWidthPx] ); const containerRef = React.useRef(); const onSelected = React.useCallback( (data: BookingCalendarSelectionData, e: React.MouseEvent) => { if (!from || !till || !width || !onRangeChange) { return; } const selectionLeft = Math.min( data.origin[0] - window.scrollX, data.target[0] - window.scrollX ); const selectionRight = Math.max( data.origin[0] - window.scrollX, data.target[0] - window.scrollX ); const selectionTop = Math.min( data.origin[1] - window.scrollY, data.target[1] - window.scrollY ); const selectionBottom = Math.max( data.origin[1] - window.scrollY, data.target[1] - window.scrollY ); const durationMs = till.valueOf() - from.valueOf(); const toTimeSpace = (num: number) => (num / (width || 1)) * durationMs; const timeSpaceStart = from.clone().add(toTimeSpace(selectionLeft), 'ms'); const timeSpaceEnd = from.clone().add(toTimeSpace(selectionRight), 'ms'); if (onSelection) { const itemSelector = `.${bookingCalendarLaneDefaultClasses.className}:not(.${bookingCalendarLanesHeaderDefaultClasses.laneClassName}) > .${bookingCalendarItemDefaultClasses.className}`; const allItems = Array.from( containerRef?.current?.querySelectorAll(itemSelector) ); const selectionAreaBB = containerRef?.current ?.querySelector(`.${bookingCalendarSelectionDefaultClasses.root}`) .getBoundingClientRect(); const selectedItems = allItems.filter((item) => { const itemBB = item.getBoundingClientRect(); const itemLeft = itemBB.left - selectionAreaBB.left; const itemTop = itemBB.top - selectionAreaBB.top; const itemRight = itemBB.right - selectionAreaBB.left; const itemBottom = itemBB.bottom - selectionAreaBB.top; // check if the item rectangle is contained or intersects or overlaps with the selection rectangle return ( itemLeft < selectionRight && itemRight > selectionLeft && itemTop < selectionBottom && itemBottom > selectionTop ); }); const selectedIds = selectedItems.map((item) => item.getAttribute('data-id') ); onSelection( bookings?.filter((b) => selectedIds.includes(b.id?.toString())), timeSpaceStart, timeSpaceEnd, e ); } }, [ from, till, width, onRangeChange, onSelection, onSelection ? bookings : null, ] ); const firstDataRowIndex = showHeader ? 2 : 1; return (
{...controlsClasses} getMoment={getMoment} items={bookings} from={from} till={till} onRangeChange={onRangeChange} step={step} filterBookingsToZoom={filterBookingsToZoom} zoomLevels={zoomLevels} filteringButton={filteringButton} showZoomAllButton={showZoomAllButton} bookingCalendarDatePicker={ <> {typeof BookingCalendarDatePicker === 'function' ? ( ) : ( BookingCalendarDatePicker )} } />
{showGrid && ( )} {showNowSection && ( )} {showSelection && ( {selectionContent} )} {showHeader && ( <>
{bookingCalendarTopLeftHeader}
{...controlsClasses} from={from} till={till} onRangeChange={ headersClickEnabled ? onHeaderClick ?? onRangeChange : undefined } step={step} />
)} {lanes.map((lane, laneIndex) => { const LaneBookingCalendarLaneHeader = lane.BookingCalendarLaneHeader ?? BookingCalendarLaneHeader; const LaneBookingCalendarLane = lane.BookingCalendarLane ?? BookingCalendarLane; return (
laneKey={lane.laneKey ?? laneIndex} data={lane.data} />
); })}
{showTimeAxis && (
)}
{children}
); }