Example #1
0
	var getCol = function(def) {
		
		if (def.path == '__name__') {
			def.path = list.namePath;
		}
		
		var field = list.fields[def.path],
			col = null;
		
		if (field) {
			
			col = {
				field: field,
				path: field.path,
				type: field.type,
				label: def.label || field.label
			};
			
			if (col.type == 'relationship') {
				
				col.refList = col.field.refList;
				
				if (col.refList) {
					col.refPath = def.subpath || col.refList.namePath;
					col.subField = col.refList.fields[col.refPath];
					col.populate = { path: col.field.path, subpath: col.refPath };
				}
				
				if (!def.label && def.subpath) {
					col.label = field.label + ': ' + (col.subField ? col.subField.label : utils.keyToLabel(def.subpath));
				}
			}
			
		} else if (list.model.schema.paths[def.path] || list.model.schema.virtuals[def.path]) {
			// column refers to a path in the schema
			// TODO: this needs to handle sophisticated types, including arrays, nested Schemas, and mixed types
			col = {
				path: def.path,
				label: def.label || utils.keyToLabel(def.path)
			};
		}
		
		if (col) {
			
			col.width = def.width;
			
			if (col.path == list.namePath) {
				col.isName = true;
				nameCol = col;
			}
			
			if (field && field.col) {
				_.extend(col, field.col);
			}
			
		}
		
		return col;
	}
Example #2
0
	_.each(sections, function(section, key) {
		if ('string' == typeof section) {
			section = [section];
		}
		section = {
			lists: section,
			label: nav.flat ? keystone.list(section[0]).label : utils.keyToLabel(key)
		};
		section.key = key;
		section.lists = _.map(section.lists, function(i) {
			var list = keystone.list(i);
			if (!list) {
				var msg = 'Invalid Keystone Option (nav): list ' + i + ' has not been defined.\n';
				throw new Error(msg);
			}
			if (list.get('hidden')) {
				var msg = 'Invalid Keystone Option (nav): list ' + i + ' is hidden.\n';
				throw new Error(msg);
			}
			nav.by.list[list.key] = section;
			return list;
		});
		if (section.lists.length) {
			nav.sections.push(section);
			nav.by.section[section.key] = section;
		}
	});
Example #3
0
		section.lists = _.map(section.lists, function (i) {
			if (typeof i === 'string') {
				var list = keystone.list(i);
				if (!list) {
					throw new Error('Invalid Keystone Option (nav): list ' + i + ' has not been defined.\n');
				}
				if (list.get('hidden')) {
					throw new Error('Invalid Keystone Option (nav): list ' + i + ' is hidden.\n');
				}
				nav.by.list[list.key] = section;
				return {
					key: list.key,
					label: list.label,
					path: list.path,
				};
			} else if (_.isObject(i)) {
				if (!_.has(i, 'key')) {
					throw new Error('Invalid Keystone Option (nav): object ' + i + ' requires a "key" property.\n');
				}
				i.label = i.label || utils.keyToLabel(key);
				i.path = i.path || utils.keyToPath(key);
				i.external = true;
				nav.by.list[i.key] = section;
				return i;
			}
			throw new Error('Invalid Keystone Option (nav): ' + i + ' is in an unrecognized format.\n');
		});
Example #4
0
		section.lists = _.map(section.lists, function(i) {
			var msg;
			if ( _.isString(i)){
				var list = keystone.list(i);
				if (!list) {
					msg = 'Invalid Keystone Option (nav): list ' + i + ' has not been defined.\n';
					throw new Error(msg);
				}
				if (list.get('hidden')) {
					msg = 'Invalid Keystone Option (nav): list ' + i + ' is hidden.\n';
					throw new Error(msg);
				}
				nav.by.list[list.key] = section;
				return list;
			}else if ( _.isObject(i)){
				if(!_.has(i, "key")){
					msg = 'Invalid Keystone Option (nav): object ' + i + ' requires a "key" property.\n';
					throw new Error(msg);
				}
				i.label = i.label || utils.keyToLabel(key);
				i.path = i.path || utils.keyToPath(key);
				i.external = true;
				nav.by.list[i.key] = section;
				return i;
			}
			
			msg = 'Invalid Keystone Option (nav): ' + i + ' is in an unrecognized format.\n';
			throw new Error(msg);
		});
