import { Duration } from '../datelib/duration' ; import { EventStore , createEmptyEventStore } from './event-store' ; import { EventDef } from './event-def' ; import { EventInstance } from './event-instance' ; import { computeAlignedDayRange } from '../util/date' ; import { startOfDay } from '../datelib/marker' ; import { EventUiHash , EventUi } from '../component/event-ui' ; import { compileEventUis } from '../component/event-rendering' ; import { CalendarContext } from '../CalendarContext' ; import { getDefaultEventEnd } from '../calendar-utils' ; export interface EventMutation { datesDelta ? : Duration ; startDelta ? : Duration ; endDelta ? : Duration ; standardProps ? : any ; extendedProps ? : any ; } export function applyMutationToEventStore ( eventStore , eventConfigBase , mutation , context ) { let eventConfigs = compileEventUis ( eventStore . defs , eventConfigBase ) ; let dest = createEmptyEventStore ( ) ; for ( let defId in eventStore . defs ) { let def = eventStore . defs [ defId ] ; dest . defs [ defId ] = applyMutationToEventDef ( def , eventConfigs [ defId ] , mutation , context ) ; } for ( let instanceId in eventStore . instances ) { let instance = eventStore . instances [ instanceId ] ; let def = dest . defs [ instance . defId ] ; dest . instances [ instanceId ] = applyMutationToEventInstance ( instance , def , eventConfigs [ instance . defId ] , mutation , context ) ; } return dest ; } export type eventDefMutationApplier = ( eventDef : EventDef , mutation : EventMutation , context : CalendarContext ) => void ; function applyMutationToEventDef ( eventDef , eventConfig , mutation , context ) { let standardProps = mutation . standardProps || { } ; if ( standardProps . hasEnd == null && eventConfig . durationEditable && ( mutation . startDelta || mutation . endDelta ) ) { standardProps . hasEnd = true ; } let copy = { ... eventDef , ... standardProps , ui : { ... eventDef . ui , ... standardProps . ui } , } ; if ( mutation . extendedProps ) { copy . extendedProps = { ... copy . extendedProps , ... mutation . extendedProps } ; } for ( let applier of context . pluginHooks . eventDefMutationAppliers ) { applier ( copy , mutation , context ) ; } if ( ! copy . hasEnd && context . options . forceEventDuration ) { copy . hasEnd = true ; } return copy ; } function applyMutationToEventInstance ( eventInstance , eventDef , eventConfig , mutation , context ) { let { dateEnv } = context ; let forceAllDay = mutation . standardProps && mutation . standardProps . allDay === true ; let clearEnd = mutation . standardProps && mutation . standardProps . hasEnd === false ; let copy = { ... eventInstance } as EventInstance ; if ( forceAllDay ) { copy . range = computeAlignedDayRange ( copy . range ) ; } if ( mutation . datesDelta && eventConfig . startEditable ) { copy . range = { start : dateEnv . add ( copy . range . start , mutation . datesDelta ) , end : dateEnv . add ( copy . range . end , mutation . datesDelta ) , } ; } if ( mutation . startDelta && eventConfig . durationEditable ) { copy . range = { start : dateEnv . add ( copy . range . start , mutation . startDelta ) , end : copy . range . end , } ; } if ( mutation . endDelta && eventConfig . durationEditable ) { copy . range = { start : copy . range . start , end : dateEnv . add ( copy . range . end , mutation . endDelta ) , } ; } if ( clearEnd ) { copy . range = { start : copy . range . start , end : getDefaultEventEnd ( eventDef . allDay , copy . range . start , context ) , } ; } if ( eventDef . allDay ) { copy . range = { start : startOfDay ( copy . range . start ) , end : startOfDay ( copy . range . end ) , } ; } if ( copy . range . end < copy . range . start ) { copy . range . end = getDefaultEventEnd ( eventDef . allDay , copy . range . start , context ) ; } return copy ; }