forked from Meteor-Community-Packages/meteor-autoform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoform-api.js
1526 lines (1356 loc) · 48.6 KB
/
autoform-api.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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* global $ deps globalDefaultTemplate defaultTypeTemplates arrayTracker setDefaults */
import { Meteor } from 'meteor/meteor'
import { Tracker } from 'meteor/tracker'
import { Template } from 'meteor/templating'
import { ReactiveVar } from 'meteor/reactive-var'
import { Blaze } from 'meteor/blaze'
import MongoObject from 'mongo-object'
import { isObject } from './common'
import {
getAllFieldsInForm,
getFlatDocOfFieldValues,
markChanged
} from './autoform-inputs'
import { validateField } from './autoform-validation'
import { Hooks } from './autoform-hooks'
import { Utility } from './utility'
// This file defines the public, exported API
AutoForm = AutoForm || {} // eslint-disable-line no-global-assign
/**
* @property AutoForm.Utility
* @public
*/
AutoForm.Utility = Utility
/**
* @method AutoForm.addHooks
* @public
* @param {String[]|String|null} formIds Form `id` or array of form IDs to which these hooks apply. Specify `null` to add hooks that will run for every form.
* @param {Object} hooks Hooks to add, where supported names are "before", "after", "formToDoc", "docToForm", "onSubmit", "onSuccess", and "onError".
* @param {Boolean} replace Replaces all existing hooks with the ones in {hooks}, if true
* @returns {undefined}
*
* Defines hooks to be used by one or more forms. Extends hooks lists if called multiple times for the same
* form.
*/
AutoForm.addHooks = function autoFormAddHooks (formIds, hooks, replace) {
if (typeof formIds === 'string') {
formIds = [formIds]
}
// If formIds is null, add global hooks
if (!formIds) {
Hooks.addHooksToList(Hooks.global, hooks, replace)
}
else {
formIds.forEach(function (formId) {
// Init the hooks object if not done yet
Hooks.form[formId] = Hooks.form[formId] || Hooks.getDefault()
Hooks.addHooksToList(Hooks.form[formId], hooks, replace)
})
}
}
/**
* @method AutoForm.hooks
* @public
* @param {Object} hooks Hooks to add, where supported names are "before", "after", "formToDoc", "docToForm", "onSubmit", "onSuccess", and "onError".
* @param {Boolean} replace Replaces all existing hooks with the ones in {hooks}, if true
* @returns {undefined}
*
* Defines hooks by form id. Extends hooks lists if called multiple times for the same
* form.
*/
AutoForm.hooks = function autoFormHooks (hooks, replace) {
Object.entries(hooks).forEach(function ([formId, hooksObj]) {
AutoForm.addHooks(formId, hooksObj, replace)
})
}
/**
* @property AutoForm._hooks
* @public
*
* Hooks list to aid automated testing
*/
Object.defineProperty(AutoForm, '_hooks', {
get: function () {
return Hooks.form
}
})
/**
* @property AutoForm._globalHooks
* @public
*
* Global hooks list to aid automated testing
*/
Object.defineProperty(AutoForm, '_globalHooks', {
get: function () {
return Hooks.global
}
})
/**
* @method AutoForm._forceResetFormValues
* @public
* @param {String} formId
* @returns {undefined}
*
* Forces an AutoForm"s values to properly update.
* See https://github.com/meteor/meteor/issues/2431
*/
AutoForm._forceResetFormValues = function autoFormForceResetFormValues (formId) {
AutoForm._destroyForm[formId] =
AutoForm._destroyForm[formId] || new ReactiveVar(false)
AutoForm._destroyForm[formId].set(true)
setTimeout(function () {
AutoForm._destroyForm[formId].set(false)
}, 0)
}
/**
* @method AutoForm.resetForm
* @public
* @param {String} formId
* @param {TemplateInstance} [template] Looked up if not provided. Pass in for efficiency.
* @returns {undefined}
*
* Resets an autoform, including resetting validation errors. The same as clicking the reset button for an autoform.
*/
AutoForm.resetForm = function autoFormResetForm (formId, template) {
template = template || AutoForm.templateInstanceForForm(formId)
if (!Utility.checkTemplate(template)) return
template.$('form')[0].reset()
}
/**
* @method AutoForm.setDefaultTemplate
* @public
* @param {String} template
*/
AutoForm.setDefaultTemplate = function autoFormSetDefaultTemplate (template) {
globalDefaultTemplate = template // eslint-disable-line no-global-assign
deps.defaultTemplate.changed()
}
/**
* @method AutoForm.getDefaultTemplate
* @public
*
* Reactive.
*/
AutoForm.getDefaultTemplate = function autoFormGetDefaultTemplate () {
deps.defaultTemplate.depend()
return globalDefaultTemplate
}
/**
* @method AutoForm.setDefaultTemplateForType
* @public
* @param {String} type
* @param {String} template
*/
AutoForm.setDefaultTemplateForType = function autoFormSetDefaultTemplateForType (
type,
template
) {
if (!deps.defaultTypeTemplates[type]) {
deps.defaultTypeTemplates[type] = new Tracker.Dependency()
}
if (template !== null && !Template[`${type}_${template}`]) {
throw new Error(
`setDefaultTemplateForType can't set default template to "${template}" for type "${type}" because there is no defined template with the name "${type}_${template}"`
)
}
defaultTypeTemplates[type] = template
deps.defaultTypeTemplates[type].changed()
}
/**
* @method AutoForm.getDefaultTemplateForType
* @public
* @param {String} type
* @return {String} Template name
*
* Reactive.
*/
AutoForm.getDefaultTemplateForType = function autoFormGetDefaultTemplateForType (
type
) {
if (!deps.defaultTypeTemplates[type]) {
deps.defaultTypeTemplates[type] = new Tracker.Dependency()
}
deps.defaultTypeTemplates[type].depend()
return defaultTypeTemplates[type]
}
/**
* @private Use this to glue type and template into a valid type_template string.
*/
const toTypeTemplate = (type, template) => `${type}_${template}`
/**
* @method AutoForm.getTemplateName
* @public
* @param {String} templateType
* @param {String} templateName
* @param {String} [fieldName]
* @param {Boolean} [skipExistsCheck] Pass `true` to return a template name even if that template hasn"t been defined.
* @return {String} Template name
*
* Returns the full template name. In the simplest scenario, this is templateType_templateName
* as passed in. However, if templateName is not provided, it is looked up in the following
* manner:
*
* 1. autoform.<componentType>.template from the schema (field+type override for all forms)
* 2. autoform.template from the schema (field override for all forms)
* 3. template-<componentType> attribute on an ancestor component within the same form (form+type for all fields)
* 4. template attribute on an ancestor component within the same form (form specificity for all types and fields)
* 5. Default template for component type, as set by AutoForm.setDefaultTemplateForType
* 6. Default template, as set by AutoForm.setDefaultTemplate.
* 7. Built-in default template, currently bootstrap-3.
*/
AutoForm.getTemplateName = function autoFormGetTemplateName (
templateType,
templateName,
fieldName,
skipExistsCheck
) {
function templateExists (t) {
return !!(skipExistsCheck || Template[t])
}
// Default case: use the `template` attribute provided
const providedTemplate = toTypeTemplate(templateType, templateName)
if (templateName && templateExists(providedTemplate)) {
return providedTemplate
}
// If the attributes provided a templateName but that template didn"t exist, show a warning
if (templateName && AutoForm._debug) {
console.warn(
`${templateType}: "${templateName}" is not a valid template name. Falling back to a different template.`
)
}
// Get `autoform` object from the schema, if present.
// Skip for quickForm because it renders a form and not a field.
let schemaAutoFormDefs
if (templateType !== 'quickForm' && fieldName) {
const fieldSchema = AutoForm.getSchemaForField(fieldName)
schemaAutoFormDefs = fieldSchema && fieldSchema.autoform
}
// Fallback #1: autoform.<componentType>.template from the schema
if (
schemaAutoFormDefs &&
schemaAutoFormDefs[templateType] &&
schemaAutoFormDefs[templateType].template &&
templateExists(
toTypeTemplate(templateType, schemaAutoFormDefs[templateType].template)
)
) {
return toTypeTemplate(
templateType,
schemaAutoFormDefs[templateType].template
)
}
// Fallback #2: autoform.template from the schema
if (
schemaAutoFormDefs &&
schemaAutoFormDefs.template &&
templateExists(toTypeTemplate(templateType, schemaAutoFormDefs.template))
) {
return toTypeTemplate(templateType, schemaAutoFormDefs.template)
}
// Fallback #3: template-<componentType> attribute on an ancestor component within the same form
let templateFromAncestor = AutoForm.findAttribute(`template-${templateType}`)
let templateFromAncestorName = toTypeTemplate(
templateType,
templateFromAncestor
)
if (templateFromAncestor && templateExists(templateFromAncestorName)) {
return templateFromAncestorName
}
// Fallback #4: template attribute on an ancestor component within the same form
templateFromAncestor = AutoForm.findAttribute('template')
templateFromAncestorName = toTypeTemplate(templateType, templateFromAncestor)
if (templateFromAncestor && templateExists(templateFromAncestorName)) {
return templateFromAncestorName
}
// Fallback #5: Default template for component type, as set by AutoForm.setDefaultTemplateForType
const defaultTemplateForType = AutoForm.getDefaultTemplateForType(
templateType
)
const defaultTemplateForTypeName = toTypeTemplate(
templateType,
defaultTemplateForType
)
if (defaultTemplateForType && templateExists(defaultTemplateForTypeName)) {
return defaultTemplateForTypeName
}
// Fallback #6: Default template, as set by AutoForm.setDefaultTemplate
const defaultTemplate = AutoForm.getDefaultTemplate()
const defaultTemplateName = toTypeTemplate(templateType, defaultTemplate)
if (defaultTemplate && templateExists(defaultTemplateName)) {
return defaultTemplateName
}
// Found nothing. Return undefined
}
/**
* @method AutoForm.getFormValues
* @public
* @param {String} formId The `id` attribute of the `autoForm` you want current values for.
* @param {Template} [template] The template instance, if already known, as a performance optimization.
* @param {SimpleSchema} [ss] The SimpleSchema instance, if already known, as a performance optimization.
* @param {Boolean} [getModifier] Set to `true` to return a modifier object or `false` to return a normal object. For backwards compatibility, and object containing both is returned if this is undefined.
* @param {Boolean} [clean]
* @param {Boolean} [disabled]
* @return {Object|null}
*
* Returns an object representing the current values of all schema-based fields in the form.
* The returned object is either a normal object or a MongoDB modifier, based on the `getModifier` argument. Return value may be `null` if the form is not currently rendered on screen.
*/
AutoForm.getFormValues = function autoFormGetFormValues (
formId,
template,
ss,
getModifier,
clean = true,
disabled = false
) {
let insertDoc, updateDoc, transforms
template = template || AutoForm.templateInstanceForForm(formId)
if (
!template ||
!template.view ||
// We check for domrange later in this function
template.view.isDestroyed
) {
return null
}
// Get a reference to the SimpleSchema instance that should be used for
// determining what types we want back for each field.
ss = ss || AutoForm.getFormSchema(formId)
const form = AutoForm.getCurrentDataForForm(formId)
// By default, we do not keep empty strings
let keepEmptyStrings = false
if (form.removeEmptyStrings === false) {
keepEmptyStrings = true
}
// By default, we do filter
let filter = true
if (form.filter === false) {
filter = false
}
// By default, we do autoConvert
let autoConvert = true
if (form.autoConvert === false) {
autoConvert = false
}
// By default, we do trimStrings
let trimStrings = true
if (form.trimStrings === false) {
trimStrings = false
}
// By default, we do keepArrays
// We need keepArrays: false when we need update fields
// like "foo.2.bar" to update the proper index. But in
// most cases, we need to keep arrays together due to the mongo
// bug that creates objects rather than arrays if the array
// does not already exist in the db.
let keepArrays = true
if (form.setArrayItems === true) {
keepArrays = false
}
const hookCtx = {
template: template,
formId: formId,
schema: ss
}
// Get a preliminary doc based on the form
let doc
if (template.view._domrange) {
// Build a flat document from field values
doc = getFlatDocOfFieldValues(getAllFieldsInForm(template, disabled), ss)
// Expand the flat document
doc = AutoForm.Utility.expandObj(doc)
// When all fields that comprise a sub-object are empty, we should unset
// the whole subobject and not complain about required fields in it. For example,
// if `profile.address` has several properties but they are all null or undefined,
// we will set `profile.address=null`. This ensures that we don"t get incorrect validation
// errors about required fields that are children of optional objects.
AutoForm.Utility.bubbleEmpty(doc, keepEmptyStrings)
}
else {
// If the form is not yet rendered, use the form.doc
doc = form.doc || {}
}
// Create and clean insert doc.
if (getModifier !== true) {
// Delete any properties that are null, undefined, or empty strings,
// unless the form has requested to keep empty string.
// Do not add autoValues at this stage.
insertDoc = AutoForm.Utility.cleanNulls(doc, false, keepEmptyStrings)
// As array items are removed, gaps can appear in the numbering,
// which results in arrays that have undefined items. Here we
// remove any array items that are undefined.
//
// We do this to the insertDoc, but we don"t want to do it earlier to the
// doc, because that would cause the update modifier to have $sets for
// the wrong array indexes.
AutoForm.Utility.compactArrays(insertDoc)
if (clean) {
ss.clean(insertDoc, {
isModifier: false,
getAutoValues: false,
filter: filter,
autoConvert: autoConvert,
trimStrings: trimStrings,
mutate: true
})
}
// Pass expanded doc through formToDoc hooks
transforms = Hooks.getHooks(formId, 'formToDoc')
transforms.forEach(function formValuesTransform (transform) {
insertDoc = transform.call(hookCtx, insertDoc, ss)
})
}
// Create and clean update modifier.
if (getModifier !== false) {
// Converts to modifier object with $set and $unset.
// Do not add autoValues at this stage.
updateDoc = AutoForm.Utility.docToModifier(doc, {
keepEmptyStrings: keepEmptyStrings,
keepArrays: keepArrays
})
if (clean) {
ss.clean(updateDoc, {
isModifier: true,
getAutoValues: false,
filter: filter,
autoConvert: autoConvert,
trimStrings: trimStrings,
mutate: true
})
}
// Pass modifier through formToModifier hooks
transforms = Hooks.getHooks(formId, 'formToModifier')
transforms.forEach(function formValuesTransform (transform) {
updateDoc = transform.call(hookCtx, updateDoc)
})
}
if (getModifier === true) {
return updateDoc
}
else if (getModifier === false) {
return insertDoc
}
else {
// We return insertDoc and updateDoc when getModifier
// is undefined for backwards compatibility
return {
insertDoc: insertDoc,
updateDoc: updateDoc
}
}
}
/**
* @method AutoForm.resetValueCache
* @public
* @param {String} [formId] The `id` attribute of the `autoForm` you want current values for. Default is the closest form from the current context.
* @param {String} [fieldName] The field `name` you want to reset the cache of.
* @return {Any|undefined}
*
* Reset the cache and mark all fields as changed
*/
AutoForm.resetValueCache = function autoFormResetValueCache (formId, fieldName) {
// find AutoForm template
const template = Tracker.nonreactive(function () {
return AutoForm.templateInstanceForForm(formId)
})
template.formValues = template.formValues || {}
if (fieldName) {
if (template.formValues[fieldName]) {
template.formValues[fieldName].isMarkedChanged = true
}
// reset ancestors
const { ancestors } = fieldName
.split('.')
.slice(0, -1)
.reduce(
({ ancestors, parent }, sub) => {
parent = parent ? `${parent}.${sub}` : sub
ancestors.push(parent)
return { ancestors, parent }
},
{ ancestors: [], parent: '' }
)
for (const ancestor of ancestors) {
// fixed - ancestor was not used as key but fieldName -> no ancestors were cleared
template.formValues[ancestor].isMarkedChanged = true
}
}
else {
// fixed - template.formValues.forEach does not exist, since it"s an Object, not an Array
Object.keys(template.formValues).forEach((fieldName) => {
if (template.formValues[fieldName]) {
template.formValues[fieldName].isMarkedChanged = true
}
})
}
}
/**
* @method AutoForm.getFieldValue
* @public
* @param {String} fieldName The name of the field for which you want the current value.
* @param {String} [formId] The `id` attribute of the `autoForm` you want current values for. Default is the closest form from the current context.
* @param {Boolean} [clean] Indicates if the method should clean the values or not. Default is true.
* @return {Any|undefined}
*
* Returns the value of the field (the value that would be used if the form were submitted right now).
* This is a reactive method that will rerun whenever the current value of the requested field changes. Return value will be undefined if the field is not currently rendered.
*/
AutoForm.getFieldValue = function autoFormGetFieldValue (
fieldName,
formId,
clean = true
) {
// find AutoForm template
const template = Tracker.nonreactive(function () {
return AutoForm.templateInstanceForForm(formId)
})
if (!template) {
if (formId) {
AutoForm.rerunWhenFormRenderedOrDestroyed(formId)
}
return
}
// reactive dependency
template.formValues = template.formValues || {}
if (!template.formValues[fieldName]) {
template.formValues[fieldName] = new Tracker.Dependency()
template.formValues[fieldName].isMarkedChanged = true
}
const { isMarkedChanged, cachedValue } = template.formValues[fieldName]
template.formValues[fieldName].depend()
if (isMarkedChanged === false) return cachedValue
const doc = AutoForm.getFormValues(
formId,
template,
null,
false,
clean,
true
)
if (!doc) return
const mDoc = new MongoObject(doc)
const value = mDoc.getValueForKey(fieldName)
template.formValues[fieldName].cachedValue = value
template.formValues[fieldName].isMarkedChanged = false
return value
}
/**
* @method AutoForm.setFieldValue
* @public
* @param {String} fieldName The name of the field for which you want to set the current value.
* @param {Any} value Value of the field
* @param {String} [formId] The `id` attribute of the `autoForm` you want current values for. Default is the closest form from the current context.
* @return {Any|undefined}
*
* Sets the value for a field, resets the cache and emits changed events (reactive).
*/
AutoForm.setFieldValue = function autoFormSetFieldValue (
fieldName,
value,
formId
) {
const mDoc =
Tracker.nonreactive(function () {
return AutoForm.reactiveFormData.sourceDoc(formId)
}) || new MongoObject({})
const keyExists = Boolean(mDoc.getInfoForKey(fieldName))
keyExists
? mDoc.setValueForKey(fieldName, value)
: mDoc.addKey(fieldName, value)
AutoForm.reactiveFormData.sourceDoc(formId, mDoc)
const template = Tracker.nonreactive(function () {
return AutoForm.templateInstanceForForm(formId)
})
markChanged(template, fieldName, value)
}
/**
* @method AutoForm.setFormValues
* @public
* @param {Any} value Value of the form
* @param {String} [formId] The `id` attribute of the `autoForm` you want current values for. Default is the closest form from the current context.
* @return {Any|undefined}
*
* Sets the value for a field, resets the cache and emits changed events.
*/
AutoForm.setFormValues = function autoFormSetFormValues (value, formId) {
AutoForm.reactiveFormData.sourceDoc(formId, new MongoObject(value))
}
/**
* @method AutoForm.getInputTypeTemplateNameForElement
* @public
* @param {DOMElement} element The input DOM element, generated by an autoform input control
* @return {String}
*
* Returns the name of the template used to render the element.
*/
AutoForm.getInputTypeTemplateNameForElement = function autoFormGetInputTypeTemplateNameForElement (
element
) {
// get the enclosing view
let view = Blaze.getView(element)
// if the enclosing view is not a template, perhaps because
// the template contains a block helper like if, with, each,
// then look up the view chain until we arrive at a template
while (
view &&
view.name.includes('Template.') === false &&
view.name.includes('BlazeComponent.') === false
) {
view = view.originalParentView || view.parentView
}
if (!view) return
// View names have "Template." or "BlazeComponent." at the beginning so we slice that off.
return view.name.slice(view.name.indexOf('.') + 1)
}
/**
* @method AutoForm.getInputValue
* @public
* @param {DOMElement} [element] The input DOM element, generated by an autoform input control, which must have a `data-schema-key` attribute set to the correct schema key name.
* @param {SimpleSchema} [ss] Provide the SimpleSchema instance if you already have it.
* @return {Any}
*
* Returns the value of the field (the value that would be used if the form were submitted right now).
* Unlike `AutoForm.getFieldValue`, this function is not reactive.
*/
AutoForm.getInputValue = function autoFormGetInputValue (element, ss) {
let fieldType, val, inputTypeTemplate, autoConvert
Tracker.nonreactive(function () {
// don"t rerun when data context of element changes, can cause infinite loops
const dataContext = Blaze.getData(element)
if (dataContext && dataContext.atts) {
autoConvert = dataContext.atts.autoConvert
}
})
// Get jQuery field reference
const field = $(element)
// Get the field/schema key name
const fieldName = field.attr('data-schema-key')
// If we have a schema, we can autoconvert to the correct data type
if (ss) fieldType = ss.getQuickTypeForKey(fieldName)
// Get the name of the input type template used to render the input element
inputTypeTemplate = AutoForm.getInputTypeTemplateNameForElement(element)
// Slice off the potential theme template, after the last underscore.
const lastUnderscore = inputTypeTemplate.lastIndexOf('_')
if (lastUnderscore !== -1) {
inputTypeTemplate = inputTypeTemplate.slice(0, lastUnderscore)
}
// Figure out what registered input type was used to render this element
const typeDef = Object.values(AutoForm._inputTypeDefinitions).filter(
(def) => def.template === inputTypeTemplate
)[0]
// If field has a "data-null-value" attribute, value should always be null
if (field.attr('data-null-value') !== undefined) {
val = null
}
// Otherwise get the field"s value using the input type"s `valueOut` function if provided
else if (typeDef && typeDef.valueOut) {
val = typeDef.valueOut.call(field)
}
// Otherwise get the field"s value in a default way
else {
val = field.val()
}
// run through input"s type converter if provided
if (
val !== undefined &&
autoConvert !== false &&
typeDef &&
typeDef.valueConverters &&
fieldType
) {
const converterFunc = typeDef.valueConverters[fieldType]
if (typeof converterFunc === 'function') {
val = converterFunc.call(field, val)
}
}
return val
}
/**
* @method AutoForm.addInputType
* @public
* @param {String} name The type string that this definition is for.
* @param {Object} definition Defines how the input type should be rendered.
* @param {String} definition.componentName The component name. A template with the name <componentName>_bootstrap3, and potentially others, must be defined.
* @return {undefined}
*
* Use this method to add custom input components.
*/
AutoForm.addInputType = function afAddInputType (name, definition) {
const obj = {}
obj[name] = definition
Object.assign(AutoForm._inputTypeDefinitions, obj)
}
/**
* @method AutoForm.addFormType
* @public
* @param {String} name The type string that this definition is for.
* @param {Object} definition Defines how the submit type should work
* @param {Function} [definition.adjustInputContext] A function that accepts a single argument, which is the context with which an input template in the form will be called, potentially changes the context object, and then returns it. For example, the "readonly" and "disabled" form types use this function to add the "readonly" or "disabled" attribute, respectively, to every input within the form.
* @param {Function} [definition.adjustSchema] A function that accepts a single argument, which is the form schema, and potentially uses that to return a different schema to use instead. For example, the "update-pushArray" form type uses this function to build and return a schema that is limited by the `scope` attribute on the form. When this function is called, `this` contains useful information about the form.
* @param {Boolean} [definition.hideArrayItemButtons] Set to `true` if this form type should not show buttons for adding and removing items in an array field. The "disabled" and "readonly" form types do this.
* @param {Function} definition.onSubmit A function that does whatever should happen upon submission of this form type. When this function is called, `this` contains useful information about the form. At a minimum, you probably want to call `this.event.preventDefault()` to prevent the browser from submitting the form. Your submission logic may want to rely on additional custom form attributes, which will be available in `this.formAttributes`. If you do any additional validation and it fails, you should call `this.failedValidation()`. When your logic is done, you should call `this.result(error, result)`. If you want to end the submission process without providing a result, call `this.endSubmission()`. If you don"t call `this.result()` or `this.endSubmission()`, `endSubmit` hooks won"t be called, so for example the submit button might remain disabled. `onError` hooks will be called only if you pass an error to `this.result()`. `onSuccess` hooks will be called only if you do not pass an error to `this.result()`.
* @param {Function} [definition.shouldPrevalidate] A function that returns `true` if validation against the form schema should happen before the `onSubmit` function is called, or `false` if not. When this function is called, `this` contains useful information about the form. If this function is not provided for a form type, the default is `true`.
* @param {Function} definition.validateForm A function that validates the form and returns `true` if valid or `false` if not. This can happen during submission but also at other times. When this function is called, `this` contains useful information about the form and the validation options.
* @return {undefined}
*
* Use this method to add custom form types.
*/
AutoForm.addFormType = function afAddFormType (name, definition) {
const obj = {}
obj[name] = definition
Object.assign(AutoForm._formTypeDefinitions, obj)
}
/**
* @method AutoForm.validateField
* @public
* @param {String} formId The `id` attribute of the `autoForm` you want to validate.
* @param {String} fieldName The name of the field within the `autoForm` you want to validate.
* @param {Boolean} [skipEmpty=false] Set to `true` to skip validation if the field has no value. Useful for preventing `required` errors in form fields that the user has not yet filled out.
* @return {Boolean} Is it valid?
*
* In addition to returning a boolean that indicates whether the field is currently valid,
* this method causes the reactive validation messages to appear.
*/
AutoForm.validateField = function autoFormValidateField (
formId,
fieldName,
skipEmpty
) {
return validateField(fieldName, formId, skipEmpty, false)
}
/**
* @method AutoForm.validateForm
* @public
* @param {String} formId The `id` attribute of the `autoForm` you want to validate.
* @return {Boolean} Is it valid?
*
* In addition to returning a boolean that indicates whether the form is currently valid,
* this method causes the reactive validation messages to appear.
*/
AutoForm.validateForm = function autoFormValidateForm (formId) {
const form = AutoForm.getCurrentDataForForm(formId)
let formDoc
const formType = form.type
const ftd = Utility.getFormTypeDef(formType)
// Gather all form values
if (ftd.needsModifierAndDoc) {
formDoc = AutoForm.getFormValues(formId, null, null)
}
else if (ftd.usesModifier) {
formDoc = AutoForm.getFormValues(formId, null, null, true)
}
else {
formDoc = AutoForm.getFormValues(formId, null, null, false)
}
// If form is not currently rendered, return true
if (!formDoc) {
return true
}
return (
form.validation === 'none' ||
ftd.validateForm.call({
form: form,
formDoc: formDoc,
useCollectionSchema: false
})
)
}
/**
* @method AutoForm.getValidationContext
* @public
* @param {String} [formId] The `id` attribute of the `autoForm` for which you want the validation context
* @return {SimpleSchema.ValidationContext} The SimpleSchema validation context object.
*
* Use this method to get the validation context, which can be used to check
* the current invalid fields, manually invalidate fields, etc.
*/
AutoForm.getValidationContext = function autoFormGetValidationContext (formId) {
const form = AutoForm.getCurrentDataForForm(formId)
const ss = form._resolvedSchema
if (!ss) return
// formId may not be passed in, but we MUST pass it into namedContext to get back proper context
formId = formId || form.id
return ss.namedContext(formId)
}
/**
* @method AutoForm.findAttribute
* @public
* @param {String} attrName Attribute name
* @return {Any|undefined} Searches for the given attribute, looking up the parent context tree until the closest autoform is reached.
*
* Call this method from a UI helper. Might return undefined.
*/
AutoForm.findAttribute = function autoFormFindAttribute (attrName) {
let val
let view, viewData
function checkView () {
// Is the attribute we"re looking for on here?
// If so, stop searching
viewData = Blaze.getData(view)
if (viewData && viewData.atts && viewData.atts[attrName] !== undefined) {
val = viewData.atts[attrName]
}
else if (viewData && viewData[attrName] !== undefined) {
// When searching for "template", make sure we didn"t just
// find the one that"s on Template.dynamic
if (attrName !== 'template' || !('data' in viewData)) {
val = viewData[attrName]
}
}
}
// Loop
view = Blaze.currentView
while (val === undefined && view && view.name !== 'Template.autoForm') {
checkView()
view = view.originalParentView || view.parentView
}
// If we"ve reached the form, check there, too
if (val === undefined && view && view.name === 'Template.autoForm') {
checkView()
}
return val
}
/**
* @method AutoForm.findAttributesWithPrefix
* @public
* @param {String} prefix Attribute prefix
* @return {Object} An object containing all of the found attributes and their values, with the prefix removed from the keys.
*
* Call this method from a UI helper. Searches for attributes that start with the given prefix, looking up the parent context tree until the closest autoform is reached.
*/
AutoForm.findAttributesWithPrefix = function autoFormFindAttributesWithPrefix (
prefix
) {
const result = {}
let view, viewData, searchObj
function checkView () {
// Is the attribute we"re looking for on here?
// If so, add to result object.
viewData = Blaze.getData(view)
if (viewData && viewData.atts) {
searchObj = viewData.atts
}
else {
searchObj = viewData
}
// We do not need an isArray check, because isObject([{}]) returns [object Array]
if (isObject(searchObj)) {
Object.entries(searchObj).forEach(function ([k, v]) {
if (k.includes(prefix)) {
result[k.slice(prefix.length)] = v
}
})
}
}
// Loop
view = Blaze.currentView
while (view && view.name !== 'Template.autoForm') {
checkView()
view = view.originalParentView || view.parentView
}
// If we"ve reached the form, check there, too
if (view && view.name === 'Template.autoForm') {
checkView()
}
return result
}
/**
* @method AutoForm.debug
* @public
*
* Call this method in client code while developing to turn on extra logging.
* You need to call it just one time, usually in top level client code.
*/
AutoForm.debug = function autoFormDebug () {
AutoForm._debug = true
AutoForm.addHooks(null, {
onError: function (operation, error) {
console.log(`Error in ${this.formId}`, operation, error)
}
})
}
/**
* @property AutoForm.arrayTracker
* @public
*/
AutoForm.arrayTracker = arrayTracker
/**
* @method AutoForm.getInputType
* @param {Object} [attributes] The attributes provided to afFieldInput.
* @public
* @return {String} The input type. Most are the same as the `type` attributes for HTML input elements, but some are special strings that autoform interprets.
*
* Call this method from a UI helper to get the type string for the input control.
*/
AutoForm.getInputType = function getInputType (attributes) {
let type
const atts = AutoForm.Utility.getComponentContext(attributes, 'afFieldInput')
.atts
// If a `type` attribute is specified, we just use that
if (atts.type) return atts.type
// Get schema definition, using the item definition for array fields
const ss = AutoForm.getFormSchema()
const schemaType = ss && ss.getQuickTypeForKey(atts.name)