Example #5
0
function Field(list, path, options) {

	this.list = list;
	this._path = new Path(path);
	this.path = path;
	
	this.type = this.constructor.name;
	this.options = utils.options(this.defaults, options);
	this.label = options.label || utils.keyToLabel(this.path);
	this.typeDescription = options.typeDescription || this.typeDescription || this.type;
	
	this.templateDir = fspath.normalize( options.templateDir || ( __dirname + '../../templates/fields/' + this.type ) );
	
	var defaultTemplates = {
		"form": this.templateDir + '/' + 'form.jade',
		"initial": this.templateDir + '/' + 'initial.jade'
	};
	
	this.templates = utils.options(defaultTemplates, this.options.templates);
	this.list.automap(this);
	this.addToSchema();
	
	var note = null;
	Object.defineProperty(this, 'note', { get: function() {
		return (note === null) ? (note = (this.options.note) ? marked(this.options.note) : '') : note;
	} });
	
	
}
Example #6
0
/**
 * Field Constructor
 * =================
 *
 * Extended by fieldType Classes, should not be used directly.
 *
 * @api public
 */
function Field (list, path, options) {

	// Set field properties and options
	this.list = list;
	this._path = new Path(path);
	this.path = path;

	this.type = this.constructor.name;
	this.options = _.defaults({}, options, this.defaults);
	this.label = options.label || utils.keyToLabel(this.path);
	this.typeDescription = options.typeDescription || this.typeDescription || this.type;

	if (!options._isNested) {
		this.list.automap(this);
	}

	// Warn on required fields that aren't initial
	if (this.options.required
		&& this.options.initial === undefined
		&& this.options.default === undefined
		&& !this.options.value
		&& !this.list.get('nocreate')
		&& this.path !== this.list.mappings.name
	) {
		console.error('\nError: Invalid Configuration\n\n'
		+ 'Field (' + list.key + '.' + path + ') is required but not initial, and has no default or generated value.\n'
		+ 'Please provide a default, remove the required setting, or set initial: false to override this error.\n');
		process.exit(1);
	}

	// if dependsOn and required, set required to a function for validation
	if (this.options.dependsOn && this.options.required === true) {
		var opts = this.options;
		this.options.required = function () {
			// `this` refers to the validating document
			debug('validate dependsOn required', evalDependsOn(opts.dependsOn, this.toObject()));
			return evalDependsOn(opts.dependsOn, this.toObject());
		};
	}

	// Add the field to the schema
	this.addToSchema(options._isNested ? options._nestedSchema : this.list.schema);

	// Add pre-save handler to the list if this field watches others
	if (options._isNested && this.options.watch) {
		throw new Error('Nested fields do not support the `watch` option.');
	} else if (this.options.watch) {
		this.list.schema.pre('save', this.getPreSaveWatcher());
	}

	// Convert notes from markdown to html
	var note = null;
	Object.defineProperty(this, 'note', {
		get: function () {
			return (note === null) ? (note = (this.options.note) ? marked(this.options.note) : '') : note;
		},
	});

}
Example #7
0
	this.ops = options.options.map(function(i) {
		var op = _.isString(i) ? { value: i.trim(), label: utils.keyToLabel(i) } : i;
		if (!_.isObject(op)) {
			op = { label: '' + i, value: '' + i };
		}
		if (options.numeric && !_.isNumber(op.value)) {
			op.value = Number(op.value);
		}
		return op;
	});
