all files / keystone/fields/types/geopoint/ GeoPointType.js

45% Statements 27/60
12.5% Branches 6/48
37.5% Functions 3/8
48.21% Lines 27/56
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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120                                                                                                                                                                                          
var _ = require('lodash');
var FieldType = require('../Type');
var util = require('util');
var utils = require('keystone-utils');
 
// Validation and value parsing regular expression
var REGEXP_LNGLAT = /^\s*(\-?\d+(?:\.\d+)?)\s*\,\s*(\-?\d+(?:\.\d+)?)\s*$/;
 
/**
 * Geo FieldType Constructor
 * @extends Field
 * @api public
 */
function geopoint (list, path, options) {
	this._fixedSize = 'medium';
	// TODO: implement filtering
	options.nofilter = true;
	geopoint.super_.call(this, list, path, options);
}
util.inherits(geopoint, FieldType);
 
/**
 * Registers the field on the List's Mongoose Schema.
 * Adds a 2dsphere indexed lat/lng pair
 */
geopoint.prototype.addToSchema = function () {
	this.list.schema.path(this.path, _.defaults({ type: [Number], index: '2dsphere' }, this.options));
	this.bindUnderscoreMethods();
};
 
/**
 * Gets the field's data from an Item, as used by the React components
 */
geopoint.prototype.getData = function (item) {
	var points = item.get(this.path);
	return (points && points.length === 2) ? points : [];
};
 
/**
 * Formats the field value
 */
geopoint.prototype.format = function (item) {
	if (item.get(this.path)) {
		return item.get(this.path).reverse().join(', ');
	}
	return null;
};
 
/**
 * Asynchronously confirms that the provided value is valid
 */
geopoint.prototype.validateInput = function (data, callback) {
	var value = this.getValueFromData(data);
	var result = false;
	if (value === undefined) {
		result = true;
	} else {
		if (Array.isArray(value)) {
			value = value.length === 2 ? value.join(',') : '';
		}
		if (typeof value === 'string') {
			result = REGEXP_LNGLAT.test(value);
		}
	}
	utils.defer(callback, result);
};
 
/**
 * Asynchronously confirms that the a value is present
 */
geopoint.prototype.validateRequiredInput = function (item, data, callback) {
	var value = this.getValueFromData(data);
	var result = (value || (value === undefined && item.get(this.path) && item.get(this.path).length === 2)) ? true : false;
	utils.defer(callback, result);
};
 
/**
 * Validates that a value for this field has been provided in a data object
 *
 * Deprecated
 */
geopoint.prototype.inputIsValid = function (data, required, item) { // eslint-disable-line no-unused-vars
	var values = this.getValueFromData(data);
	// Input is valid if the field is not required, and not present
	if (values === undefined && !required) return true;
	if (Array.isArray(values)) {
		values = values.length === 2 ? values.join(',') : '';
	}
	if (typeof values !== 'string') return false;
	if ((values === '' || values.charAt(0) === ',' || values.charAt(values.length - 1) === ',') && !required) return true;
	return REGEXP_LNGLAT.test(values);
};
 
/**
 * Updates the value for this field in the item from a data object
 */
geopoint.prototype.updateItem = function (item, data, callback) {
	var value = this.getValueFromData(data);
	Iif (value === undefined) return process.nextTick(callback);
	Iif (typeof value === 'string') {
		// Value should be formatted lng,lat
		var values = REGEXP_LNGLAT.exec(value);
		if (values) {
			item.set(this.path, [values[1], values[2]]);
		} else {
			item.set(this.path, undefined);
		}
	} else Eif (Array.isArray(value)) {
		Eif (value.length === 2 && REGEXP_LNGLAT.test(_.compact(value).join(','))) {
			item.set(this.path, value);
		} else {
			item.set(this.path, undefined);
		}
	}
	process.nextTick(callback);
};
 
/* Export Field Type */
module.exports = geopoint;