Explorar o código

fixes for overlap

Adam Shaw %!s(int64=7) %!d(string=hai) anos
pai
achega
bb0e870a7d
Modificáronse 2 ficheiros con 16 adicións e 31 borrados
  1. 8 25
      src/structs/event-store.ts
  2. 8 6
      src/validation.ts

+ 8 - 25
src/structs/event-store.ts

@@ -88,36 +88,19 @@ export function getRelatedEvents(eventStore: EventStore, instanceId: string): Ev
   if (instance) {
     let def = eventStore.defs[instance.defId]
 
-    if (def.groupId) {
-      return getEventsByGroupId(eventStore, def.groupId) // will include the original def/instance
-    } else {
-      return eventTupleToStore({ def, instance })
-    }
+    return filterEventStoreDefs(eventStore, function(eventDef) {
+      return isEventDefsRelated(eventDef, def)
+    })
   }
 
   return createEmptyEventStore()
 }
 
-export function getEventsByGroupId(eventStore: EventStore, groupId: string): EventStore {
-  let dest = createEmptyEventStore()
-
-  for (let defId in eventStore.defs) {
-    let def = eventStore.defs[defId]
-
-    if (def.groupId === groupId) {
-      dest.defs[defId] = def
-    }
-  }
-
-  for (let instanceId in eventStore.instances) {
-    let instance = eventStore.instances[instanceId]
-
-    if (eventStore.defs[instance.defId].groupId === groupId) {
-      dest.instances[instanceId] = instance
-    }
-  }
-
-  return dest
+export function isEventDefsRelated(def0: EventDef, def1: EventDef): boolean {
+  return Boolean(
+    def0.defId === def1.defId ||
+    def0.groupId && def0.groupId === def1.groupId
+  )
 }
 
 export function transformRawEvents(rawEvents, func) {

+ 8 - 6
src/validation.ts

@@ -1,4 +1,4 @@
-import { EventStore, expandRecurring, eventTupleToStore, getEventsByGroupId, mapEventInstances } from './structs/event-store'
+import { EventStore, expandRecurring, eventTupleToStore, mapEventInstances, filterEventStoreDefs, isEventDefsRelated } from './structs/event-store'
 import Calendar from './Calendar'
 import { DateSpan, parseOpenDateSpan, OpenDateSpanInput, OpenDateSpan, isSpanPropsEqual, isSpanPropsMatching } from './structs/date-span'
 import { EventInstance, EventDef, EventTuple, parseEvent } from './structs/event'
@@ -63,16 +63,16 @@ function isEntitiesValid(
   for (let subjectEntity of entities) {
     for (let eventEntity of eventEntities) {
       if (
-        ( // not comparing the same event
+        ( // not comparing the same/related event
           !subjectEntity.event ||
           !eventEntity.event ||
-          subjectEntity.event.def.defId !== eventEntity.event.def.defId
+          !isEventDefsRelated(subjectEntity.event.def, eventEntity.event.def)
         ) &&
-        dateSpansCollide(subjectEntity.dateSpan, eventEntity.dateSpan)
+        dateSpansCollide(subjectEntity.dateSpan, eventEntity.dateSpan) // a collision!
       ) {
         if (
           subjectEntity.overlap === false ||
-          eventEntity.overlap === false ||
+          (eventEntity.overlap === false && subjectEntity.event) || // the eventEntity doesn't like two events colliding
           !isOverlapValid(eventEntity.event, subjectEntity.event, globalOverlap, calendar)
         ) {
           return false
@@ -146,7 +146,9 @@ function constraintToSpans(constraint: Constraint, subjectSpan: DateSpan, calend
     return eventStoreToDateSpans(store)
 
   } else if (typeof constraint === 'string') { // an ID
-    let store = getEventsByGroupId(calendar.state.eventStore, constraint)
+    let store = filterEventStoreDefs(calendar.state.eventStore, function(eventDef) {
+      return eventDef.groupId === constraint
+    })
     return eventStoreToDateSpans(store)
 
   } else if (typeof constraint === 'object' && constraint) { // non-null object