# all files / keystone/lib/list/ add.js

74.29% Statements 26/35
60% Branches 15/25
100% Functions 4/4
74.29% Lines 26/35

    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
    831×
    1×

    1×
    47×
    67×
    67×
    67×
    124×
    124×

    124×
    20×

    20×
    20×

    104×

    47×
    104×

    104×

    47×
    47×

    47×
    47×
    47×

    47×

    47×

    40×

    1×
     var _ = require('lodash');
    var utils = require('keystone-utils');

    /**
     * Adds one or more fields to the List
     * Based on Mongoose's Schema.add
     */
    function add () {
    	var add = function (obj, prefix) {
    		prefix = prefix || '';
    		var keys = Object.keys(obj);
    		for (var i = 0; i < keys.length; ++i) {
    			var key = keys[i];
    			Iif (!obj[key]) {
    				throw new Error(
    					'Invalid value for schema path `' + prefix + key + '` in `' + this.key + '`.\n'
    					+ 'Did you misspell the field type?\n'
    				);
    			}
    			if (utils.isObject(obj[key]) && (!obj[key].constructor || obj[key].constructor.name === 'Object') && (!obj[key].type || obj[key].type.type)) {
    				Eif (Object.keys(obj[key]).length) {
    					// nested object, e.g. { last: { name: String }}
    					// matches logic in mongoose/Schema:add
    					this.schema.nested[prefix + key] = true;
    					add(obj[key], prefix + key + '.');
    				} else {
    					addField(prefix + key, obj[key]); // mixed type field
    				}
    			} else {
    				addField(prefix + key, obj[key]);
    			}
    		}
    	}.bind(this);

    	var addField = function (path, options) {
    		Iif (this.isReserved(path)) {
    			throw new Error('Path ' + path + ' on list ' + this.key + ' is a reserved path');
    		}
    		this.uiElements.push({
    			type: 'field',
    			field: this.field(path, options),
    		});
    	}.bind(this);

    	var args = Array.prototype.slice.call(arguments);
    	var self = this;

    	_.forEach(args, function (def) {
    		self.schemaFields.push(def);
    		Iif (typeof def === 'string') {
    			if (def === '>>>') {
    				self.uiElements.push({
    					type: 'indent',
    				});
    			} else if (def === '<<<') {
    				self.uiElements.push({
    					type: 'outdent',
    				});
    			} else {
    				self.uiElements.push({
    					type: 'heading',
    					heading: def,
    					options: {},
    				});
    			}
    		} else {
    			Iif (def.heading && typeof def.heading === 'string') {
    				self.uiElements.push({
    					type: 'heading',
    					heading: def.heading,
    					options: def,
    				});
    			} else {
    				add(def);
    			}
    		}
    	});

    	return this;
    }

    module.exports = add;