فهرست منبع

all sorts of plugin hooks

Adam Shaw 7 سال پیش
والد
کامیت
c80551219b
5فایلهای تغییر یافته به همراه55 افزوده شده و 8 حذف شده
  1. 2 0
      src/exports.ts
  2. 12 3
      src/interactions/EventDragging.ts
  3. 23 3
      src/plugin-system.ts
  4. 9 2
      src/structs/event-mutation.ts
  5. 9 0
      src/structs/event.ts

+ 2 - 0
src/exports.ts

@@ -157,3 +157,5 @@ export { EventRenderRange, sliceEventStore } from './component/event-rendering'
 export { default as DayTable, DayTableSeg, DayTableCell } from './common/DayTable'
 
 export { Slicer } from './common/slicing-utils'
+
+export { EventMutation } from './structs/event-mutation'

+ 12 - 3
src/interactions/EventDragging.ts

@@ -139,7 +139,7 @@ export default class EventDragging { // TODO: rename to EventSelectingAndDraggin
         initialCalendar === receivingCalendar ||
         receivingComponent.opt('editable') && receivingComponent.opt('droppable')
       ) {
-        mutation = computeEventMutation(initialHit, hit)
+        mutation = computeEventMutation(initialHit, hit, receivingCalendar.pluginSystem.hooks.eventDragMutationMassagers)
 
         if (mutation) {
           mutatedRelevantEvents = applyMutationToEventStore(relevantEvents, mutation, receivingCalendar)
@@ -355,7 +355,10 @@ export default class EventDragging { // TODO: rename to EventSelectingAndDraggin
 
 }
 
-function computeEventMutation(hit0: Hit, hit1: Hit): EventMutation {
+
+export type eventDragMutationMassager = (mutation: EventMutation, hit0: Hit, hit1: Hit) => void
+
+function computeEventMutation(hit0: Hit, hit1: Hit, massagers: eventDragMutationMassager[]): EventMutation {
   let dateSpan0 = hit0.dateSpan
   let dateSpan1 = hit1.dateSpan
   let date0 = dateSpan0.range.start
@@ -383,11 +386,17 @@ function computeEventMutation(hit0: Hit, hit1: Hit): EventMutation {
       null
   )
 
-  return {
+  let mutation: EventMutation = {
     startDelta: delta,
     endDelta: delta,
     standardProps
   }
+
+  for (let massager of massagers) {
+    massager(mutation, hit0, hit1)
+  }
+
+  return mutation
 }
 
 function getComponentTouchDelay(component: DateComponent<any>): number | null {

+ 23 - 3
src/plugin-system.ts

@@ -1,12 +1,23 @@
 import { reducerFunc } from './reducers/types'
+import { eventDefParserFunc } from './structs/event'
+import { eventDragMutationMassager } from './interactions/EventDragging'
+import { eventDefMutationApplier } from './structs/event-mutation'
+
+// TODO: easier way to add new hooks? need to update a million things
 
 export interface PluginDefInput {
   deps?: PluginDef[]
   reducers?: reducerFunc[]
+  eventDefParsers?: eventDefParserFunc[]
+  eventDragMutationMassagers: eventDragMutationMassager[]
+  eventDefMutationAppliers: eventDefMutationApplier[]
 }
 
 export interface PluginHooks {
   reducers: reducerFunc[]
+  eventDefParsers: eventDefParserFunc[]
+  eventDragMutationMassagers: eventDragMutationMassager[]
+  eventDefMutationAppliers: eventDefMutationApplier[]
 }
 
 export interface PluginDef extends PluginHooks {
@@ -20,7 +31,10 @@ export function createPlugin(input: PluginDefInput): PluginDef {
   return {
     id: String(uid++),
     deps: input.deps || [],
-    reducers: input.reducers || []
+    reducers: input.reducers || [],
+    eventDefParsers: input.eventDefParsers || [],
+    eventDragMutationMassagers: input.eventDragMutationMassagers || [],
+    eventDefMutationAppliers: input.eventDefMutationAppliers || []
   }
 }
 
@@ -31,7 +45,10 @@ export class PluginSystem {
 
   constructor() {
     this.hooks = {
-      reducers: []
+      reducers: [],
+      eventDefParsers: [],
+      eventDragMutationMassagers: [],
+      eventDefMutationAppliers: []
     }
     this.addedHash = {}
   }
@@ -52,6 +69,9 @@ export class PluginSystem {
 
 function combineHooks(hooks0: PluginHooks, hooks1: PluginHooks): PluginHooks {
   return {
-    reducers: hooks0.reducers.concat(hooks1.reducers)
+    reducers: hooks0.reducers.concat(hooks1.reducers),
+    eventDefParsers: hooks0.eventDefParsers.concat(hooks1.eventDefParsers),
+    eventDragMutationMassagers: hooks0.eventDragMutationMassagers.concat(hooks1.eventDragMutationMassagers),
+    eventDefMutationAppliers: hooks0.eventDefMutationAppliers.concat(hooks1.eventDefMutationAppliers)
   }
 }

+ 9 - 2
src/structs/event-mutation.ts

@@ -23,7 +23,7 @@ export function applyMutationToEventStore(eventStore: EventStore, mutation: Even
 
   for (let defId in eventStore.defs) {
     let def = eventStore.defs[defId]
-    dest.defs[defId] = applyMutationToEventDef(def, mutation)
+    dest.defs[defId] = applyMutationToEventDef(def, mutation, calendar.pluginSystem.hooks.eventDefMutationAppliers)
   }
 
   for (let instanceId in eventStore.instances) {
@@ -35,7 +35,10 @@ export function applyMutationToEventStore(eventStore: EventStore, mutation: Even
   return dest
 }
 
-function applyMutationToEventDef(eventDef: EventDef, mutation: EventMutation): EventDef {
+export type eventDefMutationApplier = (eventDef: EventDef, mutation: EventMutation) => void
+
+
+function applyMutationToEventDef(eventDef: EventDef, mutation: EventMutation, appliers: eventDefMutationApplier[]): EventDef {
   let copy = assignTo({}, eventDef)
   let standardProps = mutation.standardProps || {}
 
@@ -55,6 +58,10 @@ function applyMutationToEventDef(eventDef: EventDef, mutation: EventMutation): E
     copy.extendedProps = assignTo({}, copy.extendedProps, mutation.extendedProps)
   }
 
+  for (let applier of appliers) {
+    applier(copy, mutation)
+  }
+
   return copy
 }
 

+ 9 - 0
src/structs/event.ts

@@ -168,6 +168,13 @@ export function parseEventDef(raw: EventNonDateInput, sourceId: string, allDay:
   def.sourceId = sourceId
   def.allDay = allDay
   def.hasEnd = hasEnd
+
+  for (let eventDefParser of calendar.pluginSystem.hooks.eventDefParsers) {
+    let newLeftovers = {}
+    eventDefParser(def, leftovers, newLeftovers)
+    leftovers = newLeftovers
+  }
+
   def.extendedProps = assignTo(leftovers, def.extendedProps || {})
 
   // help out EventApi from having user modify props
@@ -177,6 +184,8 @@ export function parseEventDef(raw: EventNonDateInput, sourceId: string, allDay:
   return def
 }
 
+export type eventDefParserFunc = (def: EventDef, props: any, leftovers: any) => void
+
 
 export function createEventInstance(
   defId: string,