import { __assign } from 'tslib' ; import { EventDef } from '../structs/event-def' ; import { EVENT_NON_DATE_REFINERS , EVENT_DATE_REFINERS } from '../structs/event-parse' ; import { EventInstance } from '../structs/event-instance' ; import { EVENT_UI_REFINERS , EventUiHash } from '../component/event-ui' ; import { EventMutation , applyMutationToEventStore } from '../structs/event-mutation' ; import { diffDates , computeAlignedDayRange } from '../util/date' ; import { createDuration , durationsEqual } from '../datelib/duration' ; import { createFormatter } from '../datelib/formatting' ; import { CalendarContext } from '../CalendarContext' ; import { getRelevantEvents , EventStore } from '../structs/event-store' ; import { Dictionary } from '../options' ; import { DateInput , DurationInput , FormatterInput , EventSourceApi , } from '../api-type-deps' ; export class EventApi { _context : CalendarContext ; _def : EventDef ; _instance : EventInstance | null ; constructor ( context , def , instance ? ) { this . _context = context ; this . _def = def ; this . _instance = instance || null ; } setProp ( name , val ) { if ( name in EVENT_DATE_REFINERS ) { console . warn ( 'Could not set date-related prop \'name\'. Use one of the date-related methods instead.' ) ; } else if ( name === 'id' ) { val = EVENT_NON_DATE_REFINERS [ name ] ( val ) ; this . mutate ( { standardProps : { publicId : val } , } ) ; } else if ( name in EVENT_NON_DATE_REFINERS ) { val = EVENT_NON_DATE_REFINERS [ name ] ( val ) ; this . mutate ( { standardProps : { [ name ] : val } , } ) ; } else if ( name in EVENT_UI_REFINERS ) { let ui = EVENT_UI_REFINERS [ name ] ( val ) ; if ( name === 'color' ) { ui = { backgroundColor : val , borderColor : val } ; } else if ( name === 'editable' ) { ui = { startEditable : val , durationEditable : val } ; } else { ui = { [ name ] : val } ; } this . mutate ( { standardProps : { ui } , } ) ; } else { console . warn ( `Could not set prop '${ name }'. Use setExtendedProp instead.` ) ; } } setExtendedProp ( name , val ) { this . mutate ( { extendedProps : { [ name ] : val } , } ) ; } setStart ( startInput , options = { } ) { let { dateEnv } = this . _context ; let start = dateEnv . createMarker ( startInput ) ; if ( start && this . _instance ) { let instanceRange = this . _instance . range ; let startDelta = diffDates ( instanceRange . start , start , dateEnv , options . granularity ) ; if ( options . maintainDuration ) { this . mutate ( { datesDelta : startDelta } ) ; } else { this . mutate ( { startDelta } ) ; } } } setEnd ( endInput , options = { } ) { let { dateEnv } = this . _context ; let end ; if ( endInput != null ) { end = dateEnv . createMarker ( endInput ) ; if ( ! end ) { return ; } } if ( this . _instance ) { if ( end ) { let endDelta = diffDates ( this . _instance . range . end , end , dateEnv , options . granularity ) ; this . mutate ( { endDelta } ) ; } else { this . mutate ( { standardProps : { hasEnd : false } } ) ; } } } setDates ( startInput , endInput , options = { } ) { let { dateEnv } = this . _context ; let standardProps = { allDay : options . allDay } as any ; let start = dateEnv . createMarker ( startInput ) ; let end ; if ( ! start ) { return ; } if ( endInput != null ) { end = dateEnv . createMarker ( endInput ) ; if ( ! end ) { return ; } } if ( this . _instance ) { let instanceRange = this . _instance . range ; if ( options . allDay === true ) { instanceRange = computeAlignedDayRange ( instanceRange ) ; } let startDelta = diffDates ( instanceRange . start , start , dateEnv , options . granularity ) ; if ( end ) { let endDelta = diffDates ( instanceRange . end , end , dateEnv , options . granularity ) ; if ( durationsEqual ( startDelta , endDelta ) ) { this . mutate ( { datesDelta : startDelta , standardProps } ) ; } else { this . mutate ( { startDelta , endDelta , standardProps } ) ; } } else { standardProps . hasEnd = false ; this . mutate ( { datesDelta : startDelta , standardProps } ) ; } } } moveStart ( deltaInput ) { let delta = createDuration ( deltaInput ) ; if ( delta ) { this . mutate ( { startDelta : delta } ) ; } } moveEnd ( deltaInput ) { let delta = createDuration ( deltaInput ) ; if ( delta ) { this . mutate ( { endDelta : delta } ) ; } } moveDates ( deltaInput ) { let delta = createDuration ( deltaInput ) ; if ( delta ) { this . mutate ( { datesDelta : delta } ) ; } } setAllDay ( allDay , options = { } ) { let standardProps = { allDay } as any ; let { maintainDuration } = options ; if ( maintainDuration == null ) { maintainDuration = this . _context . options . allDayMaintainDuration ; } if ( this . _def . allDay !== allDay ) { standardProps . hasEnd = maintainDuration ; } this . mutate ( { standardProps } ) ; } formatRange ( formatInput ) { let { dateEnv } = this . _context ; let instance = this . _instance ; let formatter = createFormatter ( formatInput ) ; if ( this . _def . hasEnd ) { return dateEnv . formatRange ( instance . range . start , instance . range . end , formatter , { forcedStartTzo : instance . forcedStartTzo , forcedEndTzo : instance . forcedEndTzo , } ) ; } return dateEnv . format ( instance . range . start , formatter , { forcedTzo : instance . forcedStartTzo , } ) ; } mutate ( mutation ) { let instance = this . _instance ; if ( instance ) { let def = this . _def ; let context = this . _context ; let { eventStore } = context . getCurrentData ( ) ; let relevantEvents = getRelevantEvents ( eventStore , instance . instanceId ) ; let eventConfigBase = { '' : { display : '' , startEditable : true , durationEditable : true , constraints : [ ] , overlap : null , allows : [ ] , backgroundColor : '' , borderColor : '' , textColor : '' , classNames : [ ] , } , } as EventUiHash ; relevantEvents = applyMutationToEventStore ( relevantEvents , eventConfigBase , mutation , context ) ; let oldEvent = new EventApi ( context , def , instance ) ; this . _def = relevantEvents . defs [ def . defId ] ; this . _instance = relevantEvents . instances [ instance . instanceId ] ; context . dispatch ( { type : 'MERGE_EVENTS' , eventStore : relevantEvents , } ) ; context . emitter . trigger ( 'eventChange' , { oldEvent , event : this , relatedEvents : buildEventApis ( relevantEvents , context , instance ) , revert ( ) { context . dispatch ( { type : 'RESET_EVENTS' , eventStore , } ) ; } , } ) ; } } remove ( ) { let context = this . _context ; let asStore = eventApiToStore ( this ) ; context . dispatch ( { type : 'REMOVE_EVENTS' , eventStore : asStore , } ) ; context . emitter . trigger ( 'eventRemove' , { event : this , relatedEvents : [ ] , revert ( ) { context . dispatch ( { type : 'MERGE_EVENTS' , eventStore : asStore , } ) ; } , } ) ; } get source ( ) : EventSourceApi | null { let { sourceId } = this . _def ; if ( sourceId ) { return new EventSourceApi ( this . _context , this . _context . getCurrentData ( ) . eventSources [ sourceId ] ) ; } return null ; } get start ( ) : Date | null { return this . _instance ? this . _context . dateEnv . toDate ( this . _instance . range . start ) : null ; } get end ( ) : Date | null { return ( this . _instance && this . _def . hasEnd ) ? this . _context . dateEnv . toDate ( this . _instance . range . end ) : null ; } get startStr ( ) : string { let instance = this . _instance ; if ( instance ) { return this . _context . dateEnv . formatIso ( instance . range . start , { omitTime : this . _def . allDay , forcedTzo : instance . forcedStartTzo , } ) ; } return '' ; } get endStr ( ) : string { let instance = this . _instance ; if ( instance && this . _def . hasEnd ) { return this . _context . dateEnv . formatIso ( instance . range . end , { omitTime : this . _def . allDay , forcedTzo : instance . forcedEndTzo , } ) ; } return '' ; } get id ( ) { return this . _def . publicId ; } get groupId ( ) { return this . _def . groupId ; } get allDay ( ) { return this . _def . allDay ; } get title ( ) { return this . _def . title ; } get url ( ) { return this . _def . url ; } get display ( ) { return this . _def . ui . display || 'auto' ; } get startEditable ( ) { return this . _def . ui . startEditable ; } get durationEditable ( ) { return this . _def . ui . durationEditable ; } get constraint ( ) { return this . _def . ui . constraints [ 0 ] || null ; } get overlap ( ) { return this . _def . ui . overlap ; } get allow ( ) { return this . _def . ui . allows [ 0 ] || null ; } get backgroundColor ( ) { return this . _def . ui . backgroundColor ; } get borderColor ( ) { return this . _def . ui . borderColor ; } get textColor ( ) { return this . _def . ui . textColor ; } get classNames ( ) { return this . _def . ui . classNames ; } get extendedProps ( ) { return this . _def . extendedProps ; } toPlainObject ( settings = { } ) { let def = this . _def ; let { ui } = def ; let { startStr , endStr } = this ; let res = { } ; if ( def . title ) { res . title = def . title ; } if ( startStr ) { res . start = startStr ; } if ( endStr ) { res . end = endStr ; } if ( def . publicId ) { res . id = def . publicId ; } if ( def . groupId ) { res . groupId = def . groupId ; } if ( def . url ) { res . url = def . url ; } if ( ui . display && ui . display !== 'auto' ) { res . display = ui . display ; } if ( settings . collapseColor && ui . backgroundColor && ui . backgroundColor === ui . borderColor ) { res . color = ui . backgroundColor ; } else { if ( ui . backgroundColor ) { res . backgroundColor = ui . backgroundColor ; } if ( ui . borderColor ) { res . borderColor = ui . borderColor ; } } if ( ui . textColor ) { res . textColor = ui . textColor ; } if ( ui . classNames . length ) { res . classNames = ui . classNames ; } if ( Object . keys ( def . extendedProps ) . length ) { if ( settings . collapseExtendedProps ) { __assign ( res , def . extendedProps ) ; } else { res . extendedProps = def . extendedProps ; } } return res ; } toJSON ( ) { return this . toPlainObject ( ) ; } } export function eventApiToStore ( eventApi ) { let def = eventApi . _def ; let instance = eventApi . _instance ; return { defs : { [ def . defId ] : def } , instances : instance ? { [ instance . instanceId ] : instance } : { } , } ; } export function buildEventApis ( eventStore , context , excludeInstance ? ) { let { defs , instances } = eventStore ; let eventApis = [ ] ; let excludeInstanceId = excludeInstance ? excludeInstance . instanceId : '' ; for ( let id in instances ) { let instance = instances [ id ] ; let def = defs [ instance . defId ] ; if ( instance . instanceId !== excludeInstanceId ) { eventApis . push ( new EventApi ( context , def , instance ) ) ; } } return eventApis ; }