Skip to content

Commit

Permalink
more adjustment to new context, move things out of Calendar
Browse files Browse the repository at this point in the history
  • Loading branch information
arshaw committed Apr 19, 2020
1 parent 2ec473a commit d7791e7
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 92 deletions.
2 changes: 1 addition & 1 deletion packages-premium
79 changes: 8 additions & 71 deletions packages/core/src/Calendar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { DateInput } from './datelib/env'
import { DateMarker, startOfDay } from './datelib/marker'
import { createFormatter } from './datelib/formatting'
import { createDuration, DurationInput } from './datelib/duration'
import { parseDateSpan, DateSpanInput, DateSpan, buildDateSpanApi, DateSpanApi, buildDatePointApi, DatePointApi } from './structs/date-span'
import { parseDateSpan, DateSpanInput, DateSpan, DateSpanApi, DatePointApi } from './structs/date-span'
import { DateRangeInput } from './datelib/date-range'
import { EventSourceInput, parseEventSource } from './structs/event-source'
import { EventInput, parseEvent } from './structs/event'
Expand All @@ -29,6 +29,7 @@ import { applyStyleProp } from './util/dom-manip'
import { CalendarStateReducer } from './reducers/CalendarStateReducer'
import { getNow } from './reducers/current-date'
import { ReducerContext } from './reducers/ReducerContext'
import { triggerDateSelect, triggerDateUnselect } from './calendar-utils'


