Adam Shaw 7 лет назад
Родитель
Сommit
d813d5f768
2 измененных файлов с 84 добавлено и 94 удалено
  1. 29 37
      src/Calendar.ts
  2. 55 57
      src/reducers/main.ts

+ 29 - 37
src/Calendar.ts

@@ -23,6 +23,7 @@ import { assignTo } from './util/object'
 import { RenderForceFlags } from './component/Component'
 import browserContext from './common/browser-context'
 import { rangeContainsMarker } from './datelib/date-range'
+import { DateProfile } from './DateProfileGenerator'
 
 
 export default class Calendar {
@@ -622,12 +623,8 @@ export default class Calendar {
       let view = this.getViewByType(viewType)
       this.view = view
 
-      this.dispatch({ // luckily, will cause a rerender
-        type: 'SET_DATE_PROFILE',
-        dateProfile: view.computeDateProfile(
-          dateMarker || this.state.dateProfile.currentDate
-        )
-      })
+      // luckily, will always cause a rerender
+      this.setCurrentDateMarker(dateMarker || this.state.dateProfile.currentDate)
     }
   }
 
@@ -649,46 +646,35 @@ export default class Calendar {
 
 
   prev() {
-    this.dispatch({
-      type: 'SET_DATE_PROFILE',
-      dateProfile: this.view.dateProfileGenerator.buildPrev(this.state.dateProfile)
-    })
+    this.setDateProfile(
+      this.view.dateProfileGenerator.buildPrev(this.state.dateProfile)
+    )
   }
 
 
   next() {
-    this.dispatch({
-      type: 'SET_DATE_PROFILE',
-      dateProfile: this.view.dateProfileGenerator.buildNext(this.state.dateProfile)
-    })
+    this.setDateProfile(
+      this.view.dateProfileGenerator.buildNext(this.state.dateProfile)
+    )
   }
 
 
   prevYear() {
-    this.dispatch({
-      type: 'SET_DATE_PROFILE',
-      dateProfile: this.view.computeDateProfile(
-        this.dateEnv.addYears(this.state.dateProfile.currentDate, -1)
-      )
-    })
+    this.setCurrentDateMarker(
+      this.dateEnv.addYears(this.state.dateProfile.currentDate, -1)
+    )
   }
 
 
   nextYear() {
-    this.dispatch({
-      type: 'SET_DATE_PROFILE',
-      dateProfile: this.view.computeDateProfile(
-        this.dateEnv.addYears(this.state.dateProfile.currentDate, 1)
-      )
-    })
+    this.setCurrentDateMarker(
+      this.dateEnv.addYears(this.state.dateProfile.currentDate, 1)
+    )
   }
 
 
   today() {
-    this.dispatch({
-      type: 'SET_DATE_PROFILE',
-      dateProfile: this.view.computeDateProfile(this.getNow())
-    })
+    this.setCurrentDateMarker(this.getNow())
   }
 
 
@@ -700,12 +686,9 @@ export default class Calendar {
 
 
   incrementDate(delta) { // is public facing
-    this.dispatch({
-      type: 'SET_DATE_PROFILE',
-      dateProfile: this.view.computeDateProfile(
-        this.dateEnv.add(this.state.dateProfile.currentDate, delta)
-      )
-    })
+    this.setCurrentDateMarker(
+      this.dateEnv.add(this.state.dateProfile.currentDate, delta)
+    )
   }
 
 
@@ -716,9 +699,18 @@ export default class Calendar {
 
 
   setCurrentDateMarker(date: DateMarker) { // internal use only
+    this.setDateProfile(
+      this.view.computeDateProfile(date)
+    )
+  }
+
+
+  setDateProfile(dateProfile: DateProfile) {
+    this.view.updateMiscDateProps(dateProfile) // for legacy!
+
     this.dispatch({
       type: 'SET_DATE_PROFILE',
-      dateProfile: this.view.computeDateProfile(date)
+      dateProfile: dateProfile
     })
   }
 

+ 55 - 57
src/reducers/main.ts

@@ -1,9 +1,11 @@
 import Calendar from '../Calendar'
 import { DateComponentRenderState } from '../component/DateComponent'
 import { EventSourceHash } from '../structs/event-source'
-import { assignTo } from '../util/object'
 import { reduceEventSourceHash } from './event-sources'
 import { reduceEventStore } from './event-store'
+import { DateProfile } from '../DateProfileGenerator'
+import { DateSpan } from '../structs/date-span'
+import { EventInteractionState } from '../interactions/event-interaction-state'
 
 export interface CalendarState extends DateComponentRenderState {
   loadingLevel: number
@@ -11,76 +13,72 @@ export interface CalendarState extends DateComponentRenderState {
 }
 
 export function reduce(state: CalendarState, action: any, calendar: Calendar): CalendarState {
-  let newState = {
-    loadingLevel: reduceLoadingLevel(state.loadingLevel, action),
+  calendar.trigger(action.type, action) // for testing hooks
+
+  return {
+    dateProfile: reduceDateProfile(state.dateProfile, action),
     eventSources: reduceEventSourceHash(state.eventSources, action, calendar),
     eventStore: reduceEventStore(state.eventStore, action, calendar),
-    dateProfile: state.dateProfile,
-    selection: state.selection,
-    dragState: state.dragState,
-    eventResizeState: state.eventResizeState,
-    businessHoursDef: state.businessHoursDef,
-    selectedEventInstanceId: state.selectedEventInstanceId
+    businessHoursDef: state.businessHoursDef, // TODO: rename?
+    selection: reduceDateSelection(state.selection, action), // TODO: rename
+    selectedEventInstanceId: reduceSelectedEvent(state.selectedEventInstanceId, action),
+    dragState: reduceDrag(state.dragState, action),
+    eventResizeState: reduceEventResize(state.eventResizeState, action),
+    loadingLevel: reduceLoadingLevel(state.loadingLevel, action)
   }
+}
 
-  calendar.trigger(action.type, action) // for testing hooks
-
-  switch(action.type) {
-
+function reduceDateProfile(currentDateProfile: DateProfile, action: any) {
+  switch (action.type) {
     case 'SET_DATE_PROFILE':
-      if (action.dateProfile.isValid) {
-        newState.dateProfile = action.dateProfile
-        calendar.view.updateMiscDateProps(action.dateProfile)
-      }
-      break
+      return action.dateProfile
+    default:
+      return currentDateProfile
+  }
+}
 
-    case 'SELECT':
-      return assignTo({}, state, {
-        selection: action.selection
-      })
+function reduceDateSelection(currentSelection: DateSpan, action: any) {
+  switch (action.type) {
+    case 'SELECT': // TODO: rename
+      return action.selection
+    case 'UNSELECT': // TODO: rename
+      return null
+    default:
+      return currentSelection
+  }
+}
 
-    case 'UNSELECT':
-      if (state.selection) { // if already no selection, don't bother
-        return assignTo({}, state, {
-          selection: null
-        })
-      } else {
-        break
-      }
+function reduceSelectedEvent(currentInstanceId: string, action: any): string {
+  switch (action.type) {
+    case 'SELECT_EVENT':
+      return action.eventInstanceId
+    case 'CLEAR_SELECTED_EVENT':
+      return ''
+    default:
+      return currentInstanceId
+  }
+}
 
+function reduceDrag(currentDrag: EventInteractionState, action: any) {
+  switch (action.type) {
     case 'SET_DRAG':
-      return assignTo({}, state, {
-        dragState: action.dragState
-      })
-
+      return action.dragState
     case 'CLEAR_DRAG':
-      return assignTo({}, state, {
-        dragState: null
-      })
-
-    case 'SELECT_EVENT':
-      return assignTo({}, state, {
-        selectedEventInstanceId: action.eventInstanceId
-      })
-
-    case 'CLEAR_SELECTED_EVENT':
-      return assignTo({}, state, {
-        selectedEventInstanceId: null
-      })
+      return null
+    default:
+      return currentDrag
+  }
+}
 
+function reduceEventResize(currentEventResize: EventInteractionState, action: any) {
+  switch (action.type) {
     case 'SET_EVENT_RESIZE':
-      return assignTo({}, state, {
-        eventResizeState: action.eventResizeState
-      })
-
+      return action.eventResizeState
     case 'CLEAR_EVENT_RESIZE':
-      return assignTo({}, state, {
-        eventResizeState: null
-      })
-
+      return null
+    default:
+      return currentEventResize
   }
-
-  return newState
 }
 
 function reduceLoadingLevel(level: number, action): number {