Adam Shaw 7 anni fa
parent
commit
ba80ae2aa8

+ 8 - 7
src/Calendar.ts

@@ -10,7 +10,7 @@ import ViewSpecManager from './ViewSpecManager'
 import View from './View'
 import Theme from './theme/Theme'
 import { getThemeSystemClass } from './theme/ThemeRegistry'
-import { RangeInput, OptionsInput, EventObjectInput, EventSourceInput } from './types/input-types'
+import { OptionsInput } from './types/input-types'
 import { getLocale } from './datelib/locale'
 import { DateEnv, DateInput } from './datelib/env'
 import { DateMarker, startOfDay } from './datelib/marker'
@@ -22,9 +22,10 @@ import reselector from './util/reselector'
 import { assignTo } from './util/object'
 import { RenderForceFlags } from './component/Component'
 import browserContext from './common/browser-context'
-import { rangeContainsMarker } from './datelib/date-range'
+import { DateRangeInput, rangeContainsMarker } from './datelib/date-range'
 import { DateProfile } from './DateProfileGenerator'
 import { EventSourceInput, parseEventSource } from './structs/event-source'
+import { EventInput } from './structs/event'
 import { CalendarState, Action } from './reducers/types'
 
 
@@ -590,11 +591,11 @@ export default class Calendar {
   }
 
 
