import { DateRange , rangesEqual , OpenDateRange } from '../datelib/date-range' ; import { DateInput , DateEnv } from '../datelib/env' ; import { Duration } from '../datelib/duration' ; import { createEventInstance } from './event-instance' ; import { parseEventDef , refineEventDef } from './event-parse' ; import { EventRenderRange , compileEventUi } from '../component/event-rendering' ; import { EventUiHash } from '../component/event-ui' ; import { CalendarContext } from '../CalendarContext' ; import { refineProps , identity , Identity } from '../options' ; export interface OpenDateSpanInput { start ? : DateInput ; end ? : DateInput ; allDay ? : boolean ; [ otherProp ] : any ; } export interface DateSpanInput extends OpenDateSpanInput { start : DateInput ; end : DateInput ; } export interface OpenDateSpan { range : OpenDateRange ; allDay : boolean ; [ otherProp ] : any ; } export interface DateSpan extends OpenDateSpan { range : DateRange ; } export interface RangeApi { start : Date ; end : Date ; startStr : string ; endStr : string ; } export interface DateSpanApi extends RangeApi { allDay : boolean ; } export interface RangeApiWithTimeZone extends RangeApi { timeZone : string ; } export interface DatePointApi { date : Date ; dateStr : string ; allDay : boolean ; } const STANDARD_PROPS = { start : identity as Identity < DateInput > , end : identity as Identity < DateInput > , allDay : Boolean , } ; export function parseDateSpan ( raw , dateEnv , defaultDuration ? ) { let span = parseOpenDateSpan ( raw , dateEnv ) ; let { range } = span ; if ( ! range . start ) { return null ; } if ( ! range . end ) { if ( defaultDuration == null ) { return null ; } range . end = dateEnv . add ( range . start , defaultDuration ) ; } return span as DateSpan ; } export function parseOpenDateSpan ( raw , dateEnv ) { let { refined : standardProps , extra } = refineProps ( raw , STANDARD_PROPS ) ; let startMeta = standardProps . start ? dateEnv . createMarkerMeta ( standardProps . start ) : null ; let endMeta = standardProps . end ? dateEnv . createMarkerMeta ( standardProps . end ) : null ; let { allDay } = standardProps ; if ( allDay == null ) { allDay = ( startMeta && startMeta . isTimeUnspecified ) && ( ! endMeta || endMeta . isTimeUnspecified ) ; } return { range : { start : startMeta ? startMeta . marker : null , end : endMeta ? endMeta . marker : null , } , allDay , ... extra , } ; } export function isDateSpansEqual ( span0 , span1 ) { return rangesEqual ( span0 . range , span1 . range ) && span0 . allDay === span1 . allDay && isSpanPropsEqual ( span0 , span1 ) ; } function isSpanPropsEqual ( span0 , span1 ) { for ( let propName in span1 ) { if ( propName !== 'range' && propName !== 'allDay' ) { if ( span0 [ propName ] !== span1 [ propName ] ) { return false ; } } } for ( let propName in span0 ) { if ( ! ( propName in span1 ) ) { return false ; } } return true ; } export function buildDateSpanApi ( span , dateEnv ) { return { ... buildRangeApi ( span . range , dateEnv , span . allDay ) , allDay : span . allDay , } ; } export function buildRangeApiWithTimeZone ( range , dateEnv , omitTime ? ) { return { ... buildRangeApi ( range , dateEnv , omitTime ) , timeZone : dateEnv . timeZone , } ; } export function buildRangeApi ( range , dateEnv , omitTime ? ) { return { start : dateEnv . toDate ( range . start ) , end : dateEnv . toDate ( range . end ) , startStr : dateEnv . formatIso ( range . start , { omitTime } ) , endStr : dateEnv . formatIso ( range . end , { omitTime } ) , } ; } export function fabricateEventRange ( dateSpan , eventUiBases , context ) { let res = refineEventDef ( { editable : false } , context ) ; let def = parseEventDef ( res . refined , res . extra , '' , dateSpan . allDay , true , context ) ; return { def , ui : compileEventUi ( def , eventUiBases ) , instance : createEventInstance ( def . defId , dateSpan . range ) , range : dateSpan . range , isStart : true , isEnd : true , } ; }