forked from Meteor-Community-Packages/meteor-autoform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoform-arrays.js
316 lines (268 loc) · 8.21 KB
/
autoform-arrays.js
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import { Tracker } from 'meteor/tracker'
import { Mongo } from 'meteor/mongo'
import { Utility } from './utility'
/**
* Track arrays; this allows us to add/remove fields or groups of fields for an array
* but still easily respect minCount and maxCount, and properly add/remove the same
* items from the database once the form is submitted.
* @constructor
*/
export const ArrayTracker = function afArrayTracker () {
const self = this
self.info = {}
}
ArrayTracker.prototype.getMinMax = function atGetMinMax (
ss,
field,
overrideMinCount,
overrideMaxCount
) {
const defs = Utility.getFieldDefinition(ss, field)
// minCount is set by the schema, but can be set higher on the field attribute
overrideMinCount = overrideMinCount || 0
let minCount = defs.minCount || 0
minCount = Math.max(overrideMinCount, minCount)
// maxCount is set by the schema, but can be set lower on the field attribute
overrideMaxCount = overrideMaxCount || Infinity
let maxCount = defs.maxCount || Infinity
maxCount = Math.min(overrideMaxCount, maxCount)
return { minCount: minCount, maxCount: maxCount }
}
ArrayTracker.prototype.initForm = function atInitForm (formId) {
const self = this
if (self.info[formId]) return
self.info[formId] = {}
}
ArrayTracker.prototype.getForm = function atInitForm (formId) {
const self = this
self.initForm(formId)
return self.info[formId]
}
ArrayTracker.prototype.ensureField = function atEnsureField (formId, field) {
const self = this
self.initForm(formId)
if (!self.info[formId][field]) self.resetField(formId, field)
}
ArrayTracker.prototype.initField = function atInitField (
formId,
field,
ss,
docCount,
overrideMinCount,
overrideMaxCount
) {
const self = this
self.ensureField(formId, field)
if (self.info[formId][field].array != null) return
// If we have a doc: The count should be the maximum of docCount or schema minCount or field minCount or 1.
// If we don't have a doc: The count should be the maximum of schema minCount or field minCount or 1.
const range = self.getMinMax(ss, field, overrideMinCount, overrideMaxCount)
const arrayCount = Math.max(range.minCount, docCount == null ? 1 : docCount)
// If this is an array of objects, collect names of object props
let childKeys = []
if (Utility.getFieldDefinition(ss, `${field}.$`).type === Object) {
const genericKey = Utility.makeKeyGeneric(field)
childKeys = ss.objectKeys(`${genericKey}.$`)
}
const collection = new Mongo.Collection(null)
const loopArray = []
for (let i = 0; i < arrayCount; i++) {
const loopCtx = createLoopCtx(
formId,
field,
i,
childKeys,
overrideMinCount,
overrideMaxCount
)
loopArray.push(loopCtx)
collection.insert(loopCtx)
}
self.info[formId][field].collection = collection
self.info[formId][field].array = loopArray
const count = loopArray.length
self.info[formId][field].count = count
self.info[formId][field].visibleCount = count
self.info[formId][field].deps.changed()
}
ArrayTracker.prototype.resetField = function atResetField (formId, field) {
const self = this
self.initForm(formId)
if (!self.info[formId][field]) {
self.info[formId][field] = {
deps: new Tracker.Dependency()
}
}
if (self.info[formId][field].collection) {
self.info[formId][field].collection.remove({})
}
self.info[formId][field].array = null
self.info[formId][field].count = 0
self.info[formId][field].visibleCount = 0
self.info[formId][field].deps.changed()
}
ArrayTracker.prototype.resetForm = function atResetForm (formId) {
const self = this
Object.keys(self.info[formId] || {}).forEach(function (field) {
self.resetField(formId, field)
})
}
ArrayTracker.prototype.untrackForm = function atUntrackForm (formId) {
const self = this
if (self.info[formId]) {
Object.keys(self.info[formId]).forEach((field) => {
if (self.info[formId][field].collection) {
self.info[formId][field].collection.remove({})
}
})
}
self.info[formId] = {}
}
ArrayTracker.prototype.tracksField = function atTracksField (formId, field) {
const self = this
self.ensureField(formId, field)
self.info[formId][field].deps.depend()
return !!self.info[formId][field].array
}
ArrayTracker.prototype.getField = function atGetField (formId, field) {
const self = this
self.ensureField(formId, field)
self.info[formId][field].deps.depend()
return self.info[formId][field].collection.find({})
}
ArrayTracker.prototype.getCount = function atGetCount (formId, field) {
const self = this
self.ensureField(formId, field)
self.info[formId][field].deps.depend()
return self.info[formId][field].count
}
ArrayTracker.prototype.getVisibleCount = function atGetVisibleCount (
formId,
field
) {
const self = this
self.ensureField(formId, field)
self.info[formId][field].deps.depend()
return self.info[formId][field].visibleCount
}
ArrayTracker.prototype.isFirstFieldlVisible = function atIsFirstFieldlVisible (
formId,
field,
currentIndex
) {
const self = this
self.ensureField(formId, field)
self.info[formId][field].deps.depend()
const firstVisibleField = self.info[formId][field].array.find(function (
currentField
) {
return !currentField.removed
})
return firstVisibleField && firstVisibleField.index === currentIndex
}
ArrayTracker.prototype.isLastFieldlVisible = function atIsLastFieldlVisible (
formId,
field,
currentIndex
) {
const self = this
self.ensureField(formId, field)
self.info[formId][field].deps.depend()
const lastVisibleField = self.info[formId][field].array
.filter(function (currentField) {
return !currentField.removed
})
.pop()
return lastVisibleField && lastVisibleField.index === currentIndex
}
ArrayTracker.prototype.addOneToField = function atAddOneToField (
formId,
field,
ss,
overrideMinCount,
overrideMaxCount
) {
const self = this
self.ensureField(formId, field)
if (!self.info[formId][field].array) return
const currentCount = self.info[formId][field].visibleCount
const maxCount = self.getMinMax(ss, field, overrideMinCount, overrideMaxCount)
.maxCount
if (currentCount < maxCount) {
const i = self.info[formId][field].array.length
// If this is an array of objects, collect names of object props
let childKeys = []
if (Utility.getFieldDefinition(ss, `${field}.$`).type === Object) {
const genericKey = Utility.makeKeyGeneric(field)
childKeys = ss.objectKeys(`${genericKey}.$`)
}
const loopCtx = createLoopCtx(
formId,
field,
i,
childKeys,
overrideMinCount,
overrideMaxCount
)
self.info[formId][field].collection.insert(loopCtx)
self.info[formId][field].array.push(loopCtx)
self.info[formId][field].count++
self.info[formId][field].visibleCount++
self.info[formId][field].deps.changed()
AutoForm.resetValueCache(formId, field)
}
}
ArrayTracker.prototype.removeFromFieldAtIndex = function atRemoveFromFieldAtIndex (
formId,
field,
index,
ss,
overrideMinCount,
overrideMaxCount
) {
const self = this
self.ensureField(formId, field)
if (!self.info[formId][field].array) return
const currentCount = self.info[formId][field].visibleCount
const minCount = self.getMinMax(ss, field, overrideMinCount, overrideMaxCount)
.minCount
if (currentCount > minCount) {
self.info[formId][field].collection.update(
{ index: index },
{ $set: { removed: true } }
)
self.info[formId][field].array[index].removed = true
self.info[formId][field].count--
self.info[formId][field].visibleCount--
self.info[formId][field].deps.changed()
AutoForm.resetValueCache(formId, field)
}
}
/*
* PRIVATE
*/
const createLoopCtx = function (
formId,
field,
index,
childKeys,
overrideMinCount,
overrideMaxCount
) {
const loopCtx = {
formId: formId,
arrayFieldName: field,
name: field + '.' + index,
index: index,
minCount: overrideMinCount,
maxCount: overrideMaxCount
}
// If this is an array of objects, add child key names under loopCtx.current[childName] = fullKeyName
if (childKeys.length) {
loopCtx.current = {}
childKeys.forEach(function (k) {
loopCtx.current[k] = field + '.' + index + '.' + k
})
}
return loopCtx
}