Просмотр исходного кода

parse event-ui using arbitrary scoping. select uses it now

Adam Shaw 7 лет назад
Родитель
Сommit
d4f97ce11a
5 измененных файлов с 27 добавлено и 43 удалено
  1. 6 2
      src/Calendar.ts
  2. 14 30
      src/component/event-ui.ts
  3. 1 1
      src/exports.ts
  4. 2 2
      src/structs/event-source.ts
  5. 4 8
      src/validation.ts

+ 6 - 2
src/Calendar.ts

@@ -24,7 +24,7 @@ import { CalendarState, Action } from './reducers/types'
 import EventSourceApi from './api/EventSourceApi'
 import EventApi from './api/EventApi'
 import { createEmptyEventStore, EventStore, eventTupleToStore } from './structs/event-store'
-import { processScopedUiProps, EventUiHash, EventUi } from './component/event-ui'
+import { processScopedUiProps, EventUiHash, EventUi, processUnscopedUiProps } from './component/event-ui'
 import PointerDragging, { PointerDragEvent } from './dnd/PointerDragging'
 import EventDragging from './interactions/EventDragging'
 import { buildViewSpecs, ViewSpecHash, ViewSpec } from './structs/view-spec'
@@ -64,11 +64,13 @@ export default class Calendar {
 
   private buildDateEnv = memoize(buildDateEnv)
   private buildTheme = memoize(buildTheme)
-  private buildEventUiSingleBase = memoize(processScopedUiProps)
+  private buildEventUiSingleBase = memoize(processScopedUiProps.bind(null, 'event') as typeof processUnscopedUiProps) // hack for ts
+  private buildSelectionConfig = memoize(processScopedUiProps.bind(null, 'select') as typeof processUnscopedUiProps) // hack for ts
   private buildEventUiBySource = memoizeOutput(buildEventUiBySource, isPropsEqual)
   private buildEventUiBases = memoize(buildEventUiBases)
 
   eventUiBases: EventUiHash // solely for validation system
+  selectionConfig: EventUi // doesn't need all the info EventUi provides. only validation-related. TODO: separate data structs
 
   optionsManager: OptionsManager
   viewSpecs: ViewSpecHash
@@ -543,6 +545,8 @@ export default class Calendar {
       options.cmdFormatter
     )
 
+    this.selectionConfig = this.buildSelectionConfig(options, this) // needs dateEnv. do after :(
+
     // ineffecient to do every time?
     this.viewSpecs = buildViewSpecs(
       pluginHooks.viewConfigs,

+ 14 - 30
src/component/event-ui.ts

@@ -1,9 +1,11 @@
 import { Constraint, Allow, normalizeConstraint, ConstraintInput, Overlap } from '../validation'
 import { parseClassName } from '../util/html'
-import { refineProps } from '../util/misc'
+import { refineProps, capitaliseFirstLetter } from '../util/misc'
 import Calendar from '../Calendar'
 
 // TODO: better called "EventSettings" or "EventConfig"
+// TODO: move this file into structs
+// TODO: separate constraint/overlap/allow, because selection uses only that, not other props
 
 export interface UnscopedEventUiInput {
   editable?: boolean
@@ -20,7 +22,7 @@ export interface UnscopedEventUiInput {
   color?: string
 }
 
-export interface ScopedEventUiInput {
+export interface EventScopedEventUiInput { // has the word "event" in all the props
   editable?: boolean // only one not scoped
   eventStartEditable?: boolean
   eventDurationEditable?: boolean
@@ -64,21 +66,6 @@ export const UNSCOPED_EVENT_UI_PROPS = {
   textColor: String
 }
 
-const SCOPED_EVENT_UI_PROPS = { // TODO: not very DRY. instead, map to UNSCOPED_EVENT_UI_PROPS
-  editable: Boolean, // only one not scoped
-  eventStartEditable: Boolean,
-  eventDurationEditable: Boolean,
-  eventConstraint: null,
-  eventOverlap: null,
-  eventAllow: null,
-  eventClassName: parseClassName,
-  eventClassNames: parseClassName,
-  eventColor: String,
-  eventBackgroundColor: String,
-  eventBorderColor: String,
-  eventTextColor: String
-}
-
 export function processUnscopedUiProps(rawProps: UnscopedEventUiInput, calendar: Calendar, leftovers?): EventUi {
   let props = refineProps(rawProps, UNSCOPED_EVENT_UI_PROPS, {}, leftovers)
   let constraint = normalizeConstraint(props.constraint, calendar)
@@ -96,21 +83,18 @@ export function processUnscopedUiProps(rawProps: UnscopedEventUiInput, calendar:
   }
 }
 
-export function processScopedUiProps(rawProps: ScopedEventUiInput, calendar: Calendar, leftovers?): EventUi {
-  let props = refineProps(rawProps, SCOPED_EVENT_UI_PROPS, {}, leftovers)
-  let constraint = normalizeConstraint(props.eventConstraint, calendar)
+export function processScopedUiProps(prefix: string, rawScoped: any, calendar: Calendar, leftovers?): EventUi {
+  let rawUnscoped = {} as any
 
-  return {
-    startEditable: props.eventStartEditable != null ? props.eventStartEditable : props.editable,
-    durationEditable: props.eventDurationEditable != null ? props.eventDurationEditable : props.editable,
-    constraints: constraint != null ? [ constraint ] : [],
-    overlaps: props.eventOverlap != null ? [ props.eventOverlap ] : [],
-    allows: props.eventAllow != null ? [ props.eventAllow ] : [],
-    backgroundColor: props.eventBackgroundColor || props.eventColor,
-    borderColor: props.eventBorderColor || props.eventColor,
-    textColor: props.eventTextColor,
-    classNames: props.eventClassNames.concat(props.eventClassName)
+  for (let key in UNSCOPED_EVENT_UI_PROPS) {
+    rawUnscoped[key] = rawScoped[prefix + capitaliseFirstLetter(key)]
   }
+
+  if (prefix === 'event') {
+    rawUnscoped.editable = rawScoped.editable // special case. there is no 'eventEditable', just 'editable'
+  }
+
+  return processUnscopedUiProps(rawUnscoped, calendar, leftovers)
 }
 
 const EMPTY_EVENT_UI: EventUi = {

+ 1 - 1
src/exports.ts

@@ -61,7 +61,7 @@ export {
 } from './util/dom-manip'
 
 export { EventStore, filterEventStoreDefs, createEmptyEventStore } from './structs/event-store'
-export { EventUiHash, EventUi, processScopedUiProps, ScopedEventUiInput, combineEventUis } from './component/event-ui'
+export { EventUiHash, EventUi, processScopedUiProps, EventScopedEventUiInput, combineEventUis } from './component/event-ui'
 export { default as Splitter, SplittableProps, EMPTY_PROPS } from './component/event-splitting'
 export { buildGotoAnchorHtml, getAllDayHtml, getDayClasses } from './component/date-rendering'
 

+ 2 - 2
src/structs/event-source.ts

@@ -3,7 +3,7 @@ import { EventInput } from './event'
 import Calendar from '../Calendar'
 import { DateRange } from '../datelib/date-range'
 import { EventSourceFunc } from '../event-sources/func-event-source'
-import { ScopedEventUiInput, processUnscopedUiProps } from '../component/event-ui'
+import { EventScopedEventUiInput, processUnscopedUiProps } from '../component/event-ui'
 import { EventUi } from '../component/event-ui'
 
 /*
@@ -23,7 +23,7 @@ export type EventInputTransformer = (eventInput: EventInput) => EventInput | nul
 export type EventSourceSuccessResponseHandler = (rawData: any, response: any) => EventInput[] | void
 export type EventSourceErrorResponseHandler = (error: EventSourceError) => void
 
-export interface ExtendedEventSourceInput extends ScopedEventUiInput {
+export interface ExtendedEventSourceInput extends EventScopedEventUiInput {
   id?: string | number // only accept number?
   allDayDefault?: boolean
   eventDataTransform?: EventInputTransformer

+ 4 - 8
src/validation.ts

@@ -31,19 +31,15 @@ export function isEventsValid(eventStore: EventStore, calendar: Calendar): boole
 }
 
 export function isSelectionValid(selection: DateSpan, calendar: Calendar): boolean {
-
-  // TODO: separate util for this. in scoped part!?
-  let constraint = normalizeConstraint(calendar.opt('selectConstraint'), calendar)
-  let overlap = calendar.opt('selectOverlap')
-  let allow = calendar.opt('selectAllow')
+  let { selectionConfig } = calendar
 
   return isEntitiesValid(
     [ {
       dateSpan: selection,
       event: null,
-      constraints: constraint != null ? [ constraint ] : [],
-      overlaps: overlap != null ? [ overlap ] : [],
-      allows: allow != null ? [ allow ] : []
+      constraints: selectionConfig.constraints,
+      overlaps: selectionConfig.overlaps,
+      allows: selectionConfig.allows
     } ],
     calendar
   )