forked from Meteor-Community-Packages/meteor-autoform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoform-events.js
505 lines (459 loc) · 18.4 KB
/
autoform-events.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
function beginSubmit(formId, template) {
if (!template || template._notInDOM)
return;
// Get user-defined hooks
var hooks = Hooks.getHooks(formId, 'beginSubmit');
if (hooks.length) {
_.each(hooks, function beginSubmitHooks(hook) {
hook(formId, template);
});
} else {
// If there are no user-defined hooks, by default we disable the submit button during submission
var submitButton = template.find("button[type=submit]") || template.find("input[type=submit]");
if (submitButton) {
submitButton.disabled = true;
}
}
}
function endSubmit(formId, template) {
if (!template || template._notInDOM)
return;
// Get user-defined hooks
var hooks = Hooks.getHooks(formId, 'endSubmit');
if (hooks.length) {
_.each(hooks, function endSubmitHooks(hook) {
hook(formId, template);
});
} else {
// If there are no user-defined hooks, by default we disable the submit button during submission
var submitButton = template.find("button[type=submit]") || template.find("input[type=submit]");
if (submitButton) {
submitButton.disabled = false;
}
}
}
Template.autoForm.events({
'submit form': function autoFormSubmitHandler(event, template) {
// Gather necessary form info
var formId = this.id || defaultFormId;
var data = formData[formId];
var isInsert = (data.submitType === "insert");
var isUpdate = (data.submitType === "update");
var isRemove = (data.submitType === "remove");
var isMethod = (data.submitType === "method");
var method = data.submitMethod;
var isNormalSubmit = (!isInsert && !isUpdate && !isRemove && !isMethod);
// ss will be the schema for the `schema` attribute if present,
// else the schema for the collection
var ss = data.ss;
var ssIsOverride = data.ssIsOverride;
var collection = data.collection;
var currentDoc = data.doc;
var docId = currentDoc ? currentDoc._id : null;
var isValid;
// Make sure we have a collection if we need one for the requested submit type
if (!collection) {
if (isInsert)
throw new Error("AutoForm: You must specify a collection when form type is insert.");
else if (isUpdate)
throw new Error("AutoForm: You must specify a collection when form type is update.");
else if (isRemove)
throw new Error("AutoForm: You must specify a collection when form type is remove.");
}
// Prevent browser form submission if we're planning to do our own thing
if (!isNormalSubmit) {
event.preventDefault();
}
// Gather hooks
var onSuccess = Hooks.getHooks(formId, 'onSuccess');
var onError = Hooks.getHooks(formId, 'onError');
// Prep haltSubmission function
function haltSubmission() {
event.preventDefault();
event.stopPropagation();
// Run endSubmit hooks (re-enabled submit button or form, etc.)
endSubmit(formId, template);
}
function failedValidation() {
selectFirstInvalidField(formId, ss, template);
var ec = ss.namedContext(formId);
var ik = ec.invalidKeys(), err;
if (ik) {
if (ik.length) {
// We add `message` prop to the invalidKeys.
// Maybe SS pkg should just add that property back in?
ik = _.map(ik, function (o) {
return _.extend({message: ec.keyErrorMessage(o.name)}, o);
});
err = new Error(ik[0].message);
} else {
err = new Error('form failed validation');
}
err.invalidKeys = ik;
} else {
err = new Error('form failed validation');
}
_.each(onError, function onErrorEach(hook) {
hook('pre-submit validation', err, template);
});
haltSubmission();
}
// Prep callback creator function
function makeCallback(name) {
var cbCtx = {
event: event,
template: template,
formId: formId,
resetForm: function () {
AutoForm.resetForm(formId, template);
}
};
var afterHooks = Hooks.getHooks(formId, 'after', name);
return function autoFormActionCallback(error, result) {
if (error) {
preventQueuedValidation();
selectFirstInvalidField(formId, ss, template);
_.each(onError, function onErrorEach(hook) {
hook.call(cbCtx, name, error, template);
});
} else {
// By default, we reset form after successful submit, but
// you can opt out.
if (data.resetOnSuccess !== false) {
AutoForm.resetForm(formId, template);
}
_.each(onSuccess, function onSuccessEach(hook) {
hook.call(cbCtx, name, result, template);
});
}
_.each(afterHooks, function afterHooksEach(hook) {
hook.call(cbCtx, error, result, template);
});
// Run endSubmit hooks (re-enabled submit button or form, etc.)
endSubmit(formId, template);
};
}
// Prep function that calls before hooks.
// We pass the template instance in case the hook
// needs the data context.
function doBefore(docId, doc, hooks, name, next) {
// We call the hooks recursively, in order added,
// passing the result of the first hook to the
// second hook, etc.
function runHook(i, doc) {
hook = hooks[i];
if (!hook) {
// We've run all hooks; continue submission
next(doc);
return;
}
// Set up before hook context
var cb = function (d) {
// If the hook returns false, we cancel
if (d === false) {
// Run endSubmit hooks (re-enabled submit button or form, etc.)
endSubmit(formId, template);
} else {
if (!_.isObject(d)) {
throw new Error(name + " must return an object");
}
runHook(i+1, d);
}
};
var ctx = {
event: event,
template: template,
formId: formId,
resetForm: function () {
AutoForm.resetForm(formId, template);
},
result: _.once(cb)
};
var result;
if (docId) {
result = hook.call(ctx, docId, doc, template);
} else {
result = hook.call(ctx, doc, template);
}
// If the hook returns undefined, we wait for it
// to call this.result()
if (result !== void 0) {
ctx.result(result);
}
}
runHook(0, doc);
}
// Prep function that calls onSubmit hooks.
// We pass the template instance in case the hook
// needs the data context, and event in case they
// need to prevent default, etc.
function doOnSubmit(hooks, insertDoc, updateDoc, currentDoc) {
// These are called differently from the before hooks because
// they run async, but they can run in parallel and we need the
// result of all of them immediately because they can return
// false to stop normal form submission.
var hookCount = hooks.length, doneCount = 0;
if (hookCount === 0) {
// Run endSubmit hooks (re-enabled submit button or form, etc.)
endSubmit(formId, template);
return;
}
// Set up onSubmit hook context
var ctx = {
event: event,
template: template,
formId: formId,
resetForm: function () {
AutoForm.resetForm(formId, template);
},
done: function () {
doneCount++;
if (doneCount === hookCount) {
// Run endSubmit hooks (re-enabled submit button or form, etc.)
endSubmit(formId, template);
}
}
};
// Call all hooks at once.
// Pass both types of doc plus the doc attached to the form.
// If any return false, we stop normal submission, but we don't
// run endSubmit hooks until they all call this.done().
var shouldStop = false;
_.each(hooks, function eachOnSubmit(hook) {
var result = hook.call(ctx, insertDoc, updateDoc, currentDoc);
if (shouldStop === false && result === false) {
shouldStop = true;
}
});
if (shouldStop) {
event.preventDefault();
event.stopPropagation();
}
}
// If type is "remove", do that right away since we don't need to gather
// form values or validate.
if (isRemove) {
// Run beginSubmit hooks (disable submit button or form, etc.)
beginSubmit(formId, template);
// Call beforeRemove hooks if present, and stop if any return false
var beforeRemove = Hooks.getHooks(formId, 'before', 'remove');
var shouldStop = _.any(beforeRemove, function eachBeforeRemove(hook) {
return (hook(docId, template) === false);
});
if (shouldStop) {
return haltSubmission();
}
var removeCallback = makeCallback('remove');
collection.remove(docId, removeCallback);
return;
}
// Gather all form values
var formDocs = getFormValues(template, formId, ss);
var insertDoc = formDocs.insertDoc;
var updateDoc = formDocs.updateDoc;
// Pre-validate
// For inserts and updates, which have their
// own validation, we validate here only if
// there is a `schema` attribute on the form.
// Otherwise we let collection2 do the validation
// after before hooks have run.
if (data.validationType !== 'none' && (ssIsOverride || isMethod || isNormalSubmit)) {
isValid = _validateForm(formId, data, formDocs);
// If we failed pre-submit validation, we stop submission.
if (isValid === false) {
return failedValidation();
}
// NOTE: For method forms when ssIsOverride, we will validate again, later, after before hooks
// but before calling the method, against the collection schema
}
// Run beginSubmit hooks (disable submit button or form, etc.)
// NOTE: This needs to stay after getFormValues in case a
// beginSubmit hook disables inputs. We don't get values for
// disabled inputs, but if they are just disabling during submission,
// then we actually do want the values.
beginSubmit(formId, template);
// Now we will do the requested insert, update, remove, method, or normal
// browser form submission. Even though we may have already validated above,
// we do it again upon insert or update
// because collection2 validation catches additional stuff like unique and
// because our form schema need not be the same as our collection schema.
var validationOptions = {
validationContext: formId,
filter: data.filter,
autoConvert: data.autoConvert,
removeEmptyStrings: data.removeEmptyStrings
};
// INSERT FORM SUBMIT
if (isInsert) {
// Get "before.insert" hooks
var beforeInsertHooks = Hooks.getHooks(formId, 'before', 'insert');
// Run "before.insert" hooks
doBefore(null, insertDoc, beforeInsertHooks, 'before.insert hook', function (doc) {
// Make callback for insert
var insertCallback = makeCallback('insert');
// Perform insert
if (typeof collection.simpleSchema === "function" && collection.simpleSchema() != null) {
// If the collection2 pkg is used and a schema is attached, we pass a validationContext
collection.insert(doc, validationOptions, insertCallback);
} else {
// If the collection2 pkg is not used or no schema is attached, we don't pass options
// because core Meteor's `insert` function does not accept
// an options argument.
collection.insert(doc, insertCallback);
}
});
}
// UPDATE FORM SUBMIT
else if (isUpdate) {
// Get "before.update" hooks
var beforeUpdateHooks = Hooks.getHooks(formId, 'before', 'update');
// Run "before.update" hooks
doBefore(docId, updateDoc, beforeUpdateHooks, 'before.update hook', function (modifier) {
// Make callback for update
var updateCallback = makeCallback('update');
if (_.isEmpty(modifier)) { // make sure this check stays after the before hooks
// Nothing to update. Just treat it as a successful update.
updateCallback(null, 0);
}
// Perform update
collection.update(docId, modifier, validationOptions, updateCallback);
});
}
// METHOD FORM SUBMIT
else if (isMethod) {
// Get "before.methodName" hooks
var beforeMethodHooks = Hooks.getHooks(formId, 'before', method);
// Run "before.methodName" hooks
doBefore(null, insertDoc, beforeMethodHooks, 'before.method hook', function (doc) {
// When both `schema` and `collection` are supplied, we do a
// second validation now, against the collection schema,
// before calling the method.
if (ssIsOverride) {
isValid = _validateForm(formId, data, formDocs, true);
if (isValid === false) {
return failedValidation();
}
}
// Make callback for Meteor.call
var methodCallback = makeCallback(method);
// Call the method
Meteor.call(method, doc, updateDoc, docId, methodCallback);
});
}
// NORMAL FORM SUBMIT
else if (isNormalSubmit) {
// Get onSubmit hooks
var onSubmitHooks = Hooks.getHooks(formId, 'onSubmit');
doOnSubmit(onSubmitHooks, insertDoc, updateDoc, currentDoc);
}
},
'keyup [data-schema-key]': function autoFormKeyUpHandler(event, template) {
var validationType = template.data.validation || 'submitThenKeyup';
var onlyIfAlreadyInvalid = (validationType === 'submitThenKeyup');
var skipEmpty = !(event.keyCode === 8 || event.keyCode === 46); //if deleting or backspacing, don't skip empty
if ((validationType === 'keyup' || validationType === 'submitThenKeyup')) {
validateField(event.currentTarget.getAttribute("data-schema-key"), template, skipEmpty, onlyIfAlreadyInvalid);
}
},
'blur [data-schema-key]': function autoFormBlurHandler(event, template) {
var validationType = template.data.validation || 'submitThenKeyup';
var onlyIfAlreadyInvalid = (validationType === 'submitThenKeyup' || validationType === 'submitThenBlur');
if (validationType === 'keyup' || validationType === 'blur' || validationType === 'submitThenKeyup' || validationType === 'submitThenBlur') {
validateField(event.currentTarget.getAttribute("data-schema-key"), template, false, onlyIfAlreadyInvalid);
}
},
'change form': function autoFormChangeHandler(event, template) {
var self = this;
var key = event.target.getAttribute("data-schema-key");
if (!key)
return;
var formId = self.id || defaultFormId;
var data = formData[formId];
if (!data)
return;
// Update cached form values for hot code reload persistence
formPreserve.registerForm(formId, function autoFormRegFormCallback() {
return getFormValues(template, formId, data.ss).insertDoc;
});
// Update field's value for reactive show/hide of other fields by value
updateTrackedFieldValue(formId, key, getFieldValue(template, key));
// If the form should be auto-saved whenever updated, we do that on field
// changes instead of validating the field
if (data.autosave) {
$(event.currentTarget).submit();
return;
}
var validationType = data.validationType || 'submitThenKeyup';
var onlyIfAlreadyInvalid = (validationType === 'submitThenKeyup' || validationType === 'submitThenBlur');
if (validationType === 'keyup' || validationType === 'blur' || validationType === 'submitThenKeyup' || validationType === 'submitThenBlur') {
validateField(key, template, false, onlyIfAlreadyInvalid);
}
},
'reset form': function autoFormResetHandler(event, template) {
var formId = this.id || defaultFormId;
formPreserve.unregisterForm(formId);
// Reset array counts
arrayTracker.resetForm(formId);
var fd = formData[formId];
if (!fd)
return;
if (fd.ss) {
fd.ss.namedContext(formId).resetValidation();
// If simpleSchema is undefined, we haven't yet rendered the form, and therefore
// there is no need to reset validation for it. No error need be thrown.
}
//XXX We should ideally be able to call invalidateFormContext
// in all cases and that's it, but we need to figure out how
// to make Blaze forget about any changes the user made to the form
if (this.doc) {
event.preventDefault();
invalidateFormContext(formId);
template.$("[autofocus]").focus();
} else {
// Update tracked field values
// This must be done after we allow this event handler to return
// because we have to let the browser reset all fields before we
// update their values for deps.
Meteor.setTimeout(function () {
updateAllTrackedFieldValues(formId);
if (template && !template._notInDOM) {
template.$("[autofocus]").focus();
}
}, 0);
}
},
'keydown .autoform-array-item input': function (event, template) {
// When enter is pressed in an array item field, default behavior
// seems to be to "click" the remove item button. This doesn't make
// sense so we stop it.
if (event.keyCode === 13) {
event.preventDefault();
}
},
'click .autoform-remove-item': function autoFormClickRemoveItem(event, template) {
var self = this; // This type of button must be used within an afEachArrayItem block, so we know the context
event.preventDefault();
var name = self.arrayFieldName;
var minCount = self.minCount; // optional, overrides schema
var maxCount = self.maxCount; // optional, overrides schema
var index = self.index;
var data = template.data;
var formId = data && data.id || defaultFormId;
var ss = formData[formId].ss;
// remove the item we clicked
arrayTracker.removeFromFieldAtIndex(formId, name, index, ss, minCount, maxCount);
},
'click .autoform-add-item': function autoFormClickAddItem(event, template) {
event.preventDefault();
// We pull from data attributes because the button could be manually
// added anywhere, so we don't know the data context.
var btn = $(event.currentTarget);
var name = btn.attr("data-autoform-field");
var minCount = btn.attr("data-autoform-minCount"); // optional, overrides schema
var maxCount = btn.attr("data-autoform-maxCount"); // optional, overrides schema
var data = template.data;
var formId = data && data.id || defaultFormId;
var ss = formData[formId].ss;
arrayTracker.addOneToField(formId, name, ss, minCount, maxCount);
}
});