diff --git a/components/quickForm/quickForm.js b/components/quickForm/quickForm.js index 3cf2c9b0..18a31aa3 100644 --- a/components/quickForm/quickForm.js +++ b/components/quickForm/quickForm.js @@ -90,4 +90,68 @@ Template.quickForm.helpers({ grouplessFields: grouplessFieldContext } } -}) +}); + +/* Private Functions */ + +/** + * Takes a schema object and returns a sorted array of field group names for it + * + * @param {Object} schemaObj Like from mySimpleSchema.schema() + * @returns {String[]} Array of field group names + */ +function getSortedFieldGroupNames(schemaObj) { + var names = _.map(schemaObj, function (field) { + return field.autoform && field.autoform.group; + }); + + // Remove undefined + names = _.compact(names); + + // Remove duplicate names + names = _.unique(names); + + return names.sort(); +} + +/** + * Returns the schema field names that belong in the group. + * + * @param {String} groupName The group name + * @param {Object} schemaObj Like from mySimpleSchema.schema() + * @returns {String[]} Array of field names (schema keys) + */ +function getFieldsForGroup(groupName, schemaObj) { + var fields = _.map(schemaObj, function (field, fieldName) { + return (fieldName.slice(-2) !== '.$') && + field.autoform && + field.autoform.group === groupName && + fieldName; + }); + + // Remove undefined + fields = _.compact(fields); + + return fields; +} + +/** + * Returns the schema field names that don't belong to a group + * + * @param {Object} schemaObj Like from mySimpleSchema.schema() + * @returns {String[]} Array of field names (schema keys) + */ +function getFieldsWithNoGroup(schemaObj) { + var fields = _.map(schemaObj, function (field, fieldName) { + let parent = schemaObj[fieldName.substr(0, fieldName.indexOf('.'))]; + return (fieldName.slice(-2) !== '.$') && + (!field.autoform || !field.autoform.group) && + (!parent || !parent.autoform || !parent.autoform.group) && + fieldName; + }); + + // Remove undefined + fields = _.compact(fields); + + return fields; +}