-  changeView(viewType: string, dateOrRange: RangeInput | DateInput) {
+  changeView(viewType: string, dateOrRange: DateRangeInput | DateInput) {
     let dateMarker = null
 
     if (dateOrRange) {
-      if ((dateOrRange as RangeInput).start && (dateOrRange as RangeInput).end) { // a range
+      if ((dateOrRange as DateRangeInput).start && (dateOrRange as DateRangeInput).end) { // a range
         this.optionsManager.add({ // will not rerender
           visibleRange: dateOrRange
         })
@@ -1038,7 +1039,7 @@ export default class Calendar {
   }
 
 
-  renderEvent(eventInput: EventObjectInput, isSticky: boolean = false) {
+  renderEvent(eventInput: EventInput, isSticky: boolean = false) {
     // TODO
   }
 
@@ -1059,12 +1060,12 @@ export default class Calendar {
   // -----------------------------------------------------------------------------------------------------------------
 
 
-  getEventSources(): EventSource {
+  getEventSources(): EventSource[] {
     return null // TODO
   }
 
 
-  getEventSourceById(id): EventSource {
+  getEventSourceById(id): EventSource | null {
     return null // TODO
   }
 

+ 14 - 3
src/event-sources/func-event-source.ts

@@ -2,9 +2,19 @@ import { unpromisify } from '../util/promise'
 import { registerEventSourceDef } from '../structs/event-source'
 import { EventInput } from '../structs/event'
 
+export type EventSourceFunc = (
+  arg: {
+    start: Date
+    end: Date
+    timeZone: string
+  },
+  successCallback: (events: EventInput[]) => void,
+  failureCallback: (errorObj: any) => void
+) => any; // a promise-like object, or nothing
+
 registerEventSourceDef({
 
-  parseMeta(raw: any): EventInput[] {
+  parseMeta(raw: any): EventSourceFunc {
     if (typeof raw === 'function') { // short form
       return raw
     } else if (typeof raw.events === 'function') {
@@ -14,10 +24,11 @@ registerEventSourceDef({
   },
 
   fetch(arg, success, failure) {
-    const dateEnv = arg.calendar.dateEnv
+    let dateEnv = arg.calendar.dateEnv
+    let func = arg.eventSource.meta as EventSourceFunc
 
     unpromisify(
-      arg.eventSource.meta({ // the function returned from parseMeta
+      func.bind({ // the function returned from parseMeta
         start: dateEnv.toDate(arg.range.start),
         end: dateEnv.toDate(arg.range.end),
         timeZone: dateEnv.timeZone

+ 4 - 6
src/exports.ts

@@ -6,12 +6,10 @@ export const version = '<%= version %>'
 // and the below integer should be incremented.
 export const internalApiVersion = 12
 
-export {
-  BusinessHoursInput,
-  EventObjectInput,
-  EventOptionsBase,
-  OptionsInput
-} from './types/input-types'
+// types
+export { OptionsInput } from './types/input-types'
+export { EventInput } from './structs/event'
+export { BusinessHoursDef } from './structs/business-hours'
 
 export {
   applyAll,

+ 27 - 3
src/structs/event-source.ts

@@ -3,17 +3,20 @@ import { refineProps } from '../util/misc'
 import { EventInput } from './event'
 import Calendar from '../Calendar'
 import { DateRange } from '../datelib/date-range'
+import { EventSourceFunc } from '../event-sources/func-event-source'
 
 /*
 Parsing and normalization of the EventSource data type, which defines how event data is fetched.
 Contains the plugin system for defining new types if event sources.
+
+TODO: "EventSource" is the same name as a built-in type in TypeScript. Rethink.
 */
 
 export type EventInputTransformer = (eventInput: EventInput) => EventInput | null
 export type EventSourceSuccessHandler = (eventInputs: EventInput[]) => void
 export type EventSourceFailureHandler = (errorObj: any) => void
 
-export interface EventSourceInput {
+export interface ExtendedEventSourceInput {
   id?: string | number
   allDayDefault?: boolean
   eventDataTransform?: EventInputTransformer
@@ -30,9 +33,26 @@ export interface EventSourceInput {
   textColor?: string
   success?: EventSourceSuccessHandler
   failure?: EventSourceFailureHandler
+
+  // array (TODO: how to move this to array-event-source?)
+  events?: EventInput[]
+
+  // json feed (TODO: how to move this to json-feed-event-source?)
+  url?: string
+  method?: string
+  data?: object | (() => object)
+  startParam?: string
+  endParam?: string
+  timezoneParam?: string
+
   [otherProp: string]: any // in case plugins want more props
 }
 
+export type EventSourceInput =
+  ExtendedEventSourceInput | // object in extended form
+  EventSourceFunc | // just a function
+  string // a URL for a JSON feed
+
 export interface EventSource {
   sourceId: string
   sourceDefId: number // one of the few IDs that's a NUMBER not a string
@@ -111,14 +131,18 @@ export function parseEventSource(raw: EventSourceInput): EventSource | null {
     let meta = def.parseMeta(raw)
 
     if (meta) {
-      return parseEventSourceProps(raw, meta, i)
+      return parseEventSourceProps(
+        typeof raw === 'object' ? raw : {},
+        meta,
+        i
+      )
     }
   }
 
   return null
 }
 
-function parseEventSourceProps(raw: EventSourceInput, meta: object, sourceDefId: number): EventSource {
+function parseEventSourceProps(raw: ExtendedEventSourceInput, meta: object, sourceDefId: number): EventSource {
   let props = refineProps(raw, SIMPLE_SOURCE_PROPS) as EventSource
 
   props.isFetching = false

+ 30 - 87
src/types/input-types.ts

@@ -5,69 +5,18 @@ https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/fullcalenda
 
 import View from '../View'
 import { EventSourceInput } from '../structs/event-source'
-import { Duration } from '../datelib/duration'
+import { Duration, DurationInput } from '../datelib/duration'
 import { DateInput } from '../datelib/env'
 import { FormatterInput } from '../datelib/formatting'
+import { DateRangeInput } from '../datelib/date-range'
+import { BusinessHoursDef } from '../structs/business-hours'
+import { EventInput } from '../structs/event'
 
-export type DurationInput = Duration | object | string | number
 
-export interface RangeInput {
-  start?: DateInput
-  end?: DateInput
-}
-
-export type ConstraintInput = RangeInput | BusinessHoursInput | 'businessHours'
-
-export interface EventOptionsBase {
-  className?: string | string[]
-  editable?: boolean
-  startEditable?: boolean
-  durationEditable?: boolean
-  rendering?: string
-  overlap?: boolean
-  constraint?: ConstraintInput
-  color?: string
-  backgroundColor?: string
-  borderColor?: string
-  textColor?: string
-}
-
-// used for input and toLegacy output
-// when we expose the real EventObject, will need lots of changes
-export interface EventObjectInput extends EventOptionsBase, RangeInput {
-  _id?: string
-  id?: string | number
-  title: string
-  allDay?: boolean
-  url?: string
-  source?: EventSourceInput
-  [customField: string]: any // non-standard fields
-}
-
-export type EventSourceFunction = (start: Date, end: Date, timezone: string, callback: ((events: EventObjectInput[]) => void)) => void
-export type EventSourceSimpleInput = EventObjectInput[] | EventSourceFunction | string
-
-export interface EventSourceExtendedInput extends EventOptionsBase {
+// temporary!
+export type EventApi = EventInput
+export type ConstraintInput = DateRangeInput | BusinessHoursDef | 'businessHours'
 
-  // array
-  events?: EventSourceSimpleInput
-
-  // json feed
-  url?: string
-  method?: string
-  data?: object | (() => object)
-  startParam?: string
-  endParam?: string
-  timezoneParam?: string
-  success?: (eventDefs: EventObjectInput[], ajaxRes: any) => void
-  error?: (error: any, ajaxRes: any) => void
-
-  // general
-  allDayDefault?: boolean
-  eventDataTransform?(eventData: any): EventObjectInput
-}
-
-export type EventSourceInput = EventSourceSimpleInput | EventSourceExtendedInput
 
 export interface ToolbarInput {
   left?: string
@@ -103,14 +52,8 @@ export interface ButtonTextCompoundInput {
   [viewId: string]: string | undefined // needed b/c of other optional types
 }
 
-export interface BusinessHoursInput {
-  start?: DateInput
-  end?: DateInput
-  dow?: number[]
-}
-
 export interface EventSegment {
-  event: EventObjectInput
+  event: EventApi
   start: Date
   end: Date
   isStart: boolean
@@ -147,7 +90,7 @@ export interface OptionsInputBase {
   weekNumbers?: boolean
   weekNumbersWithinDays?: boolean
   weekNumberCalculation?: 'local' | 'ISO' | ((m: Date) => number)
-  businessHours?: boolean | BusinessHoursInput | BusinessHoursInput[]
+  businessHours?: BusinessHoursDef
   showNonCurrentDates?: boolean
   height?: number | 'auto' | 'parent' | (() => number)
   contentHeight?: number | 'auto' | (() => number)
@@ -174,8 +117,8 @@ export interface OptionsInputBase {
   noEventsMessage?: string
   defaultDate?: DateInput
   nowIndicator?: boolean
-  visibleRange?: ((currentDate: Date) => RangeInput) | RangeInput
-  validRange?: RangeInput
+  visibleRange?: ((currentDate: Date) => DateRangeInput) | DateRangeInput
+  validRange?: DateRangeInput
   dateIncrement?: DurationInput
   dateAlignment?: string
   duration?: DurationInput
@@ -203,7 +146,7 @@ export interface OptionsInputBase {
   selectHelper?: boolean
   unselectAuto?: boolean
   unselectCancel?: string
-  selectOverlap?: boolean | ((event: EventObjectInput) => boolean)
+  selectOverlap?: boolean | ((event: EventApi) => boolean)
   selectConstraint?: ConstraintInput
   events?: EventSourceInput
   eventSources?: EventSourceInput[]
@@ -216,7 +159,7 @@ export interface OptionsInputBase {
   eventBorderColor?: string
   eventTextColor?: string
   nextDayThreshold?: DurationInput
-  eventOrder?: string | Array<((a: EventObjectInput, b: EventObjectInput) => number) | (string | ((a: EventObjectInput, b: EventObjectInput) => number))>
+  eventOrder?: string | Array<((a: EventApi, b: EventApi) => number) | (string | ((a: EventApi, b: EventApi) => number))>
   eventRenderWait?: number | null
   editable?: boolean
   eventStartEditable?: boolean
@@ -224,7 +167,7 @@ export interface OptionsInputBase {
   dragRevertDuration?: number
   dragOpacity?: number
   dragScroll?: boolean
-  eventOverlap?: boolean | ((stillEvent: EventObjectInput, movingEvent: EventObjectInput) => boolean)
+  eventOverlap?: boolean | ((stillEvent: EventApi, movingEvent: EventApi) => boolean)
   eventConstraint?: ConstraintInput
   eventAllow?: ((dropInfo: DropInfo, draggedEvent: Event) => boolean)
   longPressDelay?: number
@@ -236,26 +179,26 @@ export interface OptionsInputBase {
   viewDestroy?(arg: { view: View, el: HTMLElement }): void
   dayRender?(arg: { view: View, date: Date, isAllDay: boolean, el: HTMLElement }): void
   windowResize?(view: View): void
-  dayClick?(arg: { date: Date, isAllDay: boolean, resource, el: HTMLElement, jsEvent: MouseEvent, view: View }): void // resource for Scheduler
-  eventClick?(arg: { el: HTMLElement, event: EventObjectInput, jsEvent: MouseEvent, view: View }): boolean | void
-  eventMouseover?(arg: { el: HTMLElement, event: EventObjectInput, jsEvent: MouseEvent, view: View }): void
-  eventMouseout?(arg: { el: HTMLElement, event: EventObjectInput, jsEvent: MouseEvent, view: View }): void
-  select?(arg: { start: Date, end: Date, isAllDay: boolean, resource, jsEvent: MouseEvent, view: View }): void // resource for Scheduler
+  dayClick?(arg: { date: Date, isAllDay: boolean, resource: any, el: HTMLElement, jsEvent: MouseEvent, view: View }): void // resource for Scheduler
+  eventClick?(arg: { el: HTMLElement, event: EventApi, jsEvent: MouseEvent, view: View }): boolean | void
+  eventMouseover?(arg: { el: HTMLElement, event: EventApi, jsEvent: MouseEvent, view: View }): void
+  eventMouseout?(arg: { el: HTMLElement, event: EventApi, jsEvent: MouseEvent, view: View }): void
+  select?(arg: { start: Date, end: Date, isAllDay: boolean, resource: any, jsEvent: MouseEvent, view: View }): void // resource for Scheduler
   unselect?(arg: { view: View, jsEvent: Event }): void
-  eventDataTransform?(eventData: any): EventObjectInput
+  eventDataTransform?(eventData: any): EventInput
   loading?(isLoading: boolean, view: View): void
-  eventRender?(arg: { event: EventObjectInput, el: HTMLElement, view: View }): void
-  eventAfterRender?(arg: { event: EventObjectInput, el: HTMLElement, view: View }): void
+  eventRender?(arg: { event: EventApi, el: HTMLElement, view: View }): void
+  eventAfterRender?(arg: { event: EventApi, el: HTMLElement, view: View }): void
   eventAfterAllRender?(arg: { view: View }): void
-  eventDestroy?(arg: { event: EventObjectInput, el: HTMLElement, view: View }): void
-  eventDragStart?(arg: { event: EventObjectInput, el: HTMLElement, jsEvent: MouseEvent, view: View }): void
-  eventDragStop?(arg: { event: EventObjectInput, el: HTMLElement, jsEvent: MouseEvent, view: View }): void
-  eventDrop?(arg: { el: HTMLElement, event: EventObjectInput, delta: Duration, revertFunc: Function, jsEvent: Event, view: View }): void
-  eventResizeStart?(arg: { el: HTMLElement, event: EventObjectInput, jsEvent: MouseEvent, view: View }): void
-  eventResizeStop?(arg: { el: HTMLElement, event: EventObjectInput, jsEvent: MouseEvent, view: View }): void
-  eventResize?(arg: { el: HTMLElement, event: EventObjectInput, delta: Duration, revertFunc: Function, jsEvent: Event, view: View }): void
+  eventDestroy?(arg: { event: EventApi, el: HTMLElement, view: View }): void
+  eventDragStart?(arg: { event: EventApi, el: HTMLElement, jsEvent: MouseEvent, view: View }): void
+  eventDragStop?(arg: { event: EventApi, el: HTMLElement, jsEvent: MouseEvent, view: View }): void
+  eventDrop?(arg: { el: HTMLElement, event: EventApi, delta: Duration, revertFunc: Function, jsEvent: Event, view: View }): void
+  eventResizeStart?(arg: { el: HTMLElement, event: EventApi, jsEvent: MouseEvent, view: View }): void
+  eventResizeStop?(arg: { el: HTMLElement, event: EventApi, jsEvent: MouseEvent, view: View }): void
+  eventResize?(arg: { el: HTMLElement, event: EventApi, delta: Duration, revertFunc: Function, jsEvent: Event, view: View }): void
   drop?(arg: { date: DateInput, isAllDay: boolean, jsEvent: MouseEvent }): void
-  eventReceive?(event: EventObjectInput): void
+  eventReceive?(event: EventApi): void
 }
 
 export interface ViewOptionsInput extends OptionsInputBase {