Skip to content

Commit

Permalink
resetOptions handles events/plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
arshaw committed Mar 26, 2019
1 parent db0e3b6 commit a89ef7f
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 39 deletions.
102 changes: 73 additions & 29 deletions src/core/Calendar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import { Duration, createDuration } from './datelib/duration'
import reduce from './reducers/main'
import { parseDateSpan, DateSpanInput, DateSpan, buildDateSpanApi, DateSpanApi, buildDatePointApi, DatePointApi } from './structs/date-span'
import { memoize, memoizeOutput } from './util/memoize'
import { mapHash, isPropsEqual, anyKeysRemoved, computeChangedProps } from './util/object'
import { mapHash, isPropsEqual, anyKeysRemoved, computeChangedProps, hashValuesToArray, isObjsSimilar } from './util/object'
import { DateRangeInput } from './datelib/date-range'
import DateProfileGenerator from './DateProfileGenerator'
import { EventSourceInput, parseEventSource, EventSourceHash } from './structs/event-source'
import { EventSourceInput, parseEventSource, EventSourceHash, EventSource } from './structs/event-source'
import { EventInput, parseEvent, EventDefHash } from './structs/event'
import { CalendarState, Action } from './reducers/types'
import EventSourceApi from './api/EventSourceApi'
Expand Down Expand Up @@ -54,18 +54,6 @@ export type DateSpanTransform = (dateSpan: DateSpan, calendar: Calendar) => any
export type CalendarInteraction = { destroy() }
export type CalendarInteractionClass = { new(calendar: Calendar): CalendarInteraction }

const SPECIAL_OPTIONS = {
events(events, calendar) {
console.log('handle events', events, calendar)
},
eventSources(eventSources, calendar) {
console.log('handle eventSources', eventSources, calendar)
},
plugins(plugins, calendar) {
console.log('handle plugins', plugins, calendar)
}
}

export default class Calendar {

// global handler registry
Expand Down Expand Up @@ -137,12 +125,7 @@ export default class Calendar {
this.pluginSystem = new PluginSystem()

// only do once. don't do in handleOptions. because can't remove plugins
let pluginDefs = refinePluginDefs(
this.optionsManager.computed.plugins || []
)
for (let pluginDef of pluginDefs) {
this.pluginSystem.add(pluginDef)
}
this.addPluginInputs(this.optionsManager.computed.plugins || [])

this.handleOptions(this.optionsManager.computed)
this.publiclyTrigger('_init') // for tests
Expand All @@ -155,6 +138,15 @@ export default class Calendar {
}


addPluginInputs(pluginInputs) {
let pluginDefs = refinePluginDefs(pluginInputs)

for (let pluginDef of pluginDefs) {
this.pluginSystem.add(pluginDef)
}
}


// public API
get view(): View {
return this.component ? this.component.view : null
Expand Down Expand Up @@ -519,16 +511,19 @@ export default class Calendar {
}
}

if (anyKeysRemoved(oldNormalOptions, normalOptions)) {
this._setOptions(options, 'reset')
} else {
this._setOptions(computeChangedProps(oldNormalOptions, normalOptions))
}
this.batchRendering(() => { // if both _setOptions and special options want to rerender

// handle special options last
for (let name in specialOptions) {
SPECIAL_OPTIONS[name](specialOptions[name], this)
}
if (anyKeysRemoved(oldNormalOptions, normalOptions)) {
this._setOptions(options, 'reset')
} else {
this._setOptions(computeChangedProps(oldNormalOptions, normalOptions))
}

// handle special options last
for (let name in specialOptions) {
SPECIAL_OPTIONS[name](specialOptions[name], this)
}
})
}


Expand Down Expand Up @@ -1342,3 +1337,52 @@ function buildEventUiBases(eventDefs: EventDefHash, eventUiSingleBase: EventUi,

return eventUiBases
}



const SPECIAL_OPTIONS = {
events(events, calendar) {
handleEventSources([ events ], calendar)
},
eventSources: handleEventSources,
plugins: handlePlugins
}


function handleEventSources(inputs, calendar: Calendar) {
let unfoundSources: EventSource[] = hashValuesToArray(calendar.state.eventSources)
let newInputs = []

for (let input of inputs) {
let inputFound = false

for (let i = 0; i < unfoundSources.length; i++) {
if (isObjsSimilar(unfoundSources[i]._raw, input)) {
unfoundSources.splice(i, 1) // delete
inputFound = true
break
}
}

if (!inputFound) {
newInputs.push(input)
}
}

for (let unfoundSource of unfoundSources) {
calendar.dispatch({
type: 'REMOVE_EVENT_SOURCE',
sourceId: unfoundSource.sourceId
})
}

for (let newInput of newInputs) {
calendar.addEventSource(newInput)
}
}


// shortcoming: won't remove plugins
function handlePlugins(inputs, calendar: Calendar) {
calendar.addPluginInputs(inputs) // will gracefully handle duplicates
}
6 changes: 5 additions & 1 deletion src/core/structs/event-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export type EventSourceInput =
string // a URL for a JSON feed

export interface EventSource {
_raw: any
sourceId: string
sourceDefId: number // one of the few IDs that's a NUMBER not a string
meta: any
Expand Down Expand Up @@ -122,12 +123,15 @@ export function parseEventSource(raw: EventSourceInput, calendar: Calendar): Eve
let meta = def.parseMeta(raw)

if (meta) {
return parseEventSourceProps(
let res = parseEventSourceProps(
typeof raw === 'object' ? raw : {},
meta,
i,
calendar
)

res._raw = raw
return res
}
}

Expand Down
53 changes: 44 additions & 9 deletions src/core/util/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@ export function arrayToHash(a): { [key: string]: true } {
}


export function hashValuesToArray(obj) {
let a = []

for (let key in obj) {
a.push(obj[key])
}

return a
}


export function isPropsEqual(obj0, obj1): boolean {

for (let key in obj0) {
Expand Down Expand Up @@ -125,18 +136,42 @@ export function computeChangedProps(obj0, obj1) {
res[prop] = obj1[prop]

} else {
let val0 = obj0[prop]
let val1 = obj1[prop]

if (
val0 !== val1 &&
!(Array.isArray(val0) && Array.isArray(val1) && isArraysEqual(val0, val1)) &&
!(typeof val0 === 'object' && typeof val1 === 'object' && isPropsEqual(val0, val1))
) {
res[prop] = val1
if (!isValuesSimilar(obj0[prop], obj1[prop])) {
res[prop] = obj1[prop]
}
}
}

return res
}

// will go recursively one level deep
export function isObjsSimilar(obj0, obj1) {

for (let prop in obj0) {
if (!(prop in obj1)) {
return false
}
}

for (let prop in obj1) {

if (!(prop in obj0)) {
return false

} else {
if (!isValuesSimilar(obj0[prop], obj1[prop])) {
return false
}
}
}

return true
}


function isValuesSimilar(val0, val1) {
return val0 === val1 ||
(Array.isArray(val0) && Array.isArray(val1) && isArraysEqual(val0, val1)) ||
(typeof val0 === 'object' && typeof val1 === 'object' && isPropsEqual(val0, val1))
}

0 comments on commit a89ef7f

Please sign in to comment.