Browse Source

plugin system. for adding reducers

Adam Shaw 7 years ago
parent
commit
1775326215
5 changed files with 24 additions and 1 deletions
  1. 9 0
      src/Calendar.ts
  2. 2 0
      src/exports.ts
  3. 8 1
      src/reducers/main.ts
  4. 3 0
      src/reducers/types.ts
  5. 2 0
      src/types/input-types.ts

+ 9 - 0
src/Calendar.ts

@@ -32,6 +32,7 @@ import { BusinessHoursInput, parseBusinessHours } from './structs/business-hours
 import PointerDragging, { PointerDragEvent } from './dnd/PointerDragging'
 import EventDragging from './interactions/EventDragging'
 import { buildViewSpecs, ViewSpecHash, ViewSpec } from './structs/view-spec'
+import { PluginSystem } from './plugin-system'
 import * as exportHooks from './exports'
 
 
@@ -58,6 +59,7 @@ export default class Calendar {
   viewSpecs: ViewSpecHash
   theme: Theme
   dateEnv: DateEnv
+  pluginSystem: PluginSystem
   defaultAllDayEventDuration: Duration
   defaultTimedEventDuration: Duration
 
@@ -100,6 +102,7 @@ export default class Calendar {
     this.viewsByType = {}
 
     this.optionsManager = new OptionsManager(overrides)
+    this.pluginSystem = new PluginSystem()
 
     this.buildDateEnv = reselector(buildDateEnv)
     this.buildTheme = reselector(buildTheme)
@@ -109,6 +112,12 @@ export default class Calendar {
       return parseBusinessHours(input, this)
     })
 
+    // only do once. don't do in handleOptions. because can't remove plugins
+    let pluginDefs = this.optionsManager.computed.plugins || []
+    for (let pluginDef of pluginDefs) {
+      this.pluginSystem.add(pluginDef)
+    }
+
     this.handleOptions(this.optionsManager.computed)
     this.hydrate()
   }

+ 2 - 0
src/exports.ts

@@ -124,3 +124,5 @@ export { formatDate, formatRange } from './formatting-api'
 export { globalDefaults } from './options'
 
 export { registerRecurringType, ParsedRecurring } from './structs/recurring-event'
+
+export { createPlugin } from './plugin-system'

+ 8 - 1
src/reducers/main.ts

@@ -7,6 +7,7 @@ import { EventInteractionUiState } from '../interactions/event-interaction-state
 import { CalendarState, Action } from './types'
 import { EventSourceHash } from '../structs/event-source'
 import { computeEventDefUis } from '../component/event-rendering'
+import { assignTo } from '../util/object'
 
 export default function(state: CalendarState, action: Action, calendar: Calendar): CalendarState {
   calendar.publiclyTrigger(action.type, action) // for testing hooks
@@ -14,7 +15,7 @@ export default function(state: CalendarState, action: Action, calendar: Calendar
   let dateProfile = reduceDateProfile(state.dateProfile, action)
   let eventSources = reduceEventSources(state.eventSources, action, dateProfile, calendar)
 
-  return {
+  let nextState = assignTo({}, state, {
     dateProfile,
     eventSources,
     eventStore: reduceEventStore(state.eventStore, action, eventSources, dateProfile, calendar),
@@ -26,7 +27,13 @@ export default function(state: CalendarState, action: Action, calendar: Calendar
     eventResize: reduceEventResize(state.eventResize, action, eventSources, calendar),
     eventSourceLoadingLevel: computeLoadingLevel(eventSources),
     loadingLevel: computeLoadingLevel(eventSources)
+  })
+
+  for (let reducerFunc of calendar.pluginSystem.hooks.reducers) {
+    nextState = reducerFunc(nextState, action, calendar)
   }
+
+  return nextState
 }
 
 function reduceDateProfile(currentDateProfile: DateProfile | null, action: Action) {

+ 3 - 0
src/reducers/types.ts

@@ -8,6 +8,7 @@ import { DateProfile } from '../DateProfileGenerator'
 import { EventInteractionState } from '../interactions/event-interaction-state'
 import { DateSpan } from '../structs/date-span'
 import { DateEnv } from '../datelib/env'
+import Calendar from '../Calendar'
 
 export interface CalendarState extends DateComponentRenderState {
   eventSources: EventSourceHash
@@ -15,6 +16,8 @@ export interface CalendarState extends DateComponentRenderState {
   loadingLevel: number
 }
 
+export type reducerFunc = (state: CalendarState, action: Action, calendar: Calendar) => CalendarState
+
 export type Action =
 
   { type: 'SET_DATE_PROFILE', dateProfile: DateProfile } |

+ 2 - 0
src/types/input-types.ts

@@ -12,6 +12,7 @@ import { DateRangeInput } from '../datelib/date-range'
 import { BusinessHoursInput } from '../structs/business-hours'
 import EventApi from '../api/EventApi'
 import { Allow, ConstraintInput, Overlap } from '../validation'
+import { PluginDef } from '../plugin-system'
 
 
 export interface ToolbarInput {
@@ -201,4 +202,5 @@ export interface ViewOptionsInput extends OptionsInputBase {
 export interface OptionsInput extends OptionsInputBase {
   buttonText?: ButtonTextCompoundInput
   views?: { [viewId: string]: ViewOptionsInput }
+  plugins: PluginDef[]
 }