Selaa lähdekoodia

make ComponentContext a plain object

Adam Shaw 6 vuotta sitten
vanhempi
sitoutus
339fdc22d4

+ 2 - 6
packages/core/src/Calendar.ts

@@ -36,7 +36,7 @@ import EventHovering from './interactions/EventHovering'
 import StandardTheme from './theme/StandardTheme'
 import { CmdFormatterFunc } from './datelib/formatting-cmd'
 import { NamedTimeZoneImplClass } from './datelib/timezone'
-import ComponentContext from './component/ComponentContext'
+import { buildComponentContext } from './component/ComponentContext'
 
 export interface DateClickApi extends DatePointApi {
   dayEl: HTMLElement
@@ -437,6 +437,7 @@ export default class Calendar {
       eventDrag: state.eventDrag,
       eventResize: state.eventResize
     }, this.buildComponentContext(
+      this,
       this.theme,
       this.dateEnv,
       this.optionsManager.computed
@@ -1276,11 +1277,6 @@ EmitterMixin.mixInto(Calendar)
 // -----------------------------------------------------------------------------------------------------------------
 
 
-function buildComponentContext(this: Calendar, theme: Theme, dateEnv: DateEnv, options) {
-  return new ComponentContext(this, theme, dateEnv, options, null)
-}
-
-
 function buildDateEnv(locale: Locale, timeZone, namedTimeZoneImpl: NamedTimeZoneImplClass, firstDay, weekNumberCalculation, weekLabel, cmdFormatter: CmdFormatterFunc) {
   return new DateEnv({
     calendarSystem: 'gregory', // TODO: make this a setting

+ 2 - 2
packages/core/src/CalendarComponent.ts

@@ -1,5 +1,5 @@
 import Component from './component/Component'
-import ComponentContext from './component/ComponentContext'
+import ComponentContext, { extendComponentContext } from './component/ComponentContext'
 import { ViewSpec } from './structs/view-spec'
 import View from './View'
 import Toolbar from './Toolbar'
@@ -380,7 +380,7 @@ function computeTitleFormat(dateProfile) {
 
 // build a context scoped to the view
 function buildComponentContext(context: ComponentContext, viewSpec: ViewSpec, view: View) {
-  return context.extend(viewSpec.options, view)
+  return extendComponentContext(context, viewSpec.options, view)
 }
 
 

+ 32 - 22
packages/core/src/component/ComponentContext.ts

@@ -6,34 +6,44 @@ import { parseFieldSpecs } from '../util/misc'
 import { createDuration, Duration } from '../datelib/duration'
 
 
-export default class ComponentContext {
-
+export default interface ComponentContext {
+  calendar: Calendar
+  view?: View
+  dateEnv: DateEnv
+  theme: Theme
+  options: any
   isRtl: boolean
   eventOrderSpecs: any
   nextDayThreshold: Duration
+}
 
-  // TODO: move plugin system into here
 
-  constructor(
-    public calendar: Calendar,
-    public theme: Theme,
-    public dateEnv: DateEnv,
-    public options: any,
-    public view?: View
-  ) {
-    this.isRtl = options.dir === 'rtl'
-    this.eventOrderSpecs = parseFieldSpecs(options.eventOrder)
-    this.nextDayThreshold = createDuration(options.nextDayThreshold)
+export function buildComponentContext(
+  calendar: Calendar,
+  theme: Theme,
+  dateEnv: DateEnv,
+  options: any,
+  view?: View
+): ComponentContext {
+  return {
+    calendar,
+    view,
+    dateEnv,
+    theme,
+    options,
+    isRtl: options.dir === 'rtl',
+    eventOrderSpecs: parseFieldSpecs(options.eventOrder),
+    nextDayThreshold: createDuration(options.nextDayThreshold)
   }
+}
 
-  extend(options?: any, view?: View) {
-    return new ComponentContext(
-      this.calendar,
-      this.theme,
-      this.dateEnv,
-      options || this.options,
-      view || this.view
-    )
-  }
 
+export function extendComponentContext(context: ComponentContext, options?: any, view?: View): ComponentContext {
+  return buildComponentContext(
+    context.calendar,
+    context.theme,
+    context.dateEnv,
+    options || context.options,
+    view || context.view
+  )
 }