all files / keystone/fields/types/key/ KeyType.js

92.86% Statements 26/28
66.67% Branches 10/15
100% Functions 4/4
92.86% Lines 26/28
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                                                                    
var FieldType = require('../Type');
var TextType = require('../text/TextType');
var util = require('util');
var utils = require('keystone-utils');
 
/**
 * Key FieldType Constructor
 * @extends Field
 * @api public
 */
function key (list, path, options) {
	this._nativeType = String;
	this._defaultSize = 'medium';
	this.separator = options.separator || '-';
	key.super_.call(this, list, path, options);
}
util.inherits(key, FieldType);
 
/* Inherit from TextType prototype */
key.prototype.addFilterToQuery = TextType.prototype.addFilterToQuery;
 
/**
 * Generates a valid key from a string
 */
key.prototype.generateKey = function (str) {
	return utils.slug(String(str), this.separator);
};
 
/**
 * Checks that a valid key has been provided in a data object
 *
 * Deprecated
 */
key.prototype.inputIsValid = function (data, required, item) {
	var value = this.getValueFromData(data);
	Iif (value === undefined && item && item.get(this.path)) {
		return true;
	}
	value = this.generateKey(value);
	return (value || !required) ? true : false;
};
 
/**
 * Updates the value for this field in the item from a data object
 */
key.prototype.updateItem = function (item, data, callback) {
	var value = this.getValueFromData(data);
	Iif (value === undefined) {
		return process.nextTick(callback);
	}
	value = this.generateKey(value);
	Eif (item.get(this.path) !== value) {
		item.set(this.path, value);
	}
	process.nextTick(callback);
};
 
/* Export Field Type */
module.exports = key;