-
Notifications
You must be signed in to change notification settings - Fork 233
/
ResourceApi.ts
205 lines (168 loc) · 5.29 KB
/
ResourceApi.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import { CalendarContext, EventApi, Dictionary } from '@fullcalendar/common'
import { __assign } from 'tslib'
import { Resource, getPublicId, ResourceHash } from '../structs/resource'
export class ResourceApi {
constructor(
private _context: CalendarContext,
public _resource: Resource,
) {
}
setProp(name: string, value: any) {
let oldResource = this._resource
this._context.dispatch({
type: 'SET_RESOURCE_PROP',
resourceId: oldResource.id,
propName: name,
propValue: value,
})
this.sync(oldResource)
}
setExtendedProp(name: string, value: any) {
let oldResource = this._resource
this._context.dispatch({
type: 'SET_RESOURCE_EXTENDED_PROP',
resourceId: oldResource.id,
propName: name,
propValue: value,
})
this.sync(oldResource)
}
private sync(oldResource: Resource) {
let context = this._context
let resourceId = oldResource.id
// TODO: what if dispatch didn't complete synchronously?
this._resource = context.getCurrentData().resourceStore[resourceId]
context.emitter.trigger('resourceChange', {
oldResource: new ResourceApi(context, oldResource),
resource: this,
revert() {
context.dispatch({
type: 'ADD_RESOURCE', // function as a merge. TODO: rename
resourceHash: {
[resourceId]: oldResource,
},
})
},
})
}
remove() {
let context = this._context
let internalResource = this._resource
let resourceId = internalResource.id
context.dispatch({
type: 'REMOVE_RESOURCE',
resourceId,
})
context.emitter.trigger('resourceRemove', {
resource: this,
revert() {
context.dispatch({
type: 'ADD_RESOURCE', // function as a merge. TODO: rename
resourceHash: {
[resourceId]: internalResource,
},
})
},
})
}
getParent(): ResourceApi | null {
let context = this._context
let parentId = this._resource.parentId
if (parentId) {
return new ResourceApi(
context,
context.getCurrentData().resourceSource[parentId],
)
}
return null
}
getChildren(): ResourceApi[] {
let thisResourceId = this._resource.id
let context = this._context
let { resourceStore } = context.getCurrentData()
let childApis: ResourceApi[] = []
for (let resourceId in resourceStore) {
if (resourceStore[resourceId].parentId === thisResourceId) {
childApis.push(
new ResourceApi(context, resourceStore[resourceId]),
)
}
}
return childApis
}
/*
this is really inefficient!
TODO: make EventApi::resourceIds a hash or keep an index in the Calendar's state
*/
getEvents(): EventApi[] {
let thisResourceId = this._resource.id
let context = this._context
let { defs, instances } = context.getCurrentData().eventStore
let eventApis: EventApi[] = []
for (let instanceId in instances) {
let instance = instances[instanceId]
let def = defs[instance.defId]
if (def.resourceIds.indexOf(thisResourceId) !== -1) { // inefficient!!!
eventApis.push(new EventApi(context, def, instance))
}
}
return eventApis
}
get id() { return getPublicId(this._resource.id) }
get title() { return this._resource.title }
get eventConstraint() { return this._resource.ui.constraints[0] || null }
get eventOverlap() { return this._resource.ui.overlap }
get eventAllow() { return this._resource.ui.allows[0] || null }
get eventBackgroundColor() { return this._resource.ui.backgroundColor }
get eventBorderColor() { return this._resource.ui.borderColor }
get eventTextColor() { return this._resource.ui.textColor }
// NOTE: user can't modify these because Object.freeze was called in event-def parsing
get eventClassNames() { return this._resource.ui.classNames }
get extendedProps() { return this._resource.extendedProps }
toPlainObject(settings: { collapseExtendedProps?: boolean, collapseEventColor?: boolean } = {}) {
let internal = this._resource
let { ui } = internal
let publicId = this.id
let res: Dictionary = {}
if (publicId) {
res.id = publicId
}
if (internal.title) {
res.title = internal.title
}
if (settings.collapseEventColor && ui.backgroundColor && ui.backgroundColor === ui.borderColor) {
res.eventColor = ui.backgroundColor
} else {
if (ui.backgroundColor) {
res.eventBackgroundColor = ui.backgroundColor
}
if (ui.borderColor) {
res.eventBorderColor = ui.borderColor
}
}
if (ui.textColor) {
res.eventTextColor = ui.textColor
}
if (ui.classNames.length) {
res.eventClassNames = ui.classNames
}
if (Object.keys(internal.extendedProps).length) {
if (settings.collapseExtendedProps) {
__assign(res, internal.extendedProps)
} else {
res.extendedProps = internal.extendedProps
}
}
return res
}
toJSON() {
return this.toPlainObject()
}
}
export function buildResourceApis(resourceStore: ResourceHash, context: CalendarContext) {
let resourceApis: ResourceApi[] = []
for (let resourceId in resourceStore) {
resourceApis.push(new ResourceApi(context, resourceStore[resourceId]))
}
return resourceApis
}