Example #8
0
function Field(list, path, options) {

	// Set field properties and options
	this.list = list;
	this._path = new Path(path);
	this.path = path;

	this.type = this.constructor.name;
	this.options = utils.options(this.defaults, options);
	this.label = options.label || utils.keyToLabel(this.path);
	this.typeDescription = options.typeDescription || this.typeDescription || this.type;

	// Add the field to the schema
	this.list.automap(this);
	this.addToSchema();

	// Warn on required fields that aren't initial
	if (this.options.required &&
        this.options.initial === undefined &&
        this.options.default === undefined &&
        !this.options.value &&
        !this.list.get('nocreate') &&
        this.path !== this.list.mappings.name
	) {
		console.error('\nError: Invalid Configuration\n\n' +
			'Field (' + list.key + '.' + path + ') is required but not initial, and has no default or generated value.\n' +
			'Please provide a default, remove the required setting, or set initial: false to override this error.\n');
		process.exit(1);
	}

	// Set up templates
	this.templateDir = fspath.normalize(options.templateDir || (__dirname + '../../templates/fields/' + this.type));

	var defaultTemplates = {
		form: this.templateDir + '/' + 'form.jade',
		initial: this.templateDir + '/' + 'initial.jade'
	};

	this.templates = utils.options(defaultTemplates, this.options.templates);

	// Add pre-save handler to the list if this field watches others
	if (this.options.watch) {
		this.list.schema.pre('save', this.getPreSaveWatcher());
	}

	// Convert notes from markdown to html
	var note = null;
	Object.defineProperty(this, 'note', { get: function() {
		return (note === null) ? (note = (this.options.note) ? marked(this.options.note) : '') : note;
	} });

}
Example #9
0
function Field(list, path, options) {
	
	this.list = list;
	this._path = new Path(path);
	this.path = path;
	
	this.type = this.constructor.name;
	this.options = utils.options(this.defaults, options);
	this.label = options.label || utils.keyToLabel(this.path);
	this.typeDescription = options.typeDescription || this.typeDescription || this.type;
	
	this.list.automap(this);
	this.addToSchema();
	
}
Example #10
0
	_.each(sections, function(section, key) {
		if ('string' === typeof section) {
			section = [section];
		}
		section = {
			lists: section,
			label: nav.flat ? keystone.list(section[0]).label : utils.keyToLabel(key)
		};
		section.key = key;
		section.lists = _.map(section.lists, function(i) {
			var msg;
			if ( _.isString(i)){
				var list = keystone.list(i);
				if (!list) {
					msg = 'Invalid Keystone Option (nav): list ' + i + ' has not been defined.\n';
					throw new Error(msg);
				}
				if (list.get('hidden')) {
					msg = 'Invalid Keystone Option (nav): list ' + i + ' is hidden.\n';
					throw new Error(msg);
				}
				nav.by.list[list.key] = section;
				return list;
			}else if ( _.isObject(i)){
				if(!_.has(i, "key")){
					msg = 'Invalid Keystone Option (nav): object ' + i + ' requires a "key" property.\n';
					throw new Error(msg);
				}
				i.label = i.label || utils.keyToLabel(key);
				i.path = i.path || utils.keyToPath(key);
				i.external = true;
				nav.by.list[i.key] = section;
				return i;
			}
			
			msg = 'Invalid Keystone Option (nav): ' + i + ' is in an unrecognized format.\n';
			throw new Error(msg);
		});
		if (section.lists.length) {
			nav.sections.push(section);
			nav.by.section[section.key] = section;
		}
	});
Example #11
0
	_.each(sections, function(section, key) {
		if ('string' == typeof section) {
			section = [section];
		}
		section = {
			lists: section,
			label: nav.flat ? keystone.list(section[0]).label : utils.keyToLabel(key)
		};
		section.key = key;
		section.lists = _.map(section.lists, function(i) {
			var list = keystone.list(i);
			if (!list) {
				console.log('Defined lists:');
				console.log(_.pluck(keystone.lists, 'path'));
				throw new Error('Keystone Nav Error: list ' + i + ' has not been defined.');
			}
			nav.by.list[list.key] = section;
			return list;
		});
		if (section.lists.length) {
			nav.sections.push(section);
			nav.by.section[section.key] = section;
		}
	});
Example #12
0
	Object.defineProperty(this, 'label', { get: function() {
		return this.get('label') || this.set('label', utils.plural(utils.keyToLabel(key)));
	}});
Example #13
0
	this.ops = options.options.map(function(i) {
		return ('string' == typeof i) ? { value: i.trim(), label: utils.keyToLabel(i) } : i;
	});