Bladeren bron

sticky events. change some dispatchers

Adam Shaw 7 jaren geleden
bovenliggende
commit
a754f7d439

+ 12 - 9
src/Calendar.ts

@@ -25,7 +25,7 @@ import browserContext from './common/browser-context'
 import { DateRangeInput, rangeContainsMarker } from './datelib/date-range'
 import { DateProfile } from './DateProfileGenerator'
 import { EventSourceInput, parseEventSource } from './structs/event-source'
-import { EventInput } from './structs/event'
+import { EventInput, EventInstance, EventDef } from './structs/event'
 import { CalendarState, Action } from './reducers/types'
 import EventSourceApi from './api/EventSourceApi'
 import EventApi from './api/EventApi'
@@ -1039,17 +1039,20 @@ export default class Calendar {
 
   addEvent(eventInput: EventInput, isSticky: boolean = false): EventApi | null {
     let subset = parseEventStore([ eventInput ], '', this.state.dateProfile.activeRange, this)
+    let def: EventDef = objectValues(subset.defs)[0]
+    let instances: EventInstance[] = objectValues(subset.instances)
 
-    this.dispatch({
-      type: 'ADD_EVENTS',
-      eventStore: subset,
-      stick: isSticky // TODO: do something with this
-    })
+    if (def) {
+
+      if (!isSticky) {
+        def.isTemporary = true // will mutate subet, which is good for ADD_EVENTS
+      }
 
-    let def = objectValues(subset.defs)[0]
-    let instances = objectValues(subset.instances)
+      this.dispatch({
+        type: 'ADD_EVENTS',
+        eventStore: subset
+      })
 
-    if (def) {
       return new EventApi(
         this,
         def,

+ 2 - 2
src/api/EventSourceApi.ts

@@ -13,8 +13,8 @@ export default class EventSourceApi {
 
   remove() {
     this.calendar.dispatch({
-      type: 'REMOVE_EVENT_SOURCES',
-      sourceIds: [ this.internalEventSource.sourceId ]
+      type: 'REMOVE_EVENT_SOURCE',
+      sourceId: this.internalEventSource.sourceId
     })
   }
 

+ 5 - 2
src/interactions-external/ExternalElementDragging.ts

@@ -106,8 +106,7 @@ export default class ExternalElementDragging {
       if (dragMeta.create) {
         receivingCalendar.dispatch({
           type: 'ADD_EVENTS',
-          eventStore: toEventStore(droppableEvent),
-          stick: dragMeta.stick // TODO: use this param in the event-store
+          eventStore: toEventStore(droppableEvent)
         })
 
         // signal that an external event landed
@@ -160,6 +159,10 @@ function computeEventForDateSpan(dateSpan: DateSpan, dragMeta: DragMeta, calenda
     Boolean(dragMeta.duration) // hasEnd
   )
 
+  if (!dragMeta.stick) {
+    def.isTemporary = true
+  }
+
   let start = dateSpan.range.start
 
   // only rely on time info if drop zone is all-day,

+ 1 - 2
src/interactions/EventDragging.ts

@@ -183,8 +183,7 @@ export default class EventDragging {
         })
         receivingCalendar.dispatch({
           type: 'ADD_EVENTS',
-          eventStore: this.mutatedRelatedEvents!,
-          stick: true // TODO: use this param
+          eventStore: this.mutatedRelatedEvents!
         })
       }
     }

+ 4 - 10
src/reducers/eventSources.ts

@@ -12,12 +12,8 @@ export default function(eventSourceHash: EventSourceHash, action: Action, datePr
     case 'ADD_EVENT_SOURCES': // already parsed
       return addSources(eventSourceHash, action.sources, dateProfile, calendar)
 
-    case 'REMOVE_EVENT_SOURCES':
-      if (action.sourceIds) {
-        return removeSources(eventSourceHash, action.sourceIds)
-      } else {
-        return {} // remove all
-      }
+    case 'REMOVE_EVENT_SOURCE':
+      return removeSource(eventSourceHash, action.sourceId)
 
     case 'SET_DATE_PROFILE':
       fetchDirtySources(eventSourceHash, action.dateProfile, calendar)
@@ -55,11 +51,9 @@ function addSources(eventSourceHash: EventSourceHash, sources: EventSource[], da
   return assignTo({}, eventSourceHash, hash)
 }
 
-function removeSources(eventSourceHash: EventSourceHash, sourceIds: string[]): EventSourceHash {
-  let idHash = arrayToHash(sourceIds)
-
+function removeSource(eventSourceHash: EventSourceHash, sourceId: string): EventSourceHash {
   return filterHash(eventSourceHash, function(eventSource: EventSource) {
-    return !idHash[eventSource.sourceId]
+    return eventSource.sourceId !== sourceId
   })
 }
 

+ 26 - 30
src/reducers/eventStore.ts

@@ -29,12 +29,12 @@ export default function(eventStore: EventStore, action: Action, sourceHash: Even
     case 'REMOVE_EVENT_INSTANCES':
       return excludeInstances(eventStore, action.instances)
 
-    case 'REMOVE_EVENT_SOURCES':
-      if (action.sourceIds) {
-        return excludeSources(eventStore, action.sourceIds)
-      } else {
-        return excludeNonSticky(eventStore)
-      }
+    case 'REMOVE_EVENT_SOURCE':
+      return excludeSource(eventStore, action.sourceId)
+
+    case 'FETCH_EVENT_SOURCES':
+      // when refetching happens for a source, clear the temporary events in it
+      return excludeTemporary(eventStore, action.sourceIds)
 
     default:
       return eventStore
@@ -60,7 +60,7 @@ function receiveEvents(
       eventSource.sourceId,
       fetchRange,
       calendar,
-      excludeSources(eventStore, [ eventSource.sourceId ]) // dest
+      excludeSource(eventStore, eventSource.sourceId) // dest
     )
   }
 
@@ -76,29 +76,14 @@ function excludeInstances(eventStore: EventStore, removals: EventInstanceHash):
   }
 }
 
-function excludeSources(eventStore: EventStore, sourceIds: string[]): EventStore {
-  let idHash = arrayToHash(sourceIds)
-
-  return {
-    defs: filterHash(eventStore.defs, function(def: EventDef) {
-      return idHash && !idHash[def.sourceId]
-    }),
-    instances: filterHash(eventStore.instances, function(instance: EventInstance) {
-      return idHash && !idHash[eventStore.defs[instance.defId].sourceId]
-    })
-  }
-}
-
-// sticky events don't have source IDs
-function excludeNonSticky(eventStore: EventStore): EventStore {
-  return {
-    defs: filterHash(eventStore.defs, function(def: EventDef) {
-      return !def.sourceId // keep sticky
-    }),
-    instances: filterHash(eventStore.instances, function(instance: EventInstance) {
-      return !eventStore.defs[instance.defId].sourceId // keep sticky
-    })
-  }
+function excludeSource(eventStore: EventStore, sourceId: string): EventStore {
+  let defs = filterHash(eventStore.defs, function(def: EventDef) {
+    return def.sourceId !== sourceId
+  })
+  let instances = filterHash(eventStore.instances, function(instance: EventInstance) {
+    return defs[instance.defId] // still exists?
+  })
+  return { defs, instances }
 }
 
 function applyMutationToRelated(eventStore: EventStore, instanceId: string, mutation: EventMutation, calendar: Calendar): EventStore {
@@ -106,3 +91,14 @@ function applyMutationToRelated(eventStore: EventStore, instanceId: string, muta
   related = applyMutationToEventStore(related, mutation, calendar)
   return mergeEventStores(eventStore, related)
 }
+
+function excludeTemporary(eventStore: EventStore, sourceIds: string[]): EventStore {
+  let sourceIdHash = arrayToHash(sourceIds)
+  let defs = filterHash(eventStore.defs, function(def: EventDef) {
+    return !(sourceIdHash[def.sourceId] && def.isTemporary)
+  })
+  let instances = filterHash(eventStore.instances, function(instance: EventInstance) {
+    return defs[instance.defId] // still exists?
+  })
+  return { defs, instances }
+}

+ 2 - 2
src/reducers/types.ts

@@ -34,12 +34,12 @@ export type Action =
   { type: 'UNSET_EVENT_RESIZE' } |
 
   { type: 'ADD_EVENT_SOURCES', sources: EventSource[] } |
-  { type: 'REMOVE_EVENT_SOURCES', sourceIds?: string[] } | // if no sourceIds, remove all
+  { type: 'REMOVE_EVENT_SOURCE', sourceId: string } |
   { type: 'FETCH_EVENT_SOURCES', sourceIds?: string[] } | // if no sourceIds, fetch all
 
   { type: 'RECEIVE_EVENTS', sourceId: string, fetchId: string, fetchRange: DateRange, rawEvents: EventInput[] } |
   { type: 'RECEIVE_EVENT_ERROR', sourceId: string, fetchId: string, fetchRange: DateRange, error: SimpleError } |
 
-  { type: 'ADD_EVENTS', eventStore: EventStore, stick: boolean } | // TODO: use stick param
+  { type: 'ADD_EVENTS', eventStore: EventStore } |
   { type: 'MUTATE_EVENTS', instanceId: string, mutation: EventMutation } |
   { type: 'REMOVE_EVENT_INSTANCES', instances: EventInstanceHash }

+ 1 - 0
src/structs/event.ts

@@ -61,6 +61,7 @@ export interface EventDef {
   borderColor: string
   textColor: string
   extendedProps: object
+  isTemporary?: boolean // if true, will disappear upon navigation
 }
 
 export interface EventInstance {