forked from Meteor-Community-Packages/meteor-autoform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoform-arrays.js
227 lines (185 loc) · 7.06 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
// 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.
ArrayTracker = function afArrayTracker() {
var self = this;
self.info = {};
};
ArrayTracker.prototype.getMinMax = function atGetMinMax(ss, field, overrideMinCount, overrideMaxCount) {
var defs = AutoForm.Utility.getDefs(ss, field);
// minCount is set by the schema, but can be set higher on the field attribute
overrideMinCount = overrideMinCount || 0;
var 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;
var maxCount = defs.maxCount || Infinity;
maxCount = Math.min(overrideMaxCount, maxCount);
return {minCount: minCount, maxCount: maxCount};
};
ArrayTracker.prototype.initForm = function atInitForm(formId) {
var self = this;
if (self.info[formId])
return;
self.info[formId] = {};
};
ArrayTracker.prototype.getForm = function atInitForm(formId) {
var self = this;
self.initForm(formId);
return self.info[formId];
};
ArrayTracker.prototype.ensureField = function atEnsureField(formId, field) {
var 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) {
var 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.
var range = self.getMinMax(ss, field, overrideMinCount, overrideMaxCount);
var arrayCount = Math.max(range.minCount, (docCount == null) ? 1 : docCount);
// If this is an array of objects, collect names of object props
var childKeys = [];
if (ss.schema(field + '.$').type === Object) {
childKeys = ss.objectKeys(SimpleSchema._makeGeneric(field) + '.$');
}
var loopArray = [];
for (var i = 0; i < arrayCount; i++) {
var loopCtx = createLoopCtx(formId, field, i, childKeys, overrideMinCount, overrideMaxCount);
loopArray.push(loopCtx);
};
self.info[formId][field].array = loopArray;
var 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) {
var self = this;
self.initForm(formId);
if (!self.info[formId][field]) {
self.info[formId][field] = {
deps: new Tracker.Dependency()
};
}
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) {
var self = this;
_.each(self.info[formId], function (info, field) {
self.resetField(formId, field);
});
};
ArrayTracker.prototype.untrackForm = function atUntrackForm(formId) {
var self = this;
self.info[formId] = {};
};
ArrayTracker.prototype.tracksField = function atTracksField(formId, field) {
var 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) {
var self = this;
self.ensureField(formId, field);
self.info[formId][field].deps.depend();
return self.info[formId][field].array;
};
ArrayTracker.prototype.getCount = function atGetCount(formId, field) {
var 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) {
var 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) {
var self = this;
self.ensureField(formId, field);
self.info[formId][field].deps.depend();
var firstVisibleField = _.find(self.info[formId][field].array, function(currentField) {
return !currentField.removed;
});
return (firstVisibleField && firstVisibleField.index === currentIndex);
};
ArrayTracker.prototype.isLastFieldlVisible = function atIsLastFieldlVisible(formId, field, currentIndex) {
var self = this;
self.ensureField(formId, field);
self.info[formId][field].deps.depend();
var lastVisibleField = _.last(_.filter(self.info[formId][field].array, function(currentField) {
return !currentField.removed;
}));
return (lastVisibleField && lastVisibleField.index === currentIndex);
};
ArrayTracker.prototype.addOneToField = function atAddOneToField(formId, field, ss, overrideMinCount, overrideMaxCount) {
var self = this;
self.ensureField(formId, field);
if (!self.info[formId][field].array) {
return;
}
var currentCount = self.info[formId][field].visibleCount
var maxCount = self.getMinMax(ss, field, overrideMinCount, overrideMaxCount).maxCount;
if (currentCount < maxCount) {
var i = self.info[formId][field].array.length;
// If this is an array of objects, collect names of object props
var childKeys = [];
if (ss.schema(field + '.$').type === Object) {
childKeys = ss.objectKeys(SimpleSchema._makeGeneric(field) + '.$');
}
var loopCtx = createLoopCtx(formId, field, i, childKeys, overrideMinCount, overrideMaxCount);
self.info[formId][field].array.push(loopCtx);
self.info[formId][field].count++;
self.info[formId][field].visibleCount++;
self.info[formId][field].deps.changed();
}
};
ArrayTracker.prototype.removeFromFieldAtIndex = function atRemoveFromFieldAtIndex(formId, field, index, ss, overrideMinCount, overrideMaxCount) {
var self = this;
self.ensureField(formId, field);
if (!self.info[formId][field].array) {
return;
}
var currentCount = self.info[formId][field].visibleCount;
var minCount = self.getMinMax(ss, field, overrideMinCount, overrideMaxCount).minCount;
if (currentCount > minCount) {
self.info[formId][field].array[index].removed = true;
self.info[formId][field].count--;
self.info[formId][field].visibleCount--;
self.info[formId][field].deps.changed();
}
}
/*
* PRIVATE
*/
var createLoopCtx = function(formId, field, index, childKeys, overrideMinCount, overrideMaxCount) {
var 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 = {};
_.each(childKeys, function (k) {
loopCtx.current[k] = field + '.' + index + '.' + k;
});
}
return loopCtx;
}