helpers/makeReportContents.js

define(['knockout'], function(ko) {
    /**
     * This will build the report view of the template selected.
     * For now it's limited to type 10, 20 and 40 field.
     * @module app/helpers/makeReportContents
     * @param  {Array} fields Array of fields
     * @param  {Array} content Report contents will be stored
     * @return {Array}         The report contents
     * @requires knockout
     */
    function makeReportContents(fields, content) {
        var _default = {
                template_block_id: null,
                numeric: null,
                single_line: null,
                multiline: null,
                background: null,
                foreground: null
            },
            temp = null,
            content = content || [];
        ko.utils.arrayForEach(fields, function(field) {
            if (field.childFields.length) {
                makeReportContents(field.childFields, content);
            }
            temp = ko.utils.extend({}, _default);
            if (field.id) {
                temp.template_block_id = field.id;
            }
            if (field.fg_changed) {
                temp.foreground = field.fg_class;
            }

            if (field.bg_changed) {
                temp.background = field.bg_class;
            }

            if (field.key === '10' && !field.read_only) {
                console.log('not read only');
                if (field.single_line) {
                    temp.single_line = field.value;
                } else {
                    temp.multiline = field.value;
                }
                // content.push(temp);
            } else if (field.key === '20') {
                temp.numeric = field.value;
                // content.push(temp);
            } else if (field.key === '40') {
                temp.question = field.value;
                // content.push(temp);
            }

            content.push(temp);

        });

        return content;
    }

    return makeReportContents;
})