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

81.25% Statements 26/32
78.57% Branches 22/28
100% Functions 1/1
81.25% Lines 26/32
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           213× 110×   103× 14×   103×     103×     103×     103×   33× 13× 20×   20× 10× 10× 10×             103×     103×       103× 96× 96× 96× 12×   96×      
var Field = require('../../').Field;
 
/**
 * Creates a new field at the specified path, with the provided options.
 * If no options are provides, returns the field at the specified path.
 */
function field (path, options) {
	if (arguments.length === 1) {
		return this.fields[path];
	}
	if (typeof options === 'function') {
		options = { type: options };
	}
	Iif (this.get('noedit')) {
		options.noedit = true;
	}
	Iif (!options.note && this.get('notes')) {
		options.note = this.get('notes')[path];
	}
	Iif (typeof options.type !== 'function') {
		throw new Error('Fields must be specified with a type function');
	}
	if (!(options.type.prototype instanceof Field)) {
		// Convert native field types to their default Keystone counterpart
		if (options.type === String) {
			options.type = Field.Types.Text;
		} else Iif (options.type === Number) {
			options.type = Field.Types.Number;
		} else if (options.type === Boolean) {
			options.type = Field.Types.Boolean;
		} else Eif (options.type === Date) {
			options.type = Field.Types.Datetime;
		} else {
			throw new Error('Unrecognised field constructor: ' + options.type);
		}
	}
 
	// Note the presence of this field type for client-side script optimisation
	this.fieldTypes[options.type.name] = true;
 
	// Wysiwyg HTML fields are handled as a special case so we can include TinyMCE as required
	Iif (options.type.name === 'html' && options.wysiwyg) {
		this.fieldTypes.wysiwyg = true;
	}
 
	var field = new options.type(this, path, options);
	this.fields[path] = field;
	this.fieldsArray.push(field);
	if (field.type === 'relationship') {
		this.relationshipFields.push(field);
	}
	return field;
}
 
module.exports = field;