Parcourir la source

event instance of a recurring series should transform indepdendently

Adam Shaw il y a 7 ans
Parent
commit
12c3484ca0
2 fichiers modifiés avec 27 ajouts et 9 suppressions
  1. 15 7
      src/structs/event-store.ts
  2. 12 2
      src/validation.ts

+ 15 - 7
src/structs/event-store.ts

@@ -93,25 +93,33 @@ export function expandRecurring(eventStore: EventStore, framingRange: DateRange,
 }
 
 // retrieves events that have the same groupId as the instance specified by `instanceId`
+// or they are the same as the instance.
+// why might instanceId not be in the store? an event from another calendar?
+// TODO: rename to getRelevantEvents and rename other stuff too
 export function getRelatedEvents(eventStore: EventStore, instanceId: string): EventStore {
   let instance = eventStore.instances[instanceId]
 
   if (instance) {
     let def = eventStore.defs[instance.defId]
 
-    return filterEventStoreDefs(eventStore, function(eventDef) {
-      return isEventDefsRelated(eventDef, def)
+    // get events/instances with same group
+    let newStore = filterEventStoreDefs(eventStore, function(lookDef) {
+      return isEventDefsGrouped(def, lookDef)
     })
+
+    // add the original
+    // TODO: wish we could use eventTupleToStore or something like it
+    newStore.defs[def.defId] = def
+    newStore.instances[instance.instanceId] = instance
+
+    return newStore
   }
 
   return createEmptyEventStore()
 }
 
-export function isEventDefsRelated(def0: EventDef, def1: EventDef): boolean {
-  return Boolean(
-    def0.defId === def1.defId ||
-    def0.groupId && def0.groupId === def1.groupId
-  )
+export function isEventDefsGrouped(def0: EventDef, def1: EventDef): boolean {
+  return Boolean(def0.groupId && def0.groupId === def1.groupId)
 }
 
 export function transformRawEvents(rawEvents, func) {

+ 12 - 2
src/validation.ts

@@ -1,4 +1,4 @@
-import { EventStore, expandRecurring, eventTupleToStore, mapEventInstances, filterEventStoreDefs, isEventDefsRelated } from './structs/event-store'
+import { EventStore, expandRecurring, eventTupleToStore, mapEventInstances, filterEventStoreDefs, isEventDefsGrouped } from './structs/event-store'
 import Calendar from './Calendar'
 import { DateSpan, parseOpenDateSpan, OpenDateSpanInput, OpenDateSpan, isSpanPropsEqual, isSpanPropsMatching, buildDateSpanApi, DateSpanApi } from './structs/date-span'
 import { EventInstance, EventDef, EventTuple, parseEvent } from './structs/event'
@@ -66,7 +66,7 @@ function isEntitiesValid(
         ( // not comparing the same/related event
           !subjectEntity.event ||
           !eventEntity.event ||
-          !isEventDefsRelated(subjectEntity.event.def, eventEntity.event.def)
+          isEventsCollidable(subjectEntity.event, eventEntity.event)
         ) &&
         dateSpansCollide(subjectEntity.dateSpan, eventEntity.dateSpan) // a collision!
       ) {
@@ -93,6 +93,16 @@ function isEntitiesValid(
   return true
 }
 
+// do we want to compare these events for collision?
+// say no if events are the same, or if they share a groupId
+function isEventsCollidable(event0: EventTuple, event1: EventTuple): boolean {
+  if (event0.instance.instanceId === event1.instance.instanceId) {
+    return false
+  }
+
+  return !isEventDefsGrouped(event0.def, event1.def)
+}
+
 function eventStoreToEntities(eventStore: EventStore, eventSources: EventSourceHash): ValidationEntity[] {
   return mapEventInstances(eventStore, function(eventInstance: EventInstance, eventDef: EventDef): ValidationEntity {
     let eventSource = eventSources[eventDef.sourceId]