export interface DateClickApi extends DatePointApi {
Expand All @@ -42,13 +43,13 @@ export interface DateSelectionApi extends DateSpanApi {
view: ViewApi
}

export type DatePointTransform = (dateSpan: DateSpan, calendar: Calendar) => any
export type DateSpanTransform = (dateSpan: DateSpan, calendar: Calendar) => any
export type DatePointTransform = (dateSpan: DateSpan, context: ReducerContext) => any
export type DateSpanTransform = (dateSpan: DateSpan, context: ReducerContext) => any

export type CalendarInteraction = { destroy() }
export type CalendarInteractionClass = { new(context: ReducerContext): CalendarInteraction }

export type OptionChangeHandler = (propValue: any, calendar: Calendar) => void
export type OptionChangeHandler = (propValue: any, context: ReducerContext) => void
export type OptionChangeHandlerMap = { [propName: string]: OptionChangeHandler }


Expand Down Expand Up @@ -293,7 +294,7 @@ export class Calendar {

// special updates
for (let name in specialUpdates) {
changeHandlers[name](specialUpdates[name], this)
changeHandlers[name](specialUpdates[name], this.state)
}
})
}
Expand Down Expand Up @@ -322,12 +323,6 @@ export class Calendar {
// -----------------------------------------------------------------------------------------------------------------


// Returns a boolean about whether the view is okay to instantiate at some point
isValidViewType(viewType: string): boolean {
return Boolean(this.state.viewSpecs[viewType])
}


changeView(viewType: string, dateOrRange?: DateRangeInput | DateInput) {
this.batchRendering(() => {
this.unselect()
Expand Down Expand Up @@ -616,7 +611,7 @@ export class Calendar {

if (selection) { // throw parse error otherwise?
this.dispatch({ type: 'SELECT_DATES', selection })
this.triggerDateSelect(selection)
triggerDateSelect(selection, null, this.state)
}
}

Expand All @@ -625,66 +620,8 @@ export class Calendar {
unselect(pev?: PointerDragEvent) {
if (this.state.dateSelection) {
this.dispatch({ type: 'UNSELECT_DATES' })
this.triggerDateUnselect(pev)
}
}


triggerDateSelect(selection: DateSpan, pev?: PointerDragEvent) {
const arg = {
...this.buildDateSpanApi(selection),
jsEvent: pev ? pev.origEvent as MouseEvent : null, // Is this always a mouse event? See #4655
view: this.state.viewApi
}

this.emitter.trigger('select', arg)
}


triggerDateUnselect(pev?: PointerDragEvent) {
this.emitter.trigger('unselect', {
jsEvent: pev ? pev.origEvent : null,
view: this.state.viewApi
})
}


// TODO: receive pev?
triggerDateClick(dateSpan: DateSpan, dayEl: HTMLElement, view: ViewApi, ev: UIEvent) {
const arg = {
...this.buildDatePointApi(dateSpan),
dayEl,
jsEvent: ev as MouseEvent, // Is this always a mouse event? See #4655
view
triggerDateUnselect(pev, this.state)
}

this.emitter.trigger('dateClick', arg)
}


buildDatePointApi(dateSpan: DateSpan) {
let props = {} as DatePointApi

for (let transform of this.state.pluginHooks.datePointTransforms) {
__assign(props, transform(dateSpan, this))
}

__assign(props, buildDatePointApi(dateSpan, this.state.dateEnv))

return props
}


buildDateSpanApi(dateSpan: DateSpan) {
let props = {} as DateSpanApi

for (let transform of this.state.pluginHooks.dateSpanTransforms) {
__assign(props, transform(dateSpan, this))
}

__assign(props, buildDateSpanApi(dateSpan, this.state.dateEnv))

return props
}


Expand Down
63 changes: 63 additions & 0 deletions packages/core/src/calendar-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { PointerDragEvent } from './interactions/pointer'
import { buildDateSpanApi, DateSpanApi, DatePointApi, DateSpan, buildDatePointApi } from './structs/date-span'
import { ReducerContext } from './reducers/ReducerContext'
import { __assign } from 'tslib'
import { ViewApi } from './ViewApi'


export function triggerDateSelect(selection: DateSpan, pev: PointerDragEvent | null, context: ReducerContext & { viewApi?: ViewApi }) {
const arg = {
...buildDateSpanApiWithContext(selection, context),
jsEvent: pev ? pev.origEvent as MouseEvent : null, // Is this always a mouse event? See #4655
view: context.viewApi || context.calendar.view
}

context.emitter.trigger('select', arg)
}


export function triggerDateUnselect(pev: PointerDragEvent | null, context: ReducerContext & { viewApi?: ViewApi }) {
context.emitter.trigger('unselect', {
jsEvent: pev ? pev.origEvent : null,
view: context.viewApi || context.calendar.view
})
}


// TODO: receive pev?
export function triggerDateClick(dateSpan: DateSpan, dayEl: HTMLElement, ev: UIEvent, context: ReducerContext & { viewApi?: ViewApi }) {
const arg = {
...buildDatePointApiWithContext(dateSpan, context),
dayEl,
jsEvent: ev as MouseEvent, // Is this always a mouse event? See #4655
view: context.viewApi || context.calendar.view
}

context.emitter.trigger('dateClick', arg)
}


export function buildDatePointApiWithContext(dateSpan: DateSpan, context: ReducerContext) {
let props = {} as DatePointApi

for (let transform of context.pluginHooks.datePointTransforms) {
__assign(props, transform(dateSpan, context))
}

__assign(props, buildDatePointApi(dateSpan, context.dateEnv))

return props
}


export function buildDateSpanApiWithContext(dateSpan: DateSpan, context: ReducerContext) {
let props = {} as DateSpanApi

for (let transform of context.pluginHooks.dateSpanTransforms) {
__assign(props, transform(dateSpan, context))
}

__assign(props, buildDateSpanApi(dateSpan, context.dateEnv))

return props
}
1 change: 1 addition & 0 deletions packages/core/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,4 @@ export { renderFill, BgEvent, BgEventProps } from './common/bg-fill'
export { WeekNumberRoot, WeekNumberRootProps } from './common/WeekNumberRoot'

export { ViewRoot, ViewRootProps } from './common/ViewRoot'
export { triggerDateSelect, triggerDateClick, buildDatePointApiWithContext } from './calendar-utils'
14 changes: 7 additions & 7 deletions packages/core/src/option-change-handlers.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { createPlugin } from './plugin-system'
import { Calendar } from './main'
import { hashValuesToArray } from './util/object'
import { EventSource } from './structs/event-source'
import { ReducerContext } from 'fullcalendar'

export const changeHandlerPlugin = createPlugin({
optionChangeHandlers: {
events(events, calendar) {
handleEventSources([ events ], calendar)
events(events, context) {
handleEventSources([ events ], context)
},
eventSources: handleEventSources
}
Expand All @@ -15,8 +15,8 @@ export const changeHandlerPlugin = createPlugin({
/*
BUG: if `event` was supplied, all previously-given `eventSources` will be wiped out
*/
function handleEventSources(inputs, calendar: Calendar) {
let unfoundSources: EventSource[] = hashValuesToArray(calendar.state.eventSources)
function handleEventSources(inputs, context: ReducerContext) {
let unfoundSources: EventSource[] = hashValuesToArray(context.getCurrentState().eventSources)
let newInputs = []

for (let input of inputs) {
Expand All @@ -36,13 +36,13 @@ function handleEventSources(inputs, calendar: Calendar) {
}

for (let unfoundSource of unfoundSources) {
calendar.dispatch({
context.dispatch({
type: 'REMOVE_EVENT_SOURCE',
sourceId: unfoundSource.sourceId
})
}

for (let newInput of newInputs) {
calendar.addEventSource(newInput)
context.calendar.addEventSource(newInput)
}
}
5 changes: 3 additions & 2 deletions packages/core/src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { EventInteractionState } from './interactions/event-interaction-state'
import { SplittableProps } from './component/event-splitting'
import { mapHash } from './util/object'
import { ReducerContext } from './reducers/ReducerContext'
import { buildDateSpanApiWithContext } from './calendar-utils'

// TODO: rename to "criteria" ?
export type ConstraintInput = 'businessHours' | string | EventInput | EventInput[]
Expand Down Expand Up @@ -154,7 +155,7 @@ function isInteractionPropsValid(state: SplittableProps, context: ReducerContext
}

if (!subjectAllow(
calendar.buildDateSpanApi(subjectDateSpan),
buildDateSpanApiWithContext(subjectDateSpan, context),
eventApi
)) {
return false
Expand Down Expand Up @@ -217,7 +218,7 @@ function isDateSelectionPropsValid(state: SplittableProps, context: ReducerConte
let fullDateSpan = { ...dateSpanMeta, ...selection }

if (!selectionAllow(
context.calendar.buildDateSpanApi(fullDateSpan),
buildDateSpanApiWithContext(fullDateSpan, context),
null
)) {
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
isInteractionValid,
ElementDragging,
ViewApi,
ReducerContext
ReducerContext,
buildDatePointApiWithContext
} from '@fullcalendar/core'
import { HitDragging } from '../interactions/HitDragging'
import { __assign } from 'tslib'
Expand Down Expand Up @@ -130,7 +131,7 @@ export class ExternalElementDragging {
let finalView = finalHit.component.context.viewApi
let dragMeta = this.dragMeta!
let arg = {
...receivingContext.calendar.buildDatePointApi(finalHit.dateSpan),
...buildDatePointApiWithContext(finalHit.dateSpan, receivingContext),
draggedEl: pev.subjectEl as HTMLElement,
jsEvent: pev.origEvent as MouseEvent, // Is this always a mouse event? See #4655
view: finalView
Expand Down
9 changes: 4 additions & 5 deletions packages/interaction/src/interactions/DateClicking.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PointerDragEvent, Interaction, InteractionSettings, interactionSettingsToStore } from '@fullcalendar/core'
import { PointerDragEvent, Interaction, InteractionSettings, interactionSettingsToStore, triggerDateClick } from '@fullcalendar/core'
import { FeaturefulElementDragging } from '../dnd/FeaturefulElementDragging'
import { HitDragging, isHitsEqual } from './HitDragging'

Expand Down Expand Up @@ -39,18 +39,17 @@ export class DateClicking extends Interaction {
// won't even fire if moving was ignored
handleDragEnd = (ev: PointerDragEvent) => {
let { component } = this
let { calendar, viewApi } = component.context
let { pointer } = this.dragging

if (!pointer.wasTouchScroll) {
let { initialHit, finalHit } = this.hitDragging

if (initialHit && finalHit && isHitsEqual(initialHit, finalHit)) {
calendar.triggerDateClick(
triggerDateClick(
initialHit.dateSpan,
initialHit.dayEl,
viewApi,
ev.origEvent
ev.origEvent,
component.context
)
}
}
Expand Down
5 changes: 3 additions & 2 deletions packages/interaction/src/interactions/DateSelecting.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {
compareNumbers, enableCursor, disableCursor, DateComponent, Hit,
DateSpan, PointerDragEvent, dateSelectionJoinTransformer,
Interaction, InteractionSettings, interactionSettingsToStore
Interaction, InteractionSettings, interactionSettingsToStore,
triggerDateSelect
} from '@fullcalendar/core'
import { HitDragging } from './HitDragging'
import { FeaturefulElementDragging } from '../dnd/FeaturefulElementDragging'
Expand Down Expand Up @@ -95,7 +96,7 @@ export class DateSelecting extends Interaction {
if (this.dragSelection) {

// selection is already rendered, so just need to report selection
this.component.context.calendar.triggerDateSelect(this.dragSelection, pev)
triggerDateSelect(this.dragSelection, pev, this.component.context)

this.dragSelection = null
}
Expand Down
5 changes: 3 additions & 2 deletions packages/interaction/src/interactions/EventDragging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import {
eventDragMutationMassager,
Interaction, InteractionSettings, interactionSettingsStore,
EventDropTransformers,
ReducerContext
ReducerContext,
buildDatePointApiWithContext
} from '@fullcalendar/core'
import { HitDragging, isHitsEqual } from './HitDragging'
import { FeaturefulElementDragging } from '../dnd/FeaturefulElementDragging'
Expand Down Expand Up @@ -300,7 +301,7 @@ export class EventDragging extends Interaction { // TODO: rename to EventSelecti
}

receivingContext.emitter.trigger('drop', {
...receivingContext.calendar.buildDatePointApi(finalHit.dateSpan),
...buildDatePointApiWithContext(finalHit.dateSpan, receivingContext),
draggedEl: ev.subjectEl as HTMLElement,
jsEvent: ev.origEvent as MouseEvent, // Is this always a mouse event? See #4655
view: finalHit.component.context.viewApi
Expand Down

0 comments on commit d7791e7

Please sign in to comment.