all files / keystone/lib/list/ getOptions.js

17.65% Statements 3/17
0% Branches 0/10
0% Functions 0/2
17.65% Lines 3/17
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                                                                                                                                          
var _ = require('lodash');
 
/**
 * Gets the options for the List, as used by the React components
 */
function getOptions () {
	var ops = {
		autocreate: this.options.autocreate,
		autokey: this.autokey,
		defaultColumns: this.options.defaultColumns,
		defaultSort: this.options.defaultSort,
		fields: {},
		hidden: this.options.hidden,
		initialFields: _.map(this.initialFields, 'path'),
		key: this.key,
		label: this.label,
		nameField: this.nameField ? this.nameField.getOptions() : null,
		nameIsEditable: this.nameIsEditable,
		nameIsInitial: this.nameIsInitial,
		nameIsVirtual: this.nameIsVirtual,
		namePath: this.namePath,
		nocreate: this.options.nocreate,
		nodelete: this.options.nodelete,
		noedit: this.options.noedit,
		path: this.path,
		perPage: this.options.perPage,
		plural: this.plural,
		searchFields: this.options.searchFields,
		singular: this.singular,
		sortable: this.options.sortable,
		sortContext: this.options.sortContext,
		track: this.options.track,
		tracking: this.tracking,
		relationships: this.relationships,
		uiElements: [],
	};
	var self = this;
	_.forEach(this.uiElements, function (el) {
		switch (el.type) {
			// TODO: handle indentation
			case 'field':
				// add the field options by path
				ops.fields[el.field.path] = el.field.getOptions();
				// don't output the name field as a ui element if it's editable as it'll
				// appear as an input in the header
				if (el.field === self.nameField && self.nameIsEditable) {
					return;
				}
				// don't output hidden fields
				if (el.field.hidden) {
					return;
				}
				// add the field to the elements array
				ops.uiElements.push({
					type: 'field',
					field: el.field.path,
				});
				break;
			case 'heading':
				ops.uiElements.push({
					type: 'heading',
					content: el.heading,
					options: el.options,
				});
				break;
		}
	});
	return ops;
}
 
module.exports = getOptions;