Selaa lähdekoodia

change validation api a bit

Adam Shaw 7 vuotta sitten
vanhempi
sitoutus
b93fd37334
3 muutettua tiedostoa jossa 35 lisäystä ja 32 poistoa
  1. 17 23
      src/component/event-splitting.ts
  2. 1 1
      src/exports.ts
  3. 17 8
      src/validation.ts

+ 17 - 23
src/component/event-splitting.ts

@@ -39,7 +39,7 @@ export default abstract class Splitter<PropsType extends SplittableProps = Split
     let keyInfos = this.getKeyInfo(props)
     let defKeys = this.getKeysForEventDefs(props.eventStore)
     let dateSelections = this.splitDateSelection(props.dateSelection)
-    let individualUi = this.splitIndividualUi(props.eventUiBases, defKeys)
+    let individualUi = this.splitIndividualUi(props.eventUiBases, defKeys) // the individual *bases*
     let eventStores = this.splitEventStore(props.eventStore, defKeys)
     let eventDrags = this.splitEventDrag(props.eventDrag, defKeys)
     let eventResizes = this.splitEventResize(props.eventResize, defKeys)
@@ -48,13 +48,13 @@ export default abstract class Splitter<PropsType extends SplittableProps = Split
     for (let key in keyInfos) {
       let keyInfo = keyInfos[key]
       let eventStore = eventStores[key] || EMPTY_EVENT_STORE
-      let buildEventUi = this.eventUiBuilders[key] = oldEventUiBuilders[key] || memoize(this._buildEventUiForKey)
+      let buildEventUi = this.eventUiBuilders[key] = oldEventUiBuilders[key] || memoize(buildEventUiForKey)
 
       splitProps[key] = {
         businessHours: keyInfo.businessHours || props.businessHours,
         dateSelection: dateSelections[key] || null,
         eventStore,
-        eventUiBases: buildEventUi.call(this, props.eventUiBases[''], keyInfo.ui, individualUi[key], key),
+        eventUiBases: buildEventUi(props.eventUiBases[''], keyInfo.ui, individualUi[key]),
         eventSelection: eventStore.instances[props.eventSelection] ? props.eventSelection : '',
         eventDrag: eventDrags[key] || null,
         eventResize: eventResizes[key] || null
@@ -165,32 +165,26 @@ export default abstract class Splitter<PropsType extends SplittableProps = Split
     return splitStates
   }
 
-  private _buildEventUiForKey(allUi: EventUi | null, eventUiForKey: EventUi | null, individualUi: EventUiHash | null, key: string) {
-    let baseParts = []
+}
 
-    if (allUi) {
-      baseParts.push(allUi)
-    }
+function buildEventUiForKey(allUi: EventUi | null, eventUiForKey: EventUi | null, individualUi: EventUiHash | null) {
+  let baseParts = []
 
-    if (eventUiForKey) {
-      baseParts.push(eventUiForKey)
-    }
-
-    let stuff = {
-      '': combineEventUis(baseParts)
-    }
+  if (allUi) {
+    baseParts.push(allUi)
+  }
 
-    if (individualUi) {
-      for (let defId in individualUi) {
-        stuff[defId] = this.filterEventUi(individualUi[defId], key)
-      }
-    }
+  if (eventUiForKey) {
+    baseParts.push(eventUiForKey)
+  }
 
-    return stuff
+  let stuff = {
+    '': combineEventUis(baseParts)
   }
 
-  private filterEventUi(ui: EventUi, key: string): EventUi {
-    return ui
+  if (individualUi) {
+    Object.assign(stuff, individualUi)
   }
 
+  return stuff
 }

+ 1 - 1
src/exports.ts

@@ -161,4 +161,4 @@ export { default as DayTable, DayTableSeg, DayTableCell } from './common/DayTabl
 export { default as Slicer, SlicedProps } from './common/slicing-utils'
 
 export { EventMutation } from './structs/event-mutation'
-export { ConstraintInput, AllowFunc, isPropsValid } from './validation'
+export { Constraint, ConstraintInput, AllowFunc, isPropsValid } from './validation'

+ 17 - 8
src/validation.ts

@@ -8,6 +8,7 @@ import { excludeInstances } from './reducers/eventStore'
 import { EventInput } from './structs/event'
 import { EventInteractionState } from './interactions/event-interaction-state'
 import { SplittableProps } from './component/event-splitting'
+import { mapHash } from './util/object'
 
 // TODO: rename to "criteria" ?
 export type ConstraintInput = 'businessHours' | string | EventInput | EventInput[]
@@ -44,13 +45,13 @@ function isNewPropsValid(newProps, calendar: Calendar) {
   return (calendar.pluginSystem.hooks.isPropsValid || isPropsValid)(props, calendar)
 }
 
-export function isPropsValid(state: SplittableProps, calendar: Calendar, dateSpanMeta = {}): boolean {
+export function isPropsValid(state: SplittableProps, calendar: Calendar, dateSpanMeta = {}, filterConfig?): boolean {
 
-  if (state.dateSelection && !isDateSelectionPropsValid(state, calendar, dateSpanMeta)) {
+  if (state.eventDrag && !isInteractionPropsValid(state, calendar, dateSpanMeta, filterConfig)) {
     return false
   }
 
-  if (state.eventDrag && !isInteractionPropsValid(state, calendar, dateSpanMeta)) {
+  if (state.dateSelection && !isDateSelectionPropsValid(state, calendar, dateSpanMeta, filterConfig)) {
     return false
   }
 
@@ -61,7 +62,7 @@ export function isPropsValid(state: SplittableProps, calendar: Calendar, dateSpa
 // Moving Event Validation
 // ------------------------------------------------------------------------------------------------------------------------
 
-function isInteractionPropsValid(state: SplittableProps, calendar: Calendar, dateSpanMeta: any): boolean {
+function isInteractionPropsValid(state: SplittableProps, calendar: Calendar, dateSpanMeta: any, filterConfig): boolean {
   let interaction = state.eventDrag // HACK: the eventDrag props is used for ALL interactions
 
   let subjectEventStore = interaction.mutatedEvents
@@ -74,6 +75,10 @@ function isInteractionPropsValid(state: SplittableProps, calendar: Calendar, dat
       { '': calendar.selectionConfig } // if not a real event, validate as a selection
   )
 
+  if (filterConfig) {
+    subjectConfigs = mapHash(subjectConfigs, filterConfig)
+  }
+
   let otherEventStore = excludeInstances(state.eventStore, interaction.affectedEvents.instances) // exclude the subject events. TODO: exclude defs too?
   let otherDefs = otherEventStore.defs
   let otherInstances = otherEventStore.instances
@@ -86,7 +91,7 @@ function isInteractionPropsValid(state: SplittableProps, calendar: Calendar, dat
     let subjectDef = subjectDefs[subjectInstance.defId]
 
     // constraint
-    if (!allConstraintPasses(subjectConfig.constraints, subjectRange, otherEventStore, state.businessHours, calendar)) {
+    if (!allConstraintsPass(subjectConfig.constraints, subjectRange, otherEventStore, state.businessHours, calendar)) {
       return false
     }
 
@@ -148,7 +153,7 @@ function isInteractionPropsValid(state: SplittableProps, calendar: Calendar, dat
 // Date Selection Validation
 // ------------------------------------------------------------------------------------------------------------------------
 
-function isDateSelectionPropsValid(state: SplittableProps, calendar: Calendar, dateSpanMeta: any): boolean {
+function isDateSelectionPropsValid(state: SplittableProps, calendar: Calendar, dateSpanMeta: any, filterConfig): boolean {
   let relevantEventStore = state.eventStore
   let relevantDefs = relevantEventStore.defs
   let relevantInstances = relevantEventStore.instances
@@ -157,8 +162,12 @@ function isDateSelectionPropsValid(state: SplittableProps, calendar: Calendar, d
   let selectionRange = selection.range
   let { selectionConfig } = calendar
 
+  if (filterConfig) {
+    selectionConfig = filterConfig(selectionConfig)
+  }
+
   // constraint
-  if (!allConstraintPasses(selectionConfig.constraints, selectionRange, relevantEventStore, state.businessHours, calendar)) {
+  if (!allConstraintsPass(selectionConfig.constraints, selectionRange, relevantEventStore, state.businessHours, calendar)) {
     return false
   }
 
@@ -205,7 +214,7 @@ function isDateSelectionPropsValid(state: SplittableProps, calendar: Calendar, d
 // Constraint Utils
 // ------------------------------------------------------------------------------------------------------------------------
 
-function allConstraintPasses(
+function allConstraintsPass(
   constraints: Constraint[],
   subjectRange: DateRange,
   otherEventStore: EventStore,