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

EventSourceApi, more Calendar methods

Adam Shaw 7 лет назад
Родитель
Сommit
c023d4c64e
5 измененных файлов с 124 добавлено и 34 удалено
  1. 81 30
      src/Calendar.ts
  2. 5 0
      src/api/EventApi.ts
  3. 22 0
      src/api/EventSourceApi.ts
  4. 5 4
      src/common/browser-context.ts
  5. 11 0
      src/util/object.ts

+ 81 - 30
src/Calendar.ts

@@ -19,7 +19,7 @@ import { Duration, createDuration } from './datelib/duration'
 import reduce from './reducers/main'
 import { parseDateSpan, DateSpanInput, DateSpan } from './structs/date-span'
 import reselector from './util/reselector'
-import { assignTo } from './util/object'
+import { assignTo, objectValues } from './util/object'
 import { RenderForceFlags } from './component/Component'
 import browserContext from './common/browser-context'
 import { DateRangeInput, rangeContainsMarker } from './datelib/date-range'
@@ -27,6 +27,9 @@ import { DateProfile } from './DateProfileGenerator'
 import { EventSourceInput, parseEventSource } from './structs/event-source'
 import { EventInput } from './structs/event'
 import { CalendarState, Action } from './reducers/types'
+import EventSourceApi from './api/EventSourceApi'
+import EventApi from './api/EventApi'
+import { parseEventStore } from './structs/event-store'
 
 
 export default class Calendar {
@@ -1034,25 +1037,66 @@ export default class Calendar {
   // -----------------------------------------------------------------------------------------------------------------
 
 
-  rerenderEvents() { // API method. destroys old events if previously rendered.
-    this.requestRerender({ events: true }) // TODO: test this
+  addEvent(eventInput: EventInput, isSticky: boolean = false): EventApi | null {
+    let subset = parseEventStore([ eventInput ], '', this.state.dateProfile.activeRange, this)
+
+    this.dispatch({
+      type: 'ADD_EVENTS',
+      eventStore: subset,
+      stick: isSticky // TODO: do something with this
+    })
+
+    let def = objectValues(subset.defs)[0]
+    let instances = objectValues(subset.instances)
+
+    if (def) {
+      return new EventApi(
+        this,
+        def,
+        instances.length === 1 ? instances[0] : null
+      )
+    }
+
+    return null
   }
 
 
-  renderEvent(eventInput: EventInput, isSticky: boolean = false) {
-    // TODO
+  // TODO: optimize
+  getEventById(id: string): EventApi | null {
+    let { defs, instances } = this.state.eventStore
+
+    id = String(id)
+
+    for (let id in instances) {
+      let instance = instances[id]
+      let def = defs[instance.defId]
+
+      if (def.publicId === id) {
+        return new EventApi(this, def, instance)
+      }
+    }
+
+    return null
   }
 
 
-  // legacyQuery operates on legacy event instance objects
-  removeEvents(legacyQuery) {
-    // TODO
+  getEvents(): EventApi[] {
+    let { defs, instances } = this.state.eventStore
+    let eventApis: EventApi[] = []
+
+    for (let id in instances) {
+      let instance = instances[id]
+      let def = defs[instance.defId]
+
+      eventApis.push(new EventApi(this, def, instance))
+    }
+
+    return eventApis
   }
 
 
-  // legacyQuery operates on legacy event instance objects
-  clientEvents(legacyQuery) {
-    // TODO
+  rerenderEvents() { // API method. destroys old events if previously rendered.
+    this.requestRerender({ events: true }) // TODO: test this
   }
 
 
@@ -1060,31 +1104,43 @@ export default class Calendar {
   // -----------------------------------------------------------------------------------------------------------------
 
 
-  getEventSources(): EventSource[] {
-    return null // TODO
-  }
+  getEventSources(): EventSourceApi[] {
+    let sourceHash = this.state.eventSources
+    let sourceApis: EventSourceApi[] = []
 
+    for (let internalId in sourceHash) {
+      sourceApis.push(new EventSourceApi(this, sourceHash[internalId]))
+    }
 
-  getEventSourceById(id): EventSource | null {
-    return null // TODO
+    return sourceApis
   }
 
 
-  addEventSource(sourceInput: EventSourceInput) {
-    let eventSource = parseEventSource(sourceInput)
-    if (eventSource) { // TODO: error otherwise?
-      this.dispatch({ type: 'ADD_EVENT_SOURCES', sources: [ eventSource ] })
+  getEventSourceById(id: string): EventSourceApi | null {
+    let sourceHash = this.state.eventSources
+
+    id = String(id)
+
+    for (let internalId in sourceHash) {
+      if (sourceHash[internalId].publicId === id) {
+        return new EventSourceApi(this, sourceHash[internalId])
+      }
     }
+
+    return null
   }
 
 
-  removeEventSources(sourceMultiQuery) {
-    // TODO
-  }
+  addEventSource(sourceInput: EventSourceInput): EventSourceApi {
+    let eventSource = parseEventSource(sourceInput)
+
+    if (eventSource) { // TODO: error otherwise?
+      this.dispatch({ type: 'ADD_EVENT_SOURCES', sources: [ eventSource ] })
 
+      return new EventSourceApi(this, eventSource)
+    }
 
-  removeEventSource(sourceQuery) {
-    // TODO
+    return null
   }
 
 
@@ -1093,11 +1149,6 @@ export default class Calendar {
   }
 
 
-  refetchEventSources(sourceMultiQuery) {
-    // TODO
-  }
-
-
 }
 
 EmitterMixin.mixIntoObj(Calendar) // for global registry

+ 5 - 0
src/api/EventApi.ts

@@ -13,4 +13,9 @@ export default class EventApi {
     this.instance = instance || null
   }
 
+
+  remove() {
+    // TODO
+  }
+
 }

+ 22 - 0
src/api/EventSourceApi.ts

@@ -0,0 +1,22 @@
+import Calendar from '../Calendar'
+import { EventSource } from '../structs/event-source'
+
+export default class EventSourceApi {
+
+  calendar: Calendar
+  internalEventSource: EventSource
+
+  constructor(calendar: Calendar, internalEventSource: EventSource) {
+    this.calendar = calendar
+    this.internalEventSource = internalEventSource
+  }
+
+  remove() {
+    // TODO
+  }
+
+  refetch() {
+    // TODO
+  }
+
+}

+ 5 - 4
src/common/browser-context.ts

@@ -92,22 +92,25 @@ export class BrowserContext {
         type: 'UNSELECT_DATES'
       })
 
+      this.dateSelectedCalendar = null // in case publicTrigger wants to reselect
+
       dateSelectedCalendar.publiclyTrigger('unselect', [
         {
           jsEvent: pev ? pev.origEvent : null,
           view: dateSelectedCalendar.view
         }
       ])
-
-      this.dateSelectedCalendar = null
     }
   }
 
   reportDateSelection(calendar: Calendar, selection: DateSpan, pev?: PointerDragEvent) {
+
     if (this.dateSelectedCalendar && this.dateSelectedCalendar !== calendar) {
       this.unselectDates(pev)
     }
 
+    this.dateSelectedCalendar = calendar // in case publicTrigger wants to unselect
+
     calendar.publiclyTrigger('select', [
       {
         start: calendar.dateEnv.toDate(selection.range.start),
@@ -117,8 +120,6 @@ export class BrowserContext {
         view: calendar.view
       }
     ])
-
-    this.dateSelectedCalendar = calendar
   }
 
   unselectEvent() {

+ 11 - 0
src/util/object.ts

@@ -112,3 +112,14 @@ export function arrayToHash(a): { [key: string]: true } {
 
   return hash
 }
+
+
+export function objectValues(obj) {
+  let vals = []
+
+  for (let key in obj) {
+    vals.push(obj[key])
+  }
+
+  return vals
+}