-
-
Notifications
You must be signed in to change notification settings - Fork 327
/
Copy pathautoform-helpers.js
438 lines (386 loc) · 12.1 KB
/
autoform-helpers.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
/* global AutoForm */
import { Template } from 'meteor/templating'
import { isFunction } from './common'
import { arrayTracker } from './autoform-arrays'
function parseOptions (options, helperName, skipSchema) {
const hash = (options || {}).hash || {}
// Find the form's schema
const ss = skipSchema === true ? {} : AutoForm.getFormSchema()
return { ...hash, ss }
}
/*
* Global template helpers (exported to app)
*/
/**
* Template helper to get the current error message from schema.
* @name afFieldMessage
* @param {Object} options
* @return {*}
*/
export const autoFormFieldMessage = function autoFormFieldMessage (options) {
options = parseOptions(options, 'afFieldMessage')
const formId = AutoForm.getFormId()
return options.ss.namedContext(formId).keyErrorMessage(options.name)
}
/*
* afFieldMessage
*/
Template.registerHelper('afFieldMessage', autoFormFieldMessage)
/**
* @name afFieldIsInvalid
* @param {Object} options
* @return {*}
*/
export const autoFormFieldIsInvalid = function autoFormFieldIsInvalid (options) {
options = parseOptions(options, 'afFieldIsInvalid')
const formId = AutoForm.getFormId()
return options.ss.namedContext(formId).keyIsInvalid(options.name)
}
/*
* afFieldIsInvalid
*/
Template.registerHelper('afFieldIsInvalid', autoFormFieldIsInvalid)
/**
* @name afArrayFieldHasMoreThanMinimum
* @param {Object} options
* @return {boolean}
*/
export const autoFormArrayFieldHasMoreThanMinimum = function autoFormArrayFieldHasMoreThanMinimum (
options
) {
options = parseOptions(options, 'afArrayFieldHasMoreThanMinimum')
const form = AutoForm.getCurrentDataPlusExtrasForForm()
// Registered form types can disable adding/removing array items
if (form.formTypeDef.hideArrayItemButtons) {
return false
}
const range = arrayTracker.getMinMax(
options.ss,
options.name,
options.minCount,
options.maxCount
)
const visibleCount = arrayTracker.getVisibleCount(form.id, options.name)
return visibleCount > range.minCount
}
/*
* afArrayFieldHasMoreThanMinimum
*/
Template.registerHelper(
'afArrayFieldHasMoreThanMinimum',
autoFormArrayFieldHasMoreThanMinimum
)
/**
* @name afArrayFieldHasLessThanMaximum
* @param {Object} options
* @return {boolean}
*/
export const autoFormArrayFieldHasLessThanMaximum = function autoFormArrayFieldHasLessThanMaximum (
options
) {
options = parseOptions(options, 'afArrayFieldHasLessThanMaximum')
const form = AutoForm.getCurrentDataPlusExtrasForForm()
// Registered form types can disable adding/removing array items
if (form.formTypeDef.hideArrayItemButtons) {
return false
}
const range = arrayTracker.getMinMax(
options.ss,
options.name,
options.minCount,
options.maxCount
)
const visibleCount = arrayTracker.getVisibleCount(form.id, options.name)
return visibleCount < range.maxCount
}
/*
* afArrayFieldHasLessThanMaximum
*/
Template.registerHelper(
'afArrayFieldHasLessThanMaximum',
autoFormArrayFieldHasLessThanMaximum
)
/**
* @name autoFormFieldValueIs
* @param {Object} options
* @return {boolean}
*/
export const autoFormFieldValueIs = function autoFormFieldValueIs (options) {
options = parseOptions(options, 'afFieldValueIs', true)
const currentValue = AutoForm.getFieldValue(options.name, options.formId)
return currentValue === options.value
}
/*
* afFieldValueIs
*/
Template.registerHelper('afFieldValueIs', autoFormFieldValueIs)
/**
* @name autoFormFieldValue
* @param {Object} options
* @return {Any}
*/
export const autoFormFieldValue = function autoFormFieldValue (options) {
options = parseOptions(options, 'afFieldValue', true)
return AutoForm.getFieldValue(
options.name,
options.formId || AutoForm.getFormId()
)
}
/*
* afFieldValue
*/
Template.registerHelper('afFieldValue', autoFormFieldValue)
/**
* @name afArrayFieldIsFirstVisible
* @return {*}
*/
export const autoFormArrayFieldIsFirstVisible = function autoFormArrayFieldIsFirstVisible () {
const context = this
return arrayTracker.isFirstFieldlVisible(
context.formId,
context.arrayFieldName,
context.index
)
}
/*
* afArrayFieldIsFirstVisible
*/
Template.registerHelper(
'afArrayFieldIsFirstVisible',
autoFormArrayFieldIsFirstVisible
)
/**
* @name afArrayFieldIsLastVisible
* @return {*}
*/
export const autoFormArrayFieldIsLastVisible = function autoFormArrayFieldIsLastVisible () {
const context = this
return arrayTracker.isLastFieldlVisible(
context.formId,
context.arrayFieldName,
context.index
)
}
/*
* afArrayFieldIsLastVisible
*/
Template.registerHelper(
'afArrayFieldIsLastVisible',
autoFormArrayFieldIsLastVisible
)
export const autoFormFieldValueContains = function autoFormFieldValueContains (
options
) {
options = parseOptions(options, 'afFieldValueContains', true)
const currentValue = AutoForm.getFieldValue(options.name, options.formId)
if (!Array.isArray(currentValue)) return false
return (
currentValue.includes(options.value) ||
(!!options.values && // for consistent return value
options.values.split(',').filter((item) => currentValue.includes(item)))
)
}
/*
* afFieldValueContains
*/
Template.registerHelper('afFieldValueContains', autoFormFieldValueContains)
/**
* @name afFieldLabelText
* @param {Object} options
* @return {Object}
*/
export const autoFormFieldLabelText = function autoFormFieldLabelText (options) {
// in some cases we want to define labels als hidden under the autoform ctx
// but remain visible in the overall schema ctx (so their name is shown
// during validation) which causes label / atts.label to be false
const self = this
if ([null, false].includes(self?.label ?? self?.atts?.label)) {
return null
}
options = parseOptions(options, 'afFieldLabelText', true)
return AutoForm.getLabelForField(options.name)
}
/*
* afFieldLabelText
*/
Template.registerHelper('afFieldLabelText', autoFormFieldLabelText)
/**
* @name afFieldNames
* @param {Object} options
* @return {{name: *}[]}
*/
export const autoFormFieldNames = function autoFormFieldNames (options) {
// TODO move to ES6 after we have tested most of this function's branches
options = parseOptions(options, 'afFieldNames')
const ss = options.ss
const name = options.name
const form = AutoForm.getCurrentDataForForm()
let namePlusDot,
genericName,
genericNamePlusDot
if (name) {
namePlusDot = `${name}.`
genericName = AutoForm.Utility.makeKeyGeneric(name)
genericNamePlusDot = `${genericName}.`
}
// Get the list of fields we want included
let fieldList = options.fields
let usedAncestorFieldList = false
if (fieldList) {
fieldList = AutoForm.Utility.stringToArray(
fieldList,
'AutoForm: fields attribute must be an array or a string containing a comma-delimited list of fields'
)
}
let ancestorFieldList = AutoForm.findAttribute('fields')
if (ancestorFieldList) {
ancestorFieldList = AutoForm.Utility.stringToArray(
ancestorFieldList,
'AutoForm: fields attribute must be an array or a string containing a comma-delimited list of fields'
)
// Use the ancestor field list as backup, unless there is
// a name and that name is listed in the ancestor field list
if (!fieldList) {
fieldList = ancestorFieldList
usedAncestorFieldList = true
}
}
if (fieldList) {
// Take only those fields in the fieldList that are descendants of the `name` field
if (name) {
// Replace generic name with real name. We assume that field names
// with $ apply to all array items. Field list will now have the
// correct array field item number instead of $.
if (genericName !== name) {
fieldList = fieldList.map(function (field) {
if (field.indexOf(genericNamePlusDot) === 0) {
const fieldName = field.slice(genericNamePlusDot.length)
return `${namePlusDot}${fieldName}`
}
return field
})
}
fieldList = fieldList.filter(function filterFieldsByName (field) {
return field.indexOf(namePlusDot) === 0
})
}
// If top level fields, be sure to remove any with $ in them
else {
fieldList = fieldList.filter(function filterArrayFields (field) {
return field.slice(-2) !== '.$' && field.indexOf('.$.') === -1
})
}
// First we filter out any fields that are subobjects where the
// parent object is also in the fieldList and is NOT the current
// field name.
// This means that if you do `fields="address,address.city"` we
// will use an afObjectField for address and include only the
// "city" field within that, but if you instead do `fields="address.city"`
// we will use a single field for the city, with no afObjectField
// template around it.
fieldList = fieldList.filter(function (field) {
const lastDotPos = field.lastIndexOf('.')
if (lastDotPos === -1) {
return true // keep
}
let parentField = field.slice(0, lastDotPos)
if (parentField.slice(-2) === '.$') {
parentField = parentField.slice(0, -2)
}
return (
!fieldList.includes(parentField) ||
parentField === name ||
parentField === genericName
)
})
}
if (!fieldList || (fieldList.length === 0 && usedAncestorFieldList)) {
// Get list of field names that are descendants of this field's name.
// If name/genericName is undefined, this will return top-level
// schema keys.
fieldList = ss.objectKeys(genericName)
if (name) {
// Tack child field name on to end of parent field name. This
// ensures that we keep the desired array index for array items.
fieldList = fieldList.map(function (field) {
return name + '.' + field
})
}
}
// If user wants to omit some fields, remove those from the array
let omitFields = options.omitFields || AutoForm.findAttribute('omitFields')
if (omitFields) {
omitFields = AutoForm.Utility.stringToArray(
omitFields,
'AutoForm: omitFields attribute must be an array or a string containing a comma-delimited list of fields'
)
fieldList = fieldList.filter((field) => !omitFields.includes(field))
// If omitFields contains generic field names (with $) we omit those too
fieldList = fieldList.filter(function (f) {
return !omitFields.includes(AutoForm.Utility.makeKeyGeneric(f))
})
}
// Filter out fields we never want
fieldList = fieldList.filter(function shouldIncludeField (field) {
const fieldDefs = AutoForm.Utility.getFieldDefinition(ss, field)
// Don't include fields that are not in the schema
if (!fieldDefs) {
return false
}
// Don't include fields with autoform.omit=true
if (fieldDefs.autoform && fieldDefs.autoform.omit === true) {
return false
}
if (
fieldDefs.autoform &&
isFunction(fieldDefs.autoform.omit) &&
fieldDefs.autoform.omit(field) === true
) {
return false
}
// Don't include fields with denyInsert=true when it's an insert form
if (fieldDefs.denyInsert && form.type === 'insert') {
return false
}
// Don't include fields with denyUpdate=true when it's an update form
if (fieldDefs.denyUpdate && form.type === 'update') {
return false
}
return true
})
// Ensure fields are not added more than once
fieldList = [...new Set(fieldList)]
// We return it as an array of objects because that
// works better with Blaze contexts
fieldList = fieldList.map(function (name) {
return { name: name }
})
return fieldList
}
/*
* afFieldNames
*/
Template.registerHelper('afFieldNames', autoFormFieldNames)
/**
* @name afSelectOptionAtts
* @return {*}
*/
export const afSelectOptionAtts = function afSelectOptionAtts () {
if (this.value === false) this.value = 'false'
const atts = 'value' in this ? { value: this.value } : {}
if (this.selected) {
atts.selected = ''
}
if (this.htmlAtts) {
Object.assign(atts, this.htmlAtts)
}
return atts
}
/*
* afSelectOptionAtts
*/
Template.registerHelper('afSelectOptionAtts', afSelectOptionAtts)
// Expects to be called with this.name available
Template.registerHelper('afOptionsFromSchema', function afOptionsFromSchema () {
return AutoForm._getOptionsForField(this.